diff --git a/content/_code-samples/escrow/js/cancel-escrow.js b/content/_code-samples/escrow/js/cancel-escrow.js index 8e1757fc2a..2886cf64ae 100644 --- a/content/_code-samples/escrow/js/cancel-escrow.js +++ b/content/_code-samples/escrow/js/cancel-escrow.js @@ -1,40 +1,37 @@ 'use strict' -const RippleAPI = require('ripple-lib').RippleAPI - -const myAddr = 'rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn' -const mySecret = 's████████████████████████████' - -const myEscrowCancellation = { - owner: myAddr, - escrowSequence: 366 -} -const myInstructions = { - maxLedgerVersionOffset: 5 +if (typeof module !== "undefined") { + // Use var here because const/let are block-scoped to the if statement. + var xrpl = require('xrpl') } -const api = new RippleAPI({server: 'wss://s2.ripple.com'}) +const secret = "sEdTPPEeMH6SAgpo6rSj8YW7a9vFfUj"; -function submitTransaction(lastClosedLedgerVersion, prepared, secret) { - const signedData = api.sign(prepared.txJSON, secret) - console.log('Transaction ID: ', signedData.id) - return api.submit(signedData.signedTransaction).then(data => { - console.log('Tentative Result: ', data.resultCode) - console.log('Tentative Message: ', data.resultMessage) - }) +const main = async () => { + try { + // Connect ------------------------------------------------------------------- + const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233'); + await client.connect(); + const wallet = await xrpl.Wallet.fromSeed(secret); + + const escrowCancelTransaction = { + "Account": wallet.address, + "TransactionType": "EscrowCancel", + "Owner": wallet.address, + "OfferSequence": 34821001, // Sequence number + }; + + xrpl.validate(escrowCancelTransaction); + + // Sign and submit the transaction -------------------------------------------- + console.log('Signing and submitting the transaction:', escrowCancelTransaction); + const response = await client.submitAndWait(escrowCancelTransaction, { wallet }); + console.log(`Finished submitting! ${JSON.stringify(response.result)}`); + + await client.disconnect(); + + } catch (error) { + console.log(error); + } } -api.connect().then(() => { - console.log('Connected') - return api.prepareEscrowCancellation(myAddr, myEscrowCancellation, myInstructions) -}).then(prepared => { - console.log('EscrowCancellation Prepared') - return api.getLedger().then(ledger => { - console.log('Current Ledger', ledger.ledgerVersion) - return submitTransaction(ledger.ledgerVersion, prepared, mySecret) - }) -}).then(() => { - api.disconnect().then(() => { - console.log('api disconnected') - process.exit() - }) -}).catch(console.error) +main() diff --git a/content/_code-samples/escrow/js/create-escrow.js b/content/_code-samples/escrow/js/create-escrow.js index a777a29cf8..c00b55e306 100644 --- a/content/_code-samples/escrow/js/create-escrow.js +++ b/content/_code-samples/escrow/js/create-escrow.js @@ -1,32 +1,31 @@ 'use strict' if (typeof module !== "undefined") { // Use var here because const/let are block-scoped to the if statement. - var xrpl = require('xrpl') -} + var xrpl = require('xrpl'); +}; -const cc = require('five-bells-condition') -const crypto = require('crypto') +const cc = require('five-bells-condition'); +const crypto = require('crypto'); + +const secret = "sEdTPPEeMH6SAgpo6rSj8YW7a9vFfUj"; const main = async () => { try { // Construct condition and fulfillment --------------------------------------- - const preimageData = crypto.randomBytes(32) - const myFulfillment = new cc.PreimageSha256() - myFulfillment.setPreimage(preimageData) - const conditionHex = myFulfillment.getConditionBinary().toString('hex').toUpperCase() + const preimageData = crypto.randomBytes(32); + const myFulfillment = new cc.PreimageSha256(); + myFulfillment.setPreimage(preimageData); + const conditionHex = myFulfillment.getConditionBinary().toString('hex').toUpperCase(); + const wallet = await xrpl.Wallet.fromSeed(secret); + console.log("Wallet: ",wallet.address); - console.log('Condition:', conditionHex) - console.log('Fulfillment:', myFulfillment.serializeBinary().toString('hex').toUpperCase()) + console.log('Condition:', conditionHex); + console.log('Fulfillment:', myFulfillment.serializeBinary().toString('hex').toUpperCase()); // Connect ------------------------------------------------------------------- - const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233') - await client.connect() - - // Get credentials from the Testnet Faucet ----------------------------------- - console.log("Requesting an address from the Testnet faucet...") - const { wallet, balance } = await client.fundWallet(); - console.log("Wallet: ",wallet.address); + const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233'); + await client.connect(); const firstRippleEpoch = 946684800; const escrowCreateTransaction = {