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
};
CONTRACT_ADDRESS
is not set correctly, can you log this out in your script, to check that it is correct.CONTRACT_ADDRESS
properly in my env file. After fixing that, it worked