scripts/folder for the hardhat tutorial, or your home directory for the Truffle tutorial, create a new file named
interact.jsadd the following lines of code:
.envfile and make sure that the
dotenvmodule is loading these variables.
CONTRACT_ADDRESSwhere your smart contract was deployed.
.envfile should look something like this:
interact.jsand see your ABI printed to the console navigate to your terminal and run
initMessage = "Hello world!"? We are now going to read that message stored in our smart contract and print it to the console.
messagefunction in our smart contract and read the init message:
npx hardhat run scripts/interact.jsin the terminal we should see this response:
updatefunction! Pretty cool, right?
updatefunction on our instantiated Contract object, like so:
.wait()on the returned transaction object. This ensures that our script waits for the transaction to be mined on the blockchain before proceeding onwards. If you were to leave this line out, your script may not be able to see the updated
messagevalue in your contract.
messagevalue. Take a moment and see if you can make the changes necessary to print out that new value!
interact.jsfile should look like at this point:
npx hardhat run scripts/interact.js --network ropsten
Updating the message...step takes a while to load before the new message is set. That is due to the mining process! If you are curious about how to track transactions while they are being mined, visit the Alchemy mempool to see the status of your transaction (whether it's pending, mined, or got dropped by the network). If your transaction got dropped, it's also helpful to check Ropsten Etherscan and search for your transaction hash.