跳转至

SeraBatcher.sol

批量执行合约支持在单笔交易中进行多个订单撮合和 SOR 执行。

主网: 0x1f4b366f4145A92978df4bEeb6BdE71bC652F034 源代码: src/SeraBatcher.sol

常量

uint256 public constant MAX_BATCH_SIZE = 20;   // Max order pairs per batch
uint256 public constant MAX_INTENT_SIZE = 10;   // Max SOR executions per batch
uint256 public constant VERSION = 2;

数据结构

AtomicBatch

struct AtomicBatch {
    MatchData[] matches;  // All-or-nothing order pairs
}

IntentExecution

struct IntentExecution {
    MatchData[] matches;           // Route legs
    bytes intentSignature;         // Taker's SOR signature
    IntentParams intent;           // SOR parameters
    uint8 uniqueTokenCount;        // Transient hash table size hint
    uint256 permitDeadline;        // ERC-2612 permit deadline
    bytes permitSignature;         // ERC-2612 permit signature
}

函数

batchMatchOrders

尽力批量撮合。单个失败不会阻止后续撮合。

function batchMatchOrders(
    MatchData[] calldata _matches,
    uint256 deadline
) external returns (uint256 failedMask)
参数 类型 说明
_matches MatchData[] 订单对数组(最多 20 个)
deadline uint256 撮合过期时间戳

返回值: failedMask — 位掩码,其中第 i 位为 1 表示第 i 个撮合失败。

事件:

event MatchFailed(bytes32 indexed orderHash0, bytes32 indexed orderHash1, bytes reason, uint256 indexed batchIndex);
event BatchExecuted(uint256 attempted, uint256 failedMask);

batchMatchOrdersAtomic

全有或全无批量撮合。如果任何撮合失败,整个交易回退。

function batchMatchOrdersAtomic(
    MatchData[] calldata _matches,
    uint256 deadline
) external
参数 类型 说明
_matches MatchData[] 订单对数组(最多 20 个)
deadline uint256 撮合过期时间戳

当撮合之间存在依赖关系且部分执行会导致不正确结果时使用此函数。

事件:

event AtomicBatchExecuted(uint256 matchCount);

batchMatchMixed

在一笔交易中执行原子批量、独立撮合和 SOR Intent。

function batchMatchMixed(
    AtomicBatch[] calldata _atomicBatches,
    MatchData[] calldata _singleMatches,
    IntentExecution[] calldata _intents,
    uint256 deadline
) external returns (uint256 failedMask)
参数 类型 说明
_atomicBatches AtomicBatch[] 全有或全无子批次(最多 20 个)
_singleMatches MatchData[] 独立订单对(最多 20 个,尽力执行)
_intents IntentExecution[] SOR 执行(最多 10 个,尽力执行)
deadline uint256 撮合过期时间戳

返回值: failedMask — 覆盖所有三组的顺序位掩码。

执行顺序:

  1. 原子批量(每个独立原子化,通过 try-catch 隔离)
  2. 单个撮合(尽力执行,失败后继续)
  3. SOR Intent(每个独立原子化,失败后继续)

事件:

event AtomicBatchFailed(uint256 batchIndex, bytes reason);
event IntentFailed(uint256 indexed intentIndex, bytes reason);
event MatchFailed(bytes32 indexed orderHash0, bytes32 indexed orderHash1, bytes reason, uint256 indexed batchIndex);
event BatchExecuted(uint256 attempted, uint256 failedMask);

错误

错误 原因
TooManyOrders 超过 20 个订单对
TooManyBatches 超过 20 个原子批次
TooManyIntents 超过 10 个 SOR Intent
InvalidSORAddress SOR 合约未配置