start fixing escrow code samples

This commit is contained in:
mDuo13
2024-09-25 14:26:54 -07:00
parent 21227d816d
commit f30e465583
6 changed files with 80 additions and 133 deletions

View File

@@ -1,42 +0,0 @@
// Code in progress.
const xrpl = require("xrpl")
async function main() {
// Create a client and connect to the network.
const client = new xrpl.Client("wss://xrplcluster.com/")
await client.connect()
// Query the most recently validated ledger for info.
const ledger = await client.request({
"command": "ledger",
"ledger_index": "validated",
})
const ledgerCloseTime = ledger["result"]["ledger"]["close_time"]
// Check the `CancelAfter` time of the escrow.
// For this example, we have the identifying hash of the `EscrowCreate` transaction.
const escrowInfo = await client.request({
"command": "tx",
"transaction": "3DC728E4DB4120AD26DD41997C42FF5AD46C0073D8692AFB8F59660D058D87A3",
})
const escrowAccountSender = escrowInfo["result"]["Account"]
const escrowCancelAfter = escrowInfo["result"]["CancelAfter"]
const escrowSequence = escrowInfo["result"]["Sequence"]
// Submit an `EscrowCancel` transaction if the cancel after time is smaller than the ledger close time.
// Any valid account can submit an escrow cancel transaction.
if (escrowCancelAfter < ledgerCloseTime) {
client.submitAndWait({
"Account": escrowAccountSender,
"TransactionType": "EscrowCancel",
"Owner": escrowAccountSender,
"OfferSequence": escrowSequence
})
}
client.disconnect()
}
main()

View File

