Step-by-step Hardhat guide to interact with a deployed Greeter smart contract by updating the greeting smart contract variable.
Step 1: Create an interact.js file
Open up the project previously created for the deployment. Then, inside your scripts/folder, create a new file named interact.js .
This file is where we'll write our interaction script.
Step 2: Read the init Greeting
We will now read the message stored in our smart contract and print it to the console.
Use the code below to call the greet function in our smart contract and read the init greeting:
In the interact.js file, add the following lines of code:
const {ethers} =require("hardhat");asyncfunctionmain() {constGreeter=awaitethers.getContractFactory("Greeter");// Start deployment, returning a promise that resolves to a contract objectconstgreeter=awaitGreeter.deploy("Hello World!"); console.log("Contract deployed to address:",greeter.address);// Read the init greetconstsays=awaitgreeter.greet();console.log(`Greeter says: ${says.toString()}`);}main().then(() =>process.exit(0)).catch(error => {console.error(error);process.exit(1);});
In the terminal, after running the file using
npx hardhat run scripts/interact.js --network node_1DLT
we should see this response:
Contract deployed to address: 0x40381F2CCba1324df6f53eaf79CBa4A1486EFdB5
Greeter says: Hello World!
Congrats! You've just successfully read smart contract data from the 1DLT blockchain, way to go!
Step 6: Update the message
Instead of just reading the message, we can also update the message saved in our smart contract using the setGreeting function!
To do so, we call the setGreeting function on our instantiated Contract object, like so:
// Update the greet
console.log(`Updating the message...`);
let setGreetingTx = await greeter.setGreeting("New greeting! Hello world!");
await setGreetingTx.wait();
Note that we make a call to .wait() on the returned transaction object. This call ensures that our script waits for the transaction to be confirmed on the blockchain before proceeding. If you were to leave this line out, your script might not be able to see the updated greeting value in your contract. The script now should look like this:
asyncfunctionmain() {constGreeter=awaitethers.getContractFactory("Greeter");// Start deployment, returning a promise that resolves to a contract objectconstgreeter=awaitGreeter.deploy("Hello World!"); console.log("Contract deployed to address:",greeter.address);// Read the init greetconstsays=awaitgreeter.greet();console.log(`Greeter says: ${says.toString()}`);// Update the greetconsole.log(`Updating the message...`);let setGreetingTx =awaitgreeter.setGreeting("New greeting! Hello world!");awaitsetGreetingTx.wait();}main().then(() =>process.exit(0)).catch(error => {console.error(error);process.exit(1);});
Step 6: Read the new message
We should be able to repeat Step 2 to read the updated greeting value. Take a moment and see if you can make the changes necessary to print out that new value!
If you need a hint, here's what your interact.js file should look like at this point:
asyncfunctionmain() {constGreeter=awaitethers.getContractFactory("Greeter");// Start deployment, returning a promise that resolves to a contract objectconstgreeter=awaitGreeter.deploy("Hello World!"); console.log("Contract deployed to address:",greeter.address);// Read the init greetconstsays=awaitgreeter.greet();console.log(`Greeter says: ${says.toString()}`);// Update the greetconsole.log(`Updating the message...`);let setGreetingTx =awaitgreeter.setGreeting("New greeting! Hello world!");awaitsetGreetingTx.wait();// Get the greet after the updateconstsaysAgain=awaitgreeter.greet();console.log(`Greeter says again: ${saysAgain.toString()}`);}main().then(() =>process.exit(0)).catch(error => {console.error(error);process.exit(1);});
Now just run the script, and you should be able to see the old message, the updating status, and the new message printed out to your terminal!
npx hardhat run scripts/interact.js --network node_1DLT
Contract deployed to address: 0xD745AAB0CdBdbcf49577bA92Da38602Cef6EA1c2
Greeter says: Hello World!
Updating the message...
Greeter says again: New greeting! Hello world!
While running that script, you may notice that the Updating the message... step takes a while to load before the new greeting is set. This time is due to the confirmation process!