mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-11-21 12:15:50 +00:00
start fixing escrow code samples
This commit is contained in:
@@ -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()
|
|
||||||
@@ -1,19 +1,16 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
if (typeof module !== "undefined") {
|
const xrpl = require('xrpl');
|
||||||
// Use var here because const/let are block-scoped to the if statement.
|
|
||||||
var xrpl = require('xrpl')
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preqrequisites:
|
// Preqrequisites:
|
||||||
// 1. Create an escrow using the create-escrow.js snippet
|
// 1. Create an escrow using the create-escrow.js snippet
|
||||||
// 2. Replace the OfferSequence with the sequence number of the escrow you created
|
// 2. Replace the OfferSequence with the sequence number of the escrow you created
|
||||||
// 3. Paste the seed of the account that created the escrow
|
// 3. Paste the seed of the account that created the escrow
|
||||||
// 4. Run the snippet
|
// 4. Run this snippet
|
||||||
|
|
||||||
const seed = "sEd7jfWyNG6J71dEojB3W9YdHp2KCjy";
|
const seed = "sEd7jfWyNG6J71dEojB3W9YdHp2KCjy"; // replace with your seed
|
||||||
const sequenceNumber = null;
|
const sequenceNumber = 0; // replace with the sequence number of your escrow
|
||||||
|
|
||||||
const main = async () => {
|
async function main() {
|
||||||
try {
|
try {
|
||||||
// Connect -------------------------------------------------------------------
|
// Connect -------------------------------------------------------------------
|
||||||
const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233');
|
const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233');
|
||||||
|
|||||||
@@ -1,9 +1,5 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
if (typeof module !== "undefined") {
|
const xrpl = require('xrpl');
|
||||||
// Use var here because const/let are block-scoped to the if statement.
|
|
||||||
var xrpl = require('xrpl');
|
|
||||||
};
|
|
||||||
|
|
||||||
const cc = require('five-bells-condition');
|
const cc = require('five-bells-condition');
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
|
|
||||||
@@ -11,13 +7,14 @@ const crypto = require('crypto');
|
|||||||
// 1. five-bells-condition: https://www.npmjs.com/package/five-bells-condition
|
// 1. five-bells-condition: https://www.npmjs.com/package/five-bells-condition
|
||||||
// 2. Crypto module: https://nodejs.org/api/crypto.html
|
// 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 seed = "sEd7jfWyNG6J71dEojB3W9YdHp2KCjy";
|
||||||
|
|
||||||
const main = async () => {
|
async function main() {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// Construct condition and fulfillment ---------------------------------------
|
// Construct condition and fulfillment ------------------------------------
|
||||||
const preimageData = crypto.randomBytes(32);
|
const preimageData = crypto.randomBytes(32);
|
||||||
const myFulfillment = new cc.PreimageSha256();
|
const myFulfillment = new cc.PreimageSha256();
|
||||||
myFulfillment.setPreimage(preimageData);
|
myFulfillment.setPreimage(preimageData);
|
||||||
@@ -26,16 +23,16 @@ const main = async () => {
|
|||||||
console.log('Condition:', conditionHex);
|
console.log('Condition:', conditionHex);
|
||||||
console.log('Fulfillment:', myFulfillment.serializeBinary().toString('hex').toUpperCase());
|
console.log('Fulfillment:', myFulfillment.serializeBinary().toString('hex').toUpperCase());
|
||||||
|
|
||||||
// Connect -------------------------------------------------------------------
|
// Connect ----------------------------------------------------------------
|
||||||
const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233');
|
const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233');
|
||||||
await client.connect();
|
await client.connect();
|
||||||
|
|
||||||
// Prepare wallet to sign the transaction -------------------------------------
|
// Prepare wallet to sign the transaction ---------------------------------
|
||||||
const wallet = await xrpl.Wallet.fromSeed(seed);
|
const wallet = await xrpl.Wallet.fromSeed(seed);
|
||||||
console.log("Wallet Address: ", wallet.address);
|
console.log("Wallet Address: ", wallet.address);
|
||||||
console.log("Seed: ", seed);
|
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
|
let finishAfter = new Date((new Date().getTime() / 1000) + 120); // 2 minutes from now
|
||||||
finishAfter = new Date(finishAfter * 1000);
|
finishAfter = new Date(finishAfter * 1000);
|
||||||
console.log("This escrow will finish after: ", finishAfter);
|
console.log("This escrow will finish after: ", finishAfter);
|
||||||
@@ -48,13 +45,15 @@ const main = async () => {
|
|||||||
"DestinationTag": 2023,
|
"DestinationTag": 2023,
|
||||||
"Condition": conditionHex,
|
"Condition": conditionHex,
|
||||||
"Fee": "12",
|
"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);
|
xrpl.validate(escrowCreateTransaction);
|
||||||
|
|
||||||
// Sign and submit the transaction --------------------------------------------
|
// Sign and submit the transaction ----------------------------------------
|
||||||
console.log('Signing and submitting the transaction:', JSON.stringify(escrowCreateTransaction, null, "\t"), "\n");
|
console.log('Signing and submitting the transaction:',
|
||||||
|
JSON.stringify(escrowCreateTransaction, null, "\t"), "\n"
|
||||||
|
);
|
||||||
const response = await client.submitAndWait(escrowCreateTransaction, { wallet });
|
const response = await client.submitAndWait(escrowCreateTransaction, { wallet });
|
||||||
console.log(`Sequence number: ${response.result.Sequence}`);
|
console.log(`Sequence number: ${response.result.Sequence}`);
|
||||||
console.log(`Finished submitting! ${JSON.stringify(response.result, null, "\t")}`);
|
console.log(`Finished submitting! ${JSON.stringify(response.result, null, "\t")}`);
|
||||||
|
|||||||
@@ -1,8 +1,5 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
if (typeof module !== "undefined") {
|
const xrpl = require('xrpl')
|
||||||
// Use var here because const/let are block-scoped to the if statement.
|
|
||||||
var xrpl = require('xrpl')
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preqrequisites:
|
// Preqrequisites:
|
||||||
// 1. Create an escrow using the create-escrow.js snippet
|
// 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
|
// 4. Paste the seed of the account that created the escrow
|
||||||
// 5. Run the snippet
|
// 5. Run the snippet
|
||||||
|
|
||||||
const seed = "sEd7jfWyNG6J71dEojB3W9YdHp2KCjy";
|
const seed = "sEd7jfWyNG6J71dEojB3W9YdHp2KCjy"; // Test seed. Don't use
|
||||||
const offerSequence = null;
|
const offerSequence = null;
|
||||||
const condition = "";
|
const condition = "";
|
||||||
const fulfillment = "";
|
const fulfillment = "";
|
||||||
|
|
||||||
const main = async () => {
|
const main = async () => {
|
||||||
try {
|
try {
|
||||||
// Connect -------------------------------------------------------------------
|
// Connect ----------------------------------------------------------------
|
||||||
const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233');
|
const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233');
|
||||||
await client.connect();
|
await client.connect();
|
||||||
|
|
||||||
// Prepare wallet to sign the transaction -------------------------------------
|
// Prepare wallet to sign the transaction ---------------------------------
|
||||||
const wallet = await xrpl.Wallet.fromSeed(seed);
|
const wallet = await xrpl.Wallet.fromSeed(seed);
|
||||||
console.log("Wallet Address: ", wallet.address);
|
console.log("Wallet Address: ", wallet.address);
|
||||||
console.log("Seed: ", seed);
|
console.log("Seed: ", seed);
|
||||||
@@ -45,7 +42,7 @@ const main = async () => {
|
|||||||
|
|
||||||
xrpl.validate(escrowFinishTransaction);
|
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"));
|
console.log('Signing and submitting the transaction:', JSON.stringify(escrowFinishTransaction, null, "\t"));
|
||||||
const response = await client.submitAndWait(escrowFinishTransaction, { wallet });
|
const response = await client.submitAndWait(escrowFinishTransaction, { wallet });
|
||||||
console.log(`Finished submitting! ${JSON.stringify(response.result, null, "\t")}`);
|
console.log(`Finished submitting! ${JSON.stringify(response.result, null, "\t")}`);
|
||||||
|
|||||||
@@ -1,56 +1,54 @@
|
|||||||
if (typeof module !== "undefined") {
|
const xrpl = require('xrpl')
|
||||||
// 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
|
|
||||||
|
|
||||||
async function main() {
|
// List the Escrows on an existing account filtered by source and destination
|
||||||
// Testnet example: rPRKeXbcFMcn69nR2bovp4bEcP8kZx7x5i
|
// https://xrpl.org/escrow.html#escrow
|
||||||
account = "rPRKeXbcFMcn69nR2bovp4bEcP8kZx7x5i"
|
// https://xrpl.org/account_objects.html#account_objects
|
||||||
|
|
||||||
// Connect to a testnet node
|
async function main() {
|
||||||
console.log("Connecting to Testnet...")
|
// Testnet example: rPRKeXbcFMcn69nR2bovp4bEcP8kZx7x5i
|
||||||
const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233/')
|
account = "rPRKeXbcFMcn69nR2bovp4bEcP8kZx7x5i"
|
||||||
await client.connect()
|
|
||||||
|
|
||||||
const response = await client.request({
|
// 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",
|
"command": "account_objects",
|
||||||
"account": account,
|
"account": account,
|
||||||
"ledger_index": "validated",
|
"ledger_index": "validated",
|
||||||
"type": "escrow"
|
"type": "escrow"
|
||||||
})
|
})
|
||||||
|
|
||||||
var incoming = []
|
var incoming = []
|
||||||
var outgoing = []
|
var outgoing = []
|
||||||
|
|
||||||
for (var i = 0; i < response.result.account_objects.length; i++) {
|
for (var i = 0; i < response.result.account_objects.length; i++) {
|
||||||
if (response.result.account_objects[i].Account == account) {
|
if (response.result.account_objects[i].Account == account) {
|
||||||
outgoing.push(response.result.account_objects[i])
|
outgoing.push(response.result.account_objects[i])
|
||||||
} else {
|
} else {
|
||||||
incoming.push(response.result.account_objects[i])
|
incoming.push(response.result.account_objects[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("\nIncoming/Received escrow(s):")
|
console.log("\nIncoming/Received escrow(s):")
|
||||||
for (var i = 0; i < incoming.length; i++) {
|
for (var i = 0; i < incoming.length; i++) {
|
||||||
console.log(`\n${i+1}. Index (ObjectID/keylet): ${incoming[i].index}`)
|
console.log(`\n${i+1}. Index (ObjectID/keylet): ${incoming[i].index}`)
|
||||||
console.log(` - Account: ${incoming[i].Account})`)
|
console.log(` - Account: ${incoming[i].Account})`)
|
||||||
console.log(` - Destination: ${incoming[i].Destination}`)
|
console.log(` - Destination: ${incoming[i].Destination}`)
|
||||||
console.log(` - Amount: ${incoming[i].Amount} drops`)
|
console.log(` - Amount: ${incoming[i].Amount} drops`)
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("\nOutgoing/Sent escrow(s):")
|
console.log("\nOutgoing/Sent escrow(s):")
|
||||||
for (var i = 0; i < outgoing.length; i++) {
|
for (var i = 0; i < outgoing.length; i++) {
|
||||||
console.log(`\n${i+1}. Index (ObjectID/keylet): ${outgoing[i].index}`)
|
console.log(`\n${i+1}. Index (ObjectID/keylet): ${outgoing[i].index}`)
|
||||||
console.log(` - Account: ${outgoing[i].Account})`)
|
console.log(` - Account: ${outgoing[i].Account})`)
|
||||||
console.log(` - Destination: ${outgoing[i].Destination}`)
|
console.log(` - Destination: ${outgoing[i].Destination}`)
|
||||||
console.log(` - Amount: ${outgoing[i].Amount} drops`)
|
console.log(` - Amount: ${outgoing[i].Amount} drops`)
|
||||||
}
|
}
|
||||||
|
|
||||||
client.disconnect()
|
client.disconnect()
|
||||||
// End main()
|
// End main()
|
||||||
}
|
}
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
{
|
{
|
||||||
"name": "escrow-examples",
|
"name": "escrow-examples",
|
||||||
"version": "0.0.1",
|
"version": "0.0.3",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"five-bells-condition": "*",
|
"five-bells-condition": "*",
|
||||||
"ripple-lib": "^0.17.6",
|
|
||||||
"xrpl": "^3.0.0"
|
"xrpl": "^3.0.0"
|
||||||
},
|
}
|
||||||
"//": "Intended for Node.js version ?.? and higher"
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user