UNPKG

@ledgerhq/coin-hedera

Version:
83 lines 3.78 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.estimateFees = void 0; const currencies_1 = require("@ledgerhq/cryptoassets/currencies"); const bignumber_js_1 = __importDefault(require("bignumber.js")); const invariant_1 = __importDefault(require("invariant")); const constants_1 = require("../constants"); const api_1 = require("../network/api"); const utils_1 = require("../network/utils"); const estimateContractCallFees = async ({ configOrCurrencyId, txIntent, }) => { let tinybars = new bignumber_js_1.default(0); let gas = new bignumber_js_1.default(0); const tokenEvmAddress = "assetReference" in txIntent.asset ? txIntent.asset.assetReference : null; const [senderEvmAddress, recipientEvmAddress] = await Promise.all([ (0, utils_1.toEVMAddress)({ configOrCurrencyId, accountId: txIntent.sender }), (0, utils_1.toEVMAddress)({ configOrCurrencyId, accountId: txIntent.recipient }), ]); if (!tokenEvmAddress || !senderEvmAddress || !recipientEvmAddress) { return { tinybars, }; } try { const [networkFees, gasLimit] = await Promise.all([ api_1.apiClient.getNetworkFees({ configOrCurrencyId }), api_1.apiClient.estimateContractCallGas({ configOrCurrencyId, senderEvmAddress, recipientEvmAddress, contractEvmAddress: tokenEvmAddress, amount: txIntent.amount, }), ]); const contractCallFees = networkFees.fees.find(fee => fee.transaction_type === constants_1.HEDERA_OPERATION_TYPES.ContractCall); const gasTinybarRate = new bignumber_js_1.default(contractCallFees?.gas ?? constants_1.DEFAULT_GAS_PRICE_TINYBARS); gas = gasLimit.multipliedBy(constants_1.ESTIMATED_GAS_SAFETY_RATE).integerValue(bignumber_js_1.default.ROUND_CEIL); tinybars = gas.multipliedBy(gasTinybarRate).integerValue(bignumber_js_1.default.ROUND_CEIL); } catch { const gasTinybarRate = constants_1.DEFAULT_GAS_PRICE_TINYBARS; gas = constants_1.DEFAULT_GAS_LIMIT; tinybars = gas.multipliedBy(gasTinybarRate).integerValue(bignumber_js_1.default.ROUND_CEIL); } return { tinybars, gas, }; }; const estimateStandardFees = async ({ currencyId, operationType, }) => { const currency = (0, currencies_1.findCryptoCurrencyById)(currencyId); (0, invariant_1.default)(currency, `hedera: currency with id ${currencyId} not found`); let tinybars; const usdRate = await (0, utils_1.getCurrencyToUSDRate)(currency).catch(() => null); if (usdRate) { tinybars = new bignumber_js_1.default(constants_1.BASE_USD_FEE_BY_OPERATION_TYPE[operationType]) .dividedBy(new bignumber_js_1.default(usdRate)) .integerValue(bignumber_js_1.default.ROUND_CEIL) .multipliedBy(constants_1.ESTIMATED_FEE_SAFETY_RATE); } else { tinybars = new bignumber_js_1.default(constants_1.DEFAULT_TINYBAR_FEE).multipliedBy(constants_1.ESTIMATED_FEE_SAFETY_RATE); } return { tinybars, }; }; const estimateFees = async (params) => { if (params.operationType === constants_1.HEDERA_OPERATION_TYPES.ContractCall) { return estimateContractCallFees({ configOrCurrencyId: params.configOrCurrencyId, txIntent: params.txIntent, }); } return estimateStandardFees({ currencyId: params.currencyId, operationType: params.operationType, }); }; exports.estimateFees = estimateFees; //# sourceMappingURL=estimateFees.js.map