ethers.js

ethers.jsarrow-up-right is a JavaScript library that allows developers to interact with EVM-compatible blockchain networks.

You can use ethers.js to interact with smart contracts deployed on the Xhavic network.


To install ethers.js run the following command:

npm install --save ethers

Before you can start using ethers.js, you need to import it into your project.

Add the following line of code to the top of your file to import ethers.js:

const ethers = require('ethers');

Connecting to Xhavicarrow-up-right

You can connect to Xhavic by instantiating a new ethers.js JsonRpcProvider object with a RPC URL of the Xhavic network:

const ethers = require('ethers');

const url = 'https://testrpc.Xhavicledger.io/';
const provider = new ethers.providers.JsonRpcProvider(url);

Reading data from the blockchainarrow-up-right

Once you have created a provider, you can use it to read data from the Xhavic network.

For example, you can use the getBlockNumber method to get the latest block:

Writing data to the blockchainarrow-up-right

In order to write data to the Xhavic network, you need to create a Signer.

You can create a Signer by instantiating a new ethers.js Wallet object, providing it with a private key and Provider.

circle-info

INFO

PRIVATE_KEY is the private key of the wallet to use when creating the signer.

Interacting with smart contractsarrow-up-right

You can use ethers.js to interact with a smart contract on Xhavic by instantiating a Contract object using the ABI and address of a deployed contract:

For write-only contracts, provide a Signer object instead of a Provider object:

circle-info

INFO

CONTRACT_ADDRESS is the address of the deployed contract.

Once you have created a Contract object, you can use it to call desired methods on the smart contract:

Last updated

Was this helpful?