gmx.gas_utils

Documentation for eth_defi.gmx.gas_utils Python module.

GMX Gas Utilities

Functions

apply_execution_buffer(base_fee, ...[, validate])

Apply the execution buffer multiplier to a base execution fee.

calculate_execution_fee(gas_limits, gas_price)

Calculate execution fee using GMX's formula.

clear_gas_limits_cache()

Clear the module-level gas limits cache.

decrease_order_gas_limit_key()

deposit_gas_limit_key()

execution_gas_fee_base_amount_key()

execution_gas_fee_base_amount_v2_1_key()

execution_gas_fee_multiplier_key()

execution_gas_fee_per_oracle_price_key()

get_gas_limits(datastore_object[, use_cache])

Given a Web3 contract object of the datastore, return a dictionary with the gas limits (as integers) that correspond to various operations used for execution fee calculation.

increase_order_gas_limit_key()

single_swap_gas_limit_key()

swap_order_gas_limit_key()

validate_execution_buffer(execution_buffer)

Check the execution buffer value and log warnings if it is dangerously low.

withdraw_gas_limit_key()

get_gas_limits(datastore_object, use_cache=True)

Given a Web3 contract object of the datastore, return a dictionary with the gas limits (as integers) that correspond to various operations used for execution fee calculation.

Uses module-level caching to avoid repeated RPC calls for the same datastore. Gas limits are cached per (chain_id, datastore_address) combination.

Parameters
  • datastore_object (web3.contract.Contract) – The connected contract instance

  • use_cache (bool) – Whether to use cached values. Default is True

Returns

Gas limit values for various operations

Return type

dict[str, int]

clear_gas_limits_cache()

Clear the module-level gas limits cache.

Call this if you need to refresh gas limit values from the contract.

calculate_execution_fee(gas_limits, gas_price, order_type='decrease_order', oracle_price_count=2)

Calculate execution fee using GMX’s formula.

GMX calculates minimum execution fee as:

adjustedGasLimit = baseGasLimit + (oracleCount * perOracleGas) + applyFactor(estimatedGasLimit, multiplierFactor) minExecutionFee = adjustedGasLimit * tx.gasprice

Where applyFactor(value, factor) = value * factor / 10^30

Parameters
  • gas_limits (dict[str, int]) – Gas limits dictionary from get_gas_limits()

  • gas_price (int) – Gas price in wei (should be maxFeePerGas for EIP-1559)

  • order_type (str) – Order type key: “increase_order”, “decrease_order”, “swap_order”, etc.

  • oracle_price_count (int) – Number of oracle prices needed (typically 2 for most orders)

Returns

Calculated execution fee in wei

Return type

int

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) emit a critical error; values below EXECUTION_BUFFER_WARNING_THRESHOLD (1.5) 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 high enough for GMX keepers to execute profitably. Any excess is refunded by GMX.

Parameters
  • base_fee (int) – Raw execution fee in wei, typically gas_limit * gas_price.

  • execution_buffer (float) – Multiplier to apply. See DEFAULT_EXECUTION_BUFFER for the recommended default.

  • validate (bool) – If True (default), call validate_execution_buffer() before applying. Set to False to skip validation when the buffer has already been validated earlier in the call chain.

Returns

The buffered execution fee in wei.

Return type

int