SHARE
TWEET

Untitled

a guest Oct 21st, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const ripple = require('ripple-lib')
  2. const api = new ripple.RippleAPI({server: 'wss://s.altnet.rippletest.net:51233'})
  3.  
  4. let maxLedgerVersion;
  5.  
  6. // Continuing after connecting to the API
  7. async function doPrepare() {
  8.   let connectResult = await api.connect();
  9.   console.log(connectResult);
  10.  
  11.   const sender = "rPFjsSMmEX5xNShegTEPYJnhPRfSQc739U"
  12.   const preparedTx = await api.prepareTransaction({
  13.     "TransactionType": "Payment",
  14.     "Account": sender,
  15.     "Amount": api.xrpToDrops("22"), // Same as "Amount": "22000000"
  16.     "Destination": "rUCzEr6jrEyMpjhs4wSdQdz4g8Y382NxfM"
  17.   }, {
  18.     // Expire this transaction if it doesn't execute within ~5 minutes:
  19.     "maxLedgerVersionOffset": 76
  20.   })
  21.   maxLedgerVersion = preparedTx.instructions.maxLedgerVersion
  22.   console.log("Prepared transaction instructions:", preparedTx.txJSON)
  23.   console.log("Transaction cost:", preparedTx.instructions.fee, "XRP")
  24.   console.log("Transaction expires after ledger:", maxLedgerVersion)
  25.   return preparedTx;
  26. }
  27.  
  28. async function signTx() {
  29.   let preparedTx = await doPrepare();
  30.   let txJSON = preparedTx.txJSON;
  31.   maxLedgerVersion = preparedTx.instructions.maxLedgerVersion;
  32.   //console.log(txJSON);
  33.  
  34.  
  35.   // Continuing from the previous step...
  36.   const response = api.sign(txJSON, "snrgBcMemBBMTZKWGp7v3HHS73LhY")
  37.   const txID = response.id
  38.   console.log("Identifying hash:", txID)
  39.   const txBlob = response.signedTransaction
  40.   console.log("Signed blob:", txBlob);
  41.  
  42.   return [txID, txBlob];
  43. }
  44.  
  45.  
  46.  
  47. // use txBlob from the previous example
  48. async function doSubmit(txBlob) {
  49.   const latestLedgerVersion = await api.getLedgerVersion()
  50.  
  51.   const result = await api.submit(txBlob)
  52.  
  53.   console.log("Tentative result code:", result.resultCode)
  54.   console.log("Tentative result message:", result.resultMessage)
  55.  
  56.   // Return the earliest ledger index this transaction could appear in
  57.   // as a result of this submission, which is the first one after the
  58.   // validated ledger at time of submission.
  59.   return latestLedgerVersion + 1
  60. }
  61.  
  62.  
  63. async function waitForValidation() {
  64.   api.on('ledger', ledger => {
  65.     console.log("Ledger version", ledger.ledgerVersion, "was just validated.")
  66.     if (ledger.ledgerVersion > maxLedgerVersion) {
  67.       console.log("If the transaction hasn't succeeded by now, it's expired")
  68.     }
  69.   })
  70. }
  71.  
  72. async function checkTxStatus(txID, earliestLedgerVersion) {
  73.   // Continues from previous examples.
  74.   // earliestLedgerVersion was noted when the transaction was submitted.
  75.   // txID was noted when the transaction was signed.
  76.   try {
  77.     //tx = await api.getTransaction(txID, {minLedgerVersion: earliestLedgerVersion})
  78.     tx = await api.getTransaction(txID)
  79.     console.log("Transaction result:", tx.outcome.result)
  80.     console.log("Balance changes:", JSON.stringify(tx.outcome.balanceChanges))
  81.     return tx.outcome.result;
  82.   } catch(error) {
  83.     console.log("Couldn't get transaction outcome:", error)
  84.   }
  85. }
  86.  
  87. (async () => {
  88.   let signTxResult = await signTx();
  89.   let txID = signTxResult[0];
  90.   let txBlob = signTxResult[1];
  91.   const earliestLedgerVersion = await doSubmit(txBlob)
  92.   console.log('earliest leadger version', earliestLedgerVersion);
  93.  
  94.   //await waitForValidation();
  95.   let txStatus = await checkTxStatus(txID, earliestLedgerVersion);
  96.   console.log('tx status', txStatus);
  97. })();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top