gmx.events
Documentation for eth_defi.gmx.events Python module.
GMX event log decoding and parsing.
This module provides utilities for decoding GMX protocol events from transaction receipts. GMX emits all events through a centralised EventEmitter contract using EventLog, EventLog1, and EventLog2 event types.
Key features:
Decode complex EventLogData structure from transaction logs
Extract order execution results (success/failure/frozen)
Parse position increase/decrease events with execution prices, PnL, and fees
Decode error reasons from failed orders
Example usage:
from web3 import Web3
from eth_defi.gmx.events import (
decode_gmx_events,
extract_order_execution_result,
)
# Get transaction receipt
receipt = web3.eth.get_transaction_receipt(tx_hash)
# Decode all GMX events from receipt
events = list(decode_gmx_events(web3, receipt))
# Extract order execution result
result = extract_order_execution_result(web3, receipt)
if result:
print(f"Order status: {result.status}")
print(f"Execution price: {result.execution_price}")
For more information on GMX events, see: https://docs.gmx.io/docs/api/contracts#event-monitoring
Functions
|
Decode GMX error reason from reasonBytes. |
|
Decode a single GMX EventLog from a transaction log entry. |
|
Decode all GMX events from a transaction receipt. |
|
Extract order execution result from a keeper transaction receipt. |
|
Extract order key from OrderCreated or OrderExecuted event in receipt. |
|
Find all events with a specific name from a transaction receipt. |
|
Compute the keccak256 hash of an event name. |
Classes
Parsed GMX event data from EventLogData structure. |
|
Result of GMX order execution. |
|
Fees from GMX order execution. |
- class GMXEventData
Bases:
objectParsed GMX event data from EventLogData structure.
GMX events contain structured data in the following categories:
Address items: Contract addresses (account, market, tokens)
Uint items: Unsigned integers (sizes, prices, fees)
Int items: Signed integers (PnL, price impact)
Bool items: Boolean flags (isLong, etc.)
Bytes32 items: Order/position keys
Bytes items: Raw byte data (error reasons)
String items: String data (reasons)
Each category has both single items and array items.
- __init__(event_name, msg_sender=None, topic1=None, topic2=None, address_items=<factory>, address_array_items=<factory>, uint_items=<factory>, uint_array_items=<factory>, int_items=<factory>, int_array_items=<factory>, bool_items=<factory>, bool_array_items=<factory>, bytes32_items=<factory>, bytes32_array_items=<factory>, bytes_items=<factory>, bytes_array_items=<factory>, string_items=<factory>, string_array_items=<factory>)
- Parameters
event_name (str) –
msg_sender (Optional[eth_typing.evm.HexAddress]) –
address_items (dict[str, eth_typing.evm.HexAddress]) –
address_array_items (dict[str, list[eth_typing.evm.HexAddress]]) –
- Return type
None
- get_address(key, default=None)
Get an address item by key.
- Parameters
key (str) –
default (Optional[eth_typing.evm.HexAddress]) –
- Return type
- get_bool(key, default=None)
Get a bool item by key.
- get_bytes(key, default=None)
Get a bytes item by key.
- get_bytes32(key, default=None)
Get a bytes32 item by key.
- get_int(key, default=None)
Get an int item by key.
- get_string(key, default=None)
Get a string item by key.
- class OrderExecutionResult
Bases:
objectResult of GMX order execution.
This dataclass aggregates information from order execution events (OrderExecuted, OrderFrozen, OrderCancelled) and position events (PositionIncrease, PositionDecrease).
- __init__(order_key, status, account=None, execution_price=None, size_delta_usd=None, size_delta_in_tokens=None, collateral_delta=None, pnl_usd=None, price_impact_usd=None, fees=None, reason=None, reason_bytes=None, decoded_error=None, position_key=None, is_long=None, collateral_token=None, collateral_token_price=None)
- Parameters
order_key (bytes) –
status (Literal['executed', 'frozen', 'cancelled']) –
account (Optional[eth_typing.evm.HexAddress]) –
fees (Optional[eth_defi.gmx.events.OrderFees]) –
collateral_token (Optional[eth_typing.evm.HexAddress]) –
- Return type
None
- class OrderFees
Bases:
objectFees from GMX order execution.
All fee values are in token amounts (not USD).
- decode_error_reason(reason_bytes)
Decode GMX error reason from reasonBytes.
GMX uses custom error selectors. This function attempts to decode common error types and their parameters.
- decode_gmx_event(web3, log, event_emitter_contract=None)
Decode a single GMX EventLog from a transaction log entry.
This function handles EventLog, EventLog1, and EventLog2 events emitted by the GMX EventEmitter contract.
- Parameters
web3 (web3.main.Web3) – Web3 instance
log (dict) – A single log entry from transaction receipt
event_emitter_contract (Optional[web3.contract.contract.Contract]) – Optional pre-built EventEmitter contract instance. When decoding many logs in a loop, passing a cached contract avoids repeated lookups.
- Returns
Parsed GMXEventData or None if not a GMX event
- Return type
- decode_gmx_events(web3, receipt)
Decode all GMX events from a transaction receipt.
- Parameters
web3 (web3.main.Web3) – Web3 instance
receipt (dict) – Transaction receipt dictionary
- Yields
Parsed GMXEventData for each GMX event found
- Return type
- extract_order_execution_result(web3, receipt, order_key=None)
Extract order execution result from a keeper transaction receipt.
This function looks for OrderExecuted, OrderFrozen, or OrderCancelled events and extracts the relevant execution data. For successful orders, it also extracts PositionIncrease/PositionDecrease data.
- Parameters
- Returns
OrderExecutionResult or None if no order events found
- Return type
- extract_order_key_from_receipt(web3, receipt)
Extract order key from OrderCreated or OrderExecuted event in receipt.
This function handles both GMX order execution models:
Two-phase orders (limit orders): Look for OrderCreated event, order is pending until keeper executes it in a separate transaction.
Single-phase orders (market orders): Look for OrderExecuted event, order is created and executed atomically in the same transaction.
- Parameters
web3 (web3.main.Web3) – Web3 instance
receipt (dict) – Transaction receipt from order creation/execution
- Returns
The 32-byte order key
- Raises
ValueError – If no OrderCreated or OrderExecuted event found in receipt
- Return type
- find_events_by_name(web3, receipt, event_name)
Find all events with a specific name from a transaction receipt.
- Parameters
- Yields
Matching GMXEventData events
- Return type
- get_event_name_hash(event_name)
Compute the keccak256 hash of an event name.
GMX uses the hash of event name strings as topic[1] in EventLog events for efficient filtering.