0

I was following This tutorial from Alchemy to build a HelloWorld smart contract.

After successfully deploying it to alchemy, when I wrote the 'Interact.js' script from the tutorial and tried to run it, it got me the following error

my solidity compiler version: 0.8.0

PS E:\programming\Tutorials\Blockchain tutorials\hello world alchemy> npx hardhat run scripts/interact.js

E:\programming\Tutorials\Blockchain tutorials\hello world alchemy\node_modules\@ethersproject\logger\src.ts\index.ts:225
        const error: any = new Error(message);
                           ^
Error: invalid contract address or ENS name (argument="addressOrName", value=undefined, code=INVALID_ARGUMENT, version=contracts/5.5.0)
    at Logger.makeError (E:\programming\Tutorials\Blockchain tutorials\hello world alchemy\node_modules\@ethersproject\logger\src.ts\index.ts:225:28)
    at Logger.throwError (E:\programming\Tutorials\Blockchain tutorials\hello world alchemy\node_modules\@ethersproject\logger\src.ts\index.ts:237:20)
    at Logger.throwArgumentError (E:\programming\Tutorials\Blockchain tutorials\hello world alchemy\node_modules\@ethersproject\logger\src.ts\index.ts:241:21)
    at Contract.BaseContract (E:\programming\Tutorials\Blockchain tutorials\hello world alchemy\node_modules\@ethersproject\contracts\src.ts\index.ts:706:20)
    at new Contract (E:\programming\Tutorials\Blockchain tutorials\hello world alchemy\node_modules\@ethersproject\contracts\lib\index.js:1047:42)
    at Object.<anonymous> (E:\programming\Tutorials\Blockchain tutorials\hello world alchemy\scripts\interact.js:17:28)
    at Module._compile (node:internal/modules/cjs/loader:1108:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
    at Module.load (node:internal/modules/cjs/loader:988:32)
    at Function.Module._load (node:internal/modules/cjs/loader:828:14)

This is my Smart contract code:

//SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract HelloWorld {
    event UpdatedMessages( string oldStr, string newStr);

    string public message;

    constructor( string memory initMessage ) {
        message = initMessage;
    }


    function update(string memory newMessage) public {
        string memory oldMessage = message;
        message = newMessage;

        emit UpdatedMessages(oldMessage, message);

    }
}

This is my interact.js code:

const API_KEY = process.env.API_KEY;
const PRIVATE_KEY = process.env.PRIVATE_KEY;
const CONTRACT_ADDRESS = process.env.CONTRACT_ADDRESS;
const { ethers } = require("hardhat");
const contract = require("../artifacts/contracts/HelloWorld.sol/HelloWorld.json");

//provider - alchemy
const alchemyProvider = new ethers.providers.AlchemyProvider(
  (network = "goerli"),
  API_KEY
);

//signer - Me
const signer = new ethers.Wallet(PRIVATE_KEY, alchemyProvider);

// contract instance
const helloWorldContract = new ethers.Contract(
  CONTRACT_ADDRESS,
  contract.abi,
  signer
);

const main = async () => {
  const message = await helloWorldContract.message();
  console.log("The message is: " + message);
  console.log("updating the message...");

  const tx = await helloWorldContract.update(
    "This is the new updated message."
  );

  await tx.wait();

  const newMessage = await helloWorldContract.message();
  console.log("The new message is: " + newMessage);
};

main()
  .then(() => process.exit(0))
  .catch((err) => {
    console.error(err);
    process.exit(1);
  });

My hardhat.config.js code:

/**
 * @type import('hardhat/config').HardhatUserConfig
 */

require('dotenv').config();
require("@nomiclabs/hardhat-ethers");

const { API_URL, PRIVATE_KEY } = process.env;

module.exports = {
  solidity: "0.8.0",
  defaultNetwork: "goerli",
  networks: {
    hardhat: {},
    goerli: {
      url: API_URL,
      accounts: [`0x${PRIVATE_KEY}`]
    }
  }
  // api key of alchemy
  // wallet password
};
2
  • 1
    It seems CONTRACT_ADDRESS is not set correctly, can you log this out in your script, to check that it is correct.
    – Richard
    Jan 25 at 15:38
  • 1
    Hey, @Richard thanks, yep I didn't set up my CONTRACT_ADDRESS properly in my env file. After fixing that, it worked Jan 25 at 17:07

1 Answer 1

0

Like Richard mentioned in the comments, it seems that CONTRACT_ADDRESS is not set correctly, based on the error you're seeing:

Error: invalid contract address or ENS name

Currently you're pulling CONTRACT_ADDRESS from your .env file. You should be able to fix this problem by updating your environment variable to the correct address.

For example, if this is your .env:

API_URL=https://eth-goerli.alchemyapi.io/v2/<redacted>
PRIVATE_KEY=<redacted>
CONTRACT_ADDRESS=0x5180db8f5c931aae63c74266b211f580155ecac8 // double check this address

You should check if the contract address 0x5180db8f5c931aae63c74266b211f580155ecac8 is actually a deployed contract on the Goerli network.

You can use etherscan to check for example: https://goerli.etherscan.io/

If it's not there, then you just need to update with the correct CONTRACT_ADDRESS!

Hope that helps :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.