Files
xrpl-dev-portal/_code-samples/modular-tutorials/send-checks.js
2025-04-24 10:49:20 -07:00

140 lines
5.0 KiB
JavaScript

// *******************************************************
// ***************** Send Check **************************
// *******************************************************
async function sendCheck() {
let net = getNet()
const client = new xrpl.Client(net)
await client.connect()
results = `\nConnected to ${net}.`
resultField.value = results
const wallet = xrpl.Wallet.fromSeed(accountSeedField.value)
let check_amount = amountField.value
if (currencyField.value != "XRP") {
check_amount = {
"currency": currencyField.value,
"value": amountField.value,
"issuer": wallet.address
}
}
const send_check_tx = {
"TransactionType": "CheckCreate",
"Account": wallet.address,
"SendMax": check_amount,
"Destination": destinationField.value
}
const check_prepared = await client.autofill(send_check_tx)
const check_signed = wallet.sign(check_prepared)
results += '\nSending ' + amountField.value + ' ' + currencyField.value + ' to ' +
destinationField.value + '...\n'
resultField.value = results
const check_result = await client.submitAndWait(check_signed.tx_blob)
if (check_result.result.meta.TransactionResult == "tesSUCCESS") {
results += 'Transaction succeeded:\n'
resultField.value += JSON.stringify(check_result.result, null, 2)
} else {
results += `Error sending transaction: ${check_result.result.meta.TransactionResult}`
resultField.value = results
}
xrpBalanceField.value = (await client.getXrpBalance(wallet.address))
client.disconnect()
} // end of sendCheck()
// *******************************************************
// ********************* Get Checks **********************
// *******************************************************
async function getChecks() {
let net = getNet()
const client = new xrpl.Client(net)
await client.connect()
let results = `\nConnected to ${net}.`
resultField.value = results
const check_objects = await client.request({
"id": 5,
"command": "account_objects",
"account": accountAddressField.value,
"ledger_index": "validated",
"type": "check"
})
resultField.value = JSON.stringify(check_objects.result, null, 2)
client.disconnect()
} // End of getChecks()
// *******************************************************
// ******************** Cash Check **********************
// *******************************************************
async function cashCheck() {
let net = getNet()
const client = new xrpl.Client(net)
await client.connect()
results = `\nConnected to ${net}.`
resultField.value = results
const wallet = xrpl.Wallet.fromSeed(accountSeedField.value)
var check_amount = amountField.value
if (currencyField.value != "XRP") {
check_amount = {
"value": amountField.value,
"currency": currencyField.value,
"issuer": issuerField.value
}
}
const cash_check_tx = {
"TransactionType": "CheckCash",
"Account": wallet.address,
"Amount": check_amount,
"CheckID": checkIdField.value
}
const cash_prepared = await client.autofill(cash_check_tx)
const cash_signed = wallet.sign(cash_prepared)
results += ' Receiving ' + amountField.value + ' ' + currencyField.value + '.\n'
resultField.value = results
const check_result = await client.submitAndWait(cash_signed.tx_blob)
if (check_result.result.meta.TransactionResult == "tesSUCCESS") {
results += 'Transaction succeeded:\n' + JSON.stringify(check_result.result, null, 2)
resultField.value = results
} else {
results += `Error sending transaction: ${check_result.result.meta.TransactionResult}`
resultField.value = results
}
xrpBalanceField.value = (await client.getXrpBalance(wallet.address))
client.disconnect()
} // end of cashCheck()
// *******************************************************
// **************** Cancel Check *************************
// *******************************************************
async function cancelCheck() {
let net = getNet()
const client = new xrpl.Client(net)
await client.connect()
results = `\nConnected to ${net}.`
resultField.value = results
const wallet = xrpl.Wallet.fromSeed(accountSeedField.value)
const cancel_check_tx = {
"TransactionType": "CheckCancel",
"Account": wallet.address,
"CheckID": checkIdField.value
}
const cancel_prepared = await client.autofill(cancel_check_tx)
const cancel_signed = wallet.sign(cancel_prepared)
results += ' Cancelling check.\n'
resultField.value = results
const check_result = await client.submitAndWait(cancel_signed.tx_blob)
if (check_result.result.meta.TransactionResult == "tesSUCCESS") {
results += `Transaction succeeded: ${check_result.result.meta.TransactionResult}`
resultField.value = results
} else {
results += `Error sending transaction: ${check_result.result.meta.TransactionResult}`
resultField.value = results
}
xrpBalanceField.value = (await client.getXrpBalance(wallet.address))
client.disconnect()
} // end of cancelCheck()