@@ -1,24 +1,21 @@
'use strict'
if (typeof module !== "undefined") {
// Use var here because const/let are block-scoped to the if statement.
var xrpl = require('xrpl')
}
const 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
// 4. Run this snippet
const seed = "sEd7jfWyNG6J71dEojB3W9YdHp2KCjy";
const sequenceNumber = null;
const seed = "sEd7jfWyNG6J71dEojB3W9YdHp2KCjy"; // replace with your seed
const sequenceNumber = 0; // replace with the sequence number of your escrow
const main = async () => {
async function main() {
try {
// Connect -------------------------------------------------------------------
const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233');
await client.connect();
// Prepare wallet to sign the transaction -------------------------------------
const wallet = await xrpl.Wallet.fromSeed(seed);
console.log("Wallet Address: ", wallet.address);
@@ -45,7 +42,7 @@ const main = async () => {
console.log(`Finished submitting! \n${JSON.stringify(response.result, null, "\t")}`);
await client.disconnect();
} catch (error) {
console.log(error);
}

View File

@@ -1,23 +1,20 @@
'use strict'
if (typeof module !== "undefined") {
// Use var here because const/let are block-scoped to the if statement.
var xrpl = require('xrpl');
};
const xrpl = require('xrpl');
const cc = require('five-bells-condition');
const crypto = require('crypto');
// Useful Documentation:-
// 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
// Your seed value, for testing purposes you can make one with the faucet:
// https://xrpl.org/resources/dev-tools/xrp-faucets
const seed = "sEd7jfWyNG6J71dEojB3W9YdHp2KCjy";
const main = async () => {
async function main() {
try {
// Construct condition and fulfillment ---------------------------------------
// Construct condition and fulfillment ------------------------------------
const preimageData = crypto.randomBytes(32);
const myFulfillment = new cc.PreimageSha256();
myFulfillment.setPreimage(preimageData);
@@ -26,16 +23,16 @@ const main = async () => {
console.log('Condition:', conditionHex);
console.log('Fulfillment:', myFulfillment.serializeBinary().toString('hex').toUpperCase());
// Connect -------------------------------------------------------------------
// Connect ----------------------------------------------------------------
const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233');
await client.connect();
// Prepare wallet to sign the transaction -------------------------------------
// 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 --------------------------------------------------
// 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);
@@ -48,19 +45,21 @@ const main = async () => {
"DestinationTag": 2023,
"Condition": conditionHex,
"Fee": "12",
"FinishAfter": xrpl.isoTimeToRippleTime(finishAfter.toISOString()), // Refer for more details: https://xrpl.org/basic-data-types.html#specifying-time
"FinishAfter": xrpl.isoTimeToRippleTime(finishAfter.toISOString()),
};
xrpl.validate(escrowCreateTransaction);
// Sign and submit the transaction --------------------------------------------
console.log('Signing and submitting the transaction:', JSON.stringify(escrowCreateTransaction, null, "\t"), "\n");
// Sign and submit the transaction ----------------------------------------
console.log('Signing and submitting the transaction:',
JSON.stringify(escrowCreateTransaction, null, "\t"), "\n"
);
const response = await client.submitAndWait(escrowCreateTransaction, { wallet });
console.log(`Sequence number: ${response.result.Sequence}`);
console.log(`Finished submitting! ${JSON.stringify(response.result, null, "\t")}`);
await client.disconnect();
} catch (error) {
console.log(error);
}

View File

@@ -1,8 +1,5 @@
'use strict'
if (typeof module !== "undefined") {
// Use var here because const/let are block-scoped to the if statement.
var xrpl = require('xrpl')
}
const xrpl = require('xrpl')
// Preqrequisites:
// 1. Create an escrow using the create-escrow.js snippet
@@ -11,18 +8,18 @@ if (typeof module !== "undefined") {
// 4. Paste the seed of the account that created the escrow
// 5. Run the snippet
const seed = "sEd7jfWyNG6J71dEojB3W9YdHp2KCjy";
const seed = "sEd7jfWyNG6J71dEojB3W9YdHp2KCjy"; // Test seed. Don't use
const offerSequence = null;
const condition = "";
const fulfillment = "";
const main = async () => {
try {
// Connect -------------------------------------------------------------------
// Connect ----------------------------------------------------------------
const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233');
await client.connect();
// Prepare wallet to sign the transaction -------------------------------------
// Prepare wallet to sign the transaction ---------------------------------
const wallet = await xrpl.Wallet.fromSeed(seed);
console.log("Wallet Address: ", wallet.address);
console.log("Seed: ", seed);
@@ -38,20 +35,20 @@ const main = async () => {
// This should equal the sequence number of the escrow transaction
"OfferSequence": offerSequence,
// Crypto condition that must be met before escrow can be completed, passed on escrow creation
"Condition": condition,
"Condition": condition,
// Fulfillment of the condition, passed on escrow creation
"Fulfillment": fulfillment,
};
xrpl.validate(escrowFinishTransaction);
// Sign and submit the transaction --------------------------------------------
// Sign and submit the transaction ----------------------------------------
console.log('Signing and submitting the transaction:', JSON.stringify(escrowFinishTransaction, null, "\t"));
const response = await client.submitAndWait(escrowFinishTransaction, { wallet });
console.log(`Finished submitting! ${JSON.stringify(response.result, null, "\t")}`);
await client.disconnect();
} catch (error) {
console.log(error);
}

View File

@@ -1,56 +1,54 @@
if (typeof module !== "undefined") {
// Use var here because const/let are block-scoped to the if statement.
var xrpl = require('xrpl')
}
// List the Escrows on an existing account filtered by source and destination
// https://xrpl.org/escrow.html#escrow
// https://xrpl.org/account_objects.html#account_objects
const xrpl = require('xrpl')
async function main() {
// Testnet example: rPRKeXbcFMcn69nR2bovp4bEcP8kZx7x5i
account = "rPRKeXbcFMcn69nR2bovp4bEcP8kZx7x5i"
// List the Escrows on an existing account filtered by source and destination
// https://xrpl.org/escrow.html#escrow
// https://xrpl.org/account_objects.html#account_objects
// Connect to a testnet node
console.log("Connecting to Testnet...")
const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233/')
await client.connect()
const response = await client.request({
"command": "account_objects",
"account": account,
"ledger_index": "validated",
"type": "escrow"
})
async function main() {
// Testnet example: rPRKeXbcFMcn69nR2bovp4bEcP8kZx7x5i
account = "rPRKeXbcFMcn69nR2bovp4bEcP8kZx7x5i"
var incoming = []
var outgoing = []
// Connect to a testnet node
console.log("Connecting to Testnet...")
const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233/')
await client.connect()
for (var i = 0; i < response.result.account_objects.length; i++) {
if (response.result.account_objects[i].Account == account) {
outgoing.push(response.result.account_objects[i])
} else {
incoming.push(response.result.account_objects[i])
}
const response = await client.request({
"command": "account_objects",
"account": account,
"ledger_index": "validated",
"type": "escrow"
})
var incoming = []
var outgoing = []
for (var i = 0; i < response.result.account_objects.length; i++) {
if (response.result.account_objects[i].Account == account) {
outgoing.push(response.result.account_objects[i])
} else {
incoming.push(response.result.account_objects[i])
}
}
console.log("\nIncoming/Received escrow(s):")
for (var i = 0; i < incoming.length; i++) {
console.log(`\n${i+1}. Index (ObjectID/keylet): ${incoming[i].index}`)
console.log(` - Account: ${incoming[i].Account})`)
console.log(` - Destination: ${incoming[i].Destination}`)
console.log(` - Amount: ${incoming[i].Amount} drops`)
}
console.log("\nOutgoing/Sent escrow(s):")
for (var i = 0; i < outgoing.length; i++) {
console.log(`\n${i+1}. Index (ObjectID/keylet): ${outgoing[i].index}`)
console.log(` - Account: ${outgoing[i].Account})`)
console.log(` - Destination: ${outgoing[i].Destination}`)
console.log(` - Amount: ${outgoing[i].Amount} drops`)
}
client.disconnect()
// End main()
}
main()
console.log("\nIncoming/Received escrow(s):")
for (var i = 0; i < incoming.length; i++) {
console.log(`\n${i+1}. Index (ObjectID/keylet): ${incoming[i].index}`)
console.log(` - Account: ${incoming[i].Account})`)
console.log(` - Destination: ${incoming[i].Destination}`)
console.log(` - Amount: ${incoming[i].Amount} drops`)
}
console.log("\nOutgoing/Sent escrow(s):")
for (var i = 0; i < outgoing.length; i++) {
console.log(`\n${i+1}. Index (ObjectID/keylet): ${outgoing[i].index}`)
console.log(` - Account: ${outgoing[i].Account})`)
console.log(` - Destination: ${outgoing[i].Destination}`)
console.log(` - Amount: ${outgoing[i].Amount} drops`)
}
client.disconnect()
// End main()
}
main()

View File

@@ -1,11 +1,9 @@
{
"name": "escrow-examples",
"version": "0.0.1",
"version": "0.0.3",
"license": "MIT",
"dependencies": {
"five-bells-condition": "*",
"ripple-lib": "^0.17.6",
"xrpl": "^3.0.0"
},
"//": "Intended for Node.js version ?.? and higher"
}
}