Created
October 1, 2021 00:07
-
-
Save mvadari/2724a57b8d46e8f58d9751a95530599c to your computer and use it in GitHub Desktop.
Apex Dev Summit 2021 xrpl.js demo code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "front-building", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\u001b[33mtrue\u001b[39m\n" | |
] | |
} | |
], | |
"source": [ | |
"import {Client} from 'xrpl'\n", | |
"const client = new Client('wss://s.altnet.rippletest.net:51233')\n", | |
"await client.connect()\n", | |
"client.isConnected()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "arctic-fifth", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"const wallet = await client.generateFaucetWallet()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "chubby-reynolds", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Wallet {\n", | |
" publicKey: \u001b[32m'ED3C742807F62043E461EA70BD01DAC575D52167DDA2C14250D50BC47EFE122240'\u001b[39m,\n", | |
" privateKey: \u001b[32m'EDF553E8869CD54D98A2748B9E80C0FB1549293ADA3BB6FE4207CB020ADAD43D1D'\u001b[39m,\n", | |
" classicAddress: \u001b[32m'rMZ5MCW1Se1JVkCPFXuqG5y6yGHZaEZR87'\u001b[39m,\n", | |
" seed: \u001b[32m'sEdTcbBTjo4j7rajsRiAPzvutmSmpQh'\u001b[39m\n", | |
"}\n" | |
] | |
} | |
], | |
"source": [ | |
"wallet" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"id": "humanitarian-plane", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[ { currency: \u001b[32m'XRP'\u001b[39m, value: \u001b[32m'939.999976'\u001b[39m } ]\n" | |
] | |
} | |
], | |
"source": [ | |
"await client.getBalances(wallet.classicAddress)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"id": "assured-healing", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{\n", | |
" TransactionType: \u001b[32m'Payment'\u001b[39m,\n", | |
" Amount: \u001b[32m'30000000'\u001b[39m,\n", | |
" Destination: \u001b[32m'rUCzEr6jrEyMpjhs4wSdQdz4g8Y382NxfM'\u001b[39m,\n", | |
" Account: \u001b[32m'rMZ5MCW1Se1JVkCPFXuqG5y6yGHZaEZR87'\u001b[39m\n", | |
"}\n" | |
] | |
} | |
], | |
"source": [ | |
"import {Payment, xrpToDrops} from 'xrpl'\n", | |
"const payment: Payment = {\n", | |
" TransactionType: 'Payment',\n", | |
" Amount: xrpToDrops(30),\n", | |
" Destination: 'rUCzEr6jrEyMpjhs4wSdQdz4g8Y382NxfM',\n", | |
" Account: wallet.classicAddress,\n", | |
"}\n", | |
"payment" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"id": "juvenile-contract", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"const response = await client.submitTransaction(wallet, payment)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"id": "streaming-donor", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{\n", | |
" id: \u001b[33m38\u001b[39m,\n", | |
" result: {\n", | |
" accepted: \u001b[33mtrue\u001b[39m,\n", | |
" account_sequence_available: \u001b[33m21458613\u001b[39m,\n", | |
" account_sequence_next: \u001b[33m21458613\u001b[39m,\n", | |
" applied: \u001b[33mtrue\u001b[39m,\n", | |
" broadcast: \u001b[33mtrue\u001b[39m,\n", | |
" engine_result: \u001b[32m'tesSUCCESS'\u001b[39m,\n", | |
" engine_result_code: \u001b[33m0\u001b[39m,\n", | |
" engine_result_message: \u001b[32m'The transaction was applied. Only final in a validated ledger.'\u001b[39m,\n", | |
" kept: \u001b[33mtrue\u001b[39m,\n", | |
" open_ledger_cost: \u001b[32m'10'\u001b[39m,\n", | |
" queued: \u001b[33mfalse\u001b[39m,\n", | |
" tx_blob: \u001b[32m'12000022000000002401476EB4201B01476F2E614000000001C9C38068400000000000000C7321ED3C742807F62043E461EA70BD01DAC575D52167DDA2C14250D50BC47EFE1222407440E41EF6872EE95F1E14FD26C800309543226F73DF74E869C2AD5866204C230695351432A9012B2035A1C83B9CA5B2A5C32C399F9A5BB3B53F65B6CB928C76AB0C8114E1925953BECBEC8280308A70FDE39615BA16985F83148008F9193579C7D32FAF4AD1A9BFFDB236F148E3'\u001b[39m,\n", | |
" tx_json: {\n", | |
" Account: \u001b[32m'rMZ5MCW1Se1JVkCPFXuqG5y6yGHZaEZR87'\u001b[39m,\n", | |
" Amount: \u001b[32m'30000000'\u001b[39m,\n", | |
" Destination: \u001b[32m'rUCzEr6jrEyMpjhs4wSdQdz4g8Y382NxfM'\u001b[39m,\n", | |
" Fee: \u001b[32m'12'\u001b[39m,\n", | |
" Flags: \u001b[33m0\u001b[39m,\n", | |
" LastLedgerSequence: \u001b[33m21458734\u001b[39m,\n", | |
" Sequence: \u001b[33m21458612\u001b[39m,\n", | |
" SigningPubKey: \u001b[32m'ED3C742807F62043E461EA70BD01DAC575D52167DDA2C14250D50BC47EFE122240'\u001b[39m,\n", | |
" TransactionType: \u001b[32m'Payment'\u001b[39m,\n", | |
" TxnSignature: \u001b[32m'E41EF6872EE95F1E14FD26C800309543226F73DF74E869C2AD5866204C230695351432A9012B2035A1C83B9CA5B2A5C32C399F9A5BB3B53F65B6CB928C76AB0C'\u001b[39m,\n", | |
" hash: \u001b[32m'DEE31A618E79B8A00ABC13C999FE504BB38B357CC2209A406F057955C9F171E3'\u001b[39m\n", | |
" },\n", | |
" validated_ledger_index: \u001b[33m21458714\u001b[39m\n", | |
" },\n", | |
" status: \u001b[32m'success'\u001b[39m,\n", | |
" type: \u001b[32m'response'\u001b[39m\n", | |
"}\n" | |
] | |
} | |
], | |
"source": [ | |
"response" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"id": "limited-florida", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{\n", | |
" id: \u001b[33m46\u001b[39m,\n", | |
" result: {\n", | |
" Account: \u001b[32m'rMZ5MCW1Se1JVkCPFXuqG5y6yGHZaEZR87'\u001b[39m,\n", | |
" Amount: \u001b[32m'30000000'\u001b[39m,\n", | |
" Destination: \u001b[32m'rUCzEr6jrEyMpjhs4wSdQdz4g8Y382NxfM'\u001b[39m,\n", | |
" Fee: \u001b[32m'12'\u001b[39m,\n", | |
" Flags: \u001b[33m0\u001b[39m,\n", | |
" LastLedgerSequence: \u001b[33m21458734\u001b[39m,\n", | |
" Sequence: \u001b[33m21458612\u001b[39m,\n", | |
" SigningPubKey: \u001b[32m'ED3C742807F62043E461EA70BD01DAC575D52167DDA2C14250D50BC47EFE122240'\u001b[39m,\n", | |
" TransactionType: \u001b[32m'Payment'\u001b[39m,\n", | |
" TxnSignature: \u001b[32m'E41EF6872EE95F1E14FD26C800309543226F73DF74E869C2AD5866204C230695351432A9012B2035A1C83B9CA5B2A5C32C399F9A5BB3B53F65B6CB928C76AB0C'\u001b[39m,\n", | |
" date: \u001b[33m686344372\u001b[39m,\n", | |
" hash: \u001b[32m'DEE31A618E79B8A00ABC13C999FE504BB38B357CC2209A406F057955C9F171E3'\u001b[39m,\n", | |
" inLedger: \u001b[33m21458716\u001b[39m,\n", | |
" ledger_index: \u001b[33m21458716\u001b[39m,\n", | |
" meta: {\n", | |
" AffectedNodes: \u001b[36m[Array]\u001b[39m,\n", | |
" TransactionIndex: \u001b[33m0\u001b[39m,\n", | |
" TransactionResult: \u001b[32m'tesSUCCESS'\u001b[39m,\n", | |
" delivered_amount: \u001b[32m'30000000'\u001b[39m\n", | |
" },\n", | |
" validated: \u001b[33mtrue\u001b[39m\n", | |
" },\n", | |
" status: \u001b[32m'success'\u001b[39m,\n", | |
" type: \u001b[32m'response'\u001b[39m\n", | |
"}\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"UnhandledPromiseRejection: Error [ERR_UNHANDLED_ERROR]: Unhandled error. ('reconnect')\n", | |
"\u001b[90m at Client.emit (events.js:364:17)\u001b[39m\n", | |
" at Connection.<anonymous> (/Users/mvadari/Documents/node_modules/\u001b[4mxrpl\u001b[24m/dist/npm/client/index.js:96:18)\n", | |
"\u001b[90m at Connection.emit (events.js:375:28)\u001b[39m\n", | |
" at /Users/mvadari/Documents/node_modules/\u001b[4mxrpl\u001b[24m/dist/npm/client/connection.js:305:26\n", | |
"\u001b[90m at processTicksAndRejections (internal/process/task_queues.js:95:5)\u001b[39m {\n", | |
" code: \u001b[32m'ERR_UNHANDLED_ERROR'\u001b[39m,\n", | |
" context: \u001b[32m'reconnect'\u001b[39m\n", | |
"}\n", | |
"UnhandledPromiseRejection: Error [ERR_UNHANDLED_ERROR]: Unhandled error. ('reconnect')\n", | |
"\u001b[90m at Client.emit (events.js:364:17)\u001b[39m\n", | |
" at Connection.<anonymous> (/Users/mvadari/Documents/node_modules/\u001b[4mxrpl\u001b[24m/dist/npm/client/index.js:96:18)\n", | |
"\u001b[90m at Connection.emit (events.js:375:28)\u001b[39m\n", | |
" at /Users/mvadari/Documents/node_modules/\u001b[4mxrpl\u001b[24m/dist/npm/client/connection.js:305:26\n", | |
"\u001b[90m at processTicksAndRejections (internal/process/task_queues.js:95:5)\u001b[39m {\n", | |
" code: \u001b[32m'ERR_UNHANDLED_ERROR'\u001b[39m,\n", | |
" context: \u001b[32m'reconnect'\u001b[39m\n", | |
"}\n", | |
"UnhandledPromiseRejection: Error [ERR_UNHANDLED_ERROR]: Unhandled error. ('reconnect')\n", | |
"\u001b[90m at Client.emit (events.js:364:17)\u001b[39m\n", | |
" at Connection.<anonymous> (/Users/mvadari/Documents/node_modules/\u001b[4mxrpl\u001b[24m/dist/npm/client/index.js:96:18)\n", | |
"\u001b[90m at Connection.emit (events.js:375:28)\u001b[39m\n", | |
" at /Users/mvadari/Documents/node_modules/\u001b[4mxrpl\u001b[24m/dist/npm/client/connection.js:286:22\n", | |
"\u001b[90m at processTicksAndRejections (internal/process/task_queues.js:95:5)\u001b[39m {\n", | |
" code: \u001b[32m'ERR_UNHANDLED_ERROR'\u001b[39m,\n", | |
" context: \u001b[32m'reconnect'\u001b[39m\n", | |
"}\n" | |
] | |
} | |
], | |
"source": [ | |
"import {TxRequest} from 'xrpl'\n", | |
"const request: TxRequest = {\n", | |
" command: 'tx',\n", | |
" transaction: 'DEE31A618E79B8A00ABC13C999FE504BB38B357CC2209A406F057955C9F171E3'\n", | |
"}\n", | |
"await client.request(request)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "liberal-element", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "TypeScript", | |
"language": "typescript", | |
"name": "tslab" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"mode": "typescript", | |
"name": "javascript", | |
"typescript": true | |
}, | |
"file_extension": ".ts", | |
"mimetype": "text/typescript", | |
"name": "typescript", | |
"version": "3.7.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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