scripts/
folder for the hardhat tutorial, or your home directory for the Truffle tutorial, create a new file named interact.js
add the following lines of code:.env
file and make sure that the dotenv
module is loading these variables.API_KEY
and the CONTRACT_ADDRESS
where your smart contract was deployed..env
file should look something like this:contract-interact.js
file:interact.js
and see your ABI printed to the console navigate to your terminal and runinitMessage = "Hello world!"
? We are now going to read that message stored in our smart contract and print it to the console.message
function in our smart contract and read the init message:npx hardhat run scripts/interact.js
in the terminal we should see this response:update
function! Pretty cool, right?update
function 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 message
value in your contract.message
value. Take a moment and see if you can make the changes necessary to print out that new value!interact.js
file 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.