@ledgerhq/coin-algorand
Version:
Ledger Algorand Coin integration
92 lines • 3.19 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("@ledgerhq/coin-framework/account/index");
const invariant_1 = __importDefault(require("invariant"));
const flatMap_1 = __importDefault(require("lodash/flatMap"));
const tokens_1 = require("./tokens");
const options = [
{
name: "mode",
type: String,
desc: "mode of transaction: send, optIn, claimReward",
},
{
name: "fees",
type: String,
desc: "how much fees",
},
{
name: "gasLimit",
type: String,
desc: "how much gasLimit. default is estimated with the recipient",
},
{
name: "memo",
type: String,
desc: "set a memo",
},
{
name: "token",
alias: "t",
type: String,
desc: "use an token account children of the account",
multiple: true,
},
];
function inferAccounts(account, opts) {
(0, invariant_1.default)(account.currency.family === "algorand", "algorand family");
if (!opts.token || opts.mode === "optIn") {
const accounts = [account];
return accounts;
}
return opts.token.map((token) => {
const subAccounts = account.subAccounts || [];
if (token) {
const subAccount = subAccounts.find(t => {
const currency = (0, index_1.getAccountCurrency)(t);
return (token.toLowerCase() === currency.ticker.toLowerCase() ||
token.toLowerCase() === (0, tokens_1.extractTokenId)(currency.id));
});
if (!subAccount) {
throw new Error("token account '" +
token +
"' not found. Available: " +
subAccounts.map(t => (0, index_1.getAccountCurrency)(t).ticker).join(", "));
}
return subAccount;
}
});
}
function inferTransactions(transactions, opts, { inferAmount }) {
return (0, flatMap_1.default)(transactions, ({ transaction, account }) => {
(0, invariant_1.default)(transaction.family === "algorand", "algorand family");
if (account.type === "Account") {
(0, invariant_1.default)(account.algorandResources, "unactivated account");
}
if (account.type === "TokenAccount") {
const isDelisted = account.token.delisted === true;
(0, invariant_1.default)(!isDelisted, "token is delisted");
}
return {
...transaction,
family: "algorand",
fees: opts.fees ? inferAmount(account, opts.fees) : null,
memo: opts.memo,
mode: opts.mode || "send",
subAccountId: account.type === "TokenAccount" ? account.id : null,
assetId: opts.token ? "algorand/asa/" + opts.token : null,
};
});
}
function makeCliTools() {
return {
options,
inferAccounts,
inferTransactions,
};
}
exports.default = makeCliTools;
//# sourceMappingURL=cli-transaction.js.map