Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Ekiserrepe/dab5311b67cb393165184af8cf2a67c5 to your computer and use it in GitHub Desktop.
Save Ekiserrepe/dab5311b67cb393165184af8cf2a67c5 to your computer and use it in GitHub Desktop.
Sub Xahau Testnet
// payment-monitor.js
// Correct import for xrpl-client
const { XrplClient } = require('xrpl-client');
// Address to monitor for incoming payments
const ADDRESS_TO_MONITOR = 'rf1NrYAsv92UPDd8nyCG4A3bez7dhYE61r';
// Connect to Xahau Testnet
const client = new XrplClient('wss://xahau-test.net');
console.log('Connecting to Xahau Testnet...');
// Handle connection
client.on('online', () => {
console.log('Connected to Xahau Testnet');
// Subscribe to ledger closes and the specific account
client.send({
command: 'subscribe',
streams: ['ledger'],
accounts: [ADDRESS_TO_MONITOR]
}).then(response => {
console.log('Subscription successful:', response);
console.log(`Monitoring incoming payments to ${ADDRESS_TO_MONITOR}...`);
}).catch(error => {
console.error('Subscription error:', error);
process.exit(1);
});
});
// Handle reconnection
client.on('reconnect', () => {
console.log('Reconnecting to Xahau Testnet...');
});
// Handle disconnection
client.on('offline', () => {
console.log('Disconnected from Xahau Testnet');
});
// Handle ledger events to show activity
client.on('ledger', (ledger) => {
console.log(`Ledger #${ledger.ledger_index || ledger.ledgerVersion} closed at ${new Date().toLocaleTimeString()}`);
});
// Handle incoming transactions
client.on('transaction', (tx) => {
// Check if it's a payment transaction
if (tx.transaction && tx.transaction.TransactionType === 'Payment') {
// Check if the destination is our monitored address
if (tx.transaction.Destination === ADDRESS_TO_MONITOR) {
console.log('\n================ INCOMING PAYMENT DETECTED ================');
console.log('Transaction ID:', tx.transaction.hash || tx.id);
console.log('From:', tx.transaction.Account);
console.log('To:', tx.transaction.Destination);
// Check if the payment has an amount in XRP
if (tx.transaction.Amount && typeof tx.transaction.Amount === 'string') {
// XRP amount is expressed in drops (1 XRP = 1,000,000 drops)
const xrpAmount = parseInt(tx.transaction.Amount) / 1000000;
console.log('Amount:', xrpAmount, 'XAH');
}
// Check if it's an issued currency payment
else if (tx.transaction.Amount && typeof tx.transaction.Amount === 'object') {
console.log('Amount:', tx.transaction.Amount.value, tx.transaction.Amount.currency);
console.log('Issuer:', tx.transaction.Amount.issuer);
}
console.log('Timestamp:', new Date().toISOString());
console.log('===========================================================\n');
}
}
});
// Handle errors
client.on('error', (error) => {
console.error('Error:', error);
});
// Keep the process running
process.on('SIGINT', () => {
console.log('Closing connection to Xahau Testnet...');
client.close();
process.exit(0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment