encoding — Fuel ABI encoding¶
Low-level encoding functions used internally to construct signed payloads
for the O2 Exchange on-chain contracts. You typically do not need to use
these directly — O2Client handles encoding
automatically.
Note
Fuel function selectors are not hash-based like Solidity’s 4-byte
keccak selectors. Instead, they are encoded as
u64(len(name)) + utf8(name).
Constants¶
- o2_sdk.encoding.GAS_MAX = 18446744073709551615¶
u64::MAX— the gas limit used for all contract calls.
Primitive encoding¶
- o2_sdk.encoding.function_selector(name)[source]¶
Encode a Fuel ABI function selector.
Format:
u64_be(len(name)) + utf8(name)
- o2_sdk.encoding.encode_identity(discriminant, address_bytes)[source]¶
Encode a Fuel
Identityenum value.Format:
u64(discriminant) + 32-byte address- Parameters:
- Returns:
40-byte encoded identity.
- Return type:
- Raises:
ValueError – If
address_bytesis not 32 bytes.
Option encoding¶
- o2_sdk.encoding.encode_option_none()[source]¶
Encode
Option::None:u64(0).- Returns:
8-byte None encoding.
- Return type:
Order encoding¶
- o2_sdk.encoding.encode_order_args(price, quantity, order_type, order_type_data=None)[source]¶
Encode
OrderArgsstruct forCreateOrdercall data.Layout:
u64(price) + u64(quantity) + order_type_encodingOrder type variants are tightly packed (no padding to largest variant size):
Variant
Discriminant
Encoding
Limit0
u64(0) + u64(price) + u64(timestamp)(24 bytes)Spot1
u64(1)(8 bytes)FillOrKill2
u64(2)(8 bytes)PostOnly3
u64(3)(8 bytes)Market4
u64(4)(8 bytes)BoundedMarket5
u64(5) + u64(max_price) + u64(min_price)(24 bytes)
Signing payload construction¶
- o2_sdk.encoding.build_session_signing_bytes(nonce, chain_id, session_address, contract_ids, expiry)[source]¶
Build the signing payload for
set_session.Layout:
u64(nonce) + u64(chain_id) + function_selector("set_session") + u64(1) [Option::Some] + u64(0) [Identity::Address] + session_address [32 bytes] + u64(expiry) + u64(len(contract_ids)) + concat(contract_ids) [32 bytes each]- Parameters:
- Returns:
The bytes to sign with
personalSign.- Return type:
- o2_sdk.encoding.build_actions_signing_bytes(nonce, calls)[source]¶
Build the signing payload for session actions.
Layout:
u64(nonce) + u64(num_calls) + for each call: contract_id [32 bytes] + u64(selector_len) + selector [variable] + u64(amount) + asset_id [32 bytes] + u64(gas) + encode_option_call_data(call_data)- Parameters:
nonce (int) – Current account nonce.
calls (list[dict]) – List of low-level call dicts (as returned by
action_to_call()).
- Returns:
The bytes to sign with
rawSign.- Return type:
- o2_sdk.encoding.action_to_call(action, market_info)[source]¶
Convert a high-level action dict to a low-level contract call dict.
Supported action types:
CreateOrder— Place a new order.CancelOrder— Cancel an existing order.SettleBalance— Settle filled proceeds.RegisterReferer— Register a referrer.
- Parameters:
- Returns:
A low-level call dict with
contract_id,function_selector,amount,asset_id,gas, andcall_data.- Return type:
- Raises:
ValueError – If the action type is unknown.