Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions services/blockchain-service/blockchain-service-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -1402,7 +1402,8 @@ export default class BlockchainServiceBase {
try {
// eth_feeHistory params: blockCount, newestBlock, rewardPercentiles
// [50] = median priority fee per block
const feeHistory = await web3Instance.eth.getFeeHistory(blockCount, 'latest', [50]);
const priorityFeePercentile = blockchain.priorityFeePercentile ?? 80;
const feeHistory = await web3Instance.eth.getFeeHistory(blockCount, 'latest', [priorityFeePercentile]);

// Extract median priority fees from each block (reward[blockIndex][percentileIndex])
const priorityFees = feeHistory.reward
Expand All @@ -1426,13 +1427,14 @@ export default class BlockchainServiceBase {

/**
* Apply buffer percentage to a gas price
* @param {BigInt} gasPrice - Gas price in wei
* @param {BigInt} maxBaseFee - base fee in wei
* @param {BigInt} maxPriorityFee - priority fee in wei
* @param {number} gasPriceBufferPercent - Buffer percentage to add
* @returns {BigInt} Gas price with buffer applied
*/
applyGasPriceBuffer(gasPrice, gasPriceBufferPercent) {
if (!gasPriceBufferPercent) return gasPrice;
return (gasPrice * BigInt(100 + Number(gasPriceBufferPercent))) / 100n;
applyGasPriceBuffer(maxBaseFee, maxPriorityFee, gasPriceBufferPercent) {
if (!gasPriceBufferPercent) return maxBaseFee + maxPriorityFee;
return ((maxBaseFee * BigInt(100 + Number(gasPriceBufferPercent))) / 100n) + maxPriorityFee;
}

/**
Expand All @@ -1449,6 +1451,7 @@ export default class BlockchainServiceBase {
// Fallback to network gas price if feeHistory not supported or empty
if (!feeHistory.supported) {
return this.applyGasPriceBuffer(
0n,
BigInt(await this.getNetworkGasPrice(blockchain)),
gasPriceBufferPercent,
);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Buffer not applied to fallback network gas price

In the fallback cases, the network gas price is passed as maxPriorityFee (second argument) while maxBaseFee is 0n. Since applyGasPriceBuffer only applies the buffer percentage to maxBaseFee, the calculation becomes ((0n * buffer) / 100n) + networkGasPrice = networkGasPrice. The buffer is never applied to the fallback gas price. The arguments appear to be in the wrong order—the network gas price should be passed as maxBaseFee and 0n as maxPriorityFee.

Additional Locations (1)

Fix in Cursor Fix in Web

Expand All @@ -1459,6 +1462,7 @@ export default class BlockchainServiceBase {

if (baseFees.length === 0 || priorityFees.length === 0) {
return this.applyGasPriceBuffer(
0n,
BigInt(await this.getNetworkGasPrice(blockchain)),
gasPriceBufferPercent,
);
Expand All @@ -1468,7 +1472,7 @@ export default class BlockchainServiceBase {
const maxBaseFee = baseFees.reduce((max, bf) => (bf > max ? bf : max), 0n);
const maxPriorityFee = priorityFees.reduce((max, pf) => (pf > max ? pf : max), 0n);

return this.applyGasPriceBuffer(maxBaseFee + maxPriorityFee, gasPriceBufferPercent);
return this.applyGasPriceBuffer(maxBaseFee, maxPriorityFee, gasPriceBufferPercent);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions services/input-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ export default class InputService {
options.blockchain?.gasPriceBufferPercent ??
this.config.blockchain?.gasPriceBufferPercent ??
undefined;
const priorityFeePercentile =
options.blockchain?.priorityFeePercentile ??
this.config.blockchain?.priorityFeePercentile ??
undefined;
const retryTxGasPriceMultiplier =
options.blockchain?.retryTxGasPriceMultiplier ??
this.config.blockchain?.retryTxGasPriceMultiplier ??
Expand All @@ -218,6 +222,7 @@ export default class InputService {
gasPriceOracleLink,
maxAllowance,
gasPriceBufferPercent,
priorityFeePercentile,
retryTxGasPriceMultiplier,
};

Expand Down