Skip to Content

L2

The rollup account: the trading key, deposits, withdrawals, order indexes, perps collateral, and the public intake journal.

CallAuth
GET /api/l2/keysLogin token or Privy JWT
POST /api/l2/keysPrivy JWT. A login token is refused
POST /api/l2/depositsPrivy JWT
POST /api/l2/withdrawalsLogin token or Privy JWT, and a signature
POST /api/l2/order-indicesLogin token or Privy JWT
POST /api/l2/allocationsLogin token or Privy JWT, and a signature
POST /api/l2/exitsLogin token or Privy JWT, and a signature
POST /api/l2/modesLogin token or Privy JWT, and a signature
GET /api/l2/intakePublic
GET /api/relay/:txHashPrivy JWT

Loaf can close deposits or withdrawals for a time. A closed route returns 403 after authentication. GET /api/l2/keys and the other routes still answer.

The relay routes on this page (key registration and deposits) have a tighter per-account limit, on top of the account limit. Loaf pays the gas. All relay routes also share one daily cap per account. Past it the call is 429 with { "error": "Too many gasless requests for this account today. Please try again tomorrow.", "code": "RATE_LIMITED" }. Retry-After gives the seconds until the cap resets.

Keys

GET /api/l2/keys

The account you sign with. With a Privy JWT, 404 until a deposit has bound an L2 account (l2 account: no L2 account for this user). A login token for an account with no trading account is refused 401 UNAUTHENTICATED.

curl 'https://api.loafmarkets.com/api/l2/keys' \ -H "authorization: Bearer $LOAF_LOGIN_TOKEN"
{ "accountIndex": 4, "epoch": 3, "journaledKey": "…80 hex…", "pending": false, "withdrawalNonce": 7 }

epoch is null until the first change-pubkey or cancel-all has been applied on L1. Sign orders at this epoch. journaledKey is the api key 0 that L1 has applied, or null until L1 has applied one. withdrawalNonce is the l2Nonce the next signed withdrawal, allocate, exit, or set-mode must use.

?apiKeyIndex= (0 to 255, default 0) and ?publicKey= change what pending means. With publicKey, pending is true until L1 has applied that key. Without it, pending is true while epoch is null.

POST /api/l2/keys

Register api key 0. The wallet signs ZkLoaf changePubKey (EIP-712, domain name ZkLoaf, version 1, chainId from market.l2.chainId, verifyingContract the proxy from market.l2.proxyAddress). Loaf sends the transaction.

{ "owner": "0xabc…", "accountIndex": 4, "apiKeyIndex": 0, "pubKey": "…", "deadline": 1765000000, "signature": "0x…" }

202 when the transaction was sent, 200 when this key is already queued:

{ "status": "SENT", "txHash": "0x…", "accountIndex": 4, "apiKeyIndex": 0, "publicKey": "…", "epoch": null, "pending": true }

status is SENT or ALREADY_QUEUED. Follow txHash on GET /api/relay/:txHash. The portfolio socket also sends relay_update and l2_keys_update.

Deposits

POST /api/l2/deposits

A gasless deposit. The caller signs an EIP-2612 permit that spends to the ZkLoaf proxy. Loaf relays it. 202:

{ "assetId": 3, "amount": "100000000", "deadline": 1765000000, "signature": "0x…" }
{ "txHash": "0x…" }

amount is the token’s base units. assetId 3 is USDC.

Withdrawals

POST /api/l2/withdrawals

A signed spot-route withdrawal. It pays the wallet that opened the account, and no other address. l2ApiKeyIndex is 0. l2Route is 1. l2AssetId is 1 to 62. amount is a decimal string in the asset’s L2 ticks (see Money and units). l2Nonce is withdrawalNonce from the keys route. Sign l2ExpiryMs at least 72 hours ahead. An expiry inside that 72-hour buffer is ORDER_EXPIRY_INVALID. maxWithdrawFee is the most withdrawal fee you accept, in USD. It is not part of the signed message. Omit it to accept 0.

{ "l2AccountIndex": 4, "l2ApiKeyIndex": 0, "l2AssetId": 3, "l2Route": 1, "amount": "50000000", "l2Nonce": 7, "l2ExpiryMs": 1765000000000, "l2ChainId": 1, "l2SigS": "…80 hex…", "l2SigE": "…80 hex…", "maxWithdrawFee": "0.05" }

202 is the accepted withdrawal. A resend of the same nonce returns it again with duplicate: true. Later states are on the portfolio transfer, not this response. The transfer’s status becomes CONFIRMED or REJECTED.

