UNPKG

@swtc/common

Version:
161 lines 5.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatArgs = formatArgs; exports.getTypeNode = getTypeNode; exports.processAffectNode = processAffectNode; exports.affectedAccounts = affectedAccounts; exports.getTypes = getTypes; exports.isValidCurrency = isValidCurrency; exports.isValidHash = isValidHash; exports.txnType = txnType; exports.reverseAmount = reverseAmount; exports.isAmountZero = isAmountZero; const constants_1 = require("./constants"); const functions_1 = require("./functions"); function formatArgs(args) { const newArgs = []; if (args) { for (const arg of args) { newArgs.push((0, functions_1.funcHexToString)(arg.Arg.Parameter)); } } return newArgs; } function getTypeNode(node) { for (const type of constants_1.NODE_TYPES) { if (node.hasOwnProperty(type)) { return node[type]; } } return null; } function processAffectNode(an) { const result = {}; constants_1.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; } 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); } 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 []; } } function isValidCurrency(currency) { if (!currency || typeof currency !== "string" || currency === "") { return false; } return constants_1.CURRENCY_RE.test(currency); } function isValidHash(hash) { if (!hash || typeof hash !== "string" || hash === "") { return false; } return constants_1.HASH_RE.test(hash); } 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"; } } function reverseAmount(amount, account) { return { value: String(-Number(amount.value)), currency: amount.currency, issuer: account }; } function isAmountZero(amount) { if (!amount) return false; return Number(amount.value) < 1e-12; } //# sourceMappingURL=utils.js.map