@ledgerhq/coin-hedera
Version:
Ledger Hedera Coin integration
67 lines • 3.06 kB
JavaScript
;
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 bignumber_js_1 = __importDefault(require("bignumber.js"));
const constants_1 = require("../constants");
const api_1 = require("../network/api");
const utils_1 = require("./utils");
const estimateContractCallFees = async (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)(txIntent.sender),
(0, utils_1.toEVMAddress)(txIntent.recipient),
]);
if (!tokenEvmAddress || !senderEvmAddress || !recipientEvmAddress) {
return {
tinybars,
};
}
try {
const [networkFees, gasLimit] = await Promise.all([
api_1.apiClient.getNetworkFees(),
api_1.apiClient.estimateContractCallGas(senderEvmAddress, recipientEvmAddress, tokenEvmAddress, 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 (currency, operationType) => {
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(params.txIntent);
}
return estimateStandardFees(params.currency, params.operationType);
};
exports.estimateFees = estimateFees;
//# sourceMappingURL=estimateFees.js.map