@bithomp/xrpl-api
Version:
A Bithomp JavaScript/TypeScript library for interacting with the XRP Ledger
64 lines (63 loc) • 2.78 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseDeliveredAmount = parseDeliveredAmount;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const client_1 = require("../../client");
const import_1 = require("../ledger/import");
const amount_1 = __importDefault(require("../ledger/amount"));
const balance_changes_1 = require("./balance_changes");
function parseDeliveredAmount(tx, balanceChanges) {
if (tx.meta.TransactionResult !== "tesSUCCESS") {
return undefined;
}
if (tx.TransactionType === "Payment") {
const txBalanceChanges = balanceChanges || (0, balance_changes_1.parseBalanceChanges)(tx.meta, (0, client_1.getNativeCurrency)());
if (txBalanceChanges) {
const account = tx.Destination;
const changes = txBalanceChanges[account];
if (changes) {
const positives = changes.filter((change) => change.value[0] !== "-");
if (positives.length === 1) {
return positives[0];
}
}
}
}
else if (tx.TransactionType === "CheckCash") {
const txBalanceChanges = balanceChanges || (0, balance_changes_1.parseBalanceChanges)(tx.meta, (0, client_1.getNativeCurrency)());
if (txBalanceChanges) {
const account = tx.Account;
const changes = txBalanceChanges[account];
if (changes) {
const positives = changes.filter((change) => change.value[0] !== "-");
if (positives.length === 1) {
return positives[0];
}
}
}
}
else if (tx.TransactionType === "Import") {
const txBalanceChanges = balanceChanges || (0, balance_changes_1.parseBalanceChanges)(tx.meta, (0, client_1.getNativeCurrency)());
const blob = (0, import_1.parseImportBlob)(tx.Blob);
if (typeof blob === "object") {
const account = blob.transaction.tx.Account;
const balanceChange = txBalanceChanges[account];
if (balanceChange && balanceChange.length === 1) {
return {
currency: balanceChange[0].currency,
value: new bignumber_js_1.default(balanceChange[0].value).abs().toString(),
};
}
}
}
if (tx.meta.delivered_amount && tx.meta.delivered_amount !== "unavailable") {
return (0, amount_1.default)(tx.meta.delivered_amount);
}
if (tx.meta.DeliveredAmount) {
return (0, amount_1.default)(tx.meta.DeliveredAmount);
}
return undefined;
}
;