Skip to content

Instantly share code, notes, and snippets.

@mvadari
Created October 1, 2021 00:07
Show Gist options
  • Save mvadari/2724a57b8d46e8f58d9751a95530599c to your computer and use it in GitHub Desktop.
Save mvadari/2724a57b8d46e8f58d9751a95530599c to your computer and use it in GitHub Desktop.
Apex Dev Summit 2021 xrpl.js demo code
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import { Client, TxRequest } from 'xrpl';
import { Payment } from 'xrpl';
import { xrpToDrops } from 'xrpl';
function sleep(ms: number) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
// set up client
const client = new Client('wss://s.altnet.rippletest.net:51233')
// client.on('error', (errorCode, errorMessage) => {console.log(errorCode + ': ' + errorMessage);});
// client.on('connected', () => {console.log('connected');});
// client.on('disconnected', (code) => {console.log('disconnected, code:', code);});
async function demo() {
try {
// connect to client
await client.connect()
console.log('connected')
// get wallet
const wallet = await client.generateFaucetWallet()
console.log(wallet)
console.log(await client.getBalances(wallet.classicAddress))
// set up transaction
const payment: Payment = {
TransactionType: 'Payment',
Account: wallet.getClassicAddress(),
Amount: xrpToDrops('30'),
Destination: 'rUCzEr6jrEyMpjhs4wSdQdz4g8Y382NxfM',
}
// autofill/sign/submit payment
const autofilledPayment = await client.autofill(payment)
console.log(autofilledPayment)
const signedPayment = wallet.signTransaction(autofilledPayment)
console.log(signedPayment)
// submit payment
const submitResponse = await client.submitSignedTransaction(signedPayment)
console.log(submitResponse)
// wait for validation (an approximation)
await sleep(8000)
// reprint balance after validation (should have decreased)
console.log(await client.getBalances(wallet.classicAddress))
// check the ledger for the transaction
const request: TxRequest = {
command: 'tx',
transaction: submitResponse.result.tx_json.hash
}
await client.request(request)
// exit
process.exit(0)
} catch (e) {
console.log('ERROR')
console.log(e)
process.exit(1)
}
}
demo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment