Withdrawal stream
Stream withdrawals from your main account.
On this page
Write a small program that subscribes to `transfer.withdrawal` on Grvt {environment} {using}, checks sequence numbers and re-subscribes on a gap.
Target environment: {environment} (chain ID {chainId}). Recorded signing cases keep their original environment.
{executionGuidance}
Read this first: {origin}/bundle/reference/streams/withdrawal.md (~57 KB, plain Markdown: this page plus the AGENTS.md rules, Authentication and sessions, WebSocket, Python SDK, Environments).
Fetch other pages from the index at {origin}/llms.txt only as you need them. {origin}/downloads/contract.json is a machine-readable contract for tools, not for reading.
Rules: follow AGENTS.md; do not send requests without the user asking you to; read credentials from environment variables; never retry writes automatically; show me the code and how to run it. URLs and generated code samples on this page: Testnet (chain ID 326). Other code on this page doesn't change: it stays on testnet until you switch it in your code. Tutorials, signing cases and live tools always use testnet.
| SDK | client. |
|---|---|
| Channel | v1.withdrawal on wss:/, or /ws/lite for lite names (the SDKs' default) |
| Selector | <main_account_id> |
| Auth | Session required on the WebSocket upgrade (Authentication) |
| Kind | event. Each frame is one event (an update to one object). |
| Sequenced | Yes: the first update's prev_sequence_number is at or below the subscribe reply's latest_sequence_number, then each one equals the last sequence_number (rules). On a gap, re-subscribe, then fetch current state over HTTP (withdrawal_history) for what you missed. |
| Contract id | transfer.withdrawal (StreamWithdrawalV1) |
Selector
Grammar <main_account_id>, e.g. <main_account_id>. Parts:
- The main account ID to request for
Frame
Type WSWithdrawalFeedDataV1.
- The stream this frame belongs to, e.g.
v1.withdrawal. - The selector this frame is for, as you subscribed with it.
- This frame's number, an unsigned 64-bit integer sent as a decimal string: compare it as an integer.
"0"on the snapshot sent right after subscribing.- With
use_global_sequence_number: true(recommended), the number is unique across the exchange. One exchange event can produce several frames, so the number isevent number × 10^5 + a counterthat tells them apart. - With
false, it counts up by one per frame of this stream and starts again when the server you are connected to restarts.
- With
- The Withdrawal object
5 fields of
Withdrawal- The sub-account to withdraw from
- The ethereum address to withdraw to
- The token currency to withdraw
- The number of tokens to withdraw
- The signature of the withdrawal
7 fields of
Signature- The address of the wallet that signed (
0x+ 40 hex characters). That wallet must be allowed to act for the account; for an order, to trade for the sub-account. - The signature's
r, as0x+ 64 hex characters. Without the0xthe signature fails to verify. - The signature's
s, as0x+ 64 hex characters. Without the0xthe signature fails to verify. - The signature's
v, 27 or 28. - When the signature stops being valid, in unix nanoseconds, measured on the exchange's clock. At most 30 days ahead (take-profit and stop-loss orders may be accepted up to 180 days ahead); one already past fails with
2001. On an order it is also the order's lifetime: an order still open then is cancelled with reasonEXPIRED. The SDKs use now + 24 hours. - A number you choose, 0 to 4,294,967,295 (uint32), so that two otherwise identical requests have different signatures. Use a new random one for each request you sign.
A transfer whose signed fields (nonce and expiration included) exactly repeat an earlier one is rejected, not ignored: it fails with
4007.Orders are different. An exact resend of a signed order can be accepted and rest on the book, but the exchange counts fills per signed order, so all copies together never fill more than the signed size, and a resent
IMMEDIATE_OR_CANCELorder whose first copy already filled is rejected. An order signed again (new nonce or expiration) is a new order and can fill in full again. - The chain id in the signed EIP-712 domain, as a string. Testnet 326, production 325;
"0"means the environment's chain id.
- The
sequence_numberof the frame before this one on this selector. If it doesn't match the last frame you accepted, you missed frames.
Errors
Subscribe errors arrive as a JSON-RPC error object {code, message} (lite {c, m}). Codes this stream can return (all error codes):
Example
Example code
import asyncio
import os
from grvt import AsyncGrvt
async def main():
async with AsyncGrvt(env=os.environ.get("GRVT_ENV", "testnet"), api_key=os.environ["GRVT_API_KEY"]) as client:
async with client.withdrawal_stream(main_account_id=os.environ["GRVT_MAIN_ACCOUNT_ID"]) as sub:
async for frame in sub:
print(frame.sequence_number, frame.feed)
asyncio.run(main())
Raw WebSocket: connect to wss://trades.testnet.grvt.io/ws/full (/ws/lite for lite names) with the Cookie: gravity=... and X-Grvt-Account-Id headers and send:
With full field names:
{
"jsonrpc": "2.0",
"method": "subscribe",
"params": {
"stream": "v1.withdrawal",
"selectors": ["$GRVT_MAIN_ACCOUNT_ID"],
"use_global_sequence_number": true
},
"id": 1
}With lite field names:
{
"j": "2.0",
"m": "subscribe",
"p": {
"s": "v1.withdrawal",
"s1": ["$GRVT_MAIN_ACCOUNT_ID"],
"ug": true
},
"i": 1
}The reply's result is a WSSubscribeResult (subs, num_snapshots, latest_sequence_number); then frames arrive in the shape under Frame above, with no JSON-RPC envelope.
Build subscription
v1.withdrawal is on the trading gateway and needs a logged-in session (Cookie: gravity=… and X-Grvt-Account-Id on the WebSocket upgrade). The docs only build the subscription and never open the socket. Log in with the SDK or follow Authentication, then subscribe with the selector <main_account_id>.