Interacting with a Smart Contract

Learn how to call smart contract methods using the web3.js library

After the last lesson, we have a smart contract deployed to the Goerli network. Now, we'll see how we can call its method using web3.js. To do this, we'll write a new script that'll first register a new address in our name service contract and then try to read the address associated with this name.

New configuration

The new script we'll write will need to know the smart contract address we deployed in the previous lesson. Since we already have the .env file to store the configuration of our application, we'll add one more variable called NAME_SERVICE_ADDRESS with the contract address.

After adding the smart contract’s address variable to the configuration file, it now looks like this:

WEB3_URL=https://goerli.infura.io/v3/c35694634e9d4838935c7a5a90d4c7b5
PRIVATE_KEY=0xbedda80c63c...620f0dc1
NAME_SERVICE_ADDRESS=0x101BCEf71570D483343Ea508d4b22bDaC3170744

Sending a transaction

We're now ready to write the new script that'll call a method on our smart contract. We’ll call this new script ...