How to send a transaction of smart contract in Ethereum

Blockchain Image

Once you’ve learned how to write a smart contract in Ethereum and deploy it in EVM you might have come up with a question how to interact with the deployed smart contract. Because smart contracts are not exposed in the internet natively and we’re not able to use http protocol to interact with it directly.

We need to know how to use Javascript libraries such as web3 and ethereumjs-tx to create a transaction and sign it with EOA’s private key then pack it in the formatted transaction to interact with the deployed smart contracts, once we’ve created a transaction we can send it out to Ethereum gossip network.

In this tutorial, I’ll cover 2 ways to write and send a transaction in Javascript code 1) Sign a constructed transaction, 2) Use ganache-cli to connect to the existing network. There are some required libraries I installed for this tutorial beforehand.

No.1 is the use to send your transaction to live networks while No.2 is just using ganache-cli everyone already knew.

$ node -v
v13.7.0

$ npm init -y
$ npm install web3@1.2.1
$ npm install ethereumjs-tx@1.3.7
$ npm list --depth=0

Sign a constructed transaction

Let’s use DAI ERC-20 token to interact with in this example and send some DAI tokens from one EOA address to another in Rinkeby network. To construct a transaction for the function you call, we need to encode ABI of the function and required variables to pass it to data field of a transaction.

encodeABI

Let’s assume we have 0.1 DAI and use transfer function of DAI smart contract, we can write a code like below for encoding ABI of transfer function.

First web3.eth.Contract can be used to get a smart contract object from JSON ABI and the contract address. Web3 library will auto convert all calls into low level ABI calls over RPC for you.

Next step is to generate a raw transaction with the encoded ABI and sign it with the account’s private key and send it out to the specific network. ChainId is Ethereum network numeric number you want to use. Rinkeby has 4 for chainId.

sendSignedTransaction

I created this test code in github and ran it. This sample, as mentioned earlier, send some DAI tokens (0.1 DAI in this example) from one address to another. The result returns the transaction hash.

$ node DaiSendRinkeby.js
The sender balance is:  100
The receiver balance is: 0
The txHash is:  0x11436802a35457c30a0ffdbc1fb5bc5c515f42589bc3370ea4a60260819225d1

Here’s the result.

Use ganache-cli to connect to the existing network

The second choice is just to connect to the live Rikeby network by using ganache-cli. This gives you interactive operations though console access. You need to unbox an empty truffle project and configure truffle.js that is the configuration file. We edit this configuration file for using Rinkeby network in Ganache.

I have truffle and truffle-hdwallet-provider installed on my machine. It’s better to install truffle library globally.

$ npm install -g truffle
$ npm install truffle-hdwallet-provider
$ truffle init

Let’s configure truffle.js configuration file as below. If you haven’t seen this file truffle.js but truffle-config.js you can rename truffle-config.js to truffle.js. Truffle library takes either truffle-config.js or truffle.js for the environmental configuration.

Now you’re able to connect Rinkeby network with your account via console. truffle console –network <network name> command can invoke a ganache-cli on your console to interact with the network.

$ truffle console --network rinkeby
truffle(rinkeby)> let accounts = await web3.eth.getAccounts()
truffle(rinkeby)> accounts[0]
'0x2Bd850558a2A9D4D9f9a4b965ad3E89C2565c9DD'

The rest of stemps are exactly the same as you wrote a Javascript code and sent a transaction, but you don’t need to construct a raw transactioin and sign it. Ganache knows your account with the private key and send it to the live network instead of the written code.

> const daiExchangeAbi = require("./erc20abi.json")
> const daiExchangeAddress = "0x5592ec0cfb4dbc12d3ab100b257153436a1f0fea"

> const daiExchangeContract = new web3.eth.Contract(JSON.parse(JSON.stringify(daiExchangeAbi)), daiExchangeAddress)

> daiExchangeContract.methods.balanceOf(accounts[0]).call(function (err, res) { console.log({ err, res }) })
{ err: null, res: '100000000000000000000' }
'100000000000000000000'

To wrap up, we need to know how to code a Javascript code to wrap a transaction in signed format and send it out to networks or how to use a tool such as ganache-cli to interact with the network.

Either case you must understand web3 Javascript library to be able to interact with a local or remote ethereum node using HTTP, IPC, etc.

Leave a Reply

Your email address will not be published. Required fields are marked *