Đăng ký luồng dữ liệu
Trang này mô tả việc đăng ký vào các luồng dữ liệu bằng cách sử dụng WebSocket API.
Tin nhắn đăng ký
Để đăng ký các nguồn cấp dữ liệu cụ thể, bạn cần gửi tin nhắn đăng ký.Thông báo đăng ký có định dạng như sau:
{
"method": "subscribe",
"subscription": { ... }
}
Ack đăng ký cung cấp ảnh chụp nhanh dữ liệu trước đó cho dữ liệu chuỗi thời gian (ví dụ: điền người dùng). các tin nhắn chụp nhanh này được gắn thẻ isSnapshot: true và có thể bị bỏ qua nếu các tin nhắn trước đó đã được xử lý.
Đối tượng đăng ký chứa các chi tiết của nguồn cấp dữ liệu cụ thể bạn muốn đăng ký.Chọn từ các loại đăng ký sau và cung cấp các thuộc tính tương ứng:
allMids: * Thông báo đăng ký:{ "type": "allMids", "dex": "<dex>" }* Định dạng dữ liệu:AllMids* Trườngdexđại diện cho perp dex đến nguồn mids từ. * Ghi chú rằng trườngdexkhông bắt buộc. Nếu không cung cấp, thì thủ phạm dex đầu tiên được sử dụng. Spot mids chỉ bao gồm với thủ phạm dex đầu tiên.notification: * Thông báo đăng ký:{ "type": "notification", "user": "<address>" }* Định dạng dữ liệu:NotificationwebData3: * Thông báo đăng ký:{ "type": "webData3", "user": "<address>" }* Định dạng dữ liệu:WebData3twapStates: * Thông báo đăng ký:{ "type": "twapStates", "user": "<address>", "dex": "<dex>" }* Định dạng dữ liệu:TwapStatesclearinghouseState:* Thông báo đăng ký:{ "type": "clearinghouseState", "user": "<address>", "dex": "<dex>" }* Định dạng dữ liệu:ClearinghouseStateopenOrders: * Thông báo đăng ký:{ "type": "openOrders", "user": "<address>", "dex": "<dex>" }* Định dạng dữ liệu:OpenOrderscandle: * Thông báo đăng ký:{ "type": "candle", "coin": "<coin_symbol>", "interval": "<candle_interval>" }* Các khoảng được hỗ trợ: "1m", "3m", "5m", "15m", "30m", "1h", "2h", "4h", "8h", "12h", "1d", "3d", "1w", "1M" * Định dạng dữ liệu:Candle[]l2Book: * Thông báo đăng ký:{ "type": "l2Book", "coin": "<coin_symbol>" }* Các tham số tuỳ chọn:nSigFigs: int,mantissa: int.fast: boolean(mức 5 nếu nhanh, mức 20 nếu chậm). * Định dạng dữ liệu:WsBooktrades: * Thông báo đăng ký:{ "type": "trades", "coin": "<coin_symbol>" }* Định dạng dữ liệu:WsTrade[]orderUpdates:- Thông báo đăng ký:
{ "type": "orderUpdates", "user": "<address>" } - Định dạng dữ liệu:
WsOrder[]
- Thông báo đăng ký:
userEvents:- Thông báo đăng ký:
{ "type": "userEvents", "user": "<address>" } - Định dạng dữ liệu:
WsUserEvent - Ghi chú: Tên kênh cho các thông điệp
userEventslà"channel": "user"
- Thông báo đăng ký:
userFills:- Thông báo đăng ký:
{ "type": "userFills", "user": "<address>" } - Tham số tùy chọn:
aggregateByTime: bool - Định dạng dữ liệu:
WsUserFills
- Thông báo đăng ký:
userFundings:- Thông báo đăng ký:
{ "type": "userFundings", "user": "<address>" } - Định dạng dữ liệu:
WsUserFundings
- Thông báo đăng ký:
userNonFundingLedgerUpdates:- Thông báo đăng ký:
{ "type": "userNonFundingLedgerUpdates", "user": "<address>" } - Định dạng dữ liệu:
WsUserNonFundingLedgerUpdates
- Thông báo đăng ký:
activeAssetCtx:- Thông báo đăng ký:
{ "type": "activeAssetCtx", "coin": "<coin_symbol>" } - Định dạng dữ liệu:
WsActiveAssetCtxhoặcWsActiveSpotAssetCtx
- Thông báo đăng ký:
activeAssetData: (chỉ hỗ trợ Perps)- Thông báo đăng ký:
{ "type": "activeAssetData", "user": "<address>", "coin": "<coin_symbol>" } - Định dạng dữ liệu:
WsActiveAssetData
- Thông báo đăng ký:
userTwapSliceFills:- Thông báo đăng ký:
{ "type": "userTwapSliceFills", "user": "<address>" } - Định dạng dữ liệu:
WsUserTwapSliceFills
- Thông báo đăng ký:
userTwapHistory:- Thông báo đăng ký:
{ "type": "userTwapHistory", "user": "<address>" } - Định dạng dữ liệu:
WsUserTwapHistory
- Thông báo đăng ký:
bbo:- Thông báo đăng ký:
{ "type": "bbo", "coin": "<coin>" } - Định dạng dữ liệu:
WsBbo
- Thông báo đăng ký:
spotState- Thông báo đăng ký:
{ "type": "spotState", "user": "<address>", "isPortfolioMargin": bool } - Định dạng dữ liệu:
WsSpotState isPortfolioMarginlà một đối số tùy chọn
- Thông báo đăng ký:
allDexsClearinghouseState- Thông báo đăng ký:
{ "type": "allDexsClearinghouseState", "user": "<address>" } - Định dạng dữ liệu:
WsAllDexsClearinghouseState
- Thông báo đăng ký:
allDexsAssetCtxs- Thông báo đăng ký:
{ "type": "allDexsAssetCtxs" } - Định dạng dữ liệu:
WsAllDexsAssetCtxs
- Thông báo đăng ký:
outcomeMetaUpdates- Thông báo đăng ký:
{ "type": "outcomeMetaUpdates" } - Định dạng dữ liệu:
WsOutcomeMetaUpdates
- Thông báo đăng ký:
fastAssetCtxs- { "method": "subscribe", "subscription": { "type": "fastAssetCtxs" } }
- Định dạng dữ liệu:
WsFastAssetCtxsmã hóa và nén bằng base64 - Để đọc dữ liệu, áp dụng theo thứ tự:\
1. base64-decode to bytes\
2. decompress: một luồng DEFLATE thô (RFC 1951) ?? không có bộ bao bọc zlib (RFC 1950) hoặc gzip (RFC 1952 ). Python
zlib.decompress(b, wbits=-15), trình duyệtnew DecompressionStream("deflate-raw"), Node\fflate.inflateSync\ 3. UTF-8 giải mã và phân tích thành JSON. 4. Ví dụ để kiểm tra việc thực hiện của bạn\ trọng tải:"q1ZyCnFWsqpWyk0syg6oULJSsjQ3NTDQM1Wq1VFyDfFAkTI2MzXQMwJLVVRWWfmFuTiiyBuamOoZKdXWAgA="\ đã giải mã:{ "BTC": { "markPx": "97500.5" }, "ETH": { "markPx": "3650.25" }, "xyz:NVDA": { "markPx": "145.2" }} - Thông điệp đầu tiên là ảnh chụp, các thông điệp tiếp theo chỉ chứa các đồng xu đã được cập nhật. Các trường không có cập nhật bị bỏ qua.
Địa chỉ người dùng
Để đăng ký dữ liệu tài khoản được liên kết với tài khoản chính hoặc tài khoản phụ, bạn phải chuyển địa chỉ thực tế của tài khoản đó. Một cạm bẫy phổ biến là sử dụng địa chỉ ví đại lý dẫn đến kết quả rỗng.
Định dạng dữ liệu
Máy chủ sẽ đáp ứng các đăng ký thành công với một tin nhắn chứa thuộc tính channel được đặt thành "subscriptionResponse", cùng với trường data cung cấp đăng ký ban đầu.Sau đó máy chủ sẽ bắt đầu gửi tin nhắn với thuộc tính channel được đặt thành loại đăng ký tương ứng, ví dụ "allMids" và trường data cung cấp dữ liệu đã đăng ký.
Định dạng trường data phụ thuộc vào loại đăng ký:
AllMids: Tất cả các mức giá trung bình.- Định dạng:
AllMids { mids: Record<string, string> } Notification: Một thông báo.- Định dạng:
Notification { notification: string } WebData3: Tổng hợp thông tin về người dùng, được sử dụng chủ yếu cho giao diện người dùng.- Định dạng:
WebData3 WsTrade[]: Một loạt các bản cập nhật thương mại.- Định dạng:
WsTrade[] WsBook: Cập nhật nhanh sổ lệnh.- Định dạng:
WsBook { coin: string; levels: [Array<WsLevel>, Array<WsLevel>]; time: number; } WsOrder: Cập nhật đơn hàng người dùng.- Định dạng:
WsOrder[] WsUserEvent: Sự kiện người dùng không phải là cập nhật thứ tự- Định dạng:
WsUserEvent { "fills": [WsFill] | "funding": WsUserFunding | "liquidation": WsLiquidation | "nonUserCancel": [WsNonUserCancel] } WsUserFills: Fill snapshot theo sau fill phát trực tiếpWsUserFundings: Ghi chú chi tiêu tài trợ theo sau là chi tiêu tài chính theo giờWsUserNonFundingLedgerUpdates: Cập nhật sổ cái không bao gồm các khoản thanh toán tài trợ: rút tiền, ký quỹ, chuyển nhượng và thanh lýWsBbo: cập nhật Bbo chỉ được gửi nếu bbo thay đổi trên một khốiWsSpotState: Cập nhật trạng thái chấm.WsAllDexsClearinghouseState: Trạng thái của nhà bù trừ trên mọi dexs dành cho người dùng cụ thểWsAllDexsAssetCtxs: Bối cảnh tài sản trên tất cả dexsWsOutcomeMetaUpdates: Thay đổi đối với siêu kết quả
Đối với các điểm cuối người dùng phát trực tuyến như WsUserFills, WsUserFundings, tin nhắn đầu tiên có isSnapshot: true và các bản cập nhật phát trực tuyến tiếp theo có isSnapshot: false.
Định nghĩa kiểu dữ liệu
Dưới đây là các định nghĩa của các kiểu dữ liệu được sử dụng trong WebSocket API:
interface WsTrade {
coin: string;
side: string;
px: string;
sz: string;
hash: string;
time: number;
// tid is 50-bit hash of (buyer_oid, seller_oid).
// For a globally unique trade id, use (block_time, coin, tid)
tid: number;
users: [string, string] // [buyer, seller]
}
// Snapshot feed, pushed on each block that is at least 0.5 since last push
interface WsBook {
coin: string;
levels: [Array<WsLevel>, Array<WsLevel>];
time: number;
}
interface WsBbo {
coin: string;
time: number;
bbo: [WsLevel | null, WsLevel | null];
}
interface WsLevel {
px: string; // price
sz: string; // size
n: number; // number of orders
}
interface Notification {
notification: string;
}
interface AllMids {
mids: Record<string, string>;
}
interface Candle {
t: number; // open millis
T: number; // close millis
s: string; // coin
i: string; // interval
o: number; // open price
c: number; // close price
h: number; // high price
l: number; // low price
v: number; // volume (base unit)
n: number; // number of trades
}
type WsUserEvent = {"fills": WsFill[]} | {"funding": WsUserFunding} | {"liquidation": WsLiquidation} | {"nonUserCancel" :WsNonUserCancel[]};
interface WsUserFills {
isSnapshot?: boolean;
user: string;
fills: Array<WsFill>;
}
interface WsFill {
coin: string;
px: string; // price
sz: string; // size
side: string;
time: number;
startPosition: string;
dir: string; // used for frontend display
closedPnl: string;
hash: string; // L1 transaction hash
oid: number; // order id
crossed: boolean; // whether order crossed the spread (was taker)
fee: string; // negative means rebate
tid: number; // unique trade id
liquidation?: FillLiquidation;
feeToken: string; // the token the fee was paid in
builderFee?: string; // amount paid to builder, also included in fee
}
interface FillLiquidation {
liquidatedUser?: string;
markPx: number;
method: "market" | "backstop";
}
interface WsUserFunding {
time: number;
coin: string;
usdc: string;
szi: string;
fundingRate: string;
}
interface WsLiquidation {
lid: number;
liquidator: string;
liquidated_user: string;
liquidated_ntl_pos: string;
liquidated_account_value: string;
}
interface WsNonUserCancel {
coin: String;
oid: number;
}
interface WsOrder {
order: WsBasicOrder;
status: string; // See https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint#query-order-status-by-oid-or-cloid for a list of possible values
statusTimestamp: number;
}
interface WsBasicOrder {
coin: string;
side: string;
limitPx: string;
sz: string;
oid: number;
timestamp: number;
origSz: string;
cloid: string | undefined;
}
interface WsActiveAssetCtx {
coin: string;
ctx: PerpsAssetCtx;
}
interface WsActiveSpotAssetCtx {
coin: string;
ctx: SpotAssetCtx;
}
type SharedAssetCtx = {
dayNtlVlm: number;
prevDayPx: number;
markPx: number;
midPx?: number;
};
type PerpsAssetCtx = SharedAssetCtx & {
funding: number;
openInterest: number;
oraclePx: number;
};
type SpotAssetCtx = SharedAssetCtx & {
circulatingSupply: number;
};
interface WsActiveAssetData {
user: string;
coin: string;
leverage: Leverage;
maxTradeSzs: [number, number];
availableToTrade: [number, number];
}
interface WsTwapSliceFill {
fill: WsFill;
twapId: number;
}
interface WsUserTwapSliceFills {
isSnapshot?: boolean;
user: string;
twapSliceFills: Array<WsTwapSliceFill>;
}
interface TwapState {
coin: string;
user: string;
side: string;
sz: number;
executedSz: number;
executedNtl: number;
minutes: number;
reduceOnly: boolean;
randomize: boolean;
timestamp: number;
}
type TwapStatus = "activated" | "terminated" | "finished" | "error";
interface WsTwapHistory {
state: TwapState;
status: {
status: TwapStatus;
description: string;
};
time: number;
}
interface WsUserTwapHistory {
isSnapshot?: boolean;
user: string;
history: Array<WsTwapHistory>;
}
// Additional undocumented fields in WebData3 will be removed on a future update
interface WebData3 {
userState: {
agentAddress: string | null;
agentValidUntil: number | null;
serverTime: number;
cumLedger: number;
isVault: boolean;
user: string;
optOutOfSpotDusting?: boolean;
dexAbstractionEnabled?: boolean;
};
perpDexStates: Array<PerpDexState>;
}
interface PerpDexState {
totalVaultEquity: number;
perpsAtOpenInterestCap?: Array<string>;
leadingVaults?: Array<LeadingVault>;
}
interface LeadingVault {
address: string;
name: string;
}
interface ClearinghouseState {
dex: string;
user: string;
clearinghouseState: InnerClearinghouseState;
}
interface InnerClearinghouseState {
assetPositions: Array<AssetPosition>;
marginSummary: MarginSummary;
crossMarginSummary: MarginSummary;
crossMaintenanceMarginUsed: number;
withdrawable: number;
}
interface MarginSummary {
accountValue: number;
totalNtlPos: number;
totalRawUsd: number;
totalMarginUsed: number;
}
interface AssetPosition { type: "oneWay"; position: Position }
interface OpenOrders {
dex: string;
user: string;
orders: Array<Order>;
}
interface TwapStates {
dex: string;
user: string;
states: Array<[number, TwapState]>;
}
interface WsSpotState {
user: string;
spotState: SpotState;
}
interface SpotState {
balances: Array<UserBalance>;
}
interface UserBalance {
coin: string;
token?: number;
hold: string;
total: string;
entryNtl: string;
}
interface WsAllDexsClearinghouseState {
user: string;
clearinghouseStates: Array<[string, InnerClearinghouseState]>;
}
interface WsAllDexsAssetCtxs {
ctxs: Array<[string, Array<PerpsAssetCtx>]>;
}
type WsOutcomeMetaUpdates = [WsOutcomeMetaUpdate];
type WsOutcomeMetaUpdate =
| { outcomeCreated: OutcomeSpec }
| { outcomeSettled: number }
| { questionUpdated: QuestionSpec }
| { questionSettled: number };
type OutcomeSpec = {
outcome: number;
name: string;
description: string;
sideSpecs: [OutcomeSideSpec, OutcomeSideSpec];
};
type OutcomeSideSpec = {
name: string;
};
type QuestionSpec = {
question: number;
name: string;
description: string;
fallbackOutcome: number;
namedOutcomes: number[];
settledNamedOutcomes: number[];
};
type FFastAssetCtx = {
markPx?: number;
midPx?: number | null;
};
type WsFastAssetCtxs = Record<string, FFastAssetCtx>;
WsUserNonFundingLedgerUpdates
interface WsUserNonFundingLedgerUpdate {
time: number;
hash: string;
delta: WsLedgerUpdate;
}
type WsLedgerUpdate =
| WsDeposit
| WsWithdraw
| WsInternalTransfer
| WsSubAccountTransfer
| WsLedgerLiquidation
| WsVaultDelta
| WsVaultWithdrawal
| WsVaultLeaderCommission
| WsSpotTransfer
| WsAccountClassTransfer
| WsSpotGenesis
| WsRewardsClaim;
interface WsDeposit {
type: "deposit";
usdc: number;
}
interface WsWithdraw {
type: "withdraw";
usdc: number;
nonce: number;
fee: number;
}
interface WsInternalTransfer {
type: "internalTransfer";
usdc: number;
user: string;
destination: string;
fee: number;
}
interface WsSubAccountTransfer {
type: "subAccountTransfer";
usdc: number;
user: string;
destination: string;
}
interface WsLedgerLiquidation {
type: "liquidation";
// NOTE: for isolated positions this is the isolated account value
accountValue: number;
leverageType: "Cross" | "Isolated";
liquidatedPositions: Array<LiquidatedPosition>;
}
interface LiquidatedPosition {
coin: string;
szi: number;
}
interface WsVaultDelta {
type: "vaultCreate" | "vaultDeposit" | "vaultDistribution";
vault: string;
usdc: number;
}
interface WsVaultWithdrawal {
type: "vaultWithdraw";
vault: string;
user: string;
requestedUsd: number;
commission: number;
closingCost: number;
basis: number;
netWithdrawnUsd: number;
}
interface WsVaultLeaderCommission {
type: "vaultLeaderCommission";
user: string;
usdc: number;
}
interface WsSpotTransfer = {
type: "spotTransfer";
token: string;
amount: number;
usdcValue: number;
user: string;
destination: string;
fee: number;
}
interface WsAccountClassTransfer = {
type: "accountClassTransfer";
usdc: number;
toPerp: boolean;
}
interface WsSpotGenesis = {
type: "spotGenesis";
token: string;
amount: number;
}
interface WsRewardsClaim = {
type: "rewardsClaim";
amount: number;
}
Vui lòng lưu ý rằng các loại dữ liệu ở trên có định dạng TypeScript và cách sử dụng chúng tương ứng với các loại đăng ký tương ứng.
Ví dụ
Dưới đây là một vài ví dụ về đăng ký cho các nguồn cấp dữ liệu khác nhau bằng cách sử dụng tin nhắn đăng ký:
- Đăng ký tất cả các giá giữa:
json
{ "method": "subscribe", "subscription": { "type": "allMids" } }
2. Đăng ký thông báo cho một người dùng cụ thể:
json
{ "method": "subscribe", "subscription": { "type": "notification", "user": "<address>" } }
3. Đăng ký dữ liệu web cho một người dùng cụ thể:
json
{ "method": "subscribe", "subscription": { "type": "webData3", "user": "<address>" } }
4. Đăng ký cập nhật nến cho một đồng xu và khoảng thời gian cụ thể:
json
{ "method": "subscribe", "subscription": { "type": "candle", "coin": "<coin_symbol>", "interval": "<candle_interval>" } }
5. Đăng ký cập nhật sách đặt hàng cho một đồng xu cụ thể:
json
{ "method": "subscribe", "subscription": { "type": "l2Book", "coin": "<coin_symbol>" } }
6. Đăng ký giao dịch cho một đồng xu cụ thể:
json
{ "method": "subscribe", "subscription": { "type": "trades", "coin": "<coin_symbol>" } }
Đang không đăng ký từ nguồn cấp dữ liệu WebSocket
Để bỏ theo dõi một nguồn cấp dữ liệu cụ thể trên Hyperliquid WebSocket API, bạn cần gửi thông báo bỏ theo dõi theo định dạng sau:
{
"method": "unsubscribe",
"subscription": { ... }
}
Đối tượng subscription phải khớp với thông điệp đăng ký đã gởi khi đăng ký vào nguồn cấp dữ liệu. Tính năng này cho phép máy chủ xác định nguồn cấp dữ liệu riêng bạn muốn bỏ theo dõi. Bằng cách gởi thông điệp bỏ theo dõi này, bạn thông báo máy chủ ngừng gửi thêm bản cập nhật cho nguồn cấp dữ liệu đã ghi rõ.
Vui lòng ghi chú rằng việc thôi đăng ký từ một nguồn cấp dữ liệu nào đó sẽ không ảnh hưởng đến những đăng ký khác mà bạn có thể đang hoạt động vào lúc đó. Để thôi đăng ký nhiều nguồn cấp dữ liệu, bạn có khả năng gửi nhiều thư thôi đăng ký, mỗi thư có thông tin đăng ký thích hợp.