Skip to main content

GPv2Trade API

The GPv2Trade library is a Solidity utility for encoding and decoding trade data in settlement batches, enabling gas-efficient batch settlements.

Data Structure

struct Data {
    uint256 sellTokenIndex;
    uint256 buyTokenIndex;
    address receiver;
    uint256 sellAmount;
    uint256 buyAmount;
    uint32 validTo;
    bytes32 appData;
    uint256 feeAmount;
    uint256 flags;
    uint256 executedAmount;
    bytes signature;
}
Rather than storing full token addresses, the struct uses an index into the tokens array for sell and buy tokens to reduce gas costs.

Functions

extractOrder

Converts trade data into a full order struct while extracting the signing scheme from packed flags.
function extractOrder(
    Data calldata trade,
    IERC20[] calldata tokens
) internal pure returns (
    GPv2Order.Data memory order,
    GPv2Signing.Scheme signingScheme
);

extractFlags

Unpacks a single uint256 value into separate order parameters.
function extractFlags(
    uint256 flags
) internal pure returns (
    bytes32 kind,
    bool partiallyFillable,
    bytes32 sellTokenBalance,
    bytes32 buyTokenBalance,
    GPv2Signing.Scheme signingScheme
);

Flag Encoding

The library uses a bitfield approach where individual bits represent different order properties:
Bit(s)FieldValues
0Order kind0 = sell, 1 = buy
1Fill type0 = fill-or-kill, 1 = partially fillable
2-3Sell token balance00 = ERC20, 01 = external, 10 = internal
4Buy token balance0 = ERC20, 1 = internal
5-6Signing scheme00 = EIP-712, 01 = eth_sign, 10 = EIP-1271, 11 = PreSign
The default 0x00 represents the most common case: a fill-or-kill sell order by an externally owned account using EIP-712 signatures.

Supported Signing Schemes

SchemeValueDescription
EIP-7120b00Standard typed data signing
eth_sign0b01Legacy message signing
EIP-12710b10Smart contract wallet validation
PreSign0b11On-chain pre-approval

Gas Efficiency

The encoding reduces calldata costs by:
  • Using token indices instead of full addresses
  • Packing multiple values into single storage slots
  • Optimizing for zero bytes in calldata (cheaper on Ethereum)
Last modified on March 4, 2026