跳轉至

做市商指南

網路與相容性

資源
API 基礎 URL https://api.sera.cx/api/v1
Ethereum 主網(chainId 1
Sera 合約 0xB5C50C5D5f038404F85970b7f5B7259C4AC0E198
Vault 合約 0xC7d4Fd2638e6630C8C61329878676b88A8A24D43
SOR 合約 0xa7A0cf7cd6f043fCA23f29d8ae5aae6b46e11c18

簽名原語。 所有交易型變更都是針對 Sera domain 的 EIP-712 類型化資料簽名。走 permit 路徑的充值使用 ERC-2612 Permit 擴充功能 — USDC、EURC 以及許多現代穩定幣支援,但並非所有 ERC-20 都支援;簽名前請呼叫 GET /permit/metadata 檢查。API key 管理使用 EIP-712 ManageApiKey payload。

已驗證客戶端。 Python eth_account >= 0.10 + requests;TypeScript ethers v6(signer.signTypedData)。已驗證支援 EIP-712 類型化資料的瀏覽器錢包:MetaMaskRabbyFrameCoinbase WalletTrustRainbow。Safe 多簽透過 EIP-1271 支援(訊息於鏈上驗證,而非透過 ecrecover)。

地址大小寫。 讀取類端點(/balances/orders/fills)將 owner_address 視為大小寫敏感 — 請傳入小寫形式。EIP-712 簽名 payload 接受 EIP-55 校驗和地址。

這是你寫 bot 第一版時希望開著的頁面。按順序講三件事:

  1. 端到端手動下達一筆限價單。
  2. 撤銷剛才那筆單。
  3. 把兩者包裝成一個由你自己的報價來源驅動的自動掛單迴圈。

不在這裡講的環節 — 建立 API key、向 Vault 充值、之後提款 — 列在下一步。等你的錢包在 Ethereum 主網上已經有 USDC 餘額、且手上有 API key,再回到本頁繼續。

程式碼片段在 Python 與 TypeScript 之間切換。挑一種就一直用 — 兩份實作是逐行等價的。

準備

pip install eth-account requests
from decimal import Decimal
import json, time, uuid
import requests
from eth_account import Account
from eth_account.messages import encode_typed_data

API     = "https://api.sera.cx/api/v1"
DOMAIN  = {
    "name": "Sera",
    "version": "1",
    "chainId": 1,                                                  # Mainnet
    "verifyingContract": "0xB5C50C5D5f038404F85970b7f5B7259C4AC0E198",
}

PRIVATE_KEY = "0x...你的錢包私鑰..."                                # 簽名者
API_KEY     = "sera_..."                                           # 見 身分驗證
API_SECRET  = "..."
OWNER       = Account.from_key(PRIVATE_KEY).address

AUTH = {"Authorization": f"Bearer {API_KEY}:{API_SECRET}"}
npm i ethers@6
import { Wallet, TypedDataDomain, randomBytes } from "ethers";

const API     = "https://api.sera.cx/api/v1";
const DOMAIN: TypedDataDomain = {
  name: "Sera",
  version: "1",
  chainId: 1,                                                      // Mainnet
  verifyingContract: "0xB5C50C5D5f038404F85970b7f5B7259C4AC0E198",
};

const PRIVATE_KEY = "0x...你的錢包私鑰...";                         // 簽名者
const API_KEY     = "sera_...";                                    // 見 身分驗證
const API_SECRET  = "...";
const wallet      = new Wallet(PRIVATE_KEY);
const OWNER       = wallet.address;

const AUTH = { "Authorization": `Bearer ${API_KEY}:${API_SECRET}` };

EIP-712 的 Order 型別資料結構與鏈上的 SeraLib.ORDER_TYPEHASH 一一對應。定義一次:

ORDER_TYPES = {
    "Order": [
        {"name": "user",                 "type": "address"},
        {"name": "expiration",           "type": "uint48"},
        {"name": "feeBps",               "type": "uint48"},
        {"name": "recipient",            "type": "address"},
        {"name": "fromToken",            "type": "address"},
        {"name": "toToken",              "type": "address"},
        {"name": "fromAmount",           "type": "uint256"},
        {"name": "toAmount",             "type": "uint256"},
        {"name": "initialDepositAmount", "type": "uint256"},
        {"name": "uuid",                 "type": "uint256"},
    ]
}

CANCEL_ORDER_TYPES = {
    "CancelOrder": [
        {"name": "owner",   "type": "address"},
        {"name": "orderId", "type": "uint256"},
    ]
}
const ORDER_TYPES = {
  Order: [
    { name: "user",                 type: "address" },
    { name: "expiration",           type: "uint48"  },
    { name: "feeBps",               type: "uint48"  },
    { name: "recipient",            type: "address" },
    { name: "fromToken",            type: "address" },
    { name: "toToken",              type: "address" },
    { name: "fromAmount",           type: "uint256" },
    { name: "toAmount",             type: "uint256" },
    { name: "initialDepositAmount", type: "uint256" },
    { name: "uuid",                 type: "uint256" },
  ],
};

const CANCEL_ORDER_TYPES = {
  CancelOrder: [
    { name: "owner",   type: "address" },
    { name: "orderId", type: "uint256" },
  ],
};

uuid_int 位元布局

每筆訂單的 uuid 是一個打包後的 256 位元整數:

┌──────────┬───────────────────┬──────────────────┬──────────┐
│[255:252] │     [251:124]     │    [123:12]      │  [11:0]  │
│ Executor │     Order ID      │    Group ID      │  Leg ID  │
│  4 bit   │      128 bit      │     112 bit      │  12 bit  │
│  (0–15)  │   (full UUID4)    │  (UUID4 >> 16)   │ (0–4095) │
└──────────┴───────────────────┴──────────────────┴──────────┘

單筆獨立訂單:group_id = order_id >> 16leg_id = 0。VL 批次第 i 個 leg:所有 leg 共享同一個 group_id(取自 leg 0 的 UUID4),leg_id = i。任何其他取值都會被伺服器拒絕。

executor_id 依部署設定。Ethereum 主網預設為 0,除非你的部署另外公告。啟動時讀一次後重複使用 — executor_id 漂移會讓所有已簽名但未結算的 UUID 失效。

def compose_uuid(order_id: str, executor_id: int = 0,
                 leg_id: int = 0, group_uuid: str | None = None) -> int:
    """把 UUID4 字串打包成鏈上複合 uint256。"""
    oid    = int(uuid.UUID(order_id))
    gsrc   = int(uuid.UUID(group_uuid)) if group_uuid else oid
    gid    = gsrc >> 16                                            # 128 位元中的最高 112 位元
    return (executor_id << 252) | (oid << 124) | (gid << 12) | leg_id

def new_order_id() -> str:
    return str(uuid.uuid4())
function composeUuid(
  orderId: string,
  executorId: bigint = 0n,
  legId: bigint = 0n,
  groupUuid: string | null = null,
): bigint {
  const oid  = BigInt("0x" + orderId.replace(/-/g, ""));
  const gsrc = groupUuid ? BigInt("0x" + groupUuid.replace(/-/g, "")) : oid;
  const gid  = gsrc >> 16n;                                       // 128 位元中的最高 112 位元
  return (executorId << 252n) | (oid << 124n) | (gid << 12n) | legId;
}

function newOrderId(): string {
  const b = randomBytes(16);
  b[6] = (b[6] & 0x0f) | 0x40;                                    // version 4
  b[8] = (b[8] & 0x3f) | 0x80;                                    // RFC 4122 variant
  const h = Buffer.from(b).toString("hex");
  return `${h.slice(0,8)}-${h.slice(8,12)}-${h.slice(12,16)}-${h.slice(16,20)}-${h.slice(20,32)}`;
}

教學 1 — 手動下達一筆限價單

要做的事:以 1.085 EURC/USDC 的價格掛買 1000 EURC。市場的基礎幣是 EURC、計價幣是 USDC。bid 的意思:花 USDC 買 EURC。

代幣地址在啟動時用 GET /tokens 解析一次;本節直接硬編碼:

USDC = {"address": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", "decimals": 6}
EURC = {"address": "0x1aBaEA1f7C830bD89Acc67eC4af516284b1bC33c", "decimals": 6}
const USDC = { address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", decimals: 6 };
const EURC = { address: "0x1aBaEA1f7C830bD89Acc67eC4af516284b1bC33c", decimals: 6 };

送往 POST /orders 的 wire payload 使用對的自然欄位(sideamountpricefrom_address=base、to_address=quote)。EIP-712 訊息是鏈上 Order 結構 — 其中 fromToken 是你支出的幣(bid 時是 USDC、ask 時是 EURC),數量是原始單位。兩者都建立、簽名、送出:

def raw(amount: Decimal, decimals: int) -> int:
    return int(amount * (Decimal(10) ** decimals))

def place_order(side: str, base: dict, quote: dict,
                amount: Decimal, price: Decimal,
                expiration: int) -> dict:
    order_id = new_order_id()
    uuid_int = compose_uuid(order_id)                              # executor_id=0

    # 對的自然 raw
    base_raw  = raw(amount, base["decimals"])
    quote_raw = raw(amount * price, quote["decimals"])

    # EIP-712 結構: fromToken 是簽名者支出的幣。
    if side == "bid":
        from_token, to_token = quote["address"], base["address"]
        from_amount, to_amount = quote_raw, base_raw
    else:
        from_token, to_token = base["address"], quote["address"]
        from_amount, to_amount = base_raw, quote_raw

    order_struct = {
        "user":                 OWNER,
        "expiration":           expiration,
        "feeBps":                0,
        "recipient":            "0x0000000000000000000000000000000000000000",
        "fromToken":            from_token,
        "toToken":              to_token,
        "fromAmount":           from_amount,
        "toAmount":             to_amount,
        "initialDepositAmount":  0,
        "uuid":                 uuid_int,
    }
    signable = encode_typed_data(DOMAIN, ORDER_TYPES, order_struct)
    signature = Account.from_key(PRIVATE_KEY).sign_message(signable).signature.hex()

    wire = {
        "owner_address": OWNER,
        "side":          side,
        "amount":        str(amount),
        "price":         str(price),
        "order_type":    "limit",
        "from_address":  base["address"],                           # base,不是 spend
        "to_address":    quote["address"],                          # quote,不是 receive
        "order_id":      order_id,
        "uuid_int":      str(uuid_int),
        "signature":     "0x" + signature.lstrip("0x"),
        "expiration":    expiration,
    }
    r = requests.post(f"{API}/orders", headers={**AUTH, "Content-Type": "application/json"},
                      data=json.dumps(wire), timeout=10)
    if r.status_code != 201:
        envelope = r.json().get("detail", {})
        raise RuntimeError(f"{envelope.get('error_code')}: {envelope.get('detail')}")
    return {"order_id": order_id, "uuid_int": str(uuid_int)}

placed = place_order("bid", EURC, USDC, Decimal("1000"),
                     Decimal("1.085"), int(time.time()) + 3600)
print("placed:", placed)
function raw(amount: string, decimals: number): bigint {
  const [w, f = ""] = amount.split(".");
  const padded = (w + f.padEnd(decimals, "0")).slice(0, w.length + decimals);
  return BigInt(padded || "0");
}

async function placeOrder(
  side: "bid" | "ask",
  base: { address: string; decimals: number },
  quote: { address: string; decimals: number },
  amount: string,
  price: string,
  expiration: number,
) {
  const orderId  = newOrderId();
  const uuidInt  = composeUuid(orderId);                           // executorId=0n

  const baseRaw  = raw(amount, base.decimals);
  const quoteRaw = raw((Number(amount) * Number(price)).toString(), quote.decimals);

  const isBid    = side === "bid";
  const fromToken   = isBid ? quote.address : base.address;
  const toToken     = isBid ? base.address  : quote.address;
  const fromAmount  = isBid ? quoteRaw      : baseRaw;
  const toAmount    = isBid ? baseRaw       : quoteRaw;

  const orderStruct = {
    user: OWNER, expiration, feeBps: 0,
    recipient: "0x0000000000000000000000000000000000000000",
    fromToken, toToken, fromAmount, toAmount,
    initialDepositAmount: 0n, uuid: uuidInt,
  };
  const signature = await wallet.signTypedData(DOMAIN, ORDER_TYPES, orderStruct);

  const wire = {
    owner_address: OWNER,
    side, amount, price, order_type: "limit",
    from_address: base.address,                                    // base,不是 spend
    to_address:   quote.address,                                   // quote,不是 receive
    order_id:     orderId,
    uuid_int:     uuidInt.toString(),
    signature,
    expiration,
  };
  const r = await fetch(`${API}/orders`, {
    method: "POST",
    headers: { ...AUTH, "Content-Type": "application/json" },
    body: JSON.stringify(wire),
  });
  if (r.status !== 201) {
    const env = (await r.json()).detail ?? {};
    throw new Error(`${env.error_code}: ${env.detail}`);
  }
  return { order_id: orderId, uuid_int: uuidInt.toString() };
}

const placed = await placeOrder("bid", EURC, USDC, "1000", "1.085",
                                Math.floor(Date.now() / 1000) + 3600);
console.log("placed:", placed);

確認訂單已掛出:

curl -H "$AUTH" "$API/orders?owner_address=$OWNER&status=pending&limit=10"

4xx 回傳的錯誤信封在 訂單端點 → 錯誤信封 中有說明。請依 error_code 分支,不要依人類 detail

教學 2 — 撤銷剛才那筆單

CancelOrder 結構只攜帶 ownerorderId(複合 uint256,不是 UUID 字串)。下單時你已經把兩者都存起來了 — 現在派上用場。

def cancel_order(order_id: str, uuid_int: int) -> None:
    signable = encode_typed_data(
        DOMAIN, CANCEL_ORDER_TYPES,
        {"owner": OWNER, "orderId": uuid_int},
    )
    signature = Account.from_key(PRIVATE_KEY).sign_message(signable).signature.hex()
    r = requests.post(
        f"{API}/orders/cancel",
        headers={**AUTH, "Content-Type": "application/json"},
        data=json.dumps({
            "owner_address": OWNER,
            "order_id":      order_id,
            "uuid_int":      str(uuid_int),
            "signature":     "0x" + signature.lstrip("0x"),
        }),
        timeout=10,
    )
    if r.status_code == 429:
        raise RuntimeError("撤單冷卻中 — 稍後重試")
    r.raise_for_status()

cancel_order(placed["order_id"], int(placed["uuid_int"]))
async function cancelOrder(orderId: string, uuidInt: bigint): Promise<void> {
  const signature = await wallet.signTypedData(
    DOMAIN, CANCEL_ORDER_TYPES,
    { owner: OWNER, orderId: uuidInt },
  );
  const r = await fetch(`${API}/orders/cancel`, {
    method: "POST",
    headers: { ...AUTH, "Content-Type": "application/json" },
    body: JSON.stringify({
      owner_address: OWNER,
      order_id: orderId,
      uuid_int: uuidInt.toString(),
      signature,
    }),
  });
  if (r.status === 429) throw new Error("撤單冷卻中 — 稍後重試");
  if (!r.ok) throw new Error(`撤單失敗: ${r.status}`);
}

await cancelOrder(placed.order_id, BigInt(placed.uuid_int));

每訂單撤單冷卻 5 分鐘 — 命中冷卻會回傳 429。若遺失 uuid_int,呼叫 GET /orders/{order_id} 即可在回應主體中取回。

教學 3 — 單交易對雙邊自動報價

這是迴圈:

迴圈:
    mid     ← get_mid("EURC/USDC")            # 你的報價來源
    if 首次迭代 or |mid − last_mid| / last_mid > drift_bps:
        cancel(open_bid, open_ask)            # 任何已過期的訂單
        bid_px  ← mid * (1 − spread_bps/1e4)
        ask_px  ← mid * (1 + spread_bps/1e4)
        open_bid ← place_order("bid", EURC, USDC, qty, bid_px, ...)
        open_ask ← place_order("ask", EURC, USDC, qty, ask_px, ...)
        last_mid ← mid
    sleep(poll_seconds)

三個可調旋鈕:

  • spread_bps — 單側半價差。每側 5 bps = 來回 10 bps。
  • drift_bps — 只在 mid 移動超過該門檻時重新掛單。避免在小幅抖動時反覆撤換。
  • poll_seconds — 喚醒間隔。1–5 秒比較常見。

報價來源 stub

報價來源由你自己提供。整合點就一個函式:

def get_mid(pair: str) -> Decimal:
    """回傳 `pair` 當前的 mid 價。

    實作要對接你自己的資料源 — 內部定價服務、Bloomberg、
    某個聚合器 REST API 等。回傳值是以 quote-per-base 表示的
    Decimal 價格(例如 EURC/USDC = 1.085)。
    """
    raise NotImplementedError("接入你的報價來源。")
async function getMid(pair: string): Promise<number> {
  // 接入你自己的報價來源 — 內部服務、Bloomberg、Refinitiv、
  // 聚合器 REST API 等。回傳 quote-per-base
  // (例如 EURC/USDC = 1.085)。
  throw new Error("接入你的報價來源。");
}

迴圈本體

SPREAD_BPS = Decimal("5")     # 單側 5 bps
DRIFT_BPS  = Decimal("3")     # 僅當 mid 移動 > 3 bps 才重新掛單
POLL_S     = 2.0
QTY        = Decimal("1000")  # 每側的 base 數量

open_bid = open_ask = None
last_mid = None

while True:
    try:
        mid = get_mid("EURC/USDC")
        if last_mid is not None and \
           abs(mid - last_mid) / last_mid * 10_000 <= DRIFT_BPS:
            time.sleep(POLL_S); continue

        # 先撤再掛 — 避免跨週期自成交。
        if open_bid: cancel_order(**open_bid)
        if open_ask: cancel_order(**open_ask)
        open_bid = open_ask = None

        half   = SPREAD_BPS / Decimal(10_000)
        bid_px = (mid * (Decimal(1) - half)).quantize(Decimal("0.0001"))
        ask_px = (mid * (Decimal(1) + half)).quantize(Decimal("0.0001"))
        expiry = int(time.time()) + 3600

        placed_bid = place_order("bid", EURC, USDC, QTY, bid_px, expiry)
        placed_ask = place_order("ask", EURC, USDC, QTY, ask_px, expiry)
        open_bid   = {"order_id": placed_bid["order_id"], "uuid_int": int(placed_bid["uuid_int"])}
        open_ask   = {"order_id": placed_ask["order_id"], "uuid_int": int(placed_ask["uuid_int"])}
        last_mid   = mid
    except RuntimeError as e:
        # 我們的型別化 error_code: INSUFFICIENT_EQUITY、STP_BLOCKED 等。
        print("place/cancel error:", e)
    except requests.RequestException as e:
        print("network error:", e)
    time.sleep(POLL_S)
const SPREAD_BPS = 5;                                              // 單側
const DRIFT_BPS  = 3;
const POLL_MS    = 2_000;
const QTY        = "1000";                                         // 每側的 base 數量

type Live = { order_id: string; uuid_int: bigint } | null;
let openBid: Live = null, openAsk: Live = null, lastMid: number | null = null;

const sleep = (ms: number) => new Promise(r => setTimeout(r, ms));

while (true) {
  try {
    const mid = await getMid("EURC/USDC");
    if (lastMid !== null &&
        Math.abs(mid - lastMid) / lastMid * 10_000 <= DRIFT_BPS) {
      await sleep(POLL_MS); continue;
    }

    if (openBid) await cancelOrder(openBid.order_id, openBid.uuid_int);
    if (openAsk) await cancelOrder(openAsk.order_id, openAsk.uuid_int);
    openBid = openAsk = null;

    const half   = SPREAD_BPS / 10_000;
    const bidPx  = (mid * (1 - half)).toFixed(4);
    const askPx  = (mid * (1 + half)).toFixed(4);
    const expiry = Math.floor(Date.now() / 1000) + 3600;

    const pb = await placeOrder("bid", EURC, USDC, QTY, bidPx, expiry);
    const pa = await placeOrder("ask", EURC, USDC, QTY, askPx, expiry);
    openBid = { order_id: pb.order_id, uuid_int: BigInt(pb.uuid_int) };
    openAsk = { order_id: pa.order_id, uuid_int: BigInt(pa.uuid_int) };
    lastMid = mid;
  } catch (e) {
    console.error("loop error:", e);
  }
  await sleep(POLL_MS);
}

進入生產前,有幾條行為值得清楚:

  • 先撤再掛。 若在撤掉舊單之前就掛新單,你的新 bid 可能會撞上舊的 ask — API 會以 STP_BLOCKED 拒絕。上面的迴圈先撤再掛。
  • INSUFFICIENT_EQUITY 是好事。 看到它表示你凍結的 vault 餘額超過可用。減小 QTY、加充值、或收緊價差。
  • 撤單冷卻以 order_id 為單位。 只有在你嘗試重複撤同一筆時才有影響。上面的迴圈每次重新報價都換新的 order_id,所以每次撤的是不同的桶。
  • 清狀態前先對帳。 cancel_order 回傳 200 只代表撮合引擎接受了取消;任何進行中成交的鏈上結算是非同步的。如果你追蹤庫存,要輪詢 GET /orders/{id}settlement_summary 直到它進入終態,再清掉本地部位。

教學 4 — 用 VL 批次做多交易對報價

上面單交易對的迴圈為 bid 凍結一份 USDC、為 ask 凍結一份 EURC。若想用一份擔保品同時報多個交易對,就送 VL 批次:所有 leg 共享同一個 group_id,撮合引擎只凍結最大單 leg 成本(而非總和)。

骨架不變;把單 leg 的 placeOrder 換成批量呼叫:

def place_vl_batch(legs: list[dict]) -> list[dict]:
    """legs: 每個為 {side, base, quote, amount, price, expiration}。"""
    leg_0_uuid = new_order_id()                                    # group_id 的來源
    signed_legs = []
    for i, leg in enumerate(legs):
        order_id = leg_0_uuid if i == 0 else new_order_id()
        uuid_int = compose_uuid(order_id, executor_id=0,
                                leg_id=i, group_uuid=leg_0_uuid)

        base, quote = leg["base"], leg["quote"]
        base_raw    = raw(leg["amount"], base["decimals"])
        quote_raw   = raw(leg["amount"] * leg["price"], quote["decimals"])
        if leg["side"] == "bid":
            ft, tt, fa, ta = quote["address"], base["address"], quote_raw, base_raw
        else:
            ft, tt, fa, ta = base["address"], quote["address"], base_raw, quote_raw

        struct = {
            "user": OWNER, "expiration": leg["expiration"],
            "feeBps": 0,
            "recipient": "0x0000000000000000000000000000000000000000",
            "fromToken": ft, "toToken": tt,
            "fromAmount": fa, "toAmount": ta,
            "initialDepositAmount": 0, "uuid": uuid_int,
        }
        signable = encode_typed_data(DOMAIN, ORDER_TYPES, struct)
        sig = Account.from_key(PRIVATE_KEY).sign_message(signable).signature.hex()
        signed_legs.append({
            "owner_address": OWNER,
            "side":          leg["side"],
            "amount":        str(leg["amount"]),
            "price":         str(leg["price"]),
            "order_type":    "limit",
            "from_address":  base["address"],
            "to_address":    quote["address"],
            "order_id":      order_id,
            "uuid_int":      str(uuid_int),
            "signature":     "0x" + sig.lstrip("0x"),
            "expiration":    leg["expiration"],
        })

    r = requests.post(
        f"{API}/orders/vl/batch",
        headers={**AUTH, "Content-Type": "application/json"},
        data=json.dumps({"orders": signed_legs}),
        timeout=10,
    )
    r.raise_for_status()
    return r.json()["order_ids"]
async function placeVlBatch(legs: {
  side: "bid" | "ask";
  base:  { address: string; decimals: number };
  quote: { address: string; decimals: number };
  amount: string;
  price:  string;
  expiration: number;
}[]): Promise<string[]> {
  const leg0Uuid = newOrderId();                                   // group_id 的來源
  const signedLegs: any[] = [];
  for (let i = 0; i < legs.length; i++) {
    const leg = legs[i];
    const orderId  = i === 0 ? leg0Uuid : newOrderId();
    const uuidInt  = composeUuid(orderId, 0n, BigInt(i), leg0Uuid);

    const baseRaw  = raw(leg.amount, leg.base.decimals);
    const quoteRaw = raw((Number(leg.amount) * Number(leg.price)).toString(),
                         leg.quote.decimals);
    const isBid    = leg.side === "bid";
    const ft = isBid ? leg.quote.address : leg.base.address;
    const tt = isBid ? leg.base.address  : leg.quote.address;
    const fa = isBid ? quoteRaw          : baseRaw;
    const ta = isBid ? baseRaw           : quoteRaw;

    const struct = {
      user: OWNER, expiration: leg.expiration, feeBps: 0,
      recipient: "0x0000000000000000000000000000000000000000",
      fromToken: ft, toToken: tt, fromAmount: fa, toAmount: ta,
      initialDepositAmount: 0n, uuid: uuidInt,
    };
    const signature = await wallet.signTypedData(DOMAIN, ORDER_TYPES, struct);
    signedLegs.push({
      owner_address: OWNER,
      side: leg.side, amount: leg.amount, price: leg.price,
      order_type: "limit",
      from_address: leg.base.address,
      to_address:   leg.quote.address,
      order_id:     orderId,
      uuid_int:     uuidInt.toString(),
      signature, expiration: leg.expiration,
    });
  }

  const r = await fetch(`${API}/orders/vl/batch`, {
    method: "POST",
    headers: { ...AUTH, "Content-Type": "application/json" },
    body: JSON.stringify({ orders: signedLegs }),
  });
  if (!r.ok) throw new Error(`VL batch failed: ${r.status}`);
  const body = await r.json();
  return body.order_ids as string[];
}

圍繞它包一層報價迴圈:為每個 pair 取 mid,按 leg 計算 bid + ask,用 POST /orders/vl/cancel(簽 CancelVLBatch{owner, vlBatchId})撤掉前一個批次,再提交新的。啟動時讀一次 GET /config → limits.vl_batch 取當前批次容量上限。

實務要點

  • order_iduuid_int 一併持久化。 每次撤單都會用到。本地狀態以 order_id 為鍵、按需再算 uuid_int 也沒問題;但成對存更不容易出錯。
  • 冪等性。 由客戶端選 order_idPOST /orders 對它冪等。提交後網路抖動?同一份 payload 重送即可 — 伺服器會去重。
  • 不要把 limits.vl_batch.max 寫死。 啟動時從 GET /config 讀,並把它當成唯一可信來源。
  • 清庫存前先看 settlement_summary 取消訂單回傳 200、或狀態 pending → cancelled,都不代表所有進行中的撮合在鏈上都落地了。輪詢 GET /orders/{id} 直到 settlement_summary.status 進入終態。

下一步