UNPKG

@ledgerhq/coin-hedera

Version:
53 lines 2.46 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.base64ToUrlSafeBase64 = exports.calculateAmount = exports.getEstimatedFees = exports.estimatedFeeSafetyRate = void 0; const bignumber_js_1 = __importDefault(require("bignumber.js")); const index_1 = __importDefault(require("@ledgerhq/live-countervalues/api/index")); const cryptoassets_1 = require("@ledgerhq/cryptoassets"); const estimateMaxSpendable_1 = require("./estimateMaxSpendable"); exports.estimatedFeeSafetyRate = 2; async function getEstimatedFees(account) { try { const data = await index_1.default.fetchLatest([ { from: account.currency, to: (0, cryptoassets_1.getFiatCurrencyByTicker)("USD"), startDate: new Date(), }, ]); if (data[0]) { return new bignumber_js_1.default(10000) .dividedBy(new bignumber_js_1.default(data[0])) .integerValue(bignumber_js_1.default.ROUND_CEIL) .multipliedBy(exports.estimatedFeeSafetyRate); } // eslint-disable-next-line no-empty } catch { } // as fees are based on a currency conversion, we stay // on the safe side here and double the estimate for "max spendable" return new bignumber_js_1.default("150200").multipliedBy(exports.estimatedFeeSafetyRate); // 0.001502 ℏ (as of 2023-03-14) } exports.getEstimatedFees = getEstimatedFees; async function calculateAmount({ account, transaction, }) { const amount = transaction.useAllAmount ? await (0, estimateMaxSpendable_1.estimateMaxSpendable)({ account }) : transaction.amount; return { amount, totalSpent: amount.plus(await getEstimatedFees(account)), }; } exports.calculateAmount = calculateAmount; // NOTE: convert from the non-url-safe version of base64 to the url-safe version (that the explorer uses) function base64ToUrlSafeBase64(data) { // Might be nice to use this alternative if .nvmrc changes to >= Node v14.18.0 // base64url encoding option isn't supported until then // Buffer.from(data, "base64").toString("base64url"); return data.replace(/\//g, "_").replace(/\+/g, "-"); } exports.base64ToUrlSafeBase64 = base64ToUrlSafeBase64; //# sourceMappingURL=utils.js.map