Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const ripple = require('ripple-lib')
- const api = new ripple.RippleAPI({server: 'wss://s.altnet.rippletest.net:51233'})
- let maxLedgerVersion;
- // Continuing after connecting to the API
- async function doPrepare() {
- let connectResult = await api.connect();
- console.log(connectResult);
- const sender = "rPFjsSMmEX5xNShegTEPYJnhPRfSQc739U"
- const preparedTx = await api.prepareTransaction({
- "TransactionType": "Payment",
- "Account": sender,
- "Amount": api.xrpToDrops("22"), // Same as "Amount": "22000000"
- "Destination": "rUCzEr6jrEyMpjhs4wSdQdz4g8Y382NxfM"
- }, {
- // Expire this transaction if it doesn't execute within ~5 minutes:
- "maxLedgerVersionOffset": 76
- })
- maxLedgerVersion = preparedTx.instructions.maxLedgerVersion
- console.log("Prepared transaction instructions:", preparedTx.txJSON)
- console.log("Transaction cost:", preparedTx.instructions.fee, "XRP")
- console.log("Transaction expires after ledger:", maxLedgerVersion)
- return preparedTx;
- }
- async function signTx() {
- let preparedTx = await doPrepare();
- let txJSON = preparedTx.txJSON;
- maxLedgerVersion = preparedTx.instructions.maxLedgerVersion;
- //console.log(txJSON);
- // Continuing from the previous step...
- const response = api.sign(txJSON, "snrgBcMemBBMTZKWGp7v3HHS73LhY")
- const txID = response.id
- console.log("Identifying hash:", txID)
- const txBlob = response.signedTransaction
- console.log("Signed blob:", txBlob);
- return [txID, txBlob];
- }
- // use txBlob from the previous example
- async function doSubmit(txBlob) {
- const latestLedgerVersion = await api.getLedgerVersion()
- const result = await api.submit(txBlob)
- console.log("Tentative result code:", result.resultCode)
- console.log("Tentative result message:", result.resultMessage)
- // Return the earliest ledger index this transaction could appear in
- // as a result of this submission, which is the first one after the
- // validated ledger at time of submission.
- return latestLedgerVersion + 1
- }
- async function waitForValidation() {
- api.on('ledger', ledger => {
- console.log("Ledger version", ledger.ledgerVersion, "was just validated.")
- if (ledger.ledgerVersion > maxLedgerVersion) {
- console.log("If the transaction hasn't succeeded by now, it's expired")
- }
- })
- }
- async function checkTxStatus(txID, earliestLedgerVersion) {
- // Continues from previous examples.
- // earliestLedgerVersion was noted when the transaction was submitted.
- // txID was noted when the transaction was signed.
- try {
- //tx = await api.getTransaction(txID, {minLedgerVersion: earliestLedgerVersion})
- tx = await api.getTransaction(txID)
- console.log("Transaction result:", tx.outcome.result)
- console.log("Balance changes:", JSON.stringify(tx.outcome.balanceChanges))
- return tx.outcome.result;
- } catch(error) {
- console.log("Couldn't get transaction outcome:", error)
- }
- }
- (async () => {
- let signTxResult = await signTx();
- let txID = signTxResult[0];
- let txBlob = signTxResult[1];
- const earliestLedgerVersion = await doSubmit(txBlob)
- console.log('earliest leadger version', earliestLedgerVersion);
- //await waitForValidation();
- let txStatus = await checkTxStatus(txID, earliestLedgerVersion);
- console.log('tx status', txStatus);
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement