Use ethers with the ChainstackProvider
to call smart contract functions.
Overview
This code snippet will show you how to use the ethers.js ChainstackProvider
to call smart contract functions, such as fetching balance and symbol.
Environment setup
Create a new directory for your project, then install the ethers.js
library.
npm install ethers
Initialize ChainstackProvider
Create a new file, index.js
, import the ethers.js library, and initialize a new ChainstackProvider
instance.
Check the docs to find what chains are supported by ChainstackProvider
.
Initialize the ABI and smart contract instance
We need to create a smart contract instance using the address and ABI.
In this case, we only use the part of the ABI describing the symbol()
and balanceOf()
functions.
This specific example retrieves the transfer logs for the APE token, but you can use any ERC-20 token smart contract address.
Function for balance and token symbol
Now, we can use the smart contract instance to fetch the data. Ethers will use eth_call
under the hood.
This function also converts the response and logs it to the console.
Set up account address and call the function
At this point, we can make a constant to hold the address of the account we want to check the balance for, then call the function.
Run the script
Now you can run the script with node index
.
Use ethers with the ChainstackProvider
to call smart contract functions.
Overview
This code snippet will show you how to use the ethers.js ChainstackProvider
to call smart contract functions, such as fetching balance and symbol.
Environment setup
Create a new directory for your project, then install the ethers.js
library.
npm install ethers
Initialize ChainstackProvider
Create a new file, index.js
, import the ethers.js library, and initialize a new ChainstackProvider
instance.
Check the docs to find what chains are supported by ChainstackProvider
.
Initialize the ABI and smart contract instance
We need to create a smart contract instance using the address and ABI.
In this case, we only use the part of the ABI describing the symbol()
and balanceOf()
functions.
This specific example retrieves the transfer logs for the APE token, but you can use any ERC-20 token smart contract address.
Function for balance and token symbol
Now, we can use the smart contract instance to fetch the data. Ethers will use eth_call
under the hood.
This function also converts the response and logs it to the console.
Set up account address and call the function
At this point, we can make a constant to hold the address of the account we want to check the balance for, then call the function.
Run the script
Now you can run the script with node index
.