跳转至

做市商指南

网络与兼容性

资源
API 基础 URL https://api.sera.cx/api/v1
以太坊主网(chainId 1
Sera 合约 0xB5C50C5D5f038404F85970b7f5B7259C4AC0E198
Vault 合约 0xC7d4Fd2638e6630C8C61329878676b88A8A24D43
SOR 合约 0xa7A0cf7cd6f043fCA23f29d8ae5aae6b46e11c18

签名原语。 所有交易型变更都是针对 Sera 域的 EIP-712 类型化数据签名。走 permit 路径的充值使用 ERC-2612 Permit 扩展 — USDC、EURC 以及许多现代稳定币支持,但并非所有 ERC-20 都支持;签名前请调用 GET /permit/metadata 检查。API key 管理使用 EIP-712 ManageApiKey 载荷。

已验证客户端。 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 签名载荷接受 EIP-55 校验和地址。

这是你写 bot 第一版时希望开着的页面。按顺序讲三件事:

  1. 端到端手动下达一笔限价单。
  2. 撤销刚才那笔单。
  3. 把两者包装成一个由你自己的报价源驱动的自动挂单循环。

不在这里讲的环节 — 创建 API key、向 Vault 充值、之后提款 — 列在下一步。等你的钱包在以太坊主网上已经有 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 按部署设定。以太坊主网默认是 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 进入终态。

下一步