UNPKG

@bithomp/xrpl-api

Version:

A Bithomp JavaScript/TypeScript library for interacting with the XRP Ledger

126 lines (125 loc) 4.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.accountObjectsToAccountLines = accountObjectsToAccountLines; exports.accountObjectsToNFTOffers = accountObjectsToNFTOffers; exports.accountObjectsToURITokens = accountObjectsToURITokens; const xrpl_1 = require("xrpl"); const { RippleStateFlags } = xrpl_1.LedgerEntry; const common_1 = require("../common"); const ledger_1 = require("../models/ledger"); function accountObjectsToAccountLines(account, accountObjects) { const notInDefaultState = accountObjects.filter((node) => { if (node.LedgerEntryType !== "RippleState") { return false; } if (node.HighLimit.issuer === account) { if (node.Flags & RippleStateFlags.lsfHighReserve) { if (isPositiveBalance(node.Balance.value)) { return false; } return true; } if (isNegativeBalance(node.Balance.value)) { return true; } } else { if (node.Flags & RippleStateFlags.lsfLowReserve) { if (isNegativeBalance(node.Balance.value)) { return false; } return true; } if (isPositiveBalance(node.Balance.value)) { return true; } } return false; }); const accountLinesFormatted = notInDefaultState.map((node) => RippleStateToTrustLine(node, account)); return accountLinesFormatted; } function isPositiveBalance(balance) { return balance !== "0" && balance[0] !== "-"; } function isNegativeBalance(balance) { return balance !== "0" && balance[0] === "-"; } const RippleStateToTrustLine = (ledgerEntry, account) => { const viewLowest = ledgerEntry.LowLimit.issuer === account; const flags = ledgerEntry.Flags; const self = viewLowest ? ledgerEntry.LowLimit : ledgerEntry.HighLimit; const counterparty = viewLowest ? ledgerEntry.HighLimit : ledgerEntry.LowLimit; const { lsfLowNoRipple, lsfHighNoRipple, lsfLowAuth, lsfHighAuth } = RippleStateFlags; const no_ripple = (flags & (viewLowest ? lsfLowNoRipple : lsfHighNoRipple)) !== 0; const no_ripple_peer = (flags & (viewLowest ? lsfHighNoRipple : lsfLowNoRipple)) !== 0; const authorized = (flags & (viewLowest ? lsfLowAuth : lsfHighAuth)) !== 0; const peer_authorized = (flags & (viewLowest ? lsfHighAuth : lsfLowAuth)) !== 0; const balance = ledgerEntry.HighLimit.issuer === account && ledgerEntry.Balance.value.startsWith("-") ? ledgerEntry.Balance.value.slice(1) : ledgerEntry.Balance.value; let lockedBalance = ledgerEntry.LockedBalance?.value; if (lockedBalance && lockedBalance.startsWith("-")) { lockedBalance = lockedBalance.slice(1); } let lockCount = undefined; if (lockedBalance) { lockCount = ledgerEntry.LockCount; } return (0, common_1.removeUndefined)({ account: counterparty.issuer, balance, currency: self.currency, limit: self.value, limit_peer: counterparty.value, lock_count: lockCount, locked_balance: lockedBalance, no_ripple, no_ripple_peer, authorized, peer_authorized, }); }; function accountObjectsToNFTOffers(accountObjects) { const nftOfferObjects = accountObjects.filter((obj) => { return obj.LedgerEntryType === "NFTokenOffer"; }); const nftOffers = nftOfferObjects.map((obj) => { let expiration = obj.Expiration; if (typeof expiration === "number") { expiration = (0, ledger_1.ledgerTimeToUnixTime)(expiration); } return (0, common_1.removeUndefined)({ nft_id: obj.NFTokenID, amount: obj.Amount, flags: obj.Flags, index: obj.index, owner: obj.Owner, destination: obj.Destination, expiration, ledger_index: obj.PreviousTxnLgrSeq, transaction_hash: obj.PreviousTxnID, }); }); return nftOffers; } function accountObjectsToURITokens(accountObjects) { const uriTokenObjects = accountObjects.filter((obj) => { return obj.LedgerEntryType === "URIToken"; }); const uritokens = uriTokenObjects.map((obj) => { return (0, common_1.removeUndefined)({ flags: obj.Flags, index: obj.index, owner: obj.Owner, issuer: obj.Issuer, uri: obj.URI, digest: obj.Digest, amount: obj.Amount, destination: obj.Destination, ledger_index: obj.PreviousTxnLgrSeq, transaction_hash: obj.PreviousTxnID, }); }); return uritokens; }