Overview¶
Web3.js is a comprehensive library that enables developers to communicate with Ethereum nodes using various protocols such as HTTP or WebSocket in JavaScript. Darwinia offers an Ethereum-Compatible API that supports JSON RPC calls similar to Ethereum. As a result, developers can utilize the Web3.js library to interact with a Darwinia node in the same way they would interact with an Ethereum node. This compatibility simplifies the process of building applications that interact with Darwinia using familiar Ethereum tools and libraries.
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 Web3.js¶
In this tutorial, we will explore the usage of web3.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-web3js
to organize and manage the code effectively. This project will serve as a workaround for implementing the web3.js functionality.
Install the Web3.js by runing 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 web3.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 web3.js.
Generate Contract Metadata¶
When interacting with a contract using Web3.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: 0x677163264bcb88A6f8F71E2B7D88F51d54325AB1
0x677163264bcb88A6f8F71E2B7D88F51d54325AB1
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 = '0x677163264bcb88A6f8F71E2B7D88F51d54325AB1'
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 = '0x677163264bcb88A6f8F71E2B7D88F51d54325AB1'
in the script updated to your deployed contract.
Run the script by the command:
The output: