Interacting with an Ethereum Smart Contract

Kerala Blockchain Academy
Blockchain Stories
Published in
5 min readJun 29, 2022

By Sumi Maria Abraham, Research & Development Engineer Kerala Blockchain Academy.

Do you remember discussing on how programmability went into practical in the Ethereum Blockchain?? If you have missed reading the early piece of information, kindly refer here. We saw there a simple, smart contract deployed on a test network. Moving on, in this feature, we’ll discuss on how to interact with that smart contract.

Image Courtesy: https://iconscout.com/, https://www.shutterstock.com/

Please note that in the previous article we deployed the smart contract in Ropsten test network. Since Ropsten test network is expected to be shut down later this year, we have used a similar smart contract deployed in Goerli testnet here.

Contract Address (Goerli): 0x44E84A10341BF772906c37fFc30CDbb132eA35f2

We can interact with an already deployed smart contract by using its contract address and the ABI. For that, we first need to connect to the blockchain network. Wallets are simple applications allowing users to connect with a blockchain network. They act as account managers who manage the user’s keys and transactions. We will be using a MetaMask wallet to connect with the Goerli test network, which got our smart contract. MetaMask installation is already described in our previous blog on HD Wallets. Kindly follow those instructions to install MetaMask.

Connecting to Blockchain network

The wallet will be connected to Ethereum Network (mainnet) by default. You can switch to a test network, as shown in the figure.

Click on the network name, and enable visibility of test networks. Now switch to Goerli test network.

To interact with the contract using the wallet, it should have some ether balance. Goerli is a test network, and its cryptocurrency (Goerli ETH) is available for free from certain sources called faucets. You can go to any of the below links and request Goerli ETH by sharing your account address.

Faucets: https://goerli-faucet.slock.it/, https://faucet.goerli.mudit.blog/. Remember, these are test ethers which do not have any monetary value.

Interacting with Smart Contract

Now we need a tool to interact with our smart contract. We will use Remix IDE for this purpose. Remix IDE is an open-source web and desktop application. You can directly access it from your web browser; just go to https://remix.ethereum.org.

Open a new file and copy the ABI from https://goerli.etherscan.io/address/0x44E84A10341BF772906c37fFc30CDbb132eA35f2#code. Save it as filename.abi

Switch to the deploy and run transaction tab from the menu on the left side. Select the environment as Injected Web3 to connect with the MetaMask. Make sure you are logged into MetaMask and connected to the Goerli testnet. Enter the contract address (0x44E84A10341BF772906c37fFc30CDbb132eA35f2) at the designated place and click on At Address as shown in the figure. Remix IDE will ask you for confirmation before loading the contract instance. An instance of the contract will be available under the heading deployed contracts. You can expand it to interact with it.

CALL vs TRANSACTION

Try to invoke the getMessage function by clicking on it. At the console below the editor space, you will get the status of the method call.

The getMessage function of our contract performs a simple read-only operation (CALL), which returns the current value of the message stored on the blockchain. You will be able to view the present value of the message in the log and below the function under deployed contracts.

Now, try to invoke the setMessage function. This function writes a value to the message and stores it on the blockchain. Give a string in the textbox and click on the setMessage.

Since it performs a write operation on the blockchain state, the method invocation is a transaction. The MetaMask will pop up and ask you for transaction confirmation. This operation carries a fee, as displayed in the wallet notification, and it will be deducted from your account. The cost depends upon the requested operation and also some dynamic network parameters. The same function may require a different fee at a later time.

Click on confirm. MetaMask will use your private key to sign the transaction and send it across the network. Once it gets verified by the network and added to the block, you will get a confirmation from MetaMask (it may take approximately 10–15 seconds). The transaction details can be seen in the Remix IDE console also.

Click on the view on etherscan option to view transaction in etherscan. The transaction input data will be in a default encoded format. Click on decode input data to view it in text format.

You can call the getMessage function again to confirm the new value of the message. Did you get the message you wrote ?. Congratulations !!.

Deploy your own Smart Contract

Do you wish to deploy the smart contract independently?. It’s simple, just copy the contract source code of the smart contract and save it in a .sol file in Remix IDE. Now go to the solidity compiler tab and compile the smart contract. Make sure you see a tick on the compilation icon.

Next, go to the deploy and run transactions tab. Make sure that your MetaMask is active and connected to the test network.

Select deployment environment as Injected Web3 to connect to MetaMask. Deploy the contract. Confirm the MetaMask transaction notification and wait until your contract deployment transaction is confirmed. Now you can interact with the newly deployed smart contract.

Images from: https://iconscout.com/, https://www.shutterstock.com/.

--

--

Kerala Blockchain Academy
Blockchain Stories

One-stop solution for quality blockchain education and research. Offers best in class blockchain certification programs in multiple blockchain domains.