UNPKG

@swtc/common

Version:
149 lines 4.69 kB
import { CURRENCY_RE, HASH_RE, NODE_TYPES } from "./constants"; import { funcHexToString as hexToString } from "./functions"; export function formatArgs(args) { const newArgs = []; if (args) { for (const arg of args) { newArgs.push(hexToString(arg.Arg.Parameter)); } } return newArgs; } export function getTypeNode(node) { for (const type of NODE_TYPES) { if (node.hasOwnProperty(type)) { return node[type]; } } return null; } export function processAffectNode(an) { const result = {}; NODE_TYPES.forEach(x => { if (an[x]) { result.diffType = x; } }); if (!result.diffType) return {}; an = an[result.diffType]; result.entryType = an.LedgerEntryType; result.ledgerIndex = an.LedgerIndex; result.fields = Object.assign({}, an.PreviousFields, an.NewFields, an.FinalFields); result.fieldsPrev = an.PreviousFields || {}; result.fieldsNew = an.NewFields || {}; result.fieldsFinal = an.FinalFields || {}; result.PreviousTxnID = an.PreviousTxnID; return result; } export function affectedAccounts(tx) { const accounts = {}; accounts[tx.transaction.Account] = 1; if (tx.transaction.Destination) { accounts[tx.transaction.Destination] = 1; } if (tx.transaction.LimitAmount) { accounts[tx.transaction.LimitAmount.issuer] = 1; } const meta = tx.meta; if (meta && meta.TransactionResult === "tesSUCCESS") { meta.AffectedNodes.forEach(n => { const node = processAffectNode(n); if (node.entryType === "AccountRoot" && node.fields.Account) { accounts[node.fields.Account] = 1; } if (node.entryType === "SkywellState") { if (node.fields.HighLimit.issuer) { accounts[node.fields.HighLimit.issuer] = 1; } if (node.fields.LowLimit.issuer) { accounts[node.fields.LowLimit.issuer] = 1; } } if (node.entryType === "Offer" && node.fields.Account) { accounts[node.fields.Account] = 1; } }); } return Object.keys(accounts); } export function getTypes(abi, foo) { try { const filtered = abi .filter(json => json.name === foo) .map(json => json.outputs.map(input => input.type)) .map(types => types); return filtered ? filtered[0] : []; } catch (error) { return []; } } export function isValidCurrency(currency) { if (!currency || typeof currency !== "string" || currency === "") { return false; } return CURRENCY_RE.test(currency); } export function isValidHash(hash) { if (!hash || typeof hash !== "string" || hash === "") { return false; } return HASH_RE.test(hash); } export function txnType(tx, account) { if (tx.Account === account || tx.Target === account || (tx.Destination && tx.Destination === account) || (tx.LimitAmount && tx.LimitAmount.issuer === account) || tx.BlackListAccountID === account) { switch (tx.TransactionType) { case "Payment": return tx.Account === account ? tx.Destination === account ? "convert" : "sent" : "received"; case "OfferCreate": return "offernew"; case "OfferCancel": return "offercancel"; case "TrustSet": return tx.Account === account ? "trusting" : "trusted"; case "RelationDel": case "AccountSet": case "SetRegularKey": case "RelationSet": case "SignSet": case "Operation": case "ConfigContract": case "AlethContract": case "Brokerage": case "SignerListSet": case "SetBlackList": case "RemoveBlackList": case "TokenIssue": case "TransferToken": case "TokenDel": return tx.TransactionType.toLowerCase(); default: return "unknown"; } } else { return "offereffect"; } } export function reverseAmount(amount, account) { return { value: String(-Number(amount.value)), currency: amount.currency, issuer: account }; } export function isAmountZero(amount) { if (!amount) return false; return Number(amount.value) < 1e-12; } //# sourceMappingURL=utils.js.map