Overview¶
Similar to Web3.js, the Ethers.js library offers a comprehensive suite of tools for interacting with Ethereum nodes using JavaScript. In addition, Darwinia provides an Ethereum-compatible API that supports JSON RPC invocations, making it fully compatible with the Ethers.js library. This compatibility enables developers to utilize the Ethers.js library to interact with a Darwinia node in a manner similar to interacting with an Ethereum node. This allows developers to leverage their existing knowledge and tools to build applications on the Darwinia network seamlessly.
Prerequisites¶
Install Solidity Compiler¶
The solc compiler is a JavaScript library used to retrieve the code and ABI metadata of a Solidity smart contract by compiling it. This allows developers to access the necessary information for interacting with the smart contract programmatically. Run the following command to install:
Check the installed result by:
The output:
Install Ethers.js¶
In this tutorial, we will explore the usage of Ethers.js by deploying a contract and performing various contract functionalities. The tutorial involves multiple script files, so let's create a new JavaScript project called example-with-ethersjs
to organize and manage the code effectively. This project will serve as a workaround for implementing the Ethers.js functionality.
Install the ethers by running the following command:
Contract Interaction¶
Note
The network provider used in this tutorial is the Koi testnet. However, the concepts and techniques covered in this tutorial are applicable to other Darwinia networks as well.
Prepare Contract¶
Create a smart contract file by running this command:
To showcase the interaction with the smart contract, we have prepared a simple Solidity smart contract. You can find the contract code pasted into the storage.sol
file. This contract will serve as the basis for demonstrating how to interact with it using ethers.js.
This contract is straightforward and easy to understand. It consists of two functionalities: storing a number and retrieving the updated number. Let's proceed to learn how to interact with this contract using ethers.js.
Generate Contract Metadata¶
When interacting with a contract using Ethers.js, it is essential to have the contract metadata. The toolchain relies on this metadata information to properly interact with the contract. To generate the metadata and store it in a metadata.json
file, you can run the following command:
The output of cat metadata.json
:
{
"contracts": {
"storage.sol:Storage": {
"abi": [
{
"inputs": [],
"name": "retrieve",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "num",
"type": "uint256"
}
],
"name": "store",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
],
"bin": "608060405234801561001057600080fd5b50610150806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80632e64cec11461003b5780636057361d14610059575b600080fd5b610043610075565b60405161005091906100a1565b60405180910390f35b610073600480360381019061006e91906100ed565b61007e565b005b60008054905090565b8060008190555050565b6000819050919050565b61009b81610088565b82525050565b60006020820190506100b66000830184610092565b92915050565b600080fd5b6100ca81610088565b81146100d557600080fd5b50565b6000813590506100e7816100c1565b92915050565b600060208284031215610103576101026100bc565b5b6000610111848285016100d8565b9150509291505056fea264697066735822122084d3253ae949e222c0defd4cd0cd1238db9d84a3fee248e7cd91529d9a759ad164736f6c63430008110033",
"hashes": {
"retrieve()": "2e64cec1",
"store(uint256)": "6057361d"
}
}
},
"version": "0.8.17+commit.8df45f5f.Linux.g++"
}
Deploy Storage Contract¶
Create a deploy script by running this command:
And paste the following content into it:
Start the deployment by running the command:
The output like this:
Attempting to deploy from account 0x6Bc9543094D17f52CF6b419FB692797E48d275d0
Contract deployed at address: 0x24e263941c13bD12EEaAdba64531385e83103908
0xE175242B7509e32a18973Ab622Ea3349a6C47429
is the address of the deployed storage contract, as a unique identifier for that contract. It will be used later to store and retrieve the number.
Store Number¶
Create a store script by running this command:
Please paste the following content into the store.js
file.
Tip
Ensure that the const contractAddress = '0x24e263941c13bD12EEaAdba64531385e83103908
in the script updated to your deployed contract.
Run the script by the command:
The output:
Retrieve Number¶
Create a retrieve script by running this command:
Please paste the following content into the retrieve.js
file.
Tip
Ensure that the const contractAddress = '0x24e263941c13bD12EEaAdba64531385e83103908
in the script updated to your deployed contract.
Run the script by the command:
The output: