DEV Community

Marlon
Marlon

Posted on

XRPL: Console log the ledger index

Tools needed:

  • Node.js
  • NPM
  • VS Code

Create a new repository, initialize NPM, and install the XRPL.js package:

mkdir helloWorld
cd helloWorld
npm init
npm i xrpl
Enter fullscreen mode Exit fullscreen mode

Create a new app.js file in the repository:

touch app.js
Enter fullscreen mode Exit fullscreen mode

Open the file and paste this code into it:

// Pull the XRPL package into the file
const XRPL = require('xrpl');

// Create an async function to console log of the ledger index
const displayLastLedgerIndex = async () => {
  // Connect to the XRPL client via a desired network
  const client = new XRPL.Client('wss://xrplcluster.com');
  await client.connect();

  // Request XRPL client information
  const ledgerInfo = await client.request({
    command: 'ledger',
    ledger_index: 'validated',
  });

  // Console log the ledger index
  console.log(`Hello World, from the XRPL. The last index was: ${ledgerInfo.result.ledger_index}`);

  // Disconnect from the XRPL client
  await client.disconnect();
};

// Call the async function
displayLastLedgerIndex();
Enter fullscreen mode Exit fullscreen mode

Now, run this code:

node app.js
Enter fullscreen mode Exit fullscreen mode

There you have it! We've both made our step in interacting with the XRPL. Please like and share if you found this information valuable.

Top comments (0)