Continuing Intro to Web3

I’m going to continue learning through Gregory McCubbin’s Web3 tutorial. I’m on part 2, which is an intro to reading data from smart contracts.

Again, I’m not going to re-create Gregory’s tutorial, this blog post right now is serving as essentially notes for myself as I go through them. But if they help someone else too yay!

I learned a thing!

We can get a JavaScript representation of an Ethereum smart contract with the web3.eth.Contract() function. This function expects two arguments: one for the smart contract ABI and one for the smart contract address.

A smart contract ABI stands for "Abstract Binary Interface", and is a JSON array that describes how a specific smart contract works.

The smart contract that we are trying to read in this example implements the ERC-20 standard, and on the documentation site you can find all of the functions that a contract implementing ERC-20 has. The ones that Gregory specifically calls out for this example are:

  • totalSupply()

  • name()

  • symbol()

  • balanceOf()

Ok, so this is pretty neat. I created the ABI const (too long to paste here) and the address was the same from the last exercise. Then I created the contract:

const contract = new web3.eth.Contract(abi, address)

With that, I was able to call the totalSupply function to get the total number of OMG tokens that are in existence:

contract.methods.totalSupply().call((err, result) => { console.log(result) })

My output for this was:

 

And this is the moment when I silly-ly realized I wasn’t actually getting the data back I expected to…

I think I will have to look into this tomorrow though because I have a migraine now. I will probably watch Gregory’s video on this example and the previous one to see what his output looks like and what I might be doing wrong.

Previous
Previous

Part 3 of Intro to Web3

Next
Next

An Intro to Web3