@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
176 lines • 6.26 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.evmCustomFeeConfig = exports.legacyInputs = exports.eip1559Inputs = void 0;
exports.isRecord = isRecord;
exports.isBigNumber = isBigNumber;
exports.weiToGwei = weiToGwei;
exports.gweiToWei = gweiToWei;
exports.isEip1559 = isEip1559;
const bignumber_js_1 = require("bignumber.js");
const GWEI_DIVISOR = new bignumber_js_1.BigNumber(10).pow(9);
function isRecord(value) {
return typeof value === "object" && value !== null;
}
function isBigNumber(value) {
return bignumber_js_1.BigNumber.isBigNumber(value);
}
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);
}
/**
* Determine whether the EVM transaction is EIP-1559 (type 2).
* Falls back to checking gasOptions for maxFeePerGas presence.
*/
function isEip1559(transaction) {
if (transaction.type === 2)
return true;
const gasOptions = transaction.gasOptions;
if (isRecord(gasOptions)) {
const medium = gasOptions.medium;
if (isRecord(medium) && isBigNumber(medium.maxFeePerGas))
return true;
}
return false;
}
function getSuggestedPriorityFeeRange(transaction) {
const gasOptions = transaction.gasOptions;
if (!isRecord(gasOptions))
return null;
const slow = gasOptions.slow;
const fast = gasOptions.fast;
if (!isRecord(slow) || !isRecord(fast))
return null;
const slowPriorityFee = isBigNumber(slow.maxPriorityFeePerGas)
? slow.maxPriorityFeePerGas
: new bignumber_js_1.BigNumber(0);
const fastPriorityFee = isBigNumber(fast.maxPriorityFeePerGas)
? fast.maxPriorityFeePerGas
: new bignumber_js_1.BigNumber(0);
if (slowPriorityFee.isZero() && fastPriorityFee.isZero())
return null;
return {
min: weiToGwei(slowPriorityFee),
max: weiToGwei(fastPriorityFee),
};
}
function getNextBlockBaseFee(transaction) {
const gasOptions = transaction.gasOptions;
if (!isRecord(gasOptions))
return null;
const medium = gasOptions.medium;
if (!isRecord(medium))
return null;
// The gas tracker provides a `nextBaseFee` (base fee), which is what we want to display
// for the "Next block" helper under maxFeePerGas.
const nextBaseFee = medium.nextBaseFee;
if (!isBigNumber(nextBaseFee))
return null;
return `${weiToGwei(nextBaseFee)} Gwei`;
}
/** Custom fee input descriptors for EIP-1559 transactions */
exports.eip1559Inputs = [
{
key: "maxPriorityFeePerGas",
type: "number",
unitLabel: "Gwei",
suggestedRange: {
getRange: transaction => {
if (!isRecord(transaction))
return null;
return getSuggestedPriorityFeeRange(transaction);
},
},
},
{
key: "maxFeePerGas",
type: "number",
unitLabel: "Gwei",
helperInfo: {
getValue: transaction => {
if (!isRecord(transaction))
return null;
return getNextBlockBaseFee(transaction);
},
},
},
];
exports.legacyInputs = [
{
key: "gasPrice",
type: "number",
unitLabel: "Gwei",
},
];
exports.evmCustomFeeConfig = {
inputs: [...exports.eip1559Inputs, ...exports.legacyInputs],
getInitialValues: (transaction) => {
const empty = {};
if (!isRecord(transaction))
return empty;
const is1559 = isEip1559(transaction);
if (is1559) {
const maxFeePerGas = transaction.maxFeePerGas;
const maxPriorityFeePerGas = transaction.maxPriorityFeePerGas;
if (isBigNumber(maxFeePerGas) && isBigNumber(maxPriorityFeePerGas)) {
return {
maxFeePerGas: weiToGwei(maxFeePerGas),
maxPriorityFeePerGas: weiToGwei(maxPriorityFeePerGas),
};
}
// Fallback to medium preset from gasOptions
const gasOptions = transaction.gasOptions;
if (isRecord(gasOptions)) {
const medium = gasOptions.medium;
if (isRecord(medium)) {
const mediumMaxFee = medium.maxFeePerGas;
const mediumPriorityFee = medium.maxPriorityFeePerGas;
return {
maxFeePerGas: isBigNumber(mediumMaxFee) ? weiToGwei(mediumMaxFee) : "",
maxPriorityFeePerGas: isBigNumber(mediumPriorityFee)
? weiToGwei(mediumPriorityFee)
: "",
};
}
}
return {
maxFeePerGas: "",
maxPriorityFeePerGas: "",
};
}
// Legacy transaction
const gasPrice = transaction.gasPrice;
const gasOptions = transaction.gasOptions;
const medium = isRecord(gasOptions) ? gasOptions.medium : undefined;
if (!isBigNumber(gasPrice) && isRecord(gasOptions) && isRecord(medium)) {
const mediumGasPrice = medium.gasPrice;
return {
gasPrice: isBigNumber(mediumGasPrice) ? weiToGwei(mediumGasPrice) : "",
};
}
return {
gasPrice: isBigNumber(gasPrice) ? weiToGwei(gasPrice) : "",
};
},
buildTransactionPatch: values => {
const patch = {
feesStrategy: "custom",
};
if ("maxFeePerGas" in values) {
patch.maxFeePerGas = gweiToWei(values.maxFeePerGas);
}
if ("maxPriorityFeePerGas" in values) {
patch.maxPriorityFeePerGas = gweiToWei(values.maxPriorityFeePerGas);
}
if ("gasPrice" in values) {
patch.gasPrice = gweiToWei(values.gasPrice);
}
return patch;
},
};
//# sourceMappingURL=fees.js.map