LLM Notice: This documentation site supports content negotiation for AI agents. Request any page with Accept: text/markdown or Accept: text/plain header to receive Markdown instead of HTML. Alternatively, append ?format=md to any URL. All markdown files are available at /md/ prefix paths. For all content in one file, visit /llms-full.txt
Skip to main content

Ethers.js

ethers.js is a powerful JavaScript library for interacting with Ethereum and other EVM-compatible blockchain networks.

In this guide, we'll walk you through how to use ethers.js to interact with smart contracts on the Flow Blockchain.


Installation

To begin using ethers.js in your project, you'll need to install the package. To do this, run the following command:


_10
bashCopy code
_10
npm install --save ethers

Setup

After you install ethers.js, the next step is to import it into your project.

To do this, add the following line of code at the beginning of your JavaScript file:


_10
const ethers = require('ethers');

Connect to Flow

To connect to the Flow Blockchain with ethers.js, you need to create a new JsonRpcProvider instance with the appropriate RPC URL for Flow:


_10
const ethers = require('ethers');
_10
_10
const url = 'https://testnet.evm.nodes.onflow.org/';
_10
const provider = new ethers.providers.JsonRpcProvider(url);

Note: If you want to connect to the Flow mainnet, replace the above URL with https://mainnet.evm.nodes.onflow.org.

Read data from the Blockchain

After you set up your provider, you can start reading data from the Flow Blockchain. For instance, to retrieve the latest block number, you can use the getBlockNumber method:


_10
async function getLatestBlock() {
_10
const latestBlock = await provider.getBlockNumber();
_10
console.log(latestBlock);
_10
}

Write data to the Blockchain

To send transactions or write data to the Flow Blockchain, you need to create a Signer. To do this, initialize a new Wallet object with your private key and the previously created Provider:


_10
const privateKey = 'YOUR_PRIVATE_KEY';
_10
const signer = new ethers.Wallet(privateKey, provider);

Note: Replace 'YOUR_PRIVATE_KEY' with the actual private key of the wallet you want to use.

Interact with smart contracts

ethers.js also allows interaction with smart contracts on the Flow Blockchain. To do this, create a Contract object using the Application Binary Interface (ABI) and the address of the deployed contract:


_10
const abi = [
_10
// ABI of deployed contract
_10
];
_10
_10
const contractAddress = 'CONTRACT_ADDRESS';
_10
_10
// read-only contract instance
_10
const contract = new ethers.Contract(contractAddress, abi, provider);

For contracts that require writing, you'll need to provide a Signer object instead of a Provider:


_10
// write-enabled contract instance
_10
const contract = new ethers.Contract(contractAddress, abi, signer);

Note: Replace 'CONTRACT_ADDRESS' with the actual address of your deployed contract.

After you set up your Contract object, you can call methods on the smart contract as needed:


_10
async function setValue(value) {
_10
const tx = await contract.set(value);
_10
console.log(tx.hash);
_10
}
_10
_10
async function getValue() {
_10
const value = await contract.get();
_10
console.log(value.toString());
_10
}