Last active
January 17, 2024 16:11
-
-
Save WietseWind/cd8a7a8c88f218fe7b768f59a665685d to your computer and use it in GitHub Desktop.
Simple B2M with hosted xPOP
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 { 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())) |
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.
Updated:
- Adds dynamic rails info from Xumm endpoints
- Adds Fee for importing B2M xPOP for previously imported account
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
we should be able to fetch the OperationLimit: 21338 // network id off the connection or server_info function call.