mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-11-04 11:55:50 +00:00
Checks - JS Bounty (#1705)
This commit is contained in:
44
content/_code-samples/checks/js/cancel-check.js
Normal file
44
content/_code-samples/checks/js/cancel-check.js
Normal file
@@ -0,0 +1,44 @@
|
||||
const xrpl = require('xrpl');
|
||||
|
||||
const secret = "sEdTPPEeMH6SAgpo6rSj8YW7a9vFfUj"; // TODO: Replace with your secret
|
||||
const checkId = ""; // TODO: Replace with your check ID
|
||||
|
||||
const main = async ()=> {
|
||||
try {
|
||||
|
||||
// Connect ----------------------------------------------------------------
|
||||
const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233');
|
||||
await client.connect();
|
||||
|
||||
// Check if the check ID is provided --------------------------------------
|
||||
if (checkId.length === 0) {
|
||||
console.log("Please edit this snippet to provide a check ID. You can get a check ID by running create_check.js.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Prepare ----------------------------------------------------------------
|
||||
const wallet = await xrpl.Wallet.fromSeed(secret);
|
||||
console.log("Wallet address: ", wallet.address);
|
||||
|
||||
const checkCancelRequest = {
|
||||
"TransactionType": "CheckCancel",
|
||||
"Account": wallet.address,
|
||||
"CheckID": checkId
|
||||
};
|
||||
|
||||
// Auto-fill the fields ---------------------------------------------------
|
||||
const prepared = await client.autofill(checkCancelRequest);
|
||||
|
||||
// Submit -----------------------------------------------------------------
|
||||
const response = await client.submitAndWait(prepared, { wallet });
|
||||
console.log(JSON.stringify(response.result, null, "\t"));
|
||||
|
||||
// Disconnect -------------------------------------------------------------
|
||||
await client.disconnect();
|
||||
|
||||
} catch (error) {
|
||||
console.error(`Error: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
43
content/_code-samples/checks/js/cash-check.js
Normal file
43
content/_code-samples/checks/js/cash-check.js
Normal file
@@ -0,0 +1,43 @@
|
||||
'use strict'
|
||||
const xrpl = require('xrpl');
|
||||
|
||||
const secret = "sEdTPPEeMH6SAgpo6rSj8YW7a9vFfUj"; // TODO: Replace with your secret
|
||||
const checkId = ""; // TODO: Replace with your check ID
|
||||
const amount = "12"; // TODO: Replace with the amount you want to cash
|
||||
|
||||
const main = async () => {
|
||||
try {
|
||||
// Connect to the testnet
|
||||
const client = new xrpl.Client("wss://s.altnet.rippletest.net:51233");
|
||||
await client.connect();
|
||||
|
||||
// Generate a wallet ------------------------------------------------------
|
||||
const wallet = await xrpl.Wallet.fromSeed(secret);
|
||||
console.log("Wallet address: ", wallet.address);
|
||||
|
||||
// Check if the check ID is provided --------------------------------------
|
||||
if (checkId.length === 0) {
|
||||
console.log("Please edit this snippet to provide a check ID. You can get a check ID by running create_check.js.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Prepare the transaction ------------------------------------------------
|
||||
const transaction = {
|
||||
TransactionType: "CheckCash",
|
||||
Account: wallet.address,
|
||||
CheckID: checkId,
|
||||
Amount: amount,
|
||||
};
|
||||
|
||||
// Submit -----------------------------------------------------------------
|
||||
const response = await client.submitAndWait(transaction, { wallet });
|
||||
console.log(JSON.stringify(response.result, null, "\t"));
|
||||
|
||||
// Disconnect -------------------------------------------------------------
|
||||
await client.disconnect();
|
||||
} catch (error) {
|
||||
console.log("Error: ", error);
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
57
content/_code-samples/checks/js/create-check.js
Normal file
57
content/_code-samples/checks/js/create-check.js
Normal file
@@ -0,0 +1,57 @@
|
||||
'use strict'
|
||||
const xrpl = require('xrpl');
|
||||
const crypto = require('crypto');
|
||||
const hash = crypto.createHash('sha512');
|
||||
|
||||
// Destination address for the check
|
||||
const destination = "rP2BPdQ9ANSK7kVWT9jkjjDxCxL7xrC7oD";
|
||||
// Amount of XRP in drops to send
|
||||
const amount = "10000000";
|
||||
|
||||
const main = async () => {
|
||||
try {
|
||||
// Connect to the XRP Ledger Test Net -------------------------------------
|
||||
console.log("Connecting to Test Net...");
|
||||
const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233');
|
||||
await client.connect();
|
||||
console.log("Connected to Test Net");
|
||||
|
||||
// Generating new wallet --------------------------------------------------
|
||||
console.log("Generating new wallet...");
|
||||
const { wallet } = await client.fundWallet();
|
||||
console.log("Wallet Address:", wallet.address);
|
||||
console.log("Wallet Seed:", wallet.seed);
|
||||
|
||||
// Prepare the transaction ------------------------------------------------
|
||||
const request = {
|
||||
"TransactionType": "CheckCreate",
|
||||
"Account": wallet.address,
|
||||
"Destination": destination,
|
||||
"SendMax": amount,
|
||||
"DestinationTag": 1,
|
||||
"Fee": "12"
|
||||
};
|
||||
|
||||
// Submit the transaction -------------------------------------------------
|
||||
console.log("Submitting transaction...");
|
||||
const tx = await client.submitAndWait(request, { wallet });
|
||||
|
||||
// Get the check ID and transaction result --------------------------------
|
||||
const checkID = tx.result.meta.AffectedNodes.reduce((prevOutput, node) => {
|
||||
if (node?.CreatedNode && node.CreatedNode?.LedgerEntryType == "Check") {
|
||||
return node.CreatedNode.LedgerIndex;
|
||||
} else {
|
||||
return prevOutput;
|
||||
}
|
||||
}, null);
|
||||
|
||||
console.log(checkID ? `Check ID: ${checkID}` : "Unable to find the CheckID from parsing the metadata. Look for the LedgerIndex of the 'Check' object within 'meta'.");
|
||||
console.log(`Transaction: ${JSON.stringify(tx, null, "\t")}`);
|
||||
|
||||
// Disconnect -------------------------------------------------------------
|
||||
await client.disconnect();
|
||||
} catch (error) {
|
||||
console.error(`Error: ${error}`)
|
||||
}
|
||||
};
|
||||
main();
|
||||
55
content/_code-samples/checks/js/get-checks.js
Normal file
55
content/_code-samples/checks/js/get-checks.js
Normal file
@@ -0,0 +1,55 @@
|
||||
'use strict'
|
||||
const xrpl = require('xrpl');
|
||||
|
||||
const address = "rEYyvXCwv1WJ6a8hW4sBx5afXbJTK97bbR"; // <-- Change this to your account
|
||||
|
||||
const main = async () => {
|
||||
try {
|
||||
// Connect ----------------------------------------------------------------
|
||||
const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233');
|
||||
await client.connect();
|
||||
|
||||
let currMarker = null;
|
||||
let account_objects = [];
|
||||
|
||||
// Loop through all account objects until marker is undefined --------------
|
||||
do {
|
||||
const payload = {
|
||||
"command": "account_objects",
|
||||
"account": address,
|
||||
"ledger_index": "validated",
|
||||
"type": "check",
|
||||
};
|
||||
|
||||
if(currMarker !== null) {
|
||||
payload.marker = currMarker;
|
||||
};
|
||||
|
||||
let { result: { account_objects : checks, marker } } = await client.request(payload);
|
||||
|
||||
if (marker === currMarker) {
|
||||
break;
|
||||
}
|
||||
|
||||
account_objects.push(...checks);
|
||||
currMarker = marker;
|
||||
|
||||
} while (typeof currMarker !== "undefined")
|
||||
|
||||
// Print results ----------------------------------------------------------
|
||||
if (account_objects.length === 0) {
|
||||
console.log("No checks found.");
|
||||
} else {
|
||||
console.log("Checks: \n", JSON.stringify(account_objects, null, "\t"));
|
||||
};
|
||||
|
||||
// Disconnect -------------------------------------------------------------
|
||||
await client.disconnect();
|
||||
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
process.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
@@ -4,6 +4,7 @@
|
||||
"version": "0.0.1",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"ripple-lib": "^1.0.0-beta.1"
|
||||
"ripple-lib": "^1.0.0-beta.1",
|
||||
"xrpl": "^2.8.0-beta.0"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user