@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
88 lines • 4.01 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAssetInfos = exports.genericPrepareTransaction = void 0;
const alpaca_1 = require("./alpaca");
const utils_1 = require("./utils");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const index_1 = require("@ledgerhq/coin-framework/account/index");
function bnEq(a, b) {
return !a && !b ? true : !a || !b ? false : a.eq(b);
}
function assetInfosFallback(transaction) {
return {
assetReference: transaction.assetReference ?? "",
assetOwner: transaction.assetOwner ?? "",
};
}
function genericPrepareTransaction(network, kind) {
return async (account, transaction) => {
const { getAssetFromToken, computeIntentType, estimateFees, validateIntent } = (0, alpaca_1.getAlpacaApi)(account.currency.id, kind);
const { assetReference, assetOwner } = getAssetFromToken
? getAssetInfos(transaction, account.freshAddress, getAssetFromToken)
: assetInfosFallback(transaction);
const customParametersFees = transaction.customFees?.parameters?.fees;
let fees = customParametersFees || null;
if (fees === null) {
fees = (await estimateFees((0, utils_1.transactionToIntent)(account, {
...transaction,
}, computeIntentType))).value;
}
if (!bnEq(transaction.fees, new bignumber_js_1.default(fees.toString()))) {
const next = {
...transaction,
fees: new bignumber_js_1.default(fees.toString()),
assetReference,
assetOwner,
customFees: {
parameters: {
fees: customParametersFees ? new bignumber_js_1.default(customParametersFees.toString()) : undefined,
},
},
};
// propagate storageLimit fee parameter when present (ex: tezos)
const feeEstimation = await estimateFees((0, utils_1.transactionToIntent)(account, {
...transaction,
}, computeIntentType));
const params = feeEstimation?.parameters;
if (params) {
const storageLimit = params["storageLimit"];
if (storageLimit !== undefined &&
(typeof storageLimit === "bigint" ||
typeof storageLimit === "number" ||
typeof storageLimit === "string")) {
next.storageLimit = new bignumber_js_1.default(storageLimit.toString());
}
}
// align with stellar/xrp: when send max (or staking intents), reflect validated amount in UI
if (transaction.useAllAmount ||
transaction["mode"] === "stake" ||
transaction["mode"] === "unstake") {
const { amount } = await validateIntent((0, utils_1.transactionToIntent)(account, {
...transaction,
}, computeIntentType));
next.amount = new bignumber_js_1.default(amount.toString());
}
return next;
}
return transaction;
};
}
exports.genericPrepareTransaction = genericPrepareTransaction;
function getAssetInfos(tr, owner, getAssetFromToken) {
if (tr.subAccountId) {
const { token } = (0, index_1.decodeTokenAccountId)(tr.subAccountId);
if (!token)
return assetInfosFallback(tr);
const asset = getAssetFromToken(token, owner);
return {
assetOwner: ("assetOwner" in asset && asset.assetOwner) || "",
assetReference: ("assetReference" in asset && asset.assetReference) || "",
};
}
return assetInfosFallback(tr);
}
exports.getAssetInfos = getAssetInfos;
//# sourceMappingURL=prepareTransaction.js.map