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.u64_be(value)[source]

Encode an integer as 8 bytes in big-endian (u64) format.

Parameters:

value (int) – The integer to encode.

Returns:

8-byte big-endian representation.

Return type:

bytes

o2_sdk.encoding.function_selector(name)[source]

Encode a Fuel ABI function selector.

Format: u64_be(len(name)) + utf8(name)

Parameters:

name (str) – The function name (e.g., "create_order").

Returns:

The encoded selector.

Return type:

bytes

o2_sdk.encoding.encode_identity(discriminant, address_bytes)[source]

Encode a Fuel Identity enum value.

Format: u64(discriminant) + 32-byte address

Parameters:
  • discriminant (int) – 0 for Address, 1 for ContractId.

  • address_bytes (bytes) – The 32-byte address.

Returns:

40-byte encoded identity.

Return type:

bytes

Raises:

ValueError – If address_bytes is not 32 bytes.

Option encoding

o2_sdk.encoding.encode_option_none()[source]

Encode Option::None: u64(0).

Returns:

8-byte None encoding.

Return type:

bytes

o2_sdk.encoding.encode_option_some(data)[source]

Encode Option::Some(data): u64(1) + data.

Parameters:

data (bytes) – The inner data bytes.

Returns:

Encoded option.

Return type:

bytes

o2_sdk.encoding.encode_option_call_data(data_or_none)[source]

Encode Option for call_data in action signing bytes.

  • Noneu64(0)

  • Someu64(1) + u64(len(data)) + data

Parameters:

data_or_none (bytes | None) – The call data bytes, or None.

Returns:

Encoded option.

Return type:

bytes

Order encoding

o2_sdk.encoding.encode_order_args(price, quantity, order_type, order_type_data=None)[source]

Encode OrderArgs struct for CreateOrder call data.

Layout: u64(price) + u64(quantity) + order_type_encoding

Order type variants are tightly packed (no padding to largest variant size):

Variant

Discriminant

Encoding

Limit

0

u64(0) + u64(price) + u64(timestamp) (24 bytes)

Spot

1

u64(1) (8 bytes)

FillOrKill

2

u64(2) (8 bytes)

PostOnly

3

u64(3) (8 bytes)

Market

4

u64(4) (8 bytes)

BoundedMarket

5

u64(5) + u64(max_price) + u64(min_price) (24 bytes)

Parameters:
  • price (int) – Scaled price (on-chain integer).

  • quantity (int) – Scaled quantity (on-chain integer).

  • order_type (str) – Order type name.

  • order_type_data (dict | None) – Additional data for Limit or BoundedMarket types.

Returns:

Encoded order arguments.

Return type:

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:
  • nonce (int) – Current account nonce.

  • chain_id (int) – The Fuel chain ID.

  • session_address (bytes) – The 32-byte session wallet address.

  • contract_ids (list[bytes]) – List of 32-byte market contract IDs.

  • expiry (int) – Session expiry (Unix timestamp).

Returns:

The bytes to sign with personalSign.

Return type:

bytes

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:
Returns:

The bytes to sign with rawSign.

Return type:

bytes

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:
  • action (dict) – A high-level action dict (e.g., {"CreateOrder": {...}}).

  • market_info (dict) – Market metadata dict with contract_id, market_id, base, quote, and accounts_registry_id.

Returns:

A low-level call dict with contract_id, function_selector, amount, asset_id, gas, and call_data.

Return type:

dict

Raises:

ValueError – If the action type is unknown.