@yuants/vendor-hyperliquid 0.9.9 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/client.js +36 -2
- package/dist/api/client.js.map +1 -1
- package/dist/api/private-api.js +52 -41
- package/dist/api/private-api.js.map +1 -1
- package/dist/api/public-api.js +22 -11
- package/dist/api/public-api.js.map +1 -1
- package/dist/api/rate-limit.js +205 -0
- package/dist/api/rate-limit.js.map +1 -0
- package/dist/api/rate-limit.test.js +129 -0
- package/dist/api/rate-limit.test.js.map +1 -0
- package/dist/api/sign.js.map +1 -1
- package/dist/api/types.js.map +1 -1
- package/dist/index.js +0 -2
- package/dist/index.js.map +1 -1
- package/dist/services/accounts/spot.js.map +1 -1
- package/dist/services/exchange.js.map +1 -1
- package/dist/services/markets/quote.js.map +1 -1
- package/dist/services/ohlc-service.js.map +1 -1
- package/dist/services/orders/cancelOrder.js.map +1 -1
- package/dist/services/orders/listOrders.js +2 -2
- package/dist/services/orders/listOrders.js.map +1 -1
- package/dist/services/orders/modifyOrder.js.map +1 -1
- package/dist/services/quotes.js.map +1 -1
- package/dist/services/utils.js.map +1 -1
- package/lib/api/client.d.ts +1 -1
- package/lib/api/client.d.ts.map +1 -1
- package/lib/api/client.js +35 -1
- package/lib/api/client.js.map +1 -1
- package/lib/api/private-api.d.ts +67 -3
- package/lib/api/private-api.d.ts.map +1 -1
- package/lib/api/private-api.js +59 -43
- package/lib/api/private-api.js.map +1 -1
- package/lib/api/public-api.d.ts +61 -11
- package/lib/api/public-api.d.ts.map +1 -1
- package/lib/api/public-api.js +33 -12
- package/lib/api/public-api.js.map +1 -1
- package/lib/api/rate-limit.d.ts +21 -0
- package/lib/api/rate-limit.d.ts.map +1 -0
- package/lib/api/rate-limit.js +214 -0
- package/lib/api/rate-limit.js.map +1 -0
- package/lib/api/rate-limit.test.d.ts +2 -0
- package/lib/api/rate-limit.test.d.ts.map +1 -0
- package/lib/api/rate-limit.test.js +131 -0
- package/lib/api/rate-limit.test.js.map +1 -0
- package/lib/api/sign.js +5 -6
- package/lib/api/sign.js.map +1 -1
- package/lib/api/types.d.ts.map +1 -1
- package/lib/api/types.js.map +1 -1
- package/lib/index.d.ts +0 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +0 -2
- package/lib/index.js.map +1 -1
- package/lib/services/accounts/perp.d.ts.map +1 -1
- package/lib/services/accounts/spot.d.ts.map +1 -1
- package/lib/services/accounts/spot.js.map +1 -1
- package/lib/services/exchange.js.map +1 -1
- package/lib/services/markets/product.d.ts.map +1 -1
- package/lib/services/markets/quote.js.map +1 -1
- package/lib/services/ohlc-service.js.map +1 -1
- package/lib/services/orders/cancelOrder.d.ts.map +1 -1
- package/lib/services/orders/cancelOrder.js.map +1 -1
- package/lib/services/orders/listOrders.d.ts.map +1 -1
- package/lib/services/orders/listOrders.js +2 -2
- package/lib/services/orders/listOrders.js.map +1 -1
- package/lib/services/orders/modifyOrder.d.ts.map +1 -1
- package/lib/services/orders/modifyOrder.js.map +1 -1
- package/lib/services/orders/submitOrder.d.ts.map +1 -1
- package/lib/services/quotes.js.map +1 -1
- package/lib/services/utils.d.ts +2 -2
- package/lib/services/utils.d.ts.map +1 -1
- package/lib/services/utils.js.map +1 -1
- package/lib/tsdoc-metadata.json +1 -1
- package/package.json +48 -48
- package/temp/build/typescript/ts_HOjXkPSo.json +1 -0
- package/temp/package-deps.json +27 -27
- package/temp/test/jest/haste-map-1a3370ad4952cc1cbecf8fc42c6e9732-7b2990dcc44090c4eba2c52e971c544c-6a2a6e27ca3579da37751287ea19ab4c +0 -0
- package/temp/test/jest/perf-cache-1a3370ad4952cc1cbecf8fc42c6e9732-da39a3ee5e6b4b0d3255bfef95601890 +1 -0
- package/temp/vendor-hyperliquid.api.json +18 -2
- package/dist/services/markets/interest-rate.js +0 -60
- package/dist/services/markets/interest-rate.js.map +0 -1
- package/dist/services/markets/ohlc.js +0 -110
- package/dist/services/markets/ohlc.js.map +0 -1
- package/lib/services/markets/interest-rate.d.ts +0 -2
- package/lib/services/markets/interest-rate.d.ts.map +0 -1
- package/lib/services/markets/interest-rate.js +0 -62
- package/lib/services/markets/interest-rate.js.map +0 -1
- package/lib/services/markets/ohlc.d.ts +0 -2
- package/lib/services/markets/ohlc.d.ts.map +0 -1
- package/lib/services/markets/ohlc.js +0 -112
- package/lib/services/markets/ohlc.js.map +0 -1
package/lib/api/public-api.d.ts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
export declare const buildUserPerpetualsAccountSummaryRequestBody: (params: {
|
|
2
|
+
user: string;
|
|
3
|
+
}) => {
|
|
4
|
+
type: string;
|
|
5
|
+
};
|
|
1
6
|
/**
|
|
2
7
|
* Get user's perpetual account summary including positions, margin, and portfolio information
|
|
3
8
|
* API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-users-perpetuals-account-summary
|
|
@@ -48,6 +53,11 @@ export declare const getUserPerpetualsAccountSummary: (params: {
|
|
|
48
53
|
}[];
|
|
49
54
|
time: number;
|
|
50
55
|
}>;
|
|
56
|
+
export declare const buildPerpetualsMetaDataRequestBody: (params?: {
|
|
57
|
+
dex?: string;
|
|
58
|
+
}) => {
|
|
59
|
+
type: string;
|
|
60
|
+
};
|
|
51
61
|
/**
|
|
52
62
|
* Get perpetual market metadata including available assets, their specifications, and margin tiers
|
|
53
63
|
* API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-perpetuals-metadata
|
|
@@ -66,17 +76,17 @@ export declare const getPerpetualsMetaData: (params?: {
|
|
|
66
76
|
onlyIsolated?: boolean;
|
|
67
77
|
isDelisted?: boolean;
|
|
68
78
|
}[];
|
|
69
|
-
marginTables: [
|
|
70
|
-
|
|
71
|
-
{
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}[];
|
|
77
|
-
}
|
|
78
|
-
][];
|
|
79
|
+
marginTables: [number, {
|
|
80
|
+
description: string;
|
|
81
|
+
marginTiers: {
|
|
82
|
+
lowerBound: string;
|
|
83
|
+
maxLeverage: number;
|
|
84
|
+
}[];
|
|
85
|
+
}][];
|
|
79
86
|
}>;
|
|
87
|
+
export declare const buildSpotMetaDataRequestBody: () => {
|
|
88
|
+
type: string;
|
|
89
|
+
};
|
|
80
90
|
/**
|
|
81
91
|
* Get spot market metadata including available tokens and trading pairs
|
|
82
92
|
* API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/spot#retrieve-spot-metadata
|
|
@@ -101,6 +111,13 @@ export declare const getSpotMetaData: () => Promise<{
|
|
|
101
111
|
isCanonical: boolean;
|
|
102
112
|
}[];
|
|
103
113
|
}>;
|
|
114
|
+
export declare const buildUserFundingHistoryRequestBody: (params: {
|
|
115
|
+
user: string;
|
|
116
|
+
startTime?: number;
|
|
117
|
+
endTime?: number;
|
|
118
|
+
}) => {
|
|
119
|
+
type: string;
|
|
120
|
+
};
|
|
104
121
|
/**
|
|
105
122
|
* Get user's funding rate payment history for perpetual positions
|
|
106
123
|
* API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-a-users-funding-history-or-non-funding-ledger-updates
|
|
@@ -126,10 +143,15 @@ export declare const getUserFundingHistory: (params: {
|
|
|
126
143
|
fundingRate: string;
|
|
127
144
|
};
|
|
128
145
|
}[]>;
|
|
146
|
+
export declare const buildUserTokenBalancesRequestBody: (params: {
|
|
147
|
+
user: string;
|
|
148
|
+
}) => {
|
|
149
|
+
type: string;
|
|
150
|
+
};
|
|
129
151
|
/**
|
|
130
152
|
* Get user's token balances for both spot and perpetual accounts
|
|
131
153
|
* API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/spot#retrieve-a-users-token-balances
|
|
132
|
-
* API Endpoint: POST /info (type:
|
|
154
|
+
* API Endpoint: POST /info (type: spotClearinghouseState)
|
|
133
155
|
* @param params - Query parameters
|
|
134
156
|
* @param params.user - User's wallet address
|
|
135
157
|
* @returns Promise resolving to array of token balances with held and total amounts
|
|
@@ -145,6 +167,11 @@ export declare const getUserTokenBalances: (params: {
|
|
|
145
167
|
entryNtl: string;
|
|
146
168
|
}[];
|
|
147
169
|
}>;
|
|
170
|
+
export declare const buildUserOpenOrdersRequestBody: (params: {
|
|
171
|
+
user: string;
|
|
172
|
+
}) => {
|
|
173
|
+
type: string;
|
|
174
|
+
};
|
|
148
175
|
/**
|
|
149
176
|
* Get user's currently open orders across all markets
|
|
150
177
|
* API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint#open-orders
|
|
@@ -163,6 +190,13 @@ export declare const getUserOpenOrders: (params: {
|
|
|
163
190
|
sz: string;
|
|
164
191
|
timestamp: number;
|
|
165
192
|
}[]>;
|
|
193
|
+
export declare const buildHistoricalFundingRatesRequestBody: (params: {
|
|
194
|
+
coin: string;
|
|
195
|
+
startTime: number;
|
|
196
|
+
endTime?: number;
|
|
197
|
+
}) => {
|
|
198
|
+
type: string;
|
|
199
|
+
};
|
|
166
200
|
/**
|
|
167
201
|
* Get historical funding rates for a specific perpetual asset
|
|
168
202
|
* API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-historical-funding-rates
|
|
@@ -183,6 +217,9 @@ export declare const getHistoricalFundingRates: (params: {
|
|
|
183
217
|
premium: string;
|
|
184
218
|
time: number;
|
|
185
219
|
}[]>;
|
|
220
|
+
export declare const buildAllMidsRequestBody: () => {
|
|
221
|
+
type: string;
|
|
222
|
+
};
|
|
186
223
|
/**
|
|
187
224
|
* Get current mid prices for all tradable assets
|
|
188
225
|
* API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint#retrieve-mids-for-all-coins
|
|
@@ -190,6 +227,9 @@ export declare const getHistoricalFundingRates: (params: {
|
|
|
190
227
|
* @returns Promise resolving to record of asset symbols mapped to their mid prices
|
|
191
228
|
*/
|
|
192
229
|
export declare const getAllMids: () => Promise<Record<string, string>>;
|
|
230
|
+
export declare const buildMetaAndAssetCtxsRequestBody: () => {
|
|
231
|
+
type: string;
|
|
232
|
+
};
|
|
193
233
|
/**
|
|
194
234
|
* Get metadata and asset contexts (prices, funding, OI) for all perpetuals
|
|
195
235
|
* API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-perpetuals-asset-contexts-includes-mark-price-current-funding-open-interest-etc.
|
|
@@ -215,6 +255,16 @@ export declare const getMetaAndAssetCtxs: () => Promise<[{
|
|
|
215
255
|
premium: string;
|
|
216
256
|
prevDayPx: string;
|
|
217
257
|
}[]]>;
|
|
258
|
+
export declare const buildCandleSnapshotRequestBody: (params: {
|
|
259
|
+
req: {
|
|
260
|
+
coin: string;
|
|
261
|
+
interval: string;
|
|
262
|
+
startTime: number;
|
|
263
|
+
endTime: number;
|
|
264
|
+
};
|
|
265
|
+
}) => {
|
|
266
|
+
type: string;
|
|
267
|
+
};
|
|
218
268
|
/**
|
|
219
269
|
* Get candle/K-line data for a specific asset and time range
|
|
220
270
|
* API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint#candle-snapshot
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"public-api.d.ts","sourceRoot":"","sources":["../../src/api/public-api.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"public-api.d.ts","sourceRoot":"","sources":["../../src/api/public-api.ts"],"names":[],"mappings":"AAOA,eAAO,MAAM,4CAA4C,GAAI,QAAQ;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE;;CACjC,CAAC;AAErD;;;;;;;GAOG;AACH,eAAO,MAAM,+BAA+B,GAAI,QAAQ;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE;mBAErD;QACb,YAAY,EAAE,MAAM,CAAC;QACrB,WAAW,EAAE,MAAM,CAAC;QACpB,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E,OAAO,CAAC;QACvB,UAAU,CAAC,EAAE,OAAO,CAAC;KACtB,EAAE;;eAGQ,MAAM;aACR,MAAM;eACJ,CAAC,MAAM,EAAE,MAAM,CAAC;YACnB,MAAM;WACP,MAAM;kBACC,MAAM;cACV,MAAM;aACP,MAAM;eACJ,MAAM;KAG8B,CAAC;AAExD,eAAO,MAAM,8BAA8B,GAAI,QAAQ;IACrD,GAAG,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;CAC7E;;CAAmD,CAAC;AAErD;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,iBAAiB,GAAI,QAAQ;IACxC,GAAG,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;CAC7E;OAGQ,MAAM;OACN,MAAM;OACN,MAAM;OACN,MAAM;OACN,MAAM;OACN,MAAM;OACN,MAAM;OACN,MAAM;OACN,MAAM;OACN,MAAM;IAE4C,CAAC"}
|
package/lib/api/public-api.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getCandleSnapshot = exports.getMetaAndAssetCtxs = exports.getAllMids = exports.getHistoricalFundingRates = exports.getUserOpenOrders = exports.getUserTokenBalances = exports.getUserFundingHistory = exports.getSpotMetaData = exports.getPerpetualsMetaData = exports.getUserPerpetualsAccountSummary = void 0;
|
|
3
|
+
exports.getCandleSnapshot = exports.buildCandleSnapshotRequestBody = exports.getMetaAndAssetCtxs = exports.buildMetaAndAssetCtxsRequestBody = exports.getAllMids = exports.buildAllMidsRequestBody = exports.getHistoricalFundingRates = exports.buildHistoricalFundingRatesRequestBody = exports.getUserOpenOrders = exports.buildUserOpenOrdersRequestBody = exports.getUserTokenBalances = exports.buildUserTokenBalancesRequestBody = exports.getUserFundingHistory = exports.buildUserFundingHistoryRequestBody = exports.getSpotMetaData = exports.buildSpotMetaDataRequestBody = exports.getPerpetualsMetaData = exports.buildPerpetualsMetaDataRequestBody = exports.getUserPerpetualsAccountSummary = exports.buildUserPerpetualsAccountSummaryRequestBody = void 0;
|
|
4
4
|
const client_1 = require("./client");
|
|
5
|
+
const buildInfoRequestBody = (type, params) => (Object.assign(Object.assign({}, (params !== null && params !== void 0 ? params : {})), { type }));
|
|
6
|
+
const buildUserPerpetualsAccountSummaryRequestBody = (params) => buildInfoRequestBody('clearinghouseState', params);
|
|
7
|
+
exports.buildUserPerpetualsAccountSummaryRequestBody = buildUserPerpetualsAccountSummaryRequestBody;
|
|
5
8
|
/**
|
|
6
9
|
* Get user's perpetual account summary including positions, margin, and portfolio information
|
|
7
10
|
* API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-users-perpetuals-account-summary
|
|
@@ -10,8 +13,10 @@ const client_1 = require("./client");
|
|
|
10
13
|
* @param params.user - User's wallet address
|
|
11
14
|
* @returns Promise resolving to account summary with margin information, positions, and portfolio details
|
|
12
15
|
*/
|
|
13
|
-
const getUserPerpetualsAccountSummary = (params) => (0, client_1.request)('POST', 'info',
|
|
16
|
+
const getUserPerpetualsAccountSummary = (params) => (0, client_1.request)('POST', 'info', (0, exports.buildUserPerpetualsAccountSummaryRequestBody)(params));
|
|
14
17
|
exports.getUserPerpetualsAccountSummary = getUserPerpetualsAccountSummary;
|
|
18
|
+
const buildPerpetualsMetaDataRequestBody = (params) => buildInfoRequestBody('meta', params !== null && params !== void 0 ? params : {});
|
|
19
|
+
exports.buildPerpetualsMetaDataRequestBody = buildPerpetualsMetaDataRequestBody;
|
|
15
20
|
/**
|
|
16
21
|
* Get perpetual market metadata including available assets, their specifications, and margin tiers
|
|
17
22
|
* API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-perpetuals-metadata
|
|
@@ -20,16 +25,20 @@ exports.getUserPerpetualsAccountSummary = getUserPerpetualsAccountSummary;
|
|
|
20
25
|
* @param params.dex - Optional DEX identifier
|
|
21
26
|
* @returns Promise resolving to universe of perpetual assets and margin configuration
|
|
22
27
|
*/
|
|
23
|
-
const getPerpetualsMetaData = (params) => (0, client_1.request)('POST', 'info',
|
|
28
|
+
const getPerpetualsMetaData = (params) => (0, client_1.request)('POST', 'info', (0, exports.buildPerpetualsMetaDataRequestBody)(params));
|
|
24
29
|
exports.getPerpetualsMetaData = getPerpetualsMetaData;
|
|
30
|
+
const buildSpotMetaDataRequestBody = () => buildInfoRequestBody('spotMeta');
|
|
31
|
+
exports.buildSpotMetaDataRequestBody = buildSpotMetaDataRequestBody;
|
|
25
32
|
/**
|
|
26
33
|
* Get spot market metadata including available tokens and trading pairs
|
|
27
34
|
* API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/spot#retrieve-spot-metadata
|
|
28
35
|
* API Endpoint: POST /info (type: spotMeta)
|
|
29
36
|
* @returns Promise resolving to spot universe with token information and pair configurations
|
|
30
37
|
*/
|
|
31
|
-
const getSpotMetaData = () => (0, client_1.request)('POST', 'info',
|
|
38
|
+
const getSpotMetaData = () => (0, client_1.request)('POST', 'info', (0, exports.buildSpotMetaDataRequestBody)());
|
|
32
39
|
exports.getSpotMetaData = getSpotMetaData;
|
|
40
|
+
const buildUserFundingHistoryRequestBody = (params) => buildInfoRequestBody('fundingHistory', params);
|
|
41
|
+
exports.buildUserFundingHistoryRequestBody = buildUserFundingHistoryRequestBody;
|
|
33
42
|
/**
|
|
34
43
|
* Get user's funding rate payment history for perpetual positions
|
|
35
44
|
* API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-a-users-funding-history-or-non-funding-ledger-updates
|
|
@@ -40,18 +49,22 @@ exports.getSpotMetaData = getSpotMetaData;
|
|
|
40
49
|
* @param params.endTime - Optional end timestamp in milliseconds
|
|
41
50
|
* @returns Promise resolving to array of funding payment records with amounts and rates
|
|
42
51
|
*/
|
|
43
|
-
const getUserFundingHistory = (params) => (0, client_1.request)('POST', 'info',
|
|
52
|
+
const getUserFundingHistory = (params) => (0, client_1.request)('POST', 'info', (0, exports.buildUserFundingHistoryRequestBody)(params));
|
|
44
53
|
exports.getUserFundingHistory = getUserFundingHistory;
|
|
54
|
+
const buildUserTokenBalancesRequestBody = (params) => buildInfoRequestBody('spotClearinghouseState', params);
|
|
55
|
+
exports.buildUserTokenBalancesRequestBody = buildUserTokenBalancesRequestBody;
|
|
45
56
|
/**
|
|
46
57
|
* Get user's token balances for both spot and perpetual accounts
|
|
47
58
|
* API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/spot#retrieve-a-users-token-balances
|
|
48
|
-
* API Endpoint: POST /info (type:
|
|
59
|
+
* API Endpoint: POST /info (type: spotClearinghouseState)
|
|
49
60
|
* @param params - Query parameters
|
|
50
61
|
* @param params.user - User's wallet address
|
|
51
62
|
* @returns Promise resolving to array of token balances with held and total amounts
|
|
52
63
|
*/
|
|
53
|
-
const getUserTokenBalances = (params) => (0, client_1.request)('POST', 'info',
|
|
64
|
+
const getUserTokenBalances = (params) => (0, client_1.request)('POST', 'info', (0, exports.buildUserTokenBalancesRequestBody)(params));
|
|
54
65
|
exports.getUserTokenBalances = getUserTokenBalances;
|
|
66
|
+
const buildUserOpenOrdersRequestBody = (params) => buildInfoRequestBody('openOrders', params);
|
|
67
|
+
exports.buildUserOpenOrdersRequestBody = buildUserOpenOrdersRequestBody;
|
|
55
68
|
/**
|
|
56
69
|
* Get user's currently open orders across all markets
|
|
57
70
|
* API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint#open-orders
|
|
@@ -60,8 +73,10 @@ exports.getUserTokenBalances = getUserTokenBalances;
|
|
|
60
73
|
* @param params.user - User's wallet address
|
|
61
74
|
* @returns Promise resolving to array of open order details with prices, sizes, and timestamps
|
|
62
75
|
*/
|
|
63
|
-
const getUserOpenOrders = (params) => (0, client_1.request)('POST', 'info',
|
|
76
|
+
const getUserOpenOrders = (params) => (0, client_1.request)('POST', 'info', (0, exports.buildUserOpenOrdersRequestBody)(params));
|
|
64
77
|
exports.getUserOpenOrders = getUserOpenOrders;
|
|
78
|
+
const buildHistoricalFundingRatesRequestBody = (params) => buildInfoRequestBody('fundingHistory', params);
|
|
79
|
+
exports.buildHistoricalFundingRatesRequestBody = buildHistoricalFundingRatesRequestBody;
|
|
65
80
|
/**
|
|
66
81
|
* Get historical funding rates for a specific perpetual asset
|
|
67
82
|
* API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-historical-funding-rates
|
|
@@ -72,24 +87,30 @@ exports.getUserOpenOrders = getUserOpenOrders;
|
|
|
72
87
|
* @param params.endTime - Optional end timestamp in milliseconds
|
|
73
88
|
* @returns Promise resolving to array of historical funding rate records
|
|
74
89
|
*/
|
|
75
|
-
const getHistoricalFundingRates = (params) => (0, client_1.request)('POST', 'info',
|
|
90
|
+
const getHistoricalFundingRates = (params) => (0, client_1.request)('POST', 'info', (0, exports.buildHistoricalFundingRatesRequestBody)(params));
|
|
76
91
|
exports.getHistoricalFundingRates = getHistoricalFundingRates;
|
|
92
|
+
const buildAllMidsRequestBody = () => buildInfoRequestBody('allMids');
|
|
93
|
+
exports.buildAllMidsRequestBody = buildAllMidsRequestBody;
|
|
77
94
|
/**
|
|
78
95
|
* Get current mid prices for all tradable assets
|
|
79
96
|
* API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint#retrieve-mids-for-all-coins
|
|
80
97
|
* API Endpoint: POST /info (type: allMids)
|
|
81
98
|
* @returns Promise resolving to record of asset symbols mapped to their mid prices
|
|
82
99
|
*/
|
|
83
|
-
const getAllMids = () => (0, client_1.request)('POST', 'info',
|
|
100
|
+
const getAllMids = () => (0, client_1.request)('POST', 'info', (0, exports.buildAllMidsRequestBody)());
|
|
84
101
|
exports.getAllMids = getAllMids;
|
|
102
|
+
const buildMetaAndAssetCtxsRequestBody = () => buildInfoRequestBody('metaAndAssetCtxs');
|
|
103
|
+
exports.buildMetaAndAssetCtxsRequestBody = buildMetaAndAssetCtxsRequestBody;
|
|
85
104
|
/**
|
|
86
105
|
* Get metadata and asset contexts (prices, funding, OI) for all perpetuals
|
|
87
106
|
* API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-perpetuals-asset-contexts-includes-mark-price-current-funding-open-interest-etc.
|
|
88
107
|
* API Endpoint: POST /info (type: metaAndAssetCtxs)
|
|
89
108
|
* @returns Promise resolving to metadata and current asset contexts
|
|
90
109
|
*/
|
|
91
|
-
const getMetaAndAssetCtxs = () => (0, client_1.request)('POST', 'info',
|
|
110
|
+
const getMetaAndAssetCtxs = () => (0, client_1.request)('POST', 'info', (0, exports.buildMetaAndAssetCtxsRequestBody)());
|
|
92
111
|
exports.getMetaAndAssetCtxs = getMetaAndAssetCtxs;
|
|
112
|
+
const buildCandleSnapshotRequestBody = (params) => buildInfoRequestBody('candleSnapshot', params);
|
|
113
|
+
exports.buildCandleSnapshotRequestBody = buildCandleSnapshotRequestBody;
|
|
93
114
|
/**
|
|
94
115
|
* Get candle/K-line data for a specific asset and time range
|
|
95
116
|
* API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint#candle-snapshot
|
|
@@ -102,6 +123,6 @@ exports.getMetaAndAssetCtxs = getMetaAndAssetCtxs;
|
|
|
102
123
|
* @param params.req.endTime - End timestamp in milliseconds
|
|
103
124
|
* @returns Promise resolving to candle data with OHLCV information
|
|
104
125
|
*/
|
|
105
|
-
const getCandleSnapshot = (params) => (0, client_1.request)('POST', 'info',
|
|
126
|
+
const getCandleSnapshot = (params) => (0, client_1.request)('POST', 'info', (0, exports.buildCandleSnapshotRequestBody)(params));
|
|
106
127
|
exports.getCandleSnapshot = getCandleSnapshot;
|
|
107
128
|
//# sourceMappingURL=public-api.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"public-api.js","sourceRoot":"","sources":["../../src/api/public-api.ts"],"names":[],"mappings":";;;AAAA,qCAAmC;AAEnC;;;;;;;GAOG;AACI,MAAM,+BAA+B,GAAG,CAAC,MAAwB,EAAE,EAAE,CAC1E,IAAA,gBAAO,EAoCJ,MAAM,EAAE,MAAM,kCAAO,MAAM,KAAE,IAAI,EAAE,oBAAoB,IAAG,CAAC;AArCnD,QAAA,+BAA+B,mCAqCoB;AAEhE;;;;;;;GAOG;AACI,MAAM,qBAAqB,GAAG,CAAC,MAAyB,EAAE,EAAE,CACjE,IAAA,gBAAO,EAkBJ,MAAM,EAAE,MAAM,kCAAO,CAAC,MAAM,aAAN,MAAM,cAAN,MAAM,GAAI,EAAE,CAAC,KAAE,IAAI,EAAE,MAAM,IAAG,CAAC;AAnB7C,QAAA,qBAAqB,yBAmBwB;AAE1D;;;;;GAKG;AACI,MAAM,eAAe,GAAG,GAAG,EAAE,CAClC,IAAA,gBAAO,EAiBJ,MAAM,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC,CAAC;AAlB9B,QAAA,eAAe,mBAkBe;AAE3C;;;;;;;;;GASG;AACI,MAAM,qBAAqB,GAAG,CAAC,MAA8D,EAAE,EAAE,CACtG,IAAA,gBAAO,EAML,MAAM,EAAE,MAAM,kCAAO,MAAM,KAAE,IAAI,EAAE,gBAAgB,IAAG,CAAC;AAP9C,QAAA,qBAAqB,yBAOyB;AAE3D;;;;;;;GAOG;AACI,MAAM,oBAAoB,GAAG,CAAC,MAAwB,EAAE,EAAE,CAC/D,IAAA,gBAAO,EACL,MAAM,EACN,MAAM,kCAED,MAAM,KACT,IAAI,EAAE,wBAAwB,IAEjC,CAAC;AARS,QAAA,oBAAoB,wBAQ7B;AAEJ;;;;;;;GAOG;AACI,MAAM,iBAAiB,GAAG,CAAC,MAAwB,EAAE,EAAE,CAC5D,IAAA,gBAAO,EACL,MAAM,EACN,MAAM,kCAED,MAAM,KACT,IAAI,EAAE,YAAY,IAErB,CAAC;AARS,QAAA,iBAAiB,qBAQ1B;AAEJ;;;;;;;;;GASG;AACI,MAAM,yBAAyB,GAAG,CAAC,MAA6D,EAAE,EAAE,CACzG,IAAA,gBAAO,EAAyE,MAAM,EAAE,MAAM,kCACzF,MAAM,KACT,IAAI,EAAE,gBAAgB,IACtB,CAAC;AAJQ,QAAA,yBAAyB,6BAIjC;AAEL;;;;;GAKG;AACI,MAAM,UAAU,GAAG,GAAG,EAAE,CAAC,IAAA,gBAAO,EAAyB,MAAM,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC,CAAC;AAAxF,QAAA,UAAU,cAA8E;AAErG;;;;;GAKG;AACI,MAAM,mBAAmB,GAAG,GAAG,EAAE,CACtC,IAAA,gBAAO,EAuBL,MAAM,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,kBAAkB,EAAE,CAAC,CAAC;AAxBrC,QAAA,mBAAmB,uBAwBkB;AAElD;;;;;;;;;;;GAWG;AACI,MAAM,iBAAiB,GAAG,CAAC,MAEjC,EAAE,EAAE,CACH,IAAA,gBAAO,EAaL,MAAM,EAAE,MAAM,kBAAI,IAAI,EAAE,gBAAgB,IAAK,MAAM,EAAG,CAAC;AAhB9C,QAAA,iBAAiB,qBAgB6B","sourcesContent":["import { request } from './client';\n\n/**\n * Get user's perpetual account summary including positions, margin, and portfolio information\n * API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-users-perpetuals-account-summary\n * API Endpoint: POST /info (type: clearinghouseState)\n * @param params - Query parameters\n * @param params.user - User's wallet address\n * @returns Promise resolving to account summary with margin information, positions, and portfolio details\n */\nexport const getUserPerpetualsAccountSummary = (params: { user: string }) =>\n request<{\n marginSummary: {\n accountValue: string;\n totalNtlPos: string;\n totalRawUsd: string;\n totalMarginUsed: string;\n };\n crossMarginSummary: {\n accountValue: string;\n totalNtlPos: string;\n totalRawUsd: string;\n totalMarginUsed: string;\n };\n crossMaintenanceMarginUsed: string;\n withdrawable: string;\n assetPositions: {\n type: string;\n position: {\n coin: string;\n szi: string;\n leverage: { type: string; value: number };\n entryPx: string;\n positionValue: string;\n unrealizedPnl: string;\n returnOnEquity: string;\n liquidationPx: string;\n marginUsed: string;\n maxLeverage: number;\n cumFunding: {\n allTime: string;\n sinceOpen: string;\n sinceChange: string;\n };\n };\n }[];\n time: number;\n }>('POST', 'info', { ...params, type: 'clearinghouseState' });\n\n/**\n * Get perpetual market metadata including available assets, their specifications, and margin tiers\n * API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-perpetuals-metadata\n * API Endpoint: POST /info (type: meta)\n * @param params - Optional query parameters\n * @param params.dex - Optional DEX identifier\n * @returns Promise resolving to universe of perpetual assets and margin configuration\n */\nexport const getPerpetualsMetaData = (params?: { dex?: string }) =>\n request<{\n universe: {\n name: string;\n szDecimals: number;\n maxLeverage: number;\n onlyIsolated?: boolean;\n isDelisted?: boolean;\n }[];\n marginTables: [\n number,\n {\n description: string;\n marginTiers: {\n lowerBound: string;\n maxLeverage: number;\n }[];\n },\n ][];\n }>('POST', 'info', { ...(params ?? {}), type: 'meta' });\n\n/**\n * Get spot market metadata including available tokens and trading pairs\n * API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/spot#retrieve-spot-metadata\n * API Endpoint: POST /info (type: spotMeta)\n * @returns Promise resolving to spot universe with token information and pair configurations\n */\nexport const getSpotMetaData = () =>\n request<{\n tokens: {\n name: string;\n szDecimals: number;\n weiDecimals: number;\n index: number;\n tokenId: string;\n isCanonical: boolean;\n evmContract: null;\n fullName: null;\n }[];\n universe: {\n name: string;\n tokens: number[];\n index: number;\n isCanonical: boolean;\n }[];\n }>('POST', 'info', { type: 'spotMeta' });\n\n/**\n * Get user's funding rate payment history for perpetual positions\n * API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-a-users-funding-history-or-non-funding-ledger-updates\n * API Endpoint: POST /info (type: fundingHistory)\n * @param params - Query parameters\n * @param params.user - User's wallet address\n * @param params.startTime - Optional start timestamp in milliseconds\n * @param params.endTime - Optional end timestamp in milliseconds\n * @returns Promise resolving to array of funding payment records with amounts and rates\n */\nexport const getUserFundingHistory = (params: { user: string; startTime?: number; endTime?: number }) =>\n request<\n {\n time: number;\n hash: string;\n delta: { type: string; coin: string; usdc: string; szi: string; fundingRate: string };\n }[]\n >('POST', 'info', { ...params, type: 'fundingHistory' });\n\n/**\n * Get user's token balances for both spot and perpetual accounts\n * API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/spot#retrieve-a-users-token-balances\n * API Endpoint: POST /info (type: tokenBalances)\n * @param params - Query parameters\n * @param params.user - User's wallet address\n * @returns Promise resolving to array of token balances with held and total amounts\n */\nexport const getUserTokenBalances = (params: { user: string }) =>\n request<{ balances: { coin: string; token: number; hold: string; total: string; entryNtl: string }[] }>(\n 'POST',\n 'info',\n {\n ...params,\n type: 'spotClearinghouseState',\n },\n );\n\n/**\n * Get user's currently open orders across all markets\n * API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint#open-orders\n * API Endpoint: POST /info (type: openOrders)\n * @param params - Query parameters\n * @param params.user - User's wallet address\n * @returns Promise resolving to array of open order details with prices, sizes, and timestamps\n */\nexport const getUserOpenOrders = (params: { user: string }) =>\n request<{ coin: string; limitPx: string; oid: number; side: string; sz: string; timestamp: number }[]>(\n 'POST',\n 'info',\n {\n ...params,\n type: 'openOrders',\n },\n );\n\n/**\n * Get historical funding rates for a specific perpetual asset\n * API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-historical-funding-rates\n * API Endpoint: POST /info (type: fundingHistory)\n * @param params - Query parameters\n * @param params.coin - Asset symbol (e.g., \"BTC\", \"ETH\")\n * @param params.startTime - Start timestamp in milliseconds\n * @param params.endTime - Optional end timestamp in milliseconds\n * @returns Promise resolving to array of historical funding rate records\n */\nexport const getHistoricalFundingRates = (params: { coin: string; startTime: number; endTime?: number }) =>\n request<{ coin: string; fundingRate: string; premium: string; time: number }[]>('POST', 'info', {\n ...params,\n type: 'fundingHistory',\n });\n\n/**\n * Get current mid prices for all tradable assets\n * API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint#retrieve-mids-for-all-coins\n * API Endpoint: POST /info (type: allMids)\n * @returns Promise resolving to record of asset symbols mapped to their mid prices\n */\nexport const getAllMids = () => request<Record<string, string>>('POST', 'info', { type: 'allMids' });\n\n/**\n * Get metadata and asset contexts (prices, funding, OI) for all perpetuals\n * API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-perpetuals-asset-contexts-includes-mark-price-current-funding-open-interest-etc.\n * API Endpoint: POST /info (type: metaAndAssetCtxs)\n * @returns Promise resolving to metadata and current asset contexts\n */\nexport const getMetaAndAssetCtxs = () =>\n request<\n [\n {\n universe: {\n name: string;\n szDecimals: number;\n maxLeverage: number;\n onlyIsolated?: boolean;\n isDelisted?: boolean;\n }[];\n },\n {\n dayNtlVlm: string;\n funding: string;\n impactPxs: [string, string];\n markPx: string;\n midPx: string;\n openInterest: string;\n oraclePx: string;\n premium: string;\n prevDayPx: string;\n }[],\n ]\n >('POST', 'info', { type: 'metaAndAssetCtxs' });\n\n/**\n * Get candle/K-line data for a specific asset and time range\n * API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint#candle-snapshot\n * API Endpoint: POST /info (type: candleSnapshot)\n * @param params - Query parameters\n * @param params.req - Request object with candle parameters\n * @param params.req.coin - Asset symbol\n * @param params.req.interval - Time interval (e.g., \"1m\", \"5m\", \"1h\", \"1d\")\n * @param params.req.startTime - Start timestamp in milliseconds\n * @param params.req.endTime - End timestamp in milliseconds\n * @returns Promise resolving to candle data with OHLCV information\n */\nexport const getCandleSnapshot = (params: {\n req: { coin: string; interval: string; startTime: number; endTime: number };\n}) =>\n request<\n {\n T: number;\n c: string;\n h: string;\n i: string;\n l: string;\n o: string;\n n: number;\n s: string;\n t: number;\n v: string;\n }[]\n >('POST', 'info', { type: 'candleSnapshot', ...params });\n"]}
|
|
1
|
+
{"version":3,"file":"public-api.js","sourceRoot":"","sources":["../../src/api/public-api.ts"],"names":[],"mappings":";;;AAAA,qCAAmC;AAEnC,MAAM,oBAAoB,GAAG,CAAoC,IAAY,EAAE,MAAU,EAAE,EAAE,CAAC,iCACzF,CAAC,MAAM,aAAN,MAAM,cAAN,MAAM,GAAI,EAAE,CAAC,KACjB,IAAI,IACJ,CAAC;AAEI,MAAM,4CAA4C,GAAG,CAAC,MAAwB,EAAE,EAAE,CACvF,oBAAoB,CAAC,oBAAoB,EAAE,MAAM,CAAC,CAAC;AADxC,QAAA,4CAA4C,gDACJ;AAErD;;;;;;;GAOG;AACI,MAAM,+BAA+B,GAAG,CAAC,MAAwB,EAAE,EAAE,CAC1E,IAAA,gBAAO,EAoCJ,MAAM,EAAE,MAAM,EAAE,IAAA,oDAA4C,EAAC,MAAM,CAAC,CAAC,CAAC;AArC9D,QAAA,+BAA+B,mCAqC+B;AAEpE,MAAM,kCAAkC,GAAG,CAAC,MAAyB,EAAE,EAAE,CAC9E,oBAAoB,CAAC,MAAM,EAAE,MAAM,aAAN,MAAM,cAAN,MAAM,GAAI,EAAE,CAAC,CAAC;AADhC,QAAA,kCAAkC,sCACF;AAE7C;;;;;;;GAOG;AACI,MAAM,qBAAqB,GAAG,CAAC,MAAyB,EAAE,EAAE,CACjE,IAAA,gBAAO,EAkBJ,MAAM,EAAE,MAAM,EAAE,IAAA,0CAAkC,EAAC,MAAM,CA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,CAAC;AAhB/C,QAAA,iBAAiB,qBAgB8B","sourcesContent":["import { request } from './client';\n\nconst buildInfoRequestBody = <P extends Record<string, unknown>>(type: string, params?: P) => ({\n ...(params ?? {}),\n type,\n});\n\nexport const buildUserPerpetualsAccountSummaryRequestBody = (params: { user: string }) =>\n buildInfoRequestBody('clearinghouseState', params);\n\n/**\n * Get user's perpetual account summary including positions, margin, and portfolio information\n * API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-users-perpetuals-account-summary\n * API Endpoint: POST /info (type: clearinghouseState)\n * @param params - Query parameters\n * @param params.user - User's wallet address\n * @returns Promise resolving to account summary with margin information, positions, and portfolio details\n */\nexport const getUserPerpetualsAccountSummary = (params: { user: string }) =>\n request<{\n marginSummary: {\n accountValue: string;\n totalNtlPos: string;\n totalRawUsd: string;\n totalMarginUsed: string;\n };\n crossMarginSummary: {\n accountValue: string;\n totalNtlPos: string;\n totalRawUsd: string;\n totalMarginUsed: string;\n };\n crossMaintenanceMarginUsed: string;\n withdrawable: string;\n assetPositions: {\n type: string;\n position: {\n coin: string;\n szi: string;\n leverage: { type: string; value: number };\n entryPx: string;\n positionValue: string;\n unrealizedPnl: string;\n returnOnEquity: string;\n liquidationPx: string;\n marginUsed: string;\n maxLeverage: number;\n cumFunding: {\n allTime: string;\n sinceOpen: string;\n sinceChange: string;\n };\n };\n }[];\n time: number;\n }>('POST', 'info', buildUserPerpetualsAccountSummaryRequestBody(params));\n\nexport const buildPerpetualsMetaDataRequestBody = (params?: { dex?: string }) =>\n buildInfoRequestBody('meta', params ?? {});\n\n/**\n * Get perpetual market metadata including available assets, their specifications, and margin tiers\n * API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-perpetuals-metadata\n * API Endpoint: POST /info (type: meta)\n * @param params - Optional query parameters\n * @param params.dex - Optional DEX identifier\n * @returns Promise resolving to universe of perpetual assets and margin configuration\n */\nexport const getPerpetualsMetaData = (params?: { dex?: string }) =>\n request<{\n universe: {\n name: string;\n szDecimals: number;\n maxLeverage: number;\n onlyIsolated?: boolean;\n isDelisted?: boolean;\n }[];\n marginTables: [\n number,\n {\n description: string;\n marginTiers: {\n lowerBound: string;\n maxLeverage: number;\n }[];\n },\n ][];\n }>('POST', 'info', buildPerpetualsMetaDataRequestBody(params));\n\nexport const buildSpotMetaDataRequestBody = () => buildInfoRequestBody('spotMeta');\n\n/**\n * Get spot market metadata including available tokens and trading pairs\n * API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/spot#retrieve-spot-metadata\n * API Endpoint: POST /info (type: spotMeta)\n * @returns Promise resolving to spot universe with token information and pair configurations\n */\nexport const getSpotMetaData = () =>\n request<{\n tokens: {\n name: string;\n szDecimals: number;\n weiDecimals: number;\n index: number;\n tokenId: string;\n isCanonical: boolean;\n evmContract: null;\n fullName: null;\n }[];\n universe: {\n name: string;\n tokens: number[];\n index: number;\n isCanonical: boolean;\n }[];\n }>('POST', 'info', buildSpotMetaDataRequestBody());\n\nexport const buildUserFundingHistoryRequestBody = (params: {\n user: string;\n startTime?: number;\n endTime?: number;\n}) => buildInfoRequestBody('fundingHistory', params);\n\n/**\n * Get user's funding rate payment history for perpetual positions\n * API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-a-users-funding-history-or-non-funding-ledger-updates\n * API Endpoint: POST /info (type: fundingHistory)\n * @param params - Query parameters\n * @param params.user - User's wallet address\n * @param params.startTime - Optional start timestamp in milliseconds\n * @param params.endTime - Optional end timestamp in milliseconds\n * @returns Promise resolving to array of funding payment records with amounts and rates\n */\nexport const getUserFundingHistory = (params: { user: string; startTime?: number; endTime?: number }) =>\n request<\n {\n time: number;\n hash: string;\n delta: { type: string; coin: string; usdc: string; szi: string; fundingRate: string };\n }[]\n >('POST', 'info', buildUserFundingHistoryRequestBody(params));\n\nexport const buildUserTokenBalancesRequestBody = (params: { user: string }) =>\n buildInfoRequestBody('spotClearinghouseState', params);\n\n/**\n * Get user's token balances for both spot and perpetual accounts\n * API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/spot#retrieve-a-users-token-balances\n * API Endpoint: POST /info (type: spotClearinghouseState)\n * @param params - Query parameters\n * @param params.user - User's wallet address\n * @returns Promise resolving to array of token balances with held and total amounts\n */\nexport const getUserTokenBalances = (params: { user: string }) =>\n request<{ balances: { coin: string; token: number; hold: string; total: string; entryNtl: string }[] }>(\n 'POST',\n 'info',\n buildUserTokenBalancesRequestBody(params),\n );\n\nexport const buildUserOpenOrdersRequestBody = (params: { user: string }) =>\n buildInfoRequestBody('openOrders', params);\n\n/**\n * Get user's currently open orders across all markets\n * API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint#open-orders\n * API Endpoint: POST /info (type: openOrders)\n * @param params - Query parameters\n * @param params.user - User's wallet address\n * @returns Promise resolving to array of open order details with prices, sizes, and timestamps\n */\nexport const getUserOpenOrders = (params: { user: string }) =>\n request<{ coin: string; limitPx: string; oid: number; side: string; sz: string; timestamp: number }[]>(\n 'POST',\n 'info',\n buildUserOpenOrdersRequestBody(params),\n );\n\nexport const buildHistoricalFundingRatesRequestBody = (params: {\n coin: string;\n startTime: number;\n endTime?: number;\n}) => buildInfoRequestBody('fundingHistory', params);\n\n/**\n * Get historical funding rates for a specific perpetual asset\n * API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-historical-funding-rates\n * API Endpoint: POST /info (type: fundingHistory)\n * @param params - Query parameters\n * @param params.coin - Asset symbol (e.g., \"BTC\", \"ETH\")\n * @param params.startTime - Start timestamp in milliseconds\n * @param params.endTime - Optional end timestamp in milliseconds\n * @returns Promise resolving to array of historical funding rate records\n */\nexport const getHistoricalFundingRates = (params: { coin: string; startTime: number; endTime?: number }) =>\n request<{ coin: string; fundingRate: string; premium: string; time: number }[]>(\n 'POST',\n 'info',\n buildHistoricalFundingRatesRequestBody(params),\n );\n\nexport const buildAllMidsRequestBody = () => buildInfoRequestBody('allMids');\n\n/**\n * Get current mid prices for all tradable assets\n * API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint#retrieve-mids-for-all-coins\n * API Endpoint: POST /info (type: allMids)\n * @returns Promise resolving to record of asset symbols mapped to their mid prices\n */\nexport const getAllMids = () => request<Record<string, string>>('POST', 'info', buildAllMidsRequestBody());\n\nexport const buildMetaAndAssetCtxsRequestBody = () => buildInfoRequestBody('metaAndAssetCtxs');\n\n/**\n * Get metadata and asset contexts (prices, funding, OI) for all perpetuals\n * API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/perpetuals#retrieve-perpetuals-asset-contexts-includes-mark-price-current-funding-open-interest-etc.\n * API Endpoint: POST /info (type: metaAndAssetCtxs)\n * @returns Promise resolving to metadata and current asset contexts\n */\nexport const getMetaAndAssetCtxs = () =>\n request<\n [\n {\n universe: {\n name: string;\n szDecimals: number;\n maxLeverage: number;\n onlyIsolated?: boolean;\n isDelisted?: boolean;\n }[];\n },\n {\n dayNtlVlm: string;\n funding: string;\n impactPxs: [string, string];\n markPx: string;\n midPx: string;\n openInterest: string;\n oraclePx: string;\n premium: string;\n prevDayPx: string;\n }[],\n ]\n >('POST', 'info', buildMetaAndAssetCtxsRequestBody());\n\nexport const buildCandleSnapshotRequestBody = (params: {\n req: { coin: string; interval: string; startTime: number; endTime: number };\n}) => buildInfoRequestBody('candleSnapshot', params);\n\n/**\n * Get candle/K-line data for a specific asset and time range\n * API Docs: https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint#candle-snapshot\n * API Endpoint: POST /info (type: candleSnapshot)\n * @param params - Query parameters\n * @param params.req - Request object with candle parameters\n * @param params.req.coin - Asset symbol\n * @param params.req.interval - Time interval (e.g., \"1m\", \"5m\", \"1h\", \"1d\")\n * @param params.req.startTime - Start timestamp in milliseconds\n * @param params.req.endTime - End timestamp in milliseconds\n * @returns Promise resolving to candle data with OHLCV information\n */\nexport const getCandleSnapshot = (params: {\n req: { coin: string; interval: string; startTime: number; endTime: number };\n}) =>\n request<\n {\n T: number;\n c: string;\n h: string;\n i: string;\n l: string;\n o: string;\n n: number;\n s: string;\n t: number;\n v: string;\n }[]\n >('POST', 'info', buildCandleSnapshotRequestBody(params));\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
type HttpMethod = 'GET' | 'POST';
|
|
2
|
+
type RestRequestContext = Readonly<{
|
|
3
|
+
method: HttpMethod;
|
|
4
|
+
path: string;
|
|
5
|
+
body?: unknown;
|
|
6
|
+
kind: 'info' | 'exchange' | 'explorer' | 'other';
|
|
7
|
+
infoType?: string;
|
|
8
|
+
exchangeActionType?: string;
|
|
9
|
+
exchangeBatchLength?: number;
|
|
10
|
+
}>;
|
|
11
|
+
export declare const getRestRequestContext: (method: HttpMethod, path: string, body?: unknown) => RestRequestContext;
|
|
12
|
+
export declare const getRestBaseWeight: (ctx: RestRequestContext) => number;
|
|
13
|
+
export declare const getRestEstimatedExtraWeight: (ctx: RestRequestContext) => number;
|
|
14
|
+
export declare const acquireRestIpWeightSync: (meta: Record<string, unknown>, weight: number) => void;
|
|
15
|
+
export declare const beforeRestRequest: (meta: Record<string, unknown>, ctx: RestRequestContext) => Readonly<{
|
|
16
|
+
baseWeight: number;
|
|
17
|
+
estimatedExtraWeight: number;
|
|
18
|
+
}>;
|
|
19
|
+
export declare const afterRestResponse: (meta: Record<string, unknown>, ctx: RestRequestContext, response: unknown, estimatedExtraWeight: number) => Promise<void>;
|
|
20
|
+
export {};
|
|
21
|
+
//# sourceMappingURL=rate-limit.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rate-limit.d.ts","sourceRoot":"","sources":["../../src/api/rate-limit.ts"],"names":[],"mappings":"AAEA,KAAK,UAAU,GAAG,KAAK,GAAG,MAAM,CAAC;AAEjC,KAAK,kBAAkB,GAAG,QAAQ,CAAC;IACjC,MAAM,EAAE,UAAU,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,IAAI,EAAE,MAAM,GAAG,UAAU,GAAG,UAAU,GAAG,OAAO,CAAC;IACjD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC9B,CAAC,CAAC;AAkEH,eAAO,MAAM,qBAAqB,GAChC,QAAQ,UAAU,EAClB,MAAM,MAAM,EACZ,OAAO,OAAO,KACb,kBAmBF,CAAC;AAEF,eAAO,MAAM,iBAAiB,GAAI,KAAK,kBAAkB,KAAG,MAY3D,CAAC;AA0DF,eAAO,MAAM,2BAA2B,GAAI,KAAK,kBAAkB,KAAG,MAQrE,CAAC;AAoBF,eAAO,MAAM,uBAAuB,GAAI,MAAM,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,QAAQ,MAAM,SAKpF,CAAC;AAeF,eAAO,MAAM,iBAAiB,GAC5B,MAAM,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,KAAK,kBAAkB,KACtB,QAAQ,CAAC;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,oBAAoB,EAAE,MAAM,CAAA;CAAE,CAK/D,CAAC;AAEF,eAAO,MAAM,iBAAiB,GAC5B,MAAM,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,KAAK,kBAAkB,EACvB,UAAU,OAAO,EACjB,sBAAsB,MAAM,kBAc7B,CAAC"}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.afterRestResponse = exports.beforeRestRequest = exports.acquireRestIpWeightSync = exports.getRestEstimatedExtraWeight = exports.getRestBaseWeight = exports.getRestRequestContext = void 0;
|
|
4
|
+
const utils_1 = require("@yuants/utils");
|
|
5
|
+
const REST_IP_BUCKET_ID = 'HYPERLIQUID_REST_IP_WEIGHT_1200_PER_MIN';
|
|
6
|
+
const REST_IP_BUCKET_CAPACITY = 1200;
|
|
7
|
+
const REST_IP_WEIGHT_MAX = REST_IP_BUCKET_CAPACITY * 10;
|
|
8
|
+
(0, utils_1.tokenBucket)(REST_IP_BUCKET_ID, {
|
|
9
|
+
capacity: REST_IP_BUCKET_CAPACITY,
|
|
10
|
+
refillInterval: 60000,
|
|
11
|
+
refillAmount: 1200,
|
|
12
|
+
});
|
|
13
|
+
const INFO_TYPE_TO_BASE_WEIGHT = {
|
|
14
|
+
l2Book: 2,
|
|
15
|
+
allMids: 2,
|
|
16
|
+
clearinghouseState: 2,
|
|
17
|
+
orderStatus: 2,
|
|
18
|
+
spotClearinghouseState: 2,
|
|
19
|
+
exchangeStatus: 2,
|
|
20
|
+
userRole: 60,
|
|
21
|
+
};
|
|
22
|
+
const INTERVAL_TO_MS = {
|
|
23
|
+
'1m': 60000,
|
|
24
|
+
'3m': 180000,
|
|
25
|
+
'5m': 300000,
|
|
26
|
+
'15m': 900000,
|
|
27
|
+
'30m': 1800000,
|
|
28
|
+
'1h': 3600000,
|
|
29
|
+
'2h': 7200000,
|
|
30
|
+
'4h': 14400000,
|
|
31
|
+
'8h': 28800000,
|
|
32
|
+
'12h': 43200000,
|
|
33
|
+
'1d': 86400000,
|
|
34
|
+
'3d': 259200000,
|
|
35
|
+
'1w': 604800000,
|
|
36
|
+
'1M': 2592000000,
|
|
37
|
+
};
|
|
38
|
+
const getObject = (value) => {
|
|
39
|
+
if (typeof value !== 'object' || value === null)
|
|
40
|
+
return;
|
|
41
|
+
return value;
|
|
42
|
+
};
|
|
43
|
+
const getString = (value) => {
|
|
44
|
+
if (typeof value !== 'string')
|
|
45
|
+
return;
|
|
46
|
+
return value;
|
|
47
|
+
};
|
|
48
|
+
const getNumber = (value) => {
|
|
49
|
+
if (typeof value !== 'number' || !Number.isFinite(value))
|
|
50
|
+
return;
|
|
51
|
+
return value;
|
|
52
|
+
};
|
|
53
|
+
const getArray = (value) => {
|
|
54
|
+
if (!Array.isArray(value))
|
|
55
|
+
return;
|
|
56
|
+
return value;
|
|
57
|
+
};
|
|
58
|
+
const getRestRequestContext = (method, path, body) => {
|
|
59
|
+
var _a, _b;
|
|
60
|
+
if (method === 'POST' && path === 'info') {
|
|
61
|
+
const obj = getObject(body);
|
|
62
|
+
const infoType = getString(obj === null || obj === void 0 ? void 0 : obj.type);
|
|
63
|
+
return { method, path, body, kind: 'info', infoType };
|
|
64
|
+
}
|
|
65
|
+
if (method === 'POST' && path === 'exchange') {
|
|
66
|
+
const obj = getObject(body);
|
|
67
|
+
const action = getObject(obj === null || obj === void 0 ? void 0 : obj.action);
|
|
68
|
+
const exchangeActionType = getString(action === null || action === void 0 ? void 0 : action.type);
|
|
69
|
+
const orders = getArray(action === null || action === void 0 ? void 0 : action.orders);
|
|
70
|
+
const cancels = getArray(action === null || action === void 0 ? void 0 : action.cancels);
|
|
71
|
+
const exchangeBatchLength = Math.max(1, (_b = (_a = orders === null || orders === void 0 ? void 0 : orders.length) !== null && _a !== void 0 ? _a : cancels === null || cancels === void 0 ? void 0 : cancels.length) !== null && _b !== void 0 ? _b : 1);
|
|
72
|
+
return { method, path, body, kind: 'exchange', exchangeActionType, exchangeBatchLength };
|
|
73
|
+
}
|
|
74
|
+
if (path.startsWith('explorer')) {
|
|
75
|
+
return { method, path, body, kind: 'explorer' };
|
|
76
|
+
}
|
|
77
|
+
return { method, path, body, kind: 'other' };
|
|
78
|
+
};
|
|
79
|
+
exports.getRestRequestContext = getRestRequestContext;
|
|
80
|
+
const getRestBaseWeight = (ctx) => {
|
|
81
|
+
var _a, _b, _c;
|
|
82
|
+
if (ctx.kind === 'exchange') {
|
|
83
|
+
const batchLength = Math.max(1, (_a = ctx.exchangeBatchLength) !== null && _a !== void 0 ? _a : 1);
|
|
84
|
+
return 1 + Math.floor(batchLength / 40);
|
|
85
|
+
}
|
|
86
|
+
if (ctx.kind === 'info') {
|
|
87
|
+
return (_c = INFO_TYPE_TO_BASE_WEIGHT[(_b = ctx.infoType) !== null && _b !== void 0 ? _b : '']) !== null && _c !== void 0 ? _c : 20;
|
|
88
|
+
}
|
|
89
|
+
if (ctx.kind === 'explorer') {
|
|
90
|
+
return 40;
|
|
91
|
+
}
|
|
92
|
+
return 20;
|
|
93
|
+
};
|
|
94
|
+
exports.getRestBaseWeight = getRestBaseWeight;
|
|
95
|
+
const estimateCandleSnapshotItems = (ctx) => {
|
|
96
|
+
var _a;
|
|
97
|
+
if (ctx.kind !== 'info' || ctx.infoType !== 'candleSnapshot')
|
|
98
|
+
return 0;
|
|
99
|
+
const bodyObj = getObject(ctx.body);
|
|
100
|
+
const reqObj = getObject(bodyObj === null || bodyObj === void 0 ? void 0 : bodyObj.req);
|
|
101
|
+
const interval = getString(reqObj === null || reqObj === void 0 ? void 0 : reqObj.interval);
|
|
102
|
+
const startTime = getNumber(reqObj === null || reqObj === void 0 ? void 0 : reqObj.startTime);
|
|
103
|
+
const endTime = getNumber(reqObj === null || reqObj === void 0 ? void 0 : reqObj.endTime);
|
|
104
|
+
if (!interval || startTime === undefined || endTime === undefined || endTime <= startTime)
|
|
105
|
+
return 0;
|
|
106
|
+
const intervalMs = (_a = INTERVAL_TO_MS[interval]) !== null && _a !== void 0 ? _a : 0;
|
|
107
|
+
if (!intervalMs)
|
|
108
|
+
return 0;
|
|
109
|
+
// Hyperliquid docs: Only the most recent 5000 candles are available
|
|
110
|
+
const estimated = Math.ceil((endTime - startTime) / intervalMs);
|
|
111
|
+
return Math.min(5000, Math.max(0, estimated));
|
|
112
|
+
};
|
|
113
|
+
const countArrayResponseItems = (response) => {
|
|
114
|
+
var _a;
|
|
115
|
+
const arr = getArray(response);
|
|
116
|
+
return (_a = arr === null || arr === void 0 ? void 0 : arr.length) !== null && _a !== void 0 ? _a : 0;
|
|
117
|
+
};
|
|
118
|
+
const countUserFillsItems = (response) => {
|
|
119
|
+
var _a;
|
|
120
|
+
const obj = getObject(response);
|
|
121
|
+
const fills = getArray(obj === null || obj === void 0 ? void 0 : obj.fills);
|
|
122
|
+
return (_a = fills === null || fills === void 0 ? void 0 : fills.length) !== null && _a !== void 0 ? _a : 0;
|
|
123
|
+
};
|
|
124
|
+
const extraWeighers = [
|
|
125
|
+
{
|
|
126
|
+
match: (ctx) => ctx.kind === 'info' && ctx.infoType === 'candleSnapshot',
|
|
127
|
+
divisor: 60,
|
|
128
|
+
estimateItems: estimateCandleSnapshotItems,
|
|
129
|
+
countItemsFromResponse: countArrayResponseItems,
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
match: (ctx) => ctx.kind === 'info' &&
|
|
133
|
+
(ctx.infoType === 'recentTrades' ||
|
|
134
|
+
ctx.infoType === 'historicalOrders' ||
|
|
135
|
+
ctx.infoType === 'userFills' ||
|
|
136
|
+
ctx.infoType === 'userFillsByTime' ||
|
|
137
|
+
ctx.infoType === 'fundingHistory' ||
|
|
138
|
+
ctx.infoType === 'userFunding' ||
|
|
139
|
+
ctx.infoType === 'nonUserFundingUpdates' ||
|
|
140
|
+
ctx.infoType === 'twapHistory' ||
|
|
141
|
+
ctx.infoType === 'userTwapSliceFills' ||
|
|
142
|
+
ctx.infoType === 'userTwapSliceFillsByTime' ||
|
|
143
|
+
ctx.infoType === 'delegatorHistory' ||
|
|
144
|
+
ctx.infoType === 'delegatorRewards' ||
|
|
145
|
+
ctx.infoType === 'validatorStats'),
|
|
146
|
+
divisor: 20,
|
|
147
|
+
countItemsFromResponse: (response) => countUserFillsItems(response) || countArrayResponseItems(response),
|
|
148
|
+
},
|
|
149
|
+
];
|
|
150
|
+
const getRestEstimatedExtraWeight = (ctx) => {
|
|
151
|
+
var _a, _b;
|
|
152
|
+
for (const weigher of extraWeighers) {
|
|
153
|
+
if (!weigher.match(ctx))
|
|
154
|
+
continue;
|
|
155
|
+
const items = (_b = (_a = weigher.estimateItems) === null || _a === void 0 ? void 0 : _a.call(weigher, ctx)) !== null && _b !== void 0 ? _b : 0;
|
|
156
|
+
if (items <= 0)
|
|
157
|
+
return 0;
|
|
158
|
+
return Math.ceil(items / weigher.divisor);
|
|
159
|
+
}
|
|
160
|
+
return 0;
|
|
161
|
+
};
|
|
162
|
+
exports.getRestEstimatedExtraWeight = getRestEstimatedExtraWeight;
|
|
163
|
+
const normalizeRestWeight = (meta, weight) => {
|
|
164
|
+
if (!Number.isFinite(weight)) {
|
|
165
|
+
throw (0, utils_1.newError)('HYPERLIQUID_REST_WEIGHT_INVALID', Object.assign(Object.assign({}, meta), { weight }));
|
|
166
|
+
}
|
|
167
|
+
const normalized = Math.floor(weight);
|
|
168
|
+
if (normalized <= 0) {
|
|
169
|
+
throw (0, utils_1.newError)('HYPERLIQUID_REST_WEIGHT_INVALID', Object.assign(Object.assign({}, meta), { weight: normalized }));
|
|
170
|
+
}
|
|
171
|
+
if (normalized > REST_IP_WEIGHT_MAX) {
|
|
172
|
+
throw (0, utils_1.newError)('HYPERLIQUID_REST_WEIGHT_EXCESSIVE', Object.assign(Object.assign({}, meta), { weight: normalized, maxWeight: REST_IP_WEIGHT_MAX }));
|
|
173
|
+
}
|
|
174
|
+
return normalized;
|
|
175
|
+
};
|
|
176
|
+
const acquireRestIpWeightSync = (meta, weight) => {
|
|
177
|
+
const normalized = normalizeRestWeight(meta, weight);
|
|
178
|
+
(0, utils_1.scopeError)('HYPERLIQUID_API_RATE_LIMIT', Object.assign(Object.assign({}, meta), { bucketId: REST_IP_BUCKET_ID, weight: normalized }), () => (0, utils_1.tokenBucket)(REST_IP_BUCKET_ID).acquireSync(normalized));
|
|
179
|
+
};
|
|
180
|
+
exports.acquireRestIpWeightSync = acquireRestIpWeightSync;
|
|
181
|
+
const acquireRestIpWeight = async (meta, weight) => {
|
|
182
|
+
let remaining = normalizeRestWeight(meta, weight);
|
|
183
|
+
while (remaining > 0) {
|
|
184
|
+
const chunk = Math.min(REST_IP_BUCKET_CAPACITY, remaining);
|
|
185
|
+
await (0, utils_1.scopeError)('HYPERLIQUID_API_RATE_LIMIT', Object.assign(Object.assign({}, meta), { bucketId: REST_IP_BUCKET_ID, weight: chunk, remaining }), () => (0, utils_1.tokenBucket)(REST_IP_BUCKET_ID).acquire(chunk));
|
|
186
|
+
remaining -= chunk;
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
const beforeRestRequest = (meta, ctx) => {
|
|
190
|
+
const baseWeight = (0, exports.getRestBaseWeight)(ctx);
|
|
191
|
+
const estimatedExtraWeight = (0, exports.getRestEstimatedExtraWeight)(ctx);
|
|
192
|
+
(0, exports.acquireRestIpWeightSync)(meta, baseWeight + estimatedExtraWeight);
|
|
193
|
+
return { baseWeight, estimatedExtraWeight };
|
|
194
|
+
};
|
|
195
|
+
exports.beforeRestRequest = beforeRestRequest;
|
|
196
|
+
const afterRestResponse = async (meta, ctx, response, estimatedExtraWeight) => {
|
|
197
|
+
var _a, _b;
|
|
198
|
+
for (const weigher of extraWeighers) {
|
|
199
|
+
if (!weigher.match(ctx))
|
|
200
|
+
continue;
|
|
201
|
+
const items = (_b = (_a = weigher.countItemsFromResponse) === null || _a === void 0 ? void 0 : _a.call(weigher, response)) !== null && _b !== void 0 ? _b : 0;
|
|
202
|
+
if (items <= 0)
|
|
203
|
+
return;
|
|
204
|
+
const actualExtraWeight = Math.ceil(items / weigher.divisor);
|
|
205
|
+
const delta = actualExtraWeight - estimatedExtraWeight;
|
|
206
|
+
if (delta > 0) {
|
|
207
|
+
// 不使用 acquireSync:响应后只阻塞等待,不报错
|
|
208
|
+
await acquireRestIpWeight(meta, delta);
|
|
209
|
+
}
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
exports.afterRestResponse = afterRestResponse;
|
|
214
|
+
//# sourceMappingURL=rate-limit.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rate-limit.js","sourceRoot":"","sources":["../../src/api/rate-limit.ts"],"names":[],"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AAC,CAAC;AARW,QAAA,iBAAiB,qBAQ5B;AAEK,MAAM,iBAAiB,GAAG,KAAK,EACpC,IAA6B,EAC7B,GAAuB,EACvB,QAAiB,EACjB,oBAA4B,EAC5B,EAAE;;IACF,KAAK,MAAM,OAAO,IAAI,aAAa,EAAE,CAAC;QACpC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC;YAAE,SAAS;QAClC,MAAM,KAAK,GAAG,MAAA,MAAA,OAAO,CAAC,sBAAsB,wDAAG,QAAQ,CAAC,mCAAI,CAAC,CAAC;QAC9D,IAAI,KAAK,IAAI,CAAC;YAAE,OAAO;QACvB,MAAM,iBAAiB,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;QAC7D,MAAM,KAAK,GAAG,iBAAiB,GAAG,oBAAoB,CAAC;QACvD,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;YACd,+BAA+B;YAC/B,MAAM,mBAAmB,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QACzC,CAAC;QACD,OAAO;IACT,CAAC;AACH,CAAC,CAAC;AAlBW,QAAA,iBAAiB,qBAkB5B","sourcesContent":["import { newError, scopeError, tokenBucket } from '@yuants/utils';\n\ntype HttpMethod = 'GET' | 'POST';\n\ntype RestRequestContext = Readonly<{\n method: HttpMethod;\n path: string;\n body?: unknown;\n kind: 'info' | 'exchange' | 'explorer' | 'other';\n infoType?: string;\n exchangeActionType?: string;\n exchangeBatchLength?: number;\n}>;\n\ntype ExtraWeigher = Readonly<{\n match: (ctx: RestRequestContext) => boolean;\n divisor: 20 | 60;\n estimateItems?: (ctx: RestRequestContext) => number;\n countItemsFromResponse?: (response: unknown) => number;\n}>;\n\nconst REST_IP_BUCKET_ID = 'HYPERLIQUID_REST_IP_WEIGHT_1200_PER_MIN';\nconst REST_IP_BUCKET_CAPACITY = 1200;\nconst REST_IP_WEIGHT_MAX = REST_IP_BUCKET_CAPACITY * 10;\n\ntokenBucket(REST_IP_BUCKET_ID, {\n capacity: REST_IP_BUCKET_CAPACITY,\n refillInterval: 60_000,\n refillAmount: 1200,\n});\n\nconst INFO_TYPE_TO_BASE_WEIGHT: Readonly<Record<string, number>> = {\n l2Book: 2,\n allMids: 2,\n clearinghouseState: 2,\n orderStatus: 2,\n spotClearinghouseState: 2,\n exchangeStatus: 2,\n userRole: 60,\n};\n\nconst INTERVAL_TO_MS: Readonly<Record<string, number>> = {\n '1m': 60_000,\n '3m': 180_000,\n '5m': 300_000,\n '15m': 900_000,\n '30m': 1_800_000,\n '1h': 3_600_000,\n '2h': 7_200_000,\n '4h': 14_400_000,\n '8h': 28_800_000,\n '12h': 43_200_000,\n '1d': 86_400_000,\n '3d': 259_200_000,\n '1w': 604_800_000,\n '1M': 2_592_000_000,\n};\n\nconst getObject = (value: unknown): Record<string, unknown> | undefined => {\n if (typeof value !== 'object' || value === null) return;\n return value as Record<string, unknown>;\n};\n\nconst getString = (value: unknown): string | undefined => {\n if (typeof value !== 'string') return;\n return value;\n};\n\nconst getNumber = (value: unknown): number | undefined => {\n if (typeof value !== 'number' || !Number.isFinite(value)) return;\n return value;\n};\n\nconst getArray = (value: unknown): unknown[] | undefined => {\n if (!Array.isArray(value)) return;\n return value;\n};\n\nexport const getRestRequestContext = (\n method: HttpMethod,\n path: string,\n body?: unknown,\n): RestRequestContext => {\n if (method === 'POST' && path === 'info') {\n const obj = getObject(body);\n const infoType = getString(obj?.type);\n return { method, path, body, kind: 'info', infoType };\n }\n if (method === 'POST' && path === 'exchange') {\n const obj = getObject(body);\n const action = getObject(obj?.action);\n const exchangeActionType = getString(action?.type);\n const orders = getArray(action?.orders);\n const cancels = getArray(action?.cancels);\n const exchangeBatchLength = Math.max(1, orders?.length ?? cancels?.length ?? 1);\n return { method, path, body, kind: 'exchange', exchangeActionType, exchangeBatchLength };\n }\n if (path.startsWith('explorer')) {\n return { method, path, body, kind: 'explorer' };\n }\n return { method, path, body, kind: 'other' };\n};\n\nexport const getRestBaseWeight = (ctx: RestRequestContext): number => {\n if (ctx.kind === 'exchange') {\n const batchLength = Math.max(1, ctx.exchangeBatchLength ?? 1);\n return 1 + Math.floor(batchLength / 40);\n }\n if (ctx.kind === 'info') {\n return INFO_TYPE_TO_BASE_WEIGHT[ctx.infoType ?? ''] ?? 20;\n }\n if (ctx.kind === 'explorer') {\n return 40;\n }\n return 20;\n};\n\nconst estimateCandleSnapshotItems = (ctx: RestRequestContext): number => {\n if (ctx.kind !== 'info' || ctx.infoType !== 'candleSnapshot') return 0;\n const bodyObj = getObject(ctx.body);\n const reqObj = getObject(bodyObj?.req);\n const interval = getString(reqObj?.interval);\n const startTime = getNumber(reqObj?.startTime);\n const endTime = getNumber(reqObj?.endTime);\n if (!interval || startTime === undefined || endTime === undefined || endTime <= startTime) return 0;\n\n const intervalMs = INTERVAL_TO_MS[interval] ?? 0;\n if (!intervalMs) return 0;\n\n // Hyperliquid docs: Only the most recent 5000 candles are available\n const estimated = Math.ceil((endTime - startTime) / intervalMs);\n return Math.min(5000, Math.max(0, estimated));\n};\n\nconst countArrayResponseItems = (response: unknown): number => {\n const arr = getArray(response);\n return arr?.length ?? 0;\n};\n\nconst countUserFillsItems = (response: unknown): number => {\n const obj = getObject(response);\n const fills = getArray(obj?.fills);\n return fills?.length ?? 0;\n};\n\nconst extraWeighers: ReadonlyArray<ExtraWeigher> = [\n {\n match: (ctx) => ctx.kind === 'info' && ctx.infoType === 'candleSnapshot',\n divisor: 60,\n estimateItems: estimateCandleSnapshotItems,\n countItemsFromResponse: countArrayResponseItems,\n },\n {\n match: (ctx) =>\n ctx.kind === 'info' &&\n (ctx.infoType === 'recentTrades' ||\n ctx.infoType === 'historicalOrders' ||\n ctx.infoType === 'userFills' ||\n ctx.infoType === 'userFillsByTime' ||\n ctx.infoType === 'fundingHistory' ||\n ctx.infoType === 'userFunding' ||\n ctx.infoType === 'nonUserFundingUpdates' ||\n ctx.infoType === 'twapHistory' ||\n ctx.infoType === 'userTwapSliceFills' ||\n ctx.infoType === 'userTwapSliceFillsByTime' ||\n ctx.infoType === 'delegatorHistory' ||\n ctx.infoType === 'delegatorRewards' ||\n ctx.infoType === 'validatorStats'),\n divisor: 20,\n countItemsFromResponse: (response) => countUserFillsItems(response) || countArrayResponseItems(response),\n },\n];\n\nexport const getRestEstimatedExtraWeight = (ctx: RestRequestContext): number => {\n for (const weigher of extraWeighers) {\n if (!weigher.match(ctx)) continue;\n const items = weigher.estimateItems?.(ctx) ?? 0;\n if (items <= 0) return 0;\n return Math.ceil(items / weigher.divisor);\n }\n return 0;\n};\n\nconst normalizeRestWeight = (meta: Record<string, unknown>, weight: number): number => {\n if (!Number.isFinite(weight)) {\n throw newError('HYPERLIQUID_REST_WEIGHT_INVALID', { ...meta, weight });\n }\n const normalized = Math.floor(weight);\n if (normalized <= 0) {\n throw newError('HYPERLIQUID_REST_WEIGHT_INVALID', { ...meta, weight: normalized });\n }\n if (normalized > REST_IP_WEIGHT_MAX) {\n throw newError('HYPERLIQUID_REST_WEIGHT_EXCESSIVE', {\n ...meta,\n weight: normalized,\n maxWeight: REST_IP_WEIGHT_MAX,\n });\n }\n return normalized;\n};\n\nexport const acquireRestIpWeightSync = (meta: Record<string, unknown>, weight: number) => {\n const normalized = normalizeRestWeight(meta, weight);\n scopeError('HYPERLIQUID_API_RATE_LIMIT', { ...meta, bucketId: REST_IP_BUCKET_ID, weight: normalized }, () =>\n tokenBucket(REST_IP_BUCKET_ID).acquireSync(normalized),\n );\n};\n\nconst acquireRestIpWeight = async (meta: Record<string, unknown>, weight: number) => {\n let remaining = normalizeRestWeight(meta, weight);\n while (remaining > 0) {\n const chunk = Math.min(REST_IP_BUCKET_CAPACITY, remaining);\n await scopeError(\n 'HYPERLIQUID_API_RATE_LIMIT',\n { ...meta, bucketId: REST_IP_BUCKET_ID, weight: chunk, remaining },\n () => tokenBucket(REST_IP_BUCKET_ID).acquire(chunk),\n );\n remaining -= chunk;\n }\n};\n\nexport const beforeRestRequest = (\n meta: Record<string, unknown>,\n ctx: RestRequestContext,\n): Readonly<{ baseWeight: number; estimatedExtraWeight: number }> => {\n const baseWeight = getRestBaseWeight(ctx);\n const estimatedExtraWeight = getRestEstimatedExtraWeight(ctx);\n acquireRestIpWeightSync(meta, baseWeight + estimatedExtraWeight);\n return { baseWeight, estimatedExtraWeight };\n};\n\nexport const afterRestResponse = async (\n meta: Record<string, unknown>,\n ctx: RestRequestContext,\n response: unknown,\n estimatedExtraWeight: number,\n) => {\n for (const weigher of extraWeighers) {\n if (!weigher.match(ctx)) continue;\n const items = weigher.countItemsFromResponse?.(response) ?? 0;\n if (items <= 0) return;\n const actualExtraWeight = Math.ceil(items / weigher.divisor);\n const delta = actualExtraWeight - estimatedExtraWeight;\n if (delta > 0) {\n // 不使用 acquireSync:响应后只阻塞等待,不报错\n await acquireRestIpWeight(meta, delta);\n }\n return;\n }\n};\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rate-limit.test.d.ts","sourceRoot":"","sources":["../../src/api/rate-limit.test.ts"],"names":[],"mappings":""}
|