API Reference
DocumentationLog In

This guide demonstrates how to integrate your Coinbase Cloud Node project with Ethers.js.

Prerequisites

In order to complete this guide, you need a Node.js project.

To create a new project, run:

npm init -y

Install

Once you have a working Node.js project, add ethers.js as a dependency.

To install web3.js, run the following command from the root directory of your project:

npm install ethers

Setup

Import

Before you can start using ethers.js in your project’s code, you need to import it.

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

const { ethers } = require("ethers");

Authorization

Users that create a Coinbase Node project, are provided with the following credentials:

  • An API endpoint
  • A username
  • A password

These credentials allow you to make authorized requests for reading and writing to the blockchain.

To add these credentials to your project:

  1. Create a file named .env to store environment variables
  2. Add the following environment variables to the .env file, replacing the values with your project’s credentials.
NODE_ENDPOINT=<YOUR_ENDPOINT>
NODE_USERNAME<YOUR_USERNAME>
NODE_PASSWORD<YOUR_PASSWORD>

To access these environment variables within your project, add the following lines of code to your JavaScript file:

const url = process.env.NODE_ENDPOINT;
const username = process.env.NODE_USERNAME;
const password = process.env.NODE_PASSWORD;

📘

Note: For more information on how to create a Coinbase Node project, see Using Ethereum Clients.

Connecting to a node

In order to access data from the blockchain, you need to connect to the Ethereum node provided by your Coinbase Node project.

To connect to a node using ethers.js, you can use the JsonRpcProvider method.

JsonRpcProvider accepts an endpoint URL, a username, and a password.

Copy the following code to create a node provider using ethers.js and your Coinbase Node project’s credentials:

const { ethers } = require("ethers");

// Import environment variables
const baseUrl = process.env.NODE_ENDPOINT;
const username = process.env.NODE_USERNAME;
const password = process.env.NODE_PASSWORD;

// Create node provider using project credentials
const provider = new ethers.providers.JsonRpcProvider({
   url: baseUrl,
   user: username,
   password : password
})

Accessing blockchain data

Now that you have a node provider, you can access data from the blockchain.

To demonstrate reading data from the blockchain using ethers.js, copy the following code:

// Function to read information from the blockchain
async function getBlockchainInfo () {

   let latestBlock = await provider.getBlockNumber().then((result) => {
       console.log("Latest Ethereum Block is ", result);
       return result;
     });

   let getTransaction = await provider.getBlock(latestBlock).then(result => {
       console.log("Information about block", result)
       if (result.transactions.length !== null) {
           return result.transactions[0];
       }
       else {
           console.log(`No transactions found for ${latestBlock}`)
           return null;
       }
   });

   if (getTransaction != null) {
       let getTransactionInfo = provider.getTransaction(getTransaction).then(result => {
           console.log("Information about transaction", result)
           return;
       })
   }
}

// Execute function
getBlockchainInfo()

The code above performs the following:

  • Gets the latest Ethereum block number
  • Gets a information for a block using it’s block number
  • Gets information about a transaction within a block

To run the code above, run the following command:

node app.js

Running the code results in an output similar to the following:

Latest Ethereum Block is  14923247
Information about block {
  hash: '0xe0bebf1f444ecabb02df4aeb98b106174f980845b126b77fa9355205491765dc',
  parentHash: '0x95a911940d675ebe2855ac651b05b7dea9fb3e4fe7945a346df9d709dc93ea7a',
  number: 14923247,
  timestamp: 1654640462,
  nonce: '0x000400003ed65dbc',
  difficulty: null,
  gasLimit: BigNumber { _hex: '0x01c86c31', _isBigNumber: true },
  gasUsed: BigNumber { _hex: '0x148ccc', _isBigNumber: true },
  miner: '0xd757fd54b273BB1234d4d9993f27699d28d0EDD2',
  extraData: '0x4b75436f696e506f6f6c1d8089d0f8adc7e0b8',
  transactions: [
    '0x662e3e5e26cd8504c9e4a0f167fd2eae746f7b45efc67d739b7ef567823b9c52',
 …
  ],
  baseFeePerGas: BigNumber { _hex: '0x11bec0fb6e', _isBigNumber: true },
  _difficulty: BigNumber { _hex: '0x34a2b75267e0b6', _isBigNumber: true }
}
Information about transaction {
  hash: '0x662e3e5e26cd8504c9e4a0f167fd2eae746f7b45efc67d739b7ef567823b9c52',
  type: 0,
  accessList: null,
  blockHash: '0xe0bebf1f444ecabb02df4aeb98b106174f980845b126b77fa9355205491765dc',
  blockNumber: 14923247,
  transactionIndex: 0,
  confirmations: 1,
  from: '0xc5a93444Cc4dA6EfB9e6FC6e5D3CB55A53b52396',
  gasPrice: BigNumber { _hex: '0x1b3dbe5200', _isBigNumber: true },
  gasLimit: BigNumber { _hex: '0x0668a0', _isBigNumber: true },
  to: '0x8f7684E18132dC2BEC31e5C3A552F8429F1F4157',
  value: BigNumber { _hex: '0x2ba9b09c4a2000', _isBigNumber: true },
  nonce: 886134,
  data: '0x',
  r: '0x00d57dfbb2b4c0f9cb24f557c1cfaf19af942343b06e823dc50d11c6cc2b709b',
  s: '0x1b15e6c3116e3bffcf5cfd1bac9cabc9b0d3f89e3acd7cd8d78d00d4c5cb786f',
  v: 38,
  creates: null,
  chainId: 1,
  wait: [Function (anonymous)]
}