Skip to content

Instantly share code, notes, and snippets.

@WietseWind
Last active January 17, 2024 16:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save WietseWind/cd8a7a8c88f218fe7b768f59a665685d to your computer and use it in GitHub Desktop.
Save WietseWind/cd8a7a8c88f218fe7b768f59a665685d to your computer and use it in GitHub Desktop.
Simple B2M with hosted xPOP
import { derive, utils, signAndSubmit } from 'xrpl-accountlib'
import { TxData } from 'xrpl-txdata'
import { XrplClient } from 'xrpl-client'
import { xpop, setEndpoints as xpopEndpoints } from 'xpop'
import fetch from 'node-fetch'
// The above needs:
// npm install xrpl-accountlib xrpl-txdata xrpl-client xpop node-fetch
console.log('Obtaining XRPL Testnet account (faucet)')
const faucet = await (await fetch('https://faucet.altnet.rippletest.net/accounts', { method: 'POST' })).json()
console.log(' -->', faucet.account.address)
const account = derive.familySeed(faucet.account.secret)
xpopEndpoints((await (await fetch('https://xrpl.ws-stats.com/xpop/list?json=true')).json()).bestguess)
const nodes = {
testnet: [
new XrplClient('wss://s.altnet.rippletest.net:51233'),
new XrplClient('wss://testnet.xrpl-labs.com'),
],
xahau: [
new XrplClient('wss://xahau-test.net'),
]
}
console.log('Waiting for network connections to be ready')
await Promise.all(Object.keys(nodes).map(k => Promise.race(nodes[k].map(n => n.ready()))))
console.log('Waiting for faucet account funding & obtaining network values...')
await new Promise(resolve => setTimeout(resolve, 4000)) // Wait till ledger is closed
const [
testnetParams,
xahauParams,
] = await Promise.all([
Promise.race(nodes.testnet.map(n => utils.accountAndLedgerSequence(n, account))),
Promise.race(nodes.xahau.map(n => utils.accountAndLedgerSequence(n, account))),
])
const testnetTx = {
...testnetParams.txValues,
TransactionType: 'AccountSet',
Fee: String(10_000_000),
OperationLimit: xahauParams.txValues.NetworkID,
NetworkID: undefined, // Testnet has a NetworkID < 1024, so none should be provided
}
console.log('Submitting burn...', testnetTx)
const testnetSubmitted = await Promise.race(nodes.testnet.map(n => signAndSubmit(testnetTx, n, account)))
console.log('Submitted burn to testnet')
console.log(' -->', 'https://testnet.xrpl.org/transactions/' + testnetSubmitted.tx_id)
console.log('Obtaining burn tx data...')
const txdata = new TxData(nodes.testnet.map(n => n.getState().server.uri), {
AllowNoFullHistory: true,
EndpointTimeoutMs: 2_000,
OverallTimeoutMs: 10_000,
})
const appliedtx = await txdata.getOne(testnetSubmitted.tx_id)
console.log(' -->', 'Burn validated in ledger', appliedtx?.result?.ledger_index)
console.log('Waiting for closed ledger before fetching xPOP...')
await new Promise(resolve => setTimeout(resolve, 4000)) // Wait till ledger is closed
console.log('Fetching xPOP...')
const Blob = await xpop(testnetSubmitted.tx_id, appliedtx?.result?.ledger_index, 1)
console.log(' -->', 'xPOP fetched, hex strlen:', Blob.length)
const hooksTx = {
...xahauParams.txValues,
TransactionType: 'Import',
Fee: '0',
Blob,
}
console.log('Submitting for mint of xPOP...')
const b2mSubmitted = await Promise.race(nodes.xahau.map(n => signAndSubmit(hooksTx, n, account)))
console.log(' -->', 'B2M submitted! TX on Xahau Testnet:')
console.log(' -->', 'https://test.xahauexplorer.com/explorer/' + b2mSubmitted?.tx_id)
console.log(' -->', b2mSubmitted.response.engine_result, b2mSubmitted.response.engine_result_message)
// Closing connections
Object.keys(nodes).map(k => nodes[k].map(n => n.close()))
@shortthefomo
Copy link

we should be able to fetch the OperationLimit: 21338 // network id off the connection or server_info function call.

@WietseWind
Copy link
Author

WietseWind commented Jul 10, 2023

we should be able to fetch the OperationLimit: 21338 // network id off the connection or server_info function call.

Good one. Done - getting the destination network details earlier on now, and using the value from the destination network.

Sample updated.

@WietseWind
Copy link
Author

Updated:

  • Adds dynamic rails info from Xumm endpoints
  • Adds Fee for importing B2M xPOP for previously imported account

@WietseWind
Copy link
Author

Updated:

  • Added persistent connections to network nodes (prevent reconnecting)
  • Added support for redundant nodes per network
  • Add new xPOP fetch as per https://www.npmjs.com/package/xpop
  • Improved auto-fee determination
  • More (better) comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment