Files
web/apps/base-docs/docs/tools/ethers.md
taycaldwell 47ca5b7b77 Docs Site Revamp: Tutorials (#370)
* Add discord, twitter, and github icons.

* Update docusaurus.

* Move security page to sidebar.

* Remove copyright from site footers.

* Add caret icons for dropdown css override.

* Build and style custom docs navbar.

* Resolve Icon merge conflict.

* Change Developers dropdown to Builders.

* lint: Remove unused variable

* Add tutorials; initial commit

* Add and display new frontmatter

* Convert tutorials to single page pt. 1

* Update tutorial data; fix paths

* Ecopage - rubyscore + lore logos (#373)

* Update ecosystem.json (#374)

* Fix incorrectly rendering open graph metadata (#369)

* feat(web): Serve dynamic og:metadata server-side

* feat(docs): Serve dynamic og:metadata server-side

* fix(web): Resolve linting errors

* fix(web): Linting

* fix(web): Additional linting corrections

* fix(web): Resolve Typescript-related syntax error

* refactor(web): Add ogData to data structure

* fix(ecosystem): Typo in partner image name (#375)

* feat(bridge): Add planned paused note to top of page (#376)

* Update hyperframes to use state (#377)

* Update hyperframes to use state

* Respond to feedback

* fix(bridge): Add default open graph metadata (#378)

* Added Moralis to data indexers (#371)

* Added Moralis to data indexers

* typos

* Update copy

* Remove superlatives

---------

Co-authored-by: taycaldwell <taylor.lee.caldwell@gmail.com>

* docs(bridge): Update bridge pause to new date (#381)

* mention setting `OP_NODE_L1_BEACON` (#380)

* feat(docs): Add Uniswap V3 Base Sepolia contracts (#382)

* Improve loading experience on jobs page (#389)

* Docs Site Revamp: Navbar, Sidebar, and Doc Page (#379)

* Add discord, twitter, and github icons.

* Update docusaurus.

* Move security page to sidebar.

* Remove copyright from site footers.

* Add caret icons for dropdown css override.

* Build and style custom docs navbar.

* Resolve Icon merge conflict.

* Change Developers dropdown to Builders.

* lint: Remove unused variable

* Re-add node polyfills required for cookie manager to work.

* Disable DocFeedback component.

* Disable paginator and table of contents.

* Add collapse icons for css override.

* Fix Modal overlay styles.

* Adjust DocChat floating button position.

* Reorganize and restyle sidebar for new design.

* Update gray0 and modal overlay styles.

* Add stylesheet for new doc page styles.

* Remove TODO. Add sidebar link hover styles.

* Move responsive styles to bottom.

* Disable breadcrumb component. Update layout spacing.

---------

Co-authored-by: taycaldwell <taylor.lee.caldwell@gmail.com>

* refactor(bridge): Drop bridge maintenance notice (#390)

* Add tutorials; initial commit

* Add and display new frontmatter

* Update tutorial data

* Fix frontmatter

* Update TOC

* Update tutorials page

* Update toc margin

* fix nested categories in sidebar

* Add all tutorials back link

---------

Co-authored-by: Jacob Moore <jacob.moore@coinbase.com>
Co-authored-by: Kathryn <kathryn.snow@coinbase.com>
Co-authored-by: wbnns <hello@wbnns.com>
Co-authored-by: Brian Doyle <brian.doyle@coinbase.com>
Co-authored-by: Filip Martinsson <martinsson.filip@gmail.com>
Co-authored-by: abhi <abhijeet.bhagat@gmx.com>
Co-authored-by: Matthew Bunday <matthew.bunday@coinbase.com>
2024-03-28 11:33:18 -07:00

3.3 KiB

title, slug, description, keywords, hide_table_of_contents
title slug description keywords hide_table_of_contents
ethers.js /tools/ethers Documentation for using ethers.js, a JavaScript library for EVM-compatible blockchain interactions. This page covers installation, setup, connecting to the Base network, reading and writing blockchain data, and interacting with smart contracts.
ethers.js
JavaScript library
Base network
Base mainnet
Base testnet
smart contracts
EVM-compatible
blockchain
JsonRpcProvider
Signer
ABI
interacting with smart contract
true

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 Base network.


Install

To install ethers.js run the following command:

npm install --save ethers

Setup

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 Base

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

const ethers = require('ethers');

const url = 'https://mainnet.base.org';
const provider = new ethers.providers.JsonRpcProvider(url);

:::info

To alternatively connect to Base Sepolia (testnet), change the above URL from https://mainnet.base.org to https://sepolia.base.org.

:::

Reading data from the blockchain

Once you have created a provider, you can use it to read data from the Base 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 Base 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);

:::info

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

:::

Interacting with smart contracts

You can use ethers.js to interact with a smart contract on Base 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);

:::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:

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());
}