UNPKG

@ledgerhq/live-common

Version:
87 lines 3.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fees = void 0; const bignumber_js_1 = require("bignumber.js"); const GWEI_DIVISOR = new bignumber_js_1.BigNumber(10).pow(9); function isBigNumber(value) { return bignumber_js_1.BigNumber.isBigNumber(value); } function isRecord(value) { return typeof value === "object" && value !== null; } function weiToGwei(wei) { return wei.dividedBy(GWEI_DIVISOR).decimalPlaces(6, bignumber_js_1.BigNumber.ROUND_DOWN).toFixed(); } function gweiToWei(gwei) { const val = new bignumber_js_1.BigNumber(gwei); if (val.isNaN() || val.isNegative()) return new bignumber_js_1.BigNumber(0); return val.times(GWEI_DIVISOR).integerValue(bignumber_js_1.BigNumber.ROUND_DOWN); } function getSuggestedFeesRange(transaction) { const networkInfo = transaction.networkInfo; const rawGasPrice = isRecord(networkInfo) ? networkInfo.gasPrice : transaction.gasPrice; if (isBigNumber(rawGasPrice) && rawGasPrice.gt(0)) { return { min: weiToGwei(rawGasPrice.times(0.8).integerValue(bignumber_js_1.BigNumber.ROUND_DOWN)), max: weiToGwei(rawGasPrice.times(1.2).integerValue(bignumber_js_1.BigNumber.ROUND_UP)), }; } const fees = transaction.fees; if (isBigNumber(fees) && fees.gt(0)) { return { min: weiToGwei(fees.times(0.8).integerValue(bignumber_js_1.BigNumber.ROUND_DOWN)), max: weiToGwei(fees.times(1.2).integerValue(bignumber_js_1.BigNumber.ROUND_UP)), }; } return null; } const celoCustomInputs = [ { key: "fees", type: "number", unitLabel: "Gwei", suggestedRange: { getRange: transaction => { if (!isRecord(transaction)) return null; return getSuggestedFeesRange(transaction); }, }, }, ]; exports.fees = { hasPresets: false, hasCustom: true, hasCustomAssets: true, custom: { inputs: celoCustomInputs, getInitialValues: (transaction) => { if (!isRecord(transaction)) return { fees: "" }; const feesVal = transaction.fees; if (!isBigNumber(feesVal) || feesVal.lte(0)) return { fees: "" }; return { fees: weiToGwei(feesVal) }; }, buildTransactionPatch: values => { const patch = { feesStrategy: "custom" }; const feesWei = gweiToWei(values.fees ?? "0"); if (!feesWei.isNaN() && feesWei.gt(0)) { patch.fees = feesWei; } return patch; }, }, customAssets: { options: [ { id: "eth", ticker: "ETH", label: "Ethereum", unitLabel: "Gwei" }, { id: "btc", ticker: "BTC", label: "Bitcoin", unitLabel: "sat" }, { id: "usdc", ticker: "USDC", label: "USD Coin", unitLabel: "USDC" }, { id: "usdt", ticker: "USDT", label: "Tether", unitLabel: "USDT" }, { id: "celo", ticker: "Celo", label: "Celo", unitLabel: "Gwei" }, ], defaultId: "eth", }, }; //# sourceMappingURL=fees.js.map