Basics of manually interacting with a smart contract

Overview

A smart contract is the backend of any decentralized app. The website and front-end of a dapp are there to serve as a user-friendly and easy way of interacting with the smart contracts. But what if the website goes down? Or what if you're just curious about the "behind the scenes" of a dapp?

In this article, I cover how to use a blockchain explorer (like bscscan or etherscan) to manually call functions on a smart contract.

There are two types of functions you can call in a smart contract, a read operation and a write operation. I'll go through each in more detail below so you can understand how to interact with a smart contract manually if needed.

Read Operations

A read operation is providing some data from what is already present on the blockchain. For instance, how much of a certain token your wallet contains is information already on the blockchain.

I'll do just that by checking how much CAKE I have in my Metamask wallet.

The first thing I need is the smart contract address for CAKE. I can use a site like CoinGecko to see that the address is 0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82. Then I'll look that address up on bscscan. If I was looking up a smart contract on the ethereum blockchain I would go to etherscan

Next on that address page, I'll click on "Contract" and then on "Read Contract". This page will display all of the read functions. Shown below.

Read Contract Tab

For my example, I'm going to find the balanceOf function, then enter my wallet address and get the amount of CAKE in my wallet.

CAKE Token Balance

Now as we can see from the screenshot above it's letting me know I have 146800968875596945 CAKE. I wish this was true. But this number actually has 18 decimals in it (we can tell that from another read function called decimals), so the real number is 146800968875596945 / 10 ** 18 = .146 CAKE.

Write Operations

A write operation adds a new transaction onto the blockchain and is therefore adding new information. An example of this is harvesting tokens from a yield farm. The transaction adds proof on the blockchain that I have harvested these pending tokens and that they are now in my wallet.

I'm going to complete an example of this below. Specifically, I'm going to harvest from the CAKE pool that I've been using on PancakeSwap.

Again, I'll be using bscscan for this since PancakeSwap is on the Binance Smart Chain, but if you were doing this on a smart contract on the ethereum network, you could use etherscan.

First I need to find the smart contract address of the CAKE pool that I want to harvest from. I know from previous interaction with this pool that this address is 0x73feaa1ee314f8c655e354234017be2193c9e24e. Then I'll search this address, go to "Contract" and then "Write Contract".

Now for write operations I'll need to connect bscscan with my Metamask wallet. This is the same as connecting any other application to Metamask.

Write Contract Tab

Important: please do not call these functions unless you are absolutely confident in how they work and what input to provide. Providing the wrong input can result in losing tokens.

I know from looking at my previous transactions harvesting from this pool, that I need to call the leaveStaking function with an input of 0. This will then send all the pending tokens to my address. Again, it's important to know what to input when performing a write operation.

Harvest Operation Input

Input shown above, this will then pop up a Metamask window to confirm the transaction just like on any dapp. I click confirm and transaction is created. Once it's done, I'll have harvested my tokens just like I could have on the website.