Powered By GitBook
πŸ’»
Interacting with a Smart Contract
Step by step guide on interacting with a deployed Ethereum smart contract by updating a smart contract variable.
Before starting this tutorial on interacting with a smart contract, you should have completed part 1 β€” Hello World Smart Contract (creating and deploying a smart contract). In part 3 we'll go over submitting our contract to Etherscan so anyone can understand how to interact with it!

Part 2: Interact with your Smart Contract

Now that we've successfully deployed a smart contract to the ropsten network, let's test out our web3 skills and interact with it!

Step 1: Create a interact.js file

This is the file where we'll write our interaction script. We'll be using the Ethers.js library that you previously installed in Part 1.
Inside your 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:
1
// interact.js
2
​
3
const API_KEY = process.env.API_KEY;
4
const PRIVATE_KEY = process.env.PRIVATE_KEY;
5
const CONTRACT_ADDRESS = process.env.CONTRACT_ADDRESS;
Copied!

Step 2: Update your .env file

We will be using new environment variables, so we need to define them in our .env file and make sure that the dotenv module is loading these variables.
We'll need to add a definition for our Alchemy API_KEY and the CONTRACT_ADDRESS where your smart contract was deployed.
Your .env file should look something like this:
1
# .env
2
​
3
API_URL = "https://eth-ropsten.alchemyapi.io/v2/<your-api-key>"
4
API_KEY = "<your-api-key>"
5
PRIVATE_KEY = "<your-metamask-private-key>"
6
CONTRACT_ADDRESS = "0x<your contract address>"
Copied!

Step 3: Grab your contract ABI

Our contract ABI (Application Binary Interface) is the interface to interact with our smart contract. You can learn more about Contract ABIs here. Hardhat (and Truffle) automatically generates an ABI for us and saves it in the HelloWorld.json file. In order to use this we'll need to parse out the contents by adding the following lines of code to our contract-interact.js file:
1
// interact.js
2
​
3
// For Truffle
4
const contract = require("./build/contracts/HelloWorld.json");
5
​
6
// For Hardhat
7
const contract = require("../artifacts/contracts/HelloWorld.sol/HelloWorld.json");
Copied!
If you want to see the ABI you can print it to your console:
1
console.log(JSON.stringify(contract.abi));
Copied!
To run interact.js and see your ABI printed to the console navigate to your terminal and run
Hardhat:
1
npx hardhat run scripts/interact.js
Copied!
Truffle:
1
node interact.js
Copied!

Step 4: Create an instance of your contract

In order to interact with our contract we need to create an instance of it in our code. To do so with Ethers.js, we'll need to work with three concepts:
    1.
    Provider - this is a node provider that gives you read and write access to the blockchain.
    2.
    Signer - this represents an Ethereum account that has the ability to sign transactions.
    3.
    Contract - this is an Ethers.js object that represents a specific contract deployed on-chain.
We'll use the contract ABI from the previous step to create our instance of the contract:
1
// interact.js
2
​
3
// Provider
4
const alchemyProvider = new ethers.providers.AlchemyProvider(network="ropsten", API_KEY);
5
​
6
// Signer
7
const signer = new ethers.Wallet(PRIVATE_KEY, alchemyProvider);
8
​
9
// Contract
10
const helloWorldContract = new ethers.Contract(CONTRACT_ADDRESS, contract.abi, signer);
Copied!
You can read more about Providers, Signers, and Contracts in the Ethers.js documentation.

Step 5: Read the init message

Remember when we deployed our contract with theinitMessage = "Hello world!"? We are now going to read that message stored in our smart contract and print it to the console.
In JavaScript we use asynchronous functions to interact with networks. Check out this article to learn more about async functions.
Use the code below to call the message function in our smart contract and read the init message:
1
// interact.js
2
​
3
// ...
4
​
5
async function main() {
6
const message = await helloWorldContract.message();
7
console.log("The message is: " + message);
8
}
9
main();
Copied!
After running the file using npx hardhat run scripts/interact.js in the terminal we should see this response:
1
The message is: Hello world!
Copied!
Congrats! You've just successfully read smart contract data from the Ethereum blockchain, way to go!

Step 6: Update the message

Now instead of just reading the message, we can also update the message saved in our smart contract using the update function! Pretty cool, right?
In order to do so we can directly call the update function on our instantiated Contract object, like so:
1
// interact.js
2
​
3
// ...
4
​
5
async function main() {
6
const message = await helloWorldContract.message();
7
console.log("The message is: " + message);
8
9
console.log("Updating the message...");
10
const tx = await helloWorldContract.update("This is the new message.");
11
await tx.wait();
12
}
13
main();
Copied!
Note that we make a call to .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.

Step 7: Read the new message

You should be able to repeat Step 5 to read the updated message 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:
1
// interact.js
2
​
3
const API_KEY = process.env.API_KEY;
4
const PRIVATE_KEY = process.env.PRIVATE_KEY;
5
const CONTRACT_ADDRESS = process.env.CONTRACT_ADDRESS;
6
​
7
const contract = require("../artifacts/contracts/HelloWorld.sol/HelloWorld.json");
8
​
9
// provider - Alchemy
10
const alchemyProvider = new ethers.providers.AlchemyProvider(network="ropsten", API_KEY);
11
​
12
// signer - you
13
const signer = new ethers.Wallet(PRIVATE_KEY, alchemyProvider);
14
​
15
// contract instance
16
const helloWorldContract = new ethers.Contract(CONTRACT_ADDRESS, contract.abi, signer);
17
​
18
async function main() {
19
const message = await helloWorldContract.message();
20
console.log("The message is: " + message);
21
​
22
console.log("Updating the message...");
23
const tx = await helloWorldContract.update("this is the new message");
24
await tx.wait();
25
​
26
const newMessage = await helloWorldContract.message();
27
console.log("The new message is: " + newMessage);
28
}
29
​
30
main();
Copied!
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 ropsten
1
The message is: Hello World!
2
Updating the message...
3
The new message is: This is the new message.
Copied!
While you are running that script, you may notice that the 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.
And that's it! You've now deployed AND interacted with an Ethereum smart contract. If you'd like to publish your contract to Etherscan so that anyone will know how to interact with it, check out part 3: submitting your smart contract to etherscan!
πŸŽ‰
​
Once you complete this tutorial, let us know how your experience was or if you have any feedback by tagging us on Twitter @alchemyplatform!
Last modified 8d ago