Web3 through a beginner’s eyes
This article was entirely written by one of our interns and reflects a personal view of her using our platform as a beginner.
I have been developing an interest towards Blockchain recently. After my 4th semester exams I applied for internships and I was lucky enough that BlockVigil took me in.
My job role was to write a smart contracts and make signed web3 calls to it through Infura. I came across a variety of errors while doing that. I realized that web3 is not very beginner friendly. So I took to writing this document. I hope this document helps other beginners like me.
My task was to write a smart contract for a payment channel and make web3 calls to it through web3py using an Infura URL. I began with writing the smart contract on remix, deployed it there and got the contract address
, bytecode
and ABI
from there. I input those values into the web3 code and began making calls to that contract.
The open channel function worked fine. But when I tried the join channel function, the transactions were stuck in “pending state”. This was due to low values for gasPrice
.
The gasPrice
is to be accepted in wei
and not gwei
. And while building a transaction, it is mandatory to specify the gas
, gasPrice
, chainId
, value
fields.
transaction = {
'to': CONTRACT_ADDRESS,
'value': 0,
'data': _data,
'gas': gas_est,
'gasPrice': 18, #Ouch! Gas not enough to bribe the miner
'nonce': 3,
'chainId': 4
}
Once this got fixed, I went to the next function to start challenge. The python console returned a transaction hash. But when I tried looking for that transaction hash on Etherscan, it said Sorry, We are unable to locate this transaction hash
. I even tried to increase the gasPrice
and gas
values, but it still displayed the same message. Only later I realized that this was an error with the nonce
value. There were many more errors that were due to the incorrect nonce value, but none of them made sense.
transaction = {
'to': CONTRACT_ADDRESS,
'data': _data,
'nonce': 3 #same nonce as the previous transaction
}
And then there was one error that said Intrinsic gas too low
which has something to do with the PoA chains and was an open issue on GitHub. I then had to switch to another testnet. And finally I could make successful web3 calls to all functions.
I had to then make web3 calls to an erc20 contract. At first I thought this would be easy because I’d already done this once. But the trouble doesn’t end here. So here instead of deploying the contract on remix, I wanted to deploy it through web3py. The first thing I ran into was type object argument after ** must be a mapping, not str
. So I changed the arguments to a mapping and it worked. The next time I tried to deploy it, it said The method eth_sendTransaction does not exist/is not available
. This meant something went wrong while signing the transaction. The account was not unlocked. I signed the transaction with the msg.sender
’s private key and yes it did work.
#to unlock account
key = PRIVATE_KEY
acct = w3.eth.account.privateKeyToAccount(key)
#use acct.address wherever public key is required
And then there was an error which said The execution failed due to an exception
. Now this is the worst part. It does not even specify what is wrong with the code. It could be nonce
, bytecode
, ABI
, gasPrice
anything. It was nonce in my case.
If you are including a gasPrice
value in the transaction, it is very necessary to run an estimateGas()
on that transaction. Otherwise you could end up getting Warning! Error encountered during contract execution [Out of gas]
.
Now on the other hand, we have EthVigil APIs. These APIs make life so much easier for a beginner. All you have to do is, go to their website, sign up with your email to get your invite code. Once you login, you just look into the docs and follow up with the code.
I made web3 calls to the erc20 contract through EthVigil. At first I tried the web interface. The remix IDE is present inside, where you can type in the code and deploy it. The EthVigil signer will sign the contract and do the rest for you. Once deployed, the functions can be called easily with just a tap of the mouse. Choose the function that you want to execute, supply the necessary arguments and then wait for the transaction to be mined.
There is an option to use the command line too. Firstly I deployed the code using a simple deploy command as in the docs. You get an amazing feature to add integrations to the contract. Once the contract is deployed, just set up a webhook listener to all the functions whose events you intend to trap. I set up ngrok listener to transfer()
and transferFrom()
functions. I registered the webhook listener through the ngrok URL, and then added it to the two functions I had chosen, and the listener is set. All you have to do after this is make calls to the functions using a simple command as below.
python cli.py <function_name> <arguments>
And you can access any function you wish to. EthVigil platform has its own design patterns for dealing with the Ethereum account address that should sign all transactions for a specific user. As I was testing for the functionality of the contract, I needed to make transactions with more than one account as msg.sender
, I used the dApp approach. All you have to do is select the Görli testnet on metamask, copy the contract address and load it into remix, and execute the function that you want to.
This is how you can make web3 calls to the smart contract effortlessly. And the documentation is so very clear that you don’t have to think twice to understand it. Just write your command either using the command line tool or use the web interface to make web3 calls to the contract and wait for the transaction to be confirmed.
If you are a beginner and have no idea about web3, you might want to try out BlockVigil instead of wasting your precious time on solving errors that have already been solved. Thanks to BlockVigil. 👌
Happy coding!