How to use any method of a smart contract using web3

In this tutorial, you are going to learn how to use a smart contract in web3. You will see how to get the smart contract address of a token, see the available functions in a smart contract and how to use any method of a smart contract using web3

In this tutorial, you are going to learn how to use a smart contract in web3. You will see how to get the smart contract address of a token, see the available functions in a smart contract and how to use any method of a smart contract using web3

The first thing you have to do is install web3 using the following command:

npm install web3

Then, to interact with a blockchain (Ethereum or BSC or any other) you need to create an instance of Web3 and give it a provider (like window.ethereum for browser wallets, for example).

After that, you need to connect to the user's wallet using any method you want. You can check out our guides if you want to learn how to connect to a wallet.

The next step is to find the smart contract address you want to interact with.

Let's say we want to interact with the USDT smart contract. We go to etherscan.io and search for USDT. On the left table, there is the contract address on the first row, that's what we need to use so let's copy it.

Now, under the Contract tab we have 2 sections, Read Contract and Write Contract. Read contract is only to get information about the smart contract or the holders and Write contract is to make transactions with the contract like sending tokens.

It is the list of all the actions you can do and all the information you can get with the smart contract. It is a list of functions and you can use them all the same way.

To use one of the contract's functions, you have to define it in the ABI (an interface that will be used to create an instance of the contract) and then use this ABI to instantiate your contract and call the function.

If you click on a function, you can see the inputs it takes and outputs it sends. It will be useful in the ABI.

Let's say we want to use the totalSupply function to get the total market cap of USDT. We would use this code:

// need a Web3 instance initialized with a provider like window.ethereum for browser wallets, in a variable called web3, to use this code stored
// also need a connected wallet using that instance

const address = "0xdac17f958d2ee523a2206206994597c13d831ec7"
const ABI = [{
  constant: true,
  inputs: [],
  name: 'totalSupply',
  outputs: [{ name: '', type: 'uint256' }],
  payable: false,
  type: 'function'
}]

const contract = new web3.eth.Contract(ABI, address)
const totalUSDTSupply = await contract.methods.totalSupply().call()

Here we only need the totalSupply function so it's the only one we define in our ABI. It takes no input and outputs a uint256 which is a large integer that represents the amount without using floaters.

To get this amount in the right unit, meaning 1 = 1USDT, you would use:

Web3.utils.fromWei(totalUSDTSupply, "ether")

Once you have the contract, you can call any method defined in the ABI through the methods property of your contract instance and add .call() after calling the function.

Thanks for reading this article!