@bithomp/xrpl-api
Version:
A Bithomp JavaScript/TypeScript library for interacting with the XRP Ledger
76 lines (75 loc) • 2.63 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseEscrowChanges = parseEscrowChanges;
const common_1 = require("../../common");
const utils_1 = require("../utils");
function parseEscrowStatus(tx, node) {
if (node.diffType === "CreatedNode") {
return "created";
}
if (node.diffType === "DeletedNode") {
if (tx.TransactionType === "EscrowCancel") {
return "cancelled";
}
if (tx.TransactionType === "EscrowFinish") {
return "executed";
}
return "deleted";
}
return undefined;
}
function parseEscrowSequence(tx) {
if (tx.TransactionType === "EscrowCreate") {
return tx.Sequence || tx.TicketSequence;
}
if (tx.TransactionType === "EscrowCancel" || tx.TransactionType === "EscrowFinish") {
return tx.OfferSequence;
}
return undefined;
}
function summarizeEscrow(tx, node) {
const final = node.diffType === "CreatedNode" ? node.newFields : node.finalFields;
const source = {
address: final.Account,
tag: final.SourceTag,
};
const destination = {
address: final.Destination,
tag: final.DestinationTag,
};
const summary = {
status: parseEscrowStatus(tx, node),
escrowIndex: node.ledgerIndex,
escrowSequence: parseEscrowSequence(tx),
amount: final.Amount,
condition: final.Condition,
source: (0, common_1.removeUndefined)(source),
destination: (0, common_1.removeUndefined)(destination),
allowCancelAfter: (0, utils_1.parseTimestamp)(final.CancelAfter),
allowExecuteAfter: (0, utils_1.parseTimestamp)(final.FinishAfter),
};
if (final.PreviousTxnID) {
summary.previousTxnID = final.PreviousTxnID;
}
else if (node.diffType === "CreatedNode") {
summary.previousTxnID = tx.hash;
}
if (final.PreviousTxnLgrSeq) {
summary.previousTxnLgrSeq = final.PreviousTxnLgrSeq;
}
else if (node.diffType === "CreatedNode") {
summary.previousTxnLgrSeq = tx.ledger_index;
}
return (0, common_1.removeUndefined)(summary);
}
function parseEscrowChanges(tx) {
const affectedNodes = tx.meta.AffectedNodes.filter((affectedNode) => {
const node = affectedNode.CreatedNode || affectedNode.ModifiedNode || affectedNode.DeletedNode;
return node.LedgerEntryType === "Escrow";
});
if (affectedNodes.length !== 1) {
return undefined;
}
const normalizedNode = (0, utils_1.normalizeNode)(affectedNodes[0]);
return summarizeEscrow(tx, normalizedNode);
}
;