Lesson 12 of 21
In Progress

Interacting with Deployed Contracts

Once you have deployed your contracts to the Ethereum blockchain, you’ll need to interact with them to call their functions and read their state. In this article, we’ll provide a guide for interacting with deployed contracts, using Hardhat, a popular development tool for Ethereum.

Prerequisites

Before you begin, you’ll need to install and set up Hardhat. To install Hardhat, you’ll need Node.js and npm. To set up Hardhat, you’ll need to create a hardhat.config.js file in the root directory of your project.

Connecting to a Network

To interact with your contracts, you’ll need to connect to a network. Hardhat supports several networks, including the Ethereum mainnet, testnets, and local networks.

To connect to a network, you’ll need to specify the network’s provider in your hardhat.config.js file. For example, to connect to the Ethereum mainnet, you can use the following provider configuration:

module.exports = {
  networks: {
    mainnet: {
      url: "https://mainnet.infura.io/v3/YOUR_PROJECT_ID",
      accounts: [
        "0xYOUR_ACCOUNT_ADDRESS",
        "0xANOTHER_ACCOUNT_ADDRESS",
      ],
    },
  },
};

To connect to a testnet, you’ll need to use the testnet’s provider URL and specify your testnet accounts. You can use a tool like Metamask to manage your testnet accounts.

To connect to a local network, you’ll need to start a local node and specify the local network’s provider URL. You can use a tool like Ganache to start a local node.

Interacting with Contracts

Once you have connected to a network, you can interact with your contracts using the hardhat ethers command.

To interact with a contract, you’ll need to specify the contract’s address, the function you want to call, and any arguments that the function requires. For example, to call the getA function of a contract called MyContract at the address 0x123, you can use the following command:

hardhat ethers call --network mainnet 0x123 getA

To call a function that requires arguments, you can pass the arguments as additional arguments to the call command. For example, to call the setA function of

the MyContract contract with the argument 123, you can use the following command:

hardhat ethers call --network mainnet 0x123 setA 123

To read the state of a contract, you can use the hardhat ethers read command. For example, to read the A state variable of the MyContract contract at the address 0x123, you can use the following command:

hardhat ethers read --network mainnet 0x123 A

Contract Wrappers

To make it easier to interact with your contracts, you can use contract wrappers. A contract wrapper is a JavaScript object that represents a contract and provides a friendly interface for calling its functions and reading its state.

To create a contract wrapper, you’ll need to use the @hardhat/contracts module. For example, to create a wrapper for the MyContract contract at the address 0x123, you can use the following code:

const { Contract } = require("@hardhat/contracts");

const myContract = new Contract("MyContract", "0x123");

You can then use the wrapper’s functions to call and read the contract’s functions and state variables. For example, to call the setA function with the argument 123, you can use the following code:

await myContract.setA(123);

To read the A state variable, you can use the following code:

const a = await myContract.A();
console.log(a);

Conclusion

In this article, we provided a guide for interacting with deployed contracts using Hardhat. By following these steps and using contract wrappers, you can easily call functions and read state variables on the Ethereum blockchain.

Exercises

To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.

Create a contract wrapper for the MyContract contract at the address 0x123 on the rinkeby testnet.

const { Contract } = require("@hardhat/contracts");

const myContract = new Contract("MyContract", "0x123", {
  network: "rinkeby",
});

Use the contract wrapper to call the setA function of the MyContract contract with the argument 123.

await myContract.setA(123);

Use the contract wrapper to read the A state variable of the MyContract contract.

const a = await myContract.A();
console.log(a);

Use the hardhat ethers read command to read the B state variable of the MyContract contract at the address 0x123 on the rinkeby testnet.

hardhat ethers read --network rinkeby 0x123 B

Use the hardhat ethers call command to call the getSum function of the MyContract contract at the address 0x123 on the rinkeby testnet.

hardhat ethers call --network rinkeby 0x123 getSum