<aside> š TABLE OF CONTENTS
1. š Introduction to XRP Ledger
3. š» Interacting with XRP Ledger
4. šļø Building dApps on XRPL with React
5. š EVM Sidechain Integration for XRPL
6. š ļø Non-EVM Development
7. š Resources and References
</aside>
To begin developing on the XRP Ledger (XRPL) using Node.js, you'll need to set up your environment. Start by installing Node.js from nodejs.org. Once installed, verify your installation by opening a terminal and running:
node --version
npm --version
These commands should display the versions of Node.js and npm installed on your system.
Next, create a new directory for your XRPL project and initialize it with npm:
mkdir xrpl-project
cd xrpl-project
npm init -y
Now, install the xrpl
library, which provides tools for interacting with the XRP Ledger:
npm install xrpl
š” Tip: Consider using a version control system like Git to track changes in your project. Initialize a Git repository with git init in your project directory.
To experiment with the XRPL without using real funds, you'll need a testnet account. Here's a script to create one using the xrpl
library:
const xrpl = require("xrpl")
async function createTestnetAccount() {
const client = new xrpl.Client("wss://s.altnet.rippletest.net:51233")
await client.connect()
try {
const fund_result = await client.fundWallet()
const test_wallet = fund_result.wallet
console.log("š New account created!")
console.log("š Address:", test_wallet.address)
console.log("š Secret:", test_wallet.seed)
console.log("š° Balance:", xrpl.dropsToXrp(fund_result.balance), "XRP")
} catch (err) {
console.error("Error creating account:", err)
} finally {
await client.disconnect()
}
}
createTestnetAccount()
Save this as create_account.js
and run it with node create_account.js
. This script connects to the XRPL Testnet, creates a new account, funds it with test XRP, and displays the account details.
ā ļø Important: Never use testnet accounts or secrets for real transactions. They are for testing only!
The XRPL provides a powerful JSON-RPC API for interacting with the ledger. Here's an example of how to use it to retrieve account information:
const WebSocket = require('ws')
function getAccountInfo(address) {
return new Promise((resolve, reject) => {
const ws = new WebSocket('wss://s1.ripple.com')
ws.on('open', function open() {
const request = {
command: 'account_info',
account: address,
strict: true,
ledger_index: 'current',
queue: true
}
ws.send(JSON.stringify(request))
})
ws.on('message', function incoming(data) {
const response = JSON.parse(data)
ws.close()
resolve(response)
})
ws.on('error', function error(err) {
reject(err)
})
})
}
async function displayAccountInfo(address) {
try {
const info = await getAccountInfo(address)
console.log('š Account Info:', JSON.stringify(info.result, null, 2))
} catch (err) {
console.error('ā Error fetching account info:', err)
}
}
// Example usage
displayAccountInfo('rG1QQv2nh2gr7RCZ1P8YYcBUKCCN633jCn')
This script establishes a WebSocket connection to a public XRPL node and requests information about a specific account. It demonstrates the basic structure of an API call, including specifying the command and parameters.
š Explore Further: Try modifying this script to use different API methods like tx to retrieve transaction information or ledger to get ledger data.
Let's create a simple application that combines account creation and information retrieval:
const xrpl = require("xrpl")
const WebSocket = require('ws')
async function createAndExploreAccount() {
const client = new xrpl.Client("wss://s.altnet.rippletest.net:51233")
await client.connect()
try {
// Create account
const fund_result = await client.fundWallet()
const test_wallet = fund_result.wallet
console.log("š New account created!")
console.log("š Address:", test_wallet.address)
console.log("š Secret:", test_wallet.seed)
console.log("š° Initial Balance:", xrpl.dropsToXrp(fund_result.balance), "XRP")
// Fetch account info
const accountInfo = await client.request({
command: "account_info",
account: test_wallet.address,
ledger_index: "validated"
})
console.log("\\\\nš Account Info:")
console.log(JSON.stringify(accountInfo.result, null, 2))
} catch (err) {
console.error("ā Error:", err)
} finally {
await client.disconnect()
}
}
createAndExploreAccount()
This script creates a new testnet account and then immediately fetches and displays its information using the XRPL API.
š Next Steps: Try expanding this application to perform other actions like sending test transactions or creating trust lines.
By mastering these basics, you'll be well-equipped to start building more complex applications on the XRP Ledger. Remember to always refer to the official XRPL documentation for the most up-to-date information and best practices.