5 Min. Read

Let’s fetch balance of XDC with Web3 and Node.js using BlocksScan — XDCRPC

by OpenScan

Insight

banner image for the post Let’s fetch balance of XDC with Web3 and Node.js using BlocksScan — XDCRPC

Blockchain technology has brought about significant advancements in the financial sector, providing a transparent, decentralized and secure system for conducting transactions. XDC Network is one such blockchain network that provides secure and cost-effective solutions for financial transactions.

If you are working on an application that uses XDC Network, you might need to fetch the balance of an XDC address. In this article, we will explore how to fetch the balance of an XDC address using Web3 and Node.js with BlockScan — XDCRPC.

Web3 is a collection of libraries that allow developers to interact with Ethereum-based blockchain networks. It provides a JavaScript API that can be used to interact with a blockchain network using Node.js. Node.js is an open-source JavaScript runtime environment that runs on the server-side.

BlocksScanXDCRPC is a public API endpoint that provides access to the XDC Network. It allows developers to interact with the network without running a full node.

Getting Started

Before we start, make sure you have Node.js installed on your machine. You also need an XDC address that you want to check the balance of.

Step 1: Create a folder Test folder and install the dependencies in it.

npm install -- S web3@1.2.2

Image description

Image description

Step 2: Now create a GetBalanceOfXDC.js file in the same folder, properly save it with JS extension. Image description

Note: Replace the address with the actual XDC address that you want to check the balance of.

You can use the below RPC endpoint:

const Web3 = require("web3");
const xdcNetwork = 'https://rpc.xdcrpc.com';
const web3 = new Web3(new Web3.providers.HttpProvider(xdcNetwork));
// let's fetch a balance
web3.eth.getBalance('0x7c75689c3DCA3f47570DA76e89eebC856f0E4a0F', async (err, result) => {
if (err) {
console.log(err);
return;
}
let balance = web3.utils.fromWei(result, "ether");
console.log(balance + " XDC");
});

Step 3: Run the code by entering the below command on the terminal, to get the balance of the XDC.

node filename.js

Image description

However, using the “getBalance’’ method directly can be slow, as it requires querying the entire blockchain. Instead, we can use the BlocksScan — XDCRPC endpoint to fetch the balance more quickly.

In this article, we explored how to fetch the balance of an XDC address using Web3 and Node.js with the — XDCRPC endpoint. For any doubts or question comment below or post on social media tagging BlocksScan.

Follow us to know more updates:

Share this Blog

Discover what's happening on OpenScan.AI

banner image for the post Export XDC Transactions for Taxes with CSV Exports

By OpenScan

csv

Export XDC Transactions for Taxes with CSV Exports

A worked walkthrough: export a full year of XDC wallet activity from OpenScan.AI as CSV files and shape them into a tax-ready report — no code required.

5 Min. Read

banner image for the post Track a Wallet's Full History with the OpenScan API

By OpenScan

api

Track a Wallet's Full History with the OpenScan API

A worked end-to-end tutorial: pull every transaction for an XDC wallet with the OpenScan.AI REST API v2 — no API key required — using curl and JavaScript.

5 Min. Read

banner image for the post Token Deployment Made Effortless with OpenScan

By OpenScan

openscan

Token Deployment Made Effortless with OpenScan

About OpenScan OpenScan is a powerful and user-friendly blockchain explorer built for the...

5 Min. Read