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

const Web3 = require('web3');

Connecting to RACE™

You can connect to RACE™ by instantiating a new web3.js Web3 object with a RPC URL of the RACE™ network:

const Web3 = require('web3');

const web3 = new Web3('https://racetestnet.io/');

Accessing data

Once you have created a provider, you can use it to read data from the RACE™ network.

For example, you can use the getBlockNumber method to get the latest block:

async function getLatestBlock(address) {
  const latestBlock = await web3.eth.getBlockNumber();
  console.log(latestBlock.toString());
}

Deploying contracts

Before you can deploy a contract to the RACE™ network using web3.js, you must first create an account.

You can create an account by using web3.eth.accounts:

const privateKey = “PRIVATE_KEY”;
const account = web3.eth.accounts.privateKeyToAccount(privateKey);

INFO

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

Last updated