FieldMeaning
journalSeqJournal sequence
accountIndexAccount index
assetIdAsset id
amountThe submitted decimal, as a number
nonceThe l2Nonce you sent
duplicatetrue when this withdrawal was already accepted
withdrawFeeUSD string, or null
status{ "status": "PENDING" }

A fee above maxWithdrawFee is WITHDRAW_FEE_ABOVE_MAX. Other refusals use the order error body.

The withdrawal shows up on GET /api/portfolio/component in transfers, with payout once L1 has paid it.

Order indexes

POST /api/l2/order-indices

Reserves a block of l2OrderIndex values for this account. The body is { "count": 64 } and nothing else. count is 1 to 4096. The account comes from the login, not the body.

201:

{ "accountIndex": 4, "firstIndex": 100, "count": 64, "reservedAtMs": 1764000000000 }

Sign l2OrderIndex from firstIndex through firstIndex + count - 1, each once. l2ExpiryMs must be at most reservedAtMs + 4261412864. A lost answer leaves the range reserved. Reserve again for a new range. 503 NOT_PROCESSED means nothing was reserved. Send the same count again.

Perps collateral and mode

These three are signed by api key 0 and spend withdrawalNonce. 202 is { "journalSeq": 4410, "duplicate": false }. A resend of the same nonce is duplicate: true. Refusals use the order error body. The 72-hour expiry buffer of orders and withdrawals does not apply to these three. An l2ExpiryMs that has passed before the engine takes the request is 422 UNKNOWN_REASON.

POST /api/l2/allocations moves collateral. fromSide and toSide are pool indexes 0 to 7, or 255 for the spot USDC balance. They must differ. amount is a decimal string in USDC L2 ticks.

{ "l2ChainId": 1, "l2AccountIndex": 4, "fromSide": 255, "toSide": 0, "amount": "10000000", "l2Nonce": 8, "l2ExpiryMs": 1765000000000, "l2SigS": "…80 hex…", "l2SigE": "…80 hex…" }

POST /api/l2/exits closes one position in a perp market whose l2.status is EXPIRED on GET /api/trade/:ticker. pool is 0 to 7. market is the perp market index, 0 to 254. An exit in any other market is 422 UNKNOWN_REASON (l2 perps: an exit closes a position in an expired market only). To close a position in an ACTIVE market, place a reduce-only order. An exit’s l2ExpiryMs past the maximum order life, about 49 days ahead, is 422 ORDER_EXPIRY_INVALID.

POST /api/l2/modes sets the account mode. mode is 0 (MANUAL) or 1 (UNIFIED). GET /api/portfolio/summary then reports accountMode and withdrawable.

Intake journal

GET /api/l2/intake?after=0&limit=20

Public. The engine’s intake journal, oldest first, rows with intakeSeq above after (default 0). limit is 1 to 50, default 20. Not cached. This route has its own per-IP limit.

{ "rows": [ { "intakeSeq": 1, "commitSeq": 1, "lines": "…", "head": "…64 hex…" } ] }

Keep lines byte for byte. Parsing and writing it again breaks head. head is sha256 of the previous head, then intakeSeq as 8 big-endian bytes, then lines as UTF-8. The previous head is 32 zero bytes before the first row. A batch commit on L1 carries the head, so anyone can check what the book took in.

An order line names its signature as signature_hash (keccak256, 0x and 64 hex digits), the same value the explorer serves as buySignatureHash and sellSignatureHash. The signature itself is not in the journal. A page can hold fewer rows than limit. The next page uses after equal to the last intakeSeq. An empty page is the end.

StatuscodeMeaning
400INVALID_REQUESTafter is not a whole number of 0 or more, limit is not a whole number from 1 to 50, or the query has another parameter
429RATE_LIMITED{ "error": "Too many requests. Please slow down." } and Retry-After

Relay status

GET /api/relay/:txHash

Privy JWT. Your own calls in a relayed transaction, and the status of each. Calls are PENDING until the transaction is mined. When it is mined, the portfolio socket sends the same object as relay_update.

{ "txHash": "0x…", "calls": [ { "kind": "deposit", "status": "LANDED", "assetId": 3, "amount": "100000000" }, { "kind": "key", "status": "LANDED", "accountIndex": 4, "apiKeyIndex": 0, "publicKey": "…" } ] }

kind send:

FieldMeaning
tokenToken
toDestination
amountAmount
nonceNonce

status on every call is PENDING, LANDED, or FAILED.

Last updated on