Once you have deployed a smart contract on the Ethereum blockchain using Hardhat, you can interact with it by calling its functions. This guide will explain how to call functions on a deployed contract, including both read and write operations. We will use the previously defined MyToken
contract as an example.
1. Prerequisites
Before you start calling functions on a deployed contract, ensure you have:
- A Hardhat project set up with a deployed contract.
- The contract's ABI (Application Binary Interface) and address.
2. Understanding Contract Functions
Smart contracts can have two types of functions:
- View Functions: These functions do not modify the state of the contract and can be called without any gas fees. They are used to read data from the blockchain.
- Non-View Functions: These functions modify the state of the contract and require gas fees to execute. They are used to perform transactions such as transferring tokens.
3. Calling Functions on a Deployed Contract
To call functions on a deployed contract, you can create a script in your Hardhat project. Below, we will create a script that demonstrates how to call both view and non-view functions of the MyToken
contract.
3.1. Create a Script
Create a new script file in the scripts
directory named callFunctions.js
:
const hre = require("hardhat");
async function main() {
// Replace with your deployed contract address
const tokenAddress = "YOUR_DEPLOYED_TOKEN_ADDRESS"; // e.g., "0x5B...B7D"
// Get the contract instance
const Token = await hre.ethers.getContractFactory("MyToken");
const token = Token.attach(tokenAddress);
// Call view functions
const name = await token.name();
const symbol = await token.symbol();
const totalSupply = await token.totalSupply();
console.log(`Token Name: ${name}`);
console.log(`Token Symbol: ${symbol}`);
console.log(`Total Supply: ${totalSupply.toString()}`);
// Call a non-view function (transfer tokens)
const [owner, addr1] = await hre.ethers.getSigners();
const amount = hre.ethers.utils.parseUnits("100", 18); // 100 tokens
console.log(`Transferring ${amount.toString()} tokens from ${owner.address} to ${addr1.address}`);
const transferTx = await token.transfer(addr1.address, amount);
await transferTx.wait();
console.log(`Transfer successful!`);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
3.2. Explanation of the Script
In this script:
- Getting the Contract Instance: We use
hre.ethers.getContractFactory
to get the contract factory and then attach it to the deployed contract address usingToken.attach(tokenAddress)
. - Calling View Functions: We call the
name
,symbol
, andtotalSupply
functions, which return the token's details without modifying the state. - Calling a Non-View Function: We call the
MyToken
0 function to send tokens from one account to another. This operation modifies the state and requires gas fees.
3.3. Running the Script
To run the script, execute the following command in your terminal:
MyToken
1
Make sure to replace MyToken
2 in the script with the actual address of your deployed token. You should see the token's name, symbol, total supply, and a confirmation of the token transfer in the console output.
4. Conclusion
Calling functions on a deployed contract is a fundamental aspect of interacting with smart contracts on the Ethereum blockchain. Using Hardhat, you can easily create scripts to call both view and non-view functions, allowing you to read data and perform transactions.