gmx.execution_buffer
Documentation for eth_defi.gmx.execution_buffer Python module.
GMX execution buffer
The execution buffer is a safety multiplier applied to the estimated execution fee when creating GMX V2 orders. It ensures that keepers — the off-chain agents responsible for executing orders — are adequately compensated even if network gas prices spike between order creation and execution. Any excess fee is refunded to the user.
Background: GMX’s two-step order execution
GMX V2 uses a two-step process for all actions (deposits, withdrawals, swaps, position opens/closes):
User creates an order and pays an upfront execution fee in native tokens (e.g. ETH on Arbitrum). This fee is meant to compensate the keeper who will execute the order on-chain.
A keeper picks up the order, executes it, and is compensated from the execution fee the user paid. Any excess is refunded back to the user’s account.
The risk is that gas prices may increase between steps 1 and 2. If the user paid
too little, the keeper would lose money and refuse to execute the order, causing it
to sit indefinitely or fail with InsufficientExecutionFee.
The execution buffer solves this by overestimating the fee. Since excess is always refunded, the only cost to the user is a temporary lock-up of native tokens.
How it works at each layer
On-chain (GMX smart contracts)
The GMX protocol applies its own gas estimation adjustment via the DataStore
contract. The key function in GasUtils.sol is adjustGasLimitForEstimate():
adjustedGas = baseGasLimit
+ (oracleCount * perOracleGas)
+ applyFactor(estimatedGasLimit, multiplierFactor)
The relevant DataStore keys (defined in Keys.sol) are:
ESTIMATED_GAS_FEE_BASE_AMOUNT_V2_1— base gas added to all estimatesESTIMATED_GAS_FEE_PER_ORACLE_PRICE— additional gas per oracle price usedESTIMATED_GAS_FEE_MULTIPLIER_FACTOR— multiplier applied to the order gas limitEXECUTION_GAS_FEE_MULTIPLIER_FACTOR— multiplier used for actual execution
See: - GasUtils.sol - Keys.sol
GMX interface (frontend)
The official GMX interface adds an additional client-side buffer on top of the
on-chain estimation, configured as executionFeeBufferBps (basis points):
Arbitrum default: 3000 bps (30%)
Avalanche default: 1000 bps (10%)
The buffer is applied to the gas price: finalGasPrice = gasPrice + (gasPrice * bufferBps / 10000).
Users can adjust this in the Settings modal under “Max Network Fee Buffer”.
See: - chains.ts - executionFee.ts
This library (eth_defi)
This library uses a simpler floating-point multiplier applied directly to the base execution fee:
execution_fee = apply_execution_buffer(base_fee, execution_buffer=2.2)
# Equivalent to: int(base_fee * 2.2)
The default of 2.2x (120% buffer) is more conservative than the GMX interface’s 30%, because programmatic/automated trading has a higher cost of failed execution and cannot rely on a user manually retrying.
Safety thresholds
Buffer value |
Behaviour |
|---|---|
< 1.2x |
Critical — keepers will likely reject the order |
< 1.5x |
Warning — risk of failure during gas spikes |
1.8–2.2x |
Recommended range for standard orders |
2.5x |
Default for bundled SL/TP orders (multiple sub-orders) |
For SL/TP bundled orders, an additional execution_fee_buffer multiplier
(default 3.0x) is applied on top of the execution buffer to account for the
extra gas consumed by multicall batching of multiple orders.
See also
eth_defi.gmx.gas_utils— low-level gas limit fetching and GMX fee formulaeth_defi.gmx.gas_monitor— gas balance monitoring and thresholds
Module Attributes
Default execution buffer multiplier for standard orders (increase, decrease, swap). |
|
Default execution buffer for bundled SL/TP operations which involve multiple sub-orders in a single multicall transaction and therefore require a higher margin. |
|
Additional fee buffer multiplier specific to the SL/TP sub-orders within a bundled transaction. |
|
Execution buffer below this value will trigger a critical error log. |
|
Execution buffer below this value will trigger a warning log. |
|
Lower bound of the recommended execution buffer range for standard orders. |
|
Upper bound of the recommended execution buffer range for standard orders. |
Functions
|
Apply the execution buffer multiplier to a base execution fee. |
|
Check the execution buffer value and log warnings if it is dangerously low. |
- DEFAULT_EXECUTION_BUFFER: float = 2.2
Default execution buffer multiplier for standard orders (increase, decrease, swap). This is a 2.2x multiplier on the base execution fee, meaning a 120% safety margin.
- DEFAULT_SLTP_EXECUTION_BUFFER: float = 2.5
Default execution buffer for bundled SL/TP operations which involve multiple sub-orders in a single multicall transaction and therefore require a higher margin.
- DEFAULT_SLTP_EXECUTION_FEE_BUFFER: float = 3.0
Additional fee buffer multiplier specific to the SL/TP sub-orders within a bundled transaction. Applied on top of the main execution buffer:
total_multiplier = execution_buffer * execution_fee_buffer.
- EXECUTION_BUFFER_CRITICAL_THRESHOLD: float = 1.2
Execution buffer below this value will trigger a critical error log. Keepers will very likely reject orders with a buffer this low.
- EXECUTION_BUFFER_WARNING_THRESHOLD: float = 1.5
Execution buffer below this value will trigger a warning log. Orders may fail during gas price spikes.
- EXECUTION_BUFFER_RECOMMENDED_MIN: float = 1.8
Lower bound of the recommended execution buffer range for standard orders.
- EXECUTION_BUFFER_RECOMMENDED_MAX: float = 2.2
Upper bound of the recommended execution buffer range for standard orders. Also the default value for
DEFAULT_EXECUTION_BUFFER.
- validate_execution_buffer(execution_buffer)
Check the execution buffer value and log warnings if it is dangerously low.
This does not raise an exception — it only emits log messages so that the caller can proceed with the order while being informed of the risk.
- Parameters
execution_buffer (float) – The multiplier to validate. Values below
EXECUTION_BUFFER_CRITICAL_THRESHOLD(1.2) will emit a critical error; values belowEXECUTION_BUFFER_WARNING_THRESHOLD(1.5) will emit a warning.- Return type
None
- apply_execution_buffer(base_fee, execution_buffer, validate=True)
Apply the execution buffer multiplier to a base execution fee.
Multiplies the base fee by the given buffer to produce a fee that is high enough for GMX keepers to execute profitably. Any excess is refunded by the GMX contracts.
Example:
base_fee = gas_limits["total"] * gas_price execution_fee = apply_execution_buffer(base_fee, execution_buffer=2.2)
- Parameters
base_fee (int) – Raw execution fee in wei, typically
gas_limit * gas_price.execution_buffer (float) – Multiplier to apply. See
DEFAULT_EXECUTION_BUFFERfor the recommended default.validate (bool) – If
True(default), callvalidate_execution_buffer()before applying. Set toFalseto skip validation (e.g. when the buffer has already been validated earlier in the call chain).
- Returns
The buffered execution fee in wei.
- Return type