UNPKG

@ledgerhq/coin-tron

Version:
162 lines 5.22 kB
import { getAccountCurrency } from "@ledgerhq/ledger-wallet-framework/account"; import invariant from "invariant"; import flatMap from "lodash/flatMap"; import zipWith from "lodash/zipWith"; import { from } from "rxjs"; import { map } from "rxjs/operators"; import { getTronSuperRepresentativeData } from "../network"; const options = [ { name: "token", alias: "t", type: String, desc: "use an token account children of the account", }, { name: "mode", type: String, desc: "mode of transaction: send, freeze, unfreeze, withdrawExpireUnfreeze, unDelegateResource, legacyUnfreeze", }, { name: "duration", type: String, desc: "duration in day", }, { name: "resource", type: String, desc: "reward ENERGY or BANDWIDTH", }, { name: "tronVoteName", type: String, multiple: true, desc: "name of the super representative voting", }, { name: "tronVoteAddress", type: String, multiple: true, desc: "address of the super representative voting", }, { name: "tronVoteCount", type: String, multiple: true, desc: "number of votes for the vote address", }, ]; function inferAccounts(account, opts) { invariant(account.currency.family === "tron", "tron family"); if (!opts.token) { const accounts = [account]; return accounts; } return opts.token.map((token) => { const subAccounts = account.subAccounts || []; if (token) { const subAccount = subAccounts.find(t => { const currency = getAccountCurrency(t); return (token.toLowerCase() === currency.ticker.toLowerCase() || token.toLowerCase() === currency.id); }); if (!subAccount) { throw new Error("token account '" + token + "' not found. Available: " + subAccounts.map(t => getAccountCurrency(t).ticker).join(", ")); } return subAccount; } }); } function inferTransactions(transactions, opts) { const mode = opts.mode || "send"; invariant([ "send", "freeze", "unfreeze", "vote", "claimReward", "withdrawExpireUnfreeze", "unDelegateResource", "legacyUnfreeze", ].includes(mode), `Unexpected mode: ${mode}`); const resource = opts.resource ? opts.resource.toUpperCase() : undefined; if (resource) { invariant(["BANDWIDTH", "ENERGY"].includes(resource), `Unexpected resource: ${resource}`); } const voteNames = opts["tronVoteName"] || []; const voteAddresses = opts["tronVoteAddress"] || []; const voteCounts = (opts["tronVoteCount"] || []).map((value) => { invariant(Number.isInteger(Number(value)), `Invalid integer: ${value}`); return parseInt(value); }); const votes = zipWith(voteNames, voteAddresses, voteCounts, (n, a, c) => ({ name: n, address: a, voteCount: c, })); return flatMap(transactions, ({ transaction, account }) => { invariant(transaction.family === "tron", "tron family"); if (account.type === "Account") { invariant(account.tronResources, "unactivated account"); } if (account.type === "TokenAccount") { const isDelisted = account.token.delisted === true; invariant(!isDelisted, "token is delisted"); } return { ...transaction, family: "tron", subAccountId: account.type === "TokenAccount" ? account.id : null, mode, resource, votes, }; }); } const formatOptStr = (str) => str || ""; const superRepresentativesFormatters = { json: (srData) => JSON.stringify(srData), default: (srData) => { const headerList = "address url voteCount isJobs"; const strList = srData.list.map(sr => `${sr.address} ${formatOptStr(sr.url)} ${sr.voteCount} ${sr.isJobs}`); const metaData = [ `nextVotingDate: ${srData.nextVotingDate}`, `totalVotes: ${srData.totalVotes}`, ]; return [headerList].concat(strList).concat(metaData).join("\n"); }, }; const tronSuperRepresentative = { args: [ { name: "max", desc: "max number of super representatives to return", type: Number, }, { name: "format", desc: Object.keys(superRepresentativesFormatters).join(" | "), type: String, }, ], job: ({ max, format, }) => from(getTronSuperRepresentativeData(max)).pipe(map((srData) => { const f = format ? superRepresentativesFormatters[format] : superRepresentativesFormatters.default; return f(srData); })), }; export default function makeCliTools() { return { options, inferAccounts, inferTransactions, commands: { tronSuperRepresentative, }, }; } //# sourceMappingURL=cli.js.map