diff --git a/content/_code-samples/escrow/js/cancel-escrow.js b/content/_code-samples/escrow/js/cancel-escrow.js index b78450ae4b..4d83c4078e 100644 --- a/content/_code-samples/escrow/js/cancel-escrow.js +++ b/content/_code-samples/escrow/js/cancel-escrow.js @@ -4,30 +4,45 @@ if (typeof module !== "undefined") { var xrpl = require('xrpl') } +// Preqrequisites: +// 1. Create an escrow using the create-escrow.js snippet +// 2. Replace the OfferSequence with the sequence number of the escrow you created +// 3. Paste the seed of the account that created the escrow +// 4. Run the snippet + +const seed = "sEd7jfWyNG6J71dEojB3W9YdHp2KCjy"; +const sequenceNumber = null; + const main = async () => { try { // 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 } = await client.fundWallet(); - console.log("Wallet: ", wallet.address); + // Prepare wallet to sign the transaction ------------------------------------- + const wallet = await xrpl.Wallet.fromSeed(seed); + console.log("Wallet Address: ", wallet.address); + console.log("Seed: ", seed); + + // Construct the escrow cancel transaction ------------------------------------ + + if(!sequenceNumber){ + throw new Error("Please specify the sequence number of the escrow you created"); + }; const escrowCancelTransaction = { "Account": wallet.address, "TransactionType": "EscrowCancel", "Owner": wallet.address, - "OfferSequence": 34821001, // Sequence number + "OfferSequence": sequenceNumber, // Sequence number }; xrpl.validate(escrowCancelTransaction); // Sign and submit the transaction -------------------------------------------- - console.log('Signing and submitting the transaction:', JSON.stringify(escrowCancelTransaction, null, "\t")); + console.log('Signing and submitting the transaction: ', JSON.stringify(escrowCancelTransaction, null, "\t")); const response = await client.submitAndWait(escrowCancelTransaction, { wallet }); - console.log(`Finished submitting! ${JSON.stringify(response.result, null, "\t")}`); + console.log(`Finished submitting! \n${JSON.stringify(response.result, null, "\t")}`); await client.disconnect(); diff --git a/content/_code-samples/escrow/js/create-escrow.js b/content/_code-samples/escrow/js/create-escrow.js index ae0f47547e..8892983e47 100644 --- a/content/_code-samples/escrow/js/create-escrow.js +++ b/content/_code-samples/escrow/js/create-escrow.js @@ -7,6 +7,13 @@ if (typeof module !== "undefined") { const cc = require('five-bells-condition'); const crypto = require('crypto'); +// Useful Documentation:- +// 1. five-bells-condition: https://www.npmjs.com/package/five-bells-condition +// 2. Crypto module: https://nodejs.org/api/crypto.html + +// Your account seed value, for testing purposes you can generate using: https://xrpl.org/xrp-test-net-faucet.html +const seed = "sEd7jfWyNG6J71dEojB3W9YdHp2KCjy"; + const main = async () => { try { @@ -23,12 +30,16 @@ const main = async () => { 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 } = await client.fundWallet(); - console.log("Wallet: ", wallet.address); + // Prepare wallet to sign the transaction ------------------------------------- + const wallet = await xrpl.Wallet.fromSeed(seed); + console.log("Wallet Address: ", wallet.address); + console.log("Seed: ", seed); + + // Set the escrow finish time -------------------------------------------------- + let finishAfter = new Date((new Date().getTime() / 1000) + 120); // 2 minutes from now + finishAfter = new Date(finishAfter * 1000); + console.log("This escrow will finish after: ", finishAfter); - const firstRippleEpoch = 946684800; const escrowCreateTransaction = { "TransactionType": "EscrowCreate", "Account": wallet.address, @@ -37,7 +48,7 @@ const main = async () => { "DestinationTag": 2023, "Condition": conditionHex, "Fee": "12", - "FinishAfter": Math.floor((new Date().getTime() / 1000) + 120) - firstRippleEpoch, // 2 minutes from now + "FinishAfter": xrpl.isoTimeToRippleTime(finishAfter.toISOString()), // Refer for more details: https://xrpl.org/basic-data-types.html#specifying-time }; xrpl.validate(escrowCreateTransaction); diff --git a/content/_code-samples/escrow/js/finish-escrow.js b/content/_code-samples/escrow/js/finish-escrow.js index ec2e2bae6d..33f71e6769 100644 --- a/content/_code-samples/escrow/js/finish-escrow.js +++ b/content/_code-samples/escrow/js/finish-escrow.js @@ -4,27 +4,43 @@ if (typeof module !== "undefined") { var xrpl = require('xrpl') } +// Preqrequisites: +// 1. Create an escrow using the create-escrow.js snippet +// 2. Replace the OfferSequence with the sequence number of the escrow you created +// 3. Replace the Condition and Fulfillment with the values from the escrow you created +// 4. Paste the seed of the account that created the escrow +// 5. Run the snippet + +const seed = "sEd7jfWyNG6J71dEojB3W9YdHp2KCjy"; +const offerSequence = null; +const condition = ""; +const fulfillment = ""; + const main = async () => { try { // 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 } = await client.fundWallet(); - console.log("Wallet: ", wallet.address); + // Prepare wallet to sign the transaction ------------------------------------- + const wallet = await xrpl.Wallet.fromSeed(seed); + console.log("Wallet Address: ", wallet.address); + console.log("Seed: ", seed); + + if((!offerSequence)|| (condition === "" || fulfillment === "")){ + throw new Error("Please specify the sequence number, condition and fulfillment of the escrow you created"); + }; const escrowFinishTransaction = { "Account": wallet.address, "TransactionType": "EscrowFinish", "Owner": wallet.address, // This should equal the sequence number of the escrow transaction - "OfferSequence": 7, + "OfferSequence": offerSequence, // Crypto condition that must be met before escrow can be completed, passed on escrow creation - "Condition": "A0258020E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855810100", + "Condition": condition, // Fulfillment of the condition, passed on escrow creation - "Fulfillment": "A0028000" + "Fulfillment": fulfillment, }; xrpl.validate(escrowFinishTransaction);