@bithomp/xrpl-api
Version:
A Bithomp JavaScript/TypeScript library for interacting with the XRP Ledger
235 lines (234 loc) • 9.42 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseOutcome = parseOutcome;
const client_1 = require("../client");
const index_1 = require("./outcome/index");
const utils_1 = require("./utils");
const common_1 = require("../common");
const ESCROW_TYPES = ["EscrowFinish", "EscrowCreate", "EscrowCancel"];
const CHANNEL_TYPES = ["PaymentChannelCreate", "PaymentChannelFund", "PaymentChannelClaim"];
const CHECK_TYPES = ["CheckCreate", "CheckCash", "CheckCancel"];
const NFTOKEN_TYPES = [
"NFTokenMint",
"NFTokenBurn",
"NFTokenCreateOffer",
"NFTokenCancelOffer",
"NFTokenAcceptOffer",
"NFTokenModify",
];
const URITOKEN_TYPES = [
"Remit",
"URITokenMint",
"URITokenBurn",
"URITokenBuy",
"URITokenCreateSellOffer",
"URITokenCancelSellOffer",
];
const AMM_TYPES = ["AMMBid", "AMMCreate", "AMMDelete", "AMMDeposit", "AMMWithdraw", "AMMVote", "AMMClawback"];
const DID_TYPES = ["DIDSet", "DIDDelete"];
const ORACLE_TYPES = ["OracleSet", "OracleDelete"];
const UNL_REPORT_TYPES = ["UNLReport"];
const MPTOKEN_TYPES = [
"MPTokenIssuanceCreate",
"MPTokenAuthorize",
"MPTokenIssuanceSet",
"MPTokenIssuanceDestroy",
"Payment",
"Clawback",
];
const CREDENTIAL_TYPES = ["CredentialCreate", "CredentialAccept", "CredentialDelete"];
const DELEGATE_TYPES = ["DelegateSet"];
const REMARKS_TYPES = ["SetRemarks"];
function parseOutcome(tx, nativeCurrency, definitions) {
const metadata = tx.meta || tx.metaData;
if (!metadata) {
return undefined;
}
const balanceChanges = getBalanceChanges(tx, nativeCurrency || (0, client_1.getNativeCurrency)());
return (0, common_1.removeUndefined)({
result: tx.meta.TransactionResult,
timestamp: (0, utils_1.parseTimestamp)(tx.date),
fee: (0, common_1.dropsToXrp)(tx.Fee),
balanceChanges,
lockedBalanceChanges: getLockedBalanceChanges(tx),
orderbookChanges: getOrderbookChanges(tx),
channelChanges: getChannelChanges(tx),
checkChanges: getCheckChanges(tx),
escrowChanges: getEscrowChanges(tx),
nftokenChanges: getNFTokenChanges(tx),
nftokenOfferChanges: getNFTokenOfferChanges(tx),
uritokenChanges: getURITokenChanges(tx),
uritokenSellOfferChanges: getURITokenSellOfferChanges(tx),
affectedObjects: getAffectedObjects(tx),
ammChanges: getAmmChanges(tx),
didChanges: getDIDChanges(tx),
oracleChanges: getOracleChanges(tx),
mptokenIssuanceChanges: getMPTokenIssuanceChanges(tx, nativeCurrency || (0, client_1.getNativeCurrency)()),
mptokenChanges: getMPTokenChanges(tx, nativeCurrency || (0, client_1.getNativeCurrency)()),
credentialChanges: getCredentialChanges(tx, nativeCurrency || (0, client_1.getNativeCurrency)()),
delegateChanges: getDelegateChanges(tx, nativeCurrency || (0, client_1.getNativeCurrency)()),
remarksChanges: getRemarksChanges(tx, nativeCurrency || (0, client_1.getNativeCurrency)()),
unlReportChanges: getUNLReportChanges(tx, nativeCurrency || (0, client_1.getNativeCurrency)()),
hooksExecutions: getHooksExecutions(tx, nativeCurrency || (0, client_1.getNativeCurrency)()),
emittedTxns: getEmittedTxns(tx, nativeCurrency || (0, client_1.getNativeCurrency)(), definitions),
parentBatchID: tx.meta.ParentBatchID,
ledgerIndex: tx.ledger_index || tx.inLedger,
ledgerVersion: tx.ledger_index || tx.inLedger,
indexInLedger: tx.meta.TransactionIndex,
deliveredAmount: (0, index_1.parseDeliveredAmount)(tx, balanceChanges),
});
}
function getBalanceChanges(tx, nativeCurrency) {
const balanceChanges = (0, index_1.parseBalanceChanges)(tx.meta, nativeCurrency);
return Object.keys(balanceChanges).length > 0 ? balanceChanges : undefined;
}
function getLockedBalanceChanges(tx) {
if (!ESCROW_TYPES.includes(tx.TransactionType)) {
return undefined;
}
const lockedBalanceChanges = (0, index_1.parseLockedBalanceChanges)(tx.meta);
return Object.keys(lockedBalanceChanges).length > 0 ? lockedBalanceChanges : undefined;
}
function getOrderbookChanges(tx) {
const orderbookChanges = (0, index_1.parseOrderbookChanges)(tx.meta);
return Object.keys(orderbookChanges).length > 0 ? orderbookChanges : undefined;
}
function getChannelChanges(tx) {
if (!CHANNEL_TYPES.includes(tx.TransactionType)) {
return undefined;
}
return (0, index_1.parseChannelChanges)(tx.meta);
}
function getCheckChanges(tx) {
if (!CHECK_TYPES.includes(tx.TransactionType)) {
return undefined;
}
return (0, index_1.parseCheckChanges)(tx.meta);
}
function getEscrowChanges(tx) {
if (!ESCROW_TYPES.includes(tx.TransactionType)) {
return undefined;
}
return (0, index_1.parseEscrowChanges)(tx);
}
function getAffectedObjects(tx) {
if (!NFTOKEN_TYPES.includes(tx.TransactionType) && !URITOKEN_TYPES.includes(tx.TransactionType)) {
return undefined;
}
const affectedObjects = (0, index_1.parseAffectedObjects)(tx);
return affectedObjects ? (0, common_1.removeUndefined)(affectedObjects) : undefined;
}
function getNFTokenChanges(tx) {
if (!NFTOKEN_TYPES.includes(tx.TransactionType)) {
return undefined;
}
const nftokenChanges = (0, index_1.parseNFTokenChanges)(tx);
return Object.keys(nftokenChanges).length > 0 ? nftokenChanges : undefined;
}
function getNFTokenOfferChanges(tx) {
if (!NFTOKEN_TYPES.includes(tx.TransactionType)) {
return undefined;
}
const nftokenOfferChanges = (0, index_1.parseNFTokenOfferChanges)(tx);
return Object.keys(nftokenOfferChanges).length > 0 ? nftokenOfferChanges : undefined;
}
function getURITokenChanges(tx) {
if (!URITOKEN_TYPES.includes(tx.TransactionType)) {
return undefined;
}
const uritokenChanges = (0, index_1.parseURITokenChanges)(tx);
return Object.keys(uritokenChanges).length > 0 ? uritokenChanges : undefined;
}
function getURITokenSellOfferChanges(tx) {
if (!URITOKEN_TYPES.includes(tx.TransactionType)) {
return undefined;
}
const uritokenSellOfferChanges = (0, index_1.parseURITokenSellOfferChanges)(tx);
return Object.keys(uritokenSellOfferChanges).length > 0 ? uritokenSellOfferChanges : undefined;
}
function getHooksExecutions(tx, nativeCurrency) {
if (nativeCurrency !== common_1.XAHAU_NATIVE_CURRENCY) {
return undefined;
}
return (0, index_1.parseHooksExecutions)(tx);
}
function getEmittedTxns(tx, nativeCurrency, definitions) {
if (nativeCurrency !== common_1.XAHAU_NATIVE_CURRENCY) {
return undefined;
}
return (0, index_1.parseEmittedTxns)(tx, nativeCurrency, definitions);
}
function getAmmChanges(tx) {
if (!AMM_TYPES.includes(tx.TransactionType)) {
return undefined;
}
const ammChanges = (0, index_1.parseAmmChanges)(tx.meta);
return ammChanges ? (0, common_1.removeUndefined)(ammChanges) : undefined;
}
function getDIDChanges(tx) {
if (!DID_TYPES.includes(tx.TransactionType)) {
return undefined;
}
const didChanges = (0, index_1.parseDIDChanges)(tx.meta);
return didChanges ? (0, common_1.removeUndefined)(didChanges) : undefined;
}
function getOracleChanges(tx) {
if (!ORACLE_TYPES.includes(tx.TransactionType)) {
return undefined;
}
const oracleChanges = (0, index_1.parseOracleChanges)(tx.meta);
return oracleChanges ? (0, common_1.removeUndefined)(oracleChanges) : undefined;
}
function getUNLReportChanges(tx, nativeCurrency) {
if (nativeCurrency !== common_1.XAHAU_NATIVE_CURRENCY) {
return undefined;
}
if (!UNL_REPORT_TYPES.includes(tx.TransactionType)) {
return undefined;
}
return (0, index_1.parseUNLReportChanges)(tx);
}
function getMPTokenChanges(tx, nativeCurrency) {
if (nativeCurrency !== common_1.MAINNET_NATIVE_CURRENCY) {
return undefined;
}
if (!MPTOKEN_TYPES.includes(tx.TransactionType)) {
return undefined;
}
const mptokenChanges = (0, index_1.parseMPTokenChanges)(tx);
return Object.keys(mptokenChanges).length > 0 ? mptokenChanges : undefined;
}
function getMPTokenIssuanceChanges(tx, nativeCurrency) {
if (nativeCurrency !== common_1.MAINNET_NATIVE_CURRENCY) {
return undefined;
}
const mptokenIssuanceChanges = (0, index_1.parseMPTokenIssuanceChanges)(tx);
return Object.keys(mptokenIssuanceChanges).length > 0 ? mptokenIssuanceChanges : undefined;
}
function getCredentialChanges(tx, nativeCurrency) {
if (nativeCurrency !== common_1.MAINNET_NATIVE_CURRENCY) {
return undefined;
}
if (!CREDENTIAL_TYPES.includes(tx.TransactionType)) {
return undefined;
}
return (0, index_1.parseCredentialChanges)(tx.meta);
}
function getDelegateChanges(tx, nativeCurrency) {
if (nativeCurrency !== common_1.MAINNET_NATIVE_CURRENCY) {
return undefined;
}
if (!DELEGATE_TYPES.includes(tx.TransactionType)) {
return undefined;
}
return (0, index_1.parseDelegateChanges)(tx.meta);
}
function getRemarksChanges(tx, nativeCurrency) {
if (nativeCurrency !== common_1.XAHAU_NATIVE_CURRENCY) {
return undefined;
}
if (!REMARKS_TYPES.includes(tx.TransactionType)) {
return undefined;
}
return (0, index_1.parseRemarksChanges)(tx);
}