Loading...
Ethers.js is a JavaScript library that allows developers to interact with the EVM compatible blockchains.
We will use this library to interact with Polygon blockchain which provides a protocol and a framework for building and connecting Ethereum-compatible blockchain networks. Polygon technology provides developers with the tools to readily deploy a stand-alone network or a secure sidechain that can optionally leverage the security of the Ethereum network via smart contracts.
In this smartbook we will interact with Polygon POS blockchain in nodejs project with ethers library.
We will use storage smart contract which will be deployed on Polygon POS blockchain and interact with it.
Our simple storage smart contract will look like this,
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Storage {
uint number;
function store(uint num) public {
number = num;
}
function retrieve() public view returns (uint){
return number;
}
}
RPC url is required which will communicate with blockchain.
Infura is mostly used for rpc urls. All we have to do is create an account and generate a project which will provide us with rpc url for our api key. Infura now provides polygon rpc urls as well.
But here we will be using maticvigil rpc url for this smartbook. It will provide rpc url for Polygon networks.
You can get test tokens from matic's faucet .
from select token section , select MATIC Token
and from select network section , select Mumbai
Then enter your address and click on submit and confirm.
In order to interact with Polygon POS blockchain , Polygon network must be configured on Metamask.
You can refer this article to configure Metamask.
There are many methods of deployment like using truffle, Hardhat etc. but using Remix IDE is the easiest method for deployment of smart contract. You can refer this for deployment process with Remix. Make sure to switch to polygon network on metamask before selecting the injected web3 option in environment from deploy and transaction section in Remix IDE.
or
You can refer this smartbook for deployment with the truffle.
First initialise nodejs project and create working file with following commands,
> npm init
> touch index.js
After that, install ethers library as follows,
> npm install --save ethers
Import ethers library in index.js
const ethers = require("ethers");
To create a contract instance we need provider and signer. Ethers library have following methods to create both,
// for provider
const provider = new ethers.providers.JsonRpcProvider("https://rpc-mumbai.maticvigil.com/v1/<API key>");
// for signer
var privateKey = "<Provate key of account>"
var signer = new ethers.Wallet(privateKey, provider);
We used wallet method from ethers which will create a signer variable with private key and provider.
Contract object takes three arguments
Here third argument can be signer or provider , if we set third argument with provider it will return read only i.e calling instance of smart contract and if we set it with signer it will return write only i.e. transaction instace of smart contract.
We will create two instances of smart contract here, one with read only access and other with write only access
Code is as follows,
var address = "<Deployed Contract Address>";
var abi = [
. . .//Deployed contract abi
];
myContract_write = new ethers.Contract(address, abi, signer) // Write only
myContract_read = new ethers.Contract(address, abi, provider) // Read only
With this all set, we can now interact with contract.
store() and retrieve() are the function from Storage Smart Contract
myContract_write.store(100).then((result) => {
console.log(result);
})
myContract_read.retrieve().then((result) => {
console.log(result);
})
Now execute index.js file with following command,
> node index.js
This is how ethers can be used to interact with Smart Contract deployed on Polygon POS blockchain with nodejs (backend).
const ethers = require("ethers");
// for provider
const provider = new ethers.providers.JsonRpcProvider("https://rpc-mumbai.maticvigil.com/v1/<API key>");
// for signer
var privateKey = "<Provate key of account>"
var signer = new ethers.Wallet(privateKey, provider);
// Address and ABI
var address = "<Deployed Contract Address>";
var abi = [
. . . //deployed contract abi
];
// Smart Contract instance creation
myContract_write = new ethers.Contract(address, abi, signer) // Write only
myContract_read = new ethers.Contract(address, abi, provider) // Read only
// Writing to Smart Contract
myContract_write.store(100).then((result) => {
console.log(result);
})
// Reading from Smart Contract
myContract_read.retrieve().then((result) => {
console.log(result);
})