ethers.js
ethers.js 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 Pivotal network.
Install
To install ethers.js run the following command:
npm install --save ethersSetup
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 Pivotal
You can connect to Pivotal by instantiating a new ethers.js JsonRpcProvider object with a RPC URL of the Pivotal network:
const ethers = require('ethers');
const url = 'https://mainnet.thepivotal.xyz';
const provider = new ethers.providers.JsonRpcProvider(url);Reading data from the blockchain
Once you have created a provider, you can use it to read data from the Pivotal network.
For example, you can use the getBlockNumber method to get the latest block:
async function getLatestBlock() {
const latestBlock = await provider.getBlockNumber();
console.log(latestBlock);
}Writing data to the blockchain
In order to write data to the Pivotal 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.
const privateKey = 'PRIVATE_KEY';
const signer = new ethers.Wallet(privateKey, provider);Interacting with smart contracts
You can use ethers.js to interact with a smart contract on Pivotal by instantiating a Contract object using the ABI and address of a deployed contract:
const abi = [
… // ABI of deployed contract
];
const contractAddress = "CONTRACT_ADDRESS"
// read only
const contract = new ethers.Contract(contractAddress, abi, provider);For write-only contracts, provide a Signer object instead of a Provider object:
// write only
const contract = new ethers.Contract(contractAddress, abi, signer);Once you have created a Contract object, you can use it to call desired methods on the smart contract:
async function setValue(value) {
const tx = await contract.set(value);
console.log(tx.hash);
}
async function getValue() {
const value = await contract.get();
console.log(value.toString());
}Last updated

