Interacting with the Contract

In this section, we will interact with the HelloWorld contract that deployed in the previous section. Do the following:

  1. Add API_KEY and CONTRACT_ADDRESS variables to the .env file. Your .env file should look like the following:

    API_URL = "https://eth-goerli.g.alchemy.com/v2/<your-api-key>"
    API_KEY = "<your-api-key>"
    PRIVATE_KEY = "<your-metamask-private-key>"
    CONTRACT_ADDRESS = "0x<your contract address>"
    

    Note that the API_KEY is the string at the end of your API_URL. The CONTRACT_ADDRESS is the address that was output at the end of the deployment workflow in the previous section.

  2. Create a file called interact.js in the scripts directory with the following content.

    const API_KEY = process.env.API_KEY;
    const PRIVATE_KEY = process.env.PRIVATE_KEY;
    const CONTRACT_ADDRESS = process.env.CONTRACT_ADDRESS;
    
    const contract = require("../artifacts/contracts/HelloWorld.sol/HelloWorld.json");
    
    // provider - Alchemy
    const alchemyProvider = new ethers.providers.AlchemyProvider(network="goerli", API_KEY);
    
    // signer - your account
    const signer = new ethers.Wallet(PRIVATE_KEY, alchemyProvider);
    
    // contract instance
    const helloWorldContract = new ethers.Contract(CONTRACT_ADDRESS, contract.abi, signer);
    
    async function main() {
        const message = await helloWorldContract.message();
        console.log("The message is: " + message); 
    
        console.log("Updating the message...");
        const tx = await helloWorldContract.update("Hello Again World!");
        await tx.wait();
    
        const newMessage = await helloWorldContract.message();
        console.log("The new message is: " + newMessage); 
    }
    
    main();
    
  3. Run the following command to update the message in the HelloWorld contract.

    npx hardhat run scripts/interact.js --network goerli
    

    You should see the following output after about half a minute. The delay is because the script waits for the transaction included in a new block in the Goerli testnet.

    The message is: Hello World!
    Updating the message...
    The new message is: Hello Again World!
    
  4. The interact.js script above requires you to enter the message in the script itself. It would be nice if we could specify the message on the command line. This can be done by using Hardhat as a library in a standalone Node.js script. Create a new file called interact-cli.js in the scripts directory with the following content.

    const hre = require("hardhat");
    const API_KEY = process.env.API_KEY;
    const PRIVATE_KEY = process.env.PRIVATE_KEY;
    const CONTRACT_ADDRESS = process.env.CONTRACT_ADDRESS;
    
    const contract = require("../artifacts/contracts/HelloWorld.sol/HelloWorld.json");
    
    // provider - Alchemy
    const alchemyProvider = new hre.ethers.providers.AlchemyProvider(network="goerli", API_KEY);
    
    // signer - your account
    const signer = new hre.ethers.Wallet(PRIVATE_KEY, alchemyProvider);
    
    // contract instance
    const helloWorldContract = new hre.ethers.Contract(CONTRACT_ADDRESS, contract.abi, signer);
    
    async function main() {
        await hre.run("compile");
    
        const args = process.argv;
        if (args.length != 3) {
            console.log("Provide a message argument (in single quotes)")
            process.exit(0);
        }
        const new_message_arg = args[2];
    
        const message = await helloWorldContract.message();
        console.log("The message is: " + message); 
    
        console.log("Updating the message...");
        const tx = await helloWorldContract.update(new_message_arg);
        await tx.wait();
    
        const newMessage = await helloWorldContract.message();
        console.log("The new message is: " + newMessage);
    }
    
    main().catch((error) => {
        console.error(error);
        process.exitCode = 1;
    });
    
  5. Run the following command to update the message stored in the HelloWorld contract from the command line. Note the single quotes surrounding the new message.

    node scripts/interact-cli.js 'Hello World 3!'
    

    You should see the following output:

    Nothing to compile
    The message is: Hello World Again!
    Updating the message...
    The new message is: Hello World 3!