Created
October 30, 2023 23:50
-
-
Save WietseWind/229130b77e1593315ef8d7844a182945 to your computer and use it in GitHub Desktop.
B2M (Burn to Mint) XRPL Mainnet to Xahau Mainnet
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 | |
const account = derive.familySeed('s....secret') | |
const b2mXrpAmount = 1337 | |
xpopEndpoints((await (await fetch('https://xrpl.ws-stats.com/xpop/list?json=true')).json()).bestguess) | |
const nodes = { | |
mainnet: [ | |
new XrplClient('wss://s2.ripple.com'), | |
new XrplClient('wss://xrplcluster.com'), | |
], | |
xahau: [ | |
new XrplClient('wss://xahau.network'), | |
] | |
} | |
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 [ | |
mainnetParams, | |
xahauParams, | |
] = await Promise.all([ | |
Promise.race(nodes.mainnet.map(n => utils.accountAndLedgerSequence(n, account))), | |
Promise.race(nodes.xahau.map(n => utils.accountAndLedgerSequence(n, account))), | |
]) | |
const mainnetTx = { | |
...mainnetParams.txValues, | |
TransactionType: 'AccountSet', | |
Fee: String(Number(b2mXrpAmount) * 1_000_000), | |
OperationLimit: xahauParams.txValues.NetworkID, | |
NetworkID: undefined, // mainnet has a NetworkID < 1024, so none should be provided | |
} | |
console.log('Submitting burn...', mainnetTx) | |
const mainnetSubmitted = await Promise.race(nodes.mainnet.map(n => signAndSubmit(mainnetTx, n, account))) | |
console.log('Submitted burn to mainnet') | |
console.log(' -->', 'https://bithomp.com/explorer/' + mainnetSubmitted.tx_id) | |
console.log('Obtaining burn tx data...') | |
const txdata = new TxData(nodes.mainnet.map(n => n.getState().server.uri), { | |
AllowNoFullHistory: true, | |
EndpointTimeoutMs: 2_000, | |
OverallTimeoutMs: 10_000, | |
}) | |
const appliedtx = await txdata.getOne(mainnetSubmitted.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(mainnetSubmitted.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 mainnet:') | |
console.log(' -->', 'https://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())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment