跳轉至

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 合約未配置