web3.js
web3.js is a JavaScript library that allows developers to interact with EVM-compatible blockchain networks. You can use web3.js to interact with smart contracts deployed on the Base network.Install
To install web3.js run the following command:Setup
Before you can start using web3.js, you need to import it into your project. Add the following line of code to the top of your file to import web3.js:Connecting to Base
You can connect to Base by instantiating a new web3.jsWeb3
object with a RPC URL of the Base network:
To alternatively connect to Base Sepolia (testnet), change the above URL from
https://mainnet.base.org
to https://sepolia.base.org
.Accessing data
Once you have created a provider, you can use it to read data from the Base network. For example, you can use thegetBlockNumber
method to get the latest block:
Deploying contracts
Before you can deploy a contract to the Base network using web3.js, you must first create an account. You can create an account by usingweb3.eth.accounts
:
PRIVATE_KEY
is the private key of the wallet to use when creating the account.Interacting with smart contracts
You can use web3.js to interact with a smart contract on Base by instantiating aContract
object using the ABI and address of a deployed contract:
Contract
object, you can use it to call desired methods on the smart contract:
For more information on deploying contracts on Base, see Deploying a Smart Contract.