@yuants/vendor-binance 0.14.4 → 0.14.5
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 +28 -21
- package/dist/api/client.js.map +1 -1
- package/dist/api/private-api.js +28 -28
- package/dist/api/private-api.js.map +1 -1
- package/dist/api/public-api.js +8 -8
- package/dist/api/public-api.js.map +1 -1
- package/lib/api/client.d.ts +3 -0
- package/lib/api/client.d.ts.map +1 -1
- package/lib/api/client.js +29 -21
- package/lib/api/client.js.map +1 -1
- package/lib/api/private-api.d.ts.map +1 -1
- package/lib/api/private-api.js +27 -27
- package/lib/api/private-api.js.map +1 -1
- package/lib/api/public-api.js +7 -7
- package/lib/api/public-api.js.map +1 -1
- package/package.json +6 -6
- package/temp/build/typescript/ts_tQj2bmGB.json +1 -1
- package/temp/package-deps.json +7 -7
- package/temp/test/jest/haste-map-f37bc94525b36254e573de220443d250-dc922b21f2d35e7315abf4c3159e25a5-63b40dbcff306581344a73f9073894a1 +0 -0
package/dist/api/client.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
var _a;
|
|
2
2
|
import { fetch, selectHTTPProxyIpRoundRobin } from '@yuants/http-services';
|
|
3
3
|
import { GlobalPrometheusRegistry, Terminal } from '@yuants/protocol';
|
|
4
|
-
import { encodeHex, encodePath, formatTime, HmacSHA256, newError, tokenBucket } from '@yuants/utils';
|
|
4
|
+
import { encodeHex, encodePath, formatTime, HmacSHA256, newError, tokenBucket, } from '@yuants/utils';
|
|
5
5
|
const MetricBinanceApiUsedWeight = GlobalPrometheusRegistry.gauge('binance_api_used_weight', '');
|
|
6
6
|
const MetricBinanceApiCounter = GlobalPrometheusRegistry.counter('binance_api_request_total', '');
|
|
7
7
|
const terminal = Terminal.fromNodeEnv();
|
|
@@ -12,26 +12,33 @@ const missingPublicIpLogAtByTerminalId = new Map();
|
|
|
12
12
|
if (shouldUseHttpProxy) {
|
|
13
13
|
globalThis.fetch = fetch;
|
|
14
14
|
}
|
|
15
|
-
export const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
15
|
+
export const BINANCE_TOKEN_BUCKET_OPTIONS_BY_ID = {
|
|
16
|
+
'api.binance.com': {
|
|
17
|
+
capacity: 6000,
|
|
18
|
+
refillInterval: 60000,
|
|
19
|
+
refillAmount: 6000,
|
|
20
|
+
},
|
|
21
|
+
'fapi.binance.com': {
|
|
22
|
+
capacity: 2400,
|
|
23
|
+
refillInterval: 60000,
|
|
24
|
+
refillAmount: 2400,
|
|
25
|
+
},
|
|
26
|
+
'papi.binance.com': {
|
|
27
|
+
capacity: 6000,
|
|
28
|
+
refillInterval: 60000,
|
|
29
|
+
refillAmount: 6000,
|
|
30
|
+
},
|
|
31
|
+
'order/unified/minute': {
|
|
32
|
+
capacity: 1200,
|
|
33
|
+
refillInterval: 60000,
|
|
34
|
+
refillAmount: 1200,
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
export const getTokenBucketOptions = (baseKey) => BINANCE_TOKEN_BUCKET_OPTIONS_BY_ID[baseKey];
|
|
38
|
+
export const spotAPIBucket = tokenBucket('api.binance.com', BINANCE_TOKEN_BUCKET_OPTIONS_BY_ID['api.binance.com']);
|
|
39
|
+
export const futureAPIBucket = tokenBucket('fapi.binance.com', BINANCE_TOKEN_BUCKET_OPTIONS_BY_ID['fapi.binance.com']);
|
|
40
|
+
export const unifiedAPIBucket = tokenBucket('papi.binance.com', BINANCE_TOKEN_BUCKET_OPTIONS_BY_ID['papi.binance.com']);
|
|
41
|
+
export const unifiedOrderAPIBucket = tokenBucket('order/unified/minute', BINANCE_TOKEN_BUCKET_OPTIONS_BY_ID['order/unified/minute']);
|
|
35
42
|
export const buildTokenBucketKey = (baseKey, ip) => encodePath([baseKey, ip]);
|
|
36
43
|
const resolveLocalPublicIp = () => {
|
|
37
44
|
var _a, _b, _c;
|
package/dist/api/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/api/client.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,KAAK,EAAE,2BAA2B,EAAE,MAAM,uBAAuB,CAAC;AAC3E,OAAO,EAAE,wBAAwB,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACtE,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAErG,MAAM,0BAA0B,GAAG,wBAAwB,CAAC,KAAK,CAAC,yBAAyB,EAAE,EAAE,CAAC,CAAC;AACjG,MAAM,uBAAuB,GAAG,wBAAwB,CAAC,OAAO,CAAC,2BAA2B,EAAE,EAAE,CAAC,CAAC;AAClG,MAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC;AACxC,MAAM,kBAAkB,GAAG,OAAO,CAAC,GAAG,CAAC,cAAc,KAAK,MAAM,CAAC;AACjE,MAAM,SAAS,GAAG,kBAAkB,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAA,UAAU,CAAC,KAAK,mCAAI,KAAK,CAAC;AACzE,MAAM,8BAA8B,GAAG,OAAS,CAAC;AACjD,MAAM,gCAAgC,GAAG,IAAI,GAAG,EAAkB,CAAC;AAEnE,IAAI,kBAAkB,EAAE,CAAC;IACvB,UAAU,CAAC,KAAK,GAAG,KAAK,CAAC;AAC3B,CAAC;AAoBD,MAAM,CAAC,MAAM,aAAa,GAAG,WAAW,CAAC,iBAAiB,EAAE;IAC1D,QAAQ,EAAE,IAAI;IACd,cAAc,EAAE,KAAM;IACtB,YAAY,EAAE,IAAI;CACnB,CAA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,CAAC,GAAG,CAC1F,CAAC,YAAY,CACd,CAAC;IACJ,CAAC;IACD,OAAO,GAAG,CAAC,IAAI,EAAgB,CAAC;AAClC,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,aAAa,GAAG,CAC3B,MAAkB,EAClB,QAAgB,EAChB,MAAsB,EACtB,cAAgC,EAChC,EAAE,CAAC,OAAO,CAAI,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,cAAc,CAAC,CAAC;AAErE,MAAM,CAAC,MAAM,cAAc,GAAG,CAC5B,UAAuB,EACvB,MAAkB,EAClB,QAAgB,EAChB,MAAsB,EACtB,cAAgC,EAChC,EAAE,CAAC,OAAO,CAAI,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,cAAc,CAAC,CAAC;AAEtE,MAAM,CAAC,MAAM,oBAAoB,GAAG,GAAgB,EAAE;IACpD,MAAM,UAAU,GAAG,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC;IAC1C,MAAM,UAAU,GAAG,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC;IAC1C,IAAI,CAAC,UAAU,IAAI,CAAC,UAAU,EAAE,CAAC;QAC/B,MAAM,IAAI,KAAK,CAAC,mEAAmE,CAAC,CAAC;IACvF,CAAC;IACD,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,CAAC;AACpC,CAAC,CAAC","sourcesContent":["import { fetch, selectHTTPProxyIpRoundRobin } from '@yuants/http-services';\nimport { GlobalPrometheusRegistry, Terminal } from '@yuants/protocol';\nimport { encodeHex, encodePath, formatTime, HmacSHA256, newError, tokenBucket } from '@yuants/utils';\n\nconst MetricBinanceApiUsedWeight = GlobalPrometheusRegistry.gauge('binance_api_used_weight', '');\nconst MetricBinanceApiCounter = GlobalPrometheusRegistry.counter('binance_api_request_total', '');\nconst terminal = Terminal.fromNodeEnv();\nconst shouldUseHttpProxy = process.env.USE_HTTP_PROXY === 'true';\nconst fetchImpl = shouldUseHttpProxy ? fetch : globalThis.fetch ?? fetch;\nconst MISSING_PUBLIC_IP_LOG_INTERVAL = 3_600_000;\nconst missingPublicIpLogAtByTerminalId = new Map<string, number>();\n\nif (shouldUseHttpProxy) {\n globalThis.fetch = fetch;\n}\n\ntype HttpMethod = 'GET' | 'POST' | 'DELETE' | 'PUT';\n\ntype RequestParams = Record<string, string | number | boolean | undefined>;\n\nexport interface IRequestContext {\n ip: string;\n}\n\nexport interface ICredential {\n access_key: string;\n secret_key: string;\n}\n\nexport interface IApiError {\n code: number;\n msg: string;\n}\n\nexport const spotAPIBucket = tokenBucket('api.binance.com', {\n capacity: 6000,\n refillInterval: 60_000,\n refillAmount: 6000,\n});\n\nexport const futureAPIBucket = tokenBucket('fapi.binance.com', {\n capacity: 2400,\n refillInterval: 60_000,\n refillAmount: 2400,\n});\n\nexport const unifiedAPIBucket = tokenBucket('papi.binance.com', {\n capacity: 6000,\n refillInterval: 60_000,\n refillAmount: 6000,\n});\n\nexport const unifiedOrderAPIBucket = tokenBucket('order/unified/minute', {\n capacity: 1200,\n refillInterval: 60_000,\n refillAmount: 1200,\n});\n\nexport const buildTokenBucketKey = (baseKey: string, ip: string): string => encodePath([baseKey, ip]);\n\nconst resolveLocalPublicIp = (): string => {\n const ip = terminal.terminalInfo.tags?.public_ip?.trim();\n if (ip) return ip;\n const now = Date.now();\n const lastLoggedAt = missingPublicIpLogAtByTerminalId.get(terminal.terminal_id) ?? 0;\n if (now - lastLoggedAt > MISSING_PUBLIC_IP_LOG_INTERVAL) {\n missingPublicIpLogAtByTerminalId.set(terminal.terminal_id, now);\n console.info(formatTime(Date.now()), 'missing terminal public_ip tag, fallback to public-ip-unknown');\n }\n return 'public-ip-unknown';\n};\n\nexport const createRequestContext = (): IRequestContext => {\n if (shouldUseHttpProxy) {\n const ip = selectHTTPProxyIpRoundRobin(terminal);\n return { ip };\n }\n return { ip: resolveLocalPublicIp() };\n};\n\n// 每个接口单独进行主动限流控制\nconst mapPathToRetryAfterUntil: Record<string, number> = {};\n\nexport const isApiError = <T>(value: T | IApiError): value is IApiError =>\n typeof (value as IApiError)?.code === 'number' && typeof (value as IApiError)?.msg === 'string';\n\nconst appendParams = (url: URL, params?: RequestParams) => {\n if (!params) return;\n const entries = Object.entries(params).filter(([, value]) => value !== undefined);\n const timestampEntry = entries.find(([key]) => key === 'timestamp');\n const restEntries = entries.filter(([key]) => key !== 'timestamp');\n restEntries\n .sort(([keyA], [keyB]) => keyA.localeCompare(keyB))\n .forEach(([key, value]) => {\n url.searchParams.set(key, String(value));\n });\n if (timestampEntry) {\n url.searchParams.set(timestampEntry[0], String(timestampEntry[1]));\n }\n};\n\nconst callApi = async <T>(\n method: HttpMethod,\n endpoint: string,\n params?: RequestParams,\n credential?: ICredential,\n requestContext?: IRequestContext,\n): Promise<T> => {\n if (shouldUseHttpProxy && !requestContext) {\n throw newError('E_PROXY_TARGET_NOT_FOUND', { reason: 'Missing request context' });\n }\n const url = new URL(endpoint);\n const normalizedParams: RequestParams = { ...params };\n if (credential) {\n if (normalizedParams.recvWindow === undefined) {\n // FYI https://developers.binance.com/docs/derivatives/usds-margined-futures/general-info#timing-security\n normalizedParams.recvWindow = 5000;\n }\n if (normalizedParams.timestamp === undefined) {\n normalizedParams.timestamp = Date.now();\n }\n }\n appendParams(url, normalizedParams);\n\n let headers: Record<string, string> | undefined;\n if (credential) {\n const signData = url.search.slice(1);\n\n const signature = encodeHex(\n await HmacSHA256(new TextEncoder().encode(signData), new TextEncoder().encode(credential.secret_key)),\n );\n url.searchParams.set('signature', signature);\n headers = {\n 'Content-Type': 'application/json;charset=utf-8',\n 'X-MBX-APIKEY': credential.access_key,\n };\n console.info(formatTime(Date.now()), 'request', method, url.host, url.pathname);\n } else {\n console.info(formatTime(Date.now()), 'request', method, url.host, url.pathname);\n }\n\n const retryAfterUntil = mapPathToRetryAfterUntil[endpoint];\n\n if (retryAfterUntil) {\n if (Date.now() <= retryAfterUntil) {\n // 主动限流\n throw newError('ACTIVE_RATE_LIMIT', {\n wait_time: `${retryAfterUntil - Date.now()}ms`,\n retryAfterUntil,\n url: `${url.host}${url.pathname}`,\n });\n }\n delete mapPathToRetryAfterUntil[endpoint];\n }\n\n MetricBinanceApiCounter.labels({ path: url.pathname, terminal_id: terminal.terminal_id }).inc();\n\n const proxyIp = shouldUseHttpProxy\n ? requestContext?.ip ?? selectHTTPProxyIpRoundRobin(terminal)\n : undefined;\n const res = await fetchImpl(\n url.href,\n shouldUseHttpProxy\n ? {\n method,\n headers,\n labels: proxyIp ? { ip: proxyIp } : undefined,\n terminal,\n }\n : {\n method,\n headers,\n },\n );\n const usedWeight1M = res.headers.get('x-mbx-used-weight-1m');\n const retryAfter = res.headers.get('Retry-After');\n if (retryAfter) {\n mapPathToRetryAfterUntil[endpoint] = Date.now() + parseInt(retryAfter, 10) * 1000;\n }\n console.info(\n formatTime(Date.now()),\n 'response',\n method,\n url.host,\n url.pathname,\n `status=${res.status}`,\n retryAfter ? `retryAfter=${retryAfter}` : '',\n `usedWeight1M=${usedWeight1M ?? 'N/A'}`,\n );\n if (usedWeight1M) {\n MetricBinanceApiUsedWeight.labels({ path: endpoint, terminal_id: terminal.terminal_id }).set(\n +usedWeight1M,\n );\n }\n return res.json() as Promise<T>;\n};\n\nexport const requestPublic = <T>(\n method: HttpMethod,\n endpoint: string,\n params?: RequestParams,\n requestContext?: IRequestContext,\n) => callApi<T>(method, endpoint, params, undefined, requestContext);\n\nexport const requestPrivate = <T>(\n credential: ICredential,\n method: HttpMethod,\n endpoint: string,\n params?: RequestParams,\n requestContext?: IRequestContext,\n) => callApi<T>(method, endpoint, params, credential, requestContext);\n\nexport const getDefaultCredential = (): ICredential => {\n const access_key = process.env.ACCESS_KEY;\n const secret_key = process.env.SECRET_KEY;\n if (!access_key || !secret_key) {\n throw new Error('Missing Binance credential: ACCESS_KEY and SECRET_KEY must be set');\n }\n return { access_key, secret_key };\n};\n"]}
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/api/client.ts"],"names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C;QACjB,0BAA0B,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,WAAW,EAAE,QAAQ,CAAC,WAAW,EAAE,CAAC,CAAC,GAAG,CAC1F,CAAC,YAAY,CACd,CAAC;IACJ,CAAC;IACD,OAAO,GAAG,CAAC,IAAI,EAAgB,CAAC;AAClC,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,aAAa,GAAG,CAC3B,MAAkB,EAClB,QAAgB,EAChB,MAAsB,EACtB,cAAgC,EAChC,EAAE,CAAC,OAAO,CAAI,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,cAAc,CAAC,CAAC;AAErE,MAAM,CAAC,MAAM,cAAc,GAAG,CAC5B,UAAuB,EACvB,MAAkB,EAClB,QAAgB,EAChB,MAAsB,EACtB,cAAgC,EAChC,EAAE,CAAC,OAAO,CAAI,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,cAAc,CAAC,CAAC;AAEtE,MAAM,CAAC,MAAM,oBAAoB,GAAG,GAAgB,EAAE;IACpD,MAAM,UAAU,GAAG,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC;IAC1C,MAAM,UAAU,GAAG,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC;IAC1C,IAAI,CAAC,UAAU,IAAI,CAAC,UAAU,EAAE,CAAC;QAC/B,MAAM,IAAI,KAAK,CAAC,mEAAmE,CAAC,CAAC;IACvF,CAAC;IACD,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,CAAC;AACpC,CAAC,CAAC","sourcesContent":["import { fetch, selectHTTPProxyIpRoundRobin } from '@yuants/http-services';\nimport { GlobalPrometheusRegistry, Terminal } from '@yuants/protocol';\nimport {\n encodeHex,\n encodePath,\n formatTime,\n HmacSHA256,\n newError,\n tokenBucket,\n type TokenBucketOptions,\n} from '@yuants/utils';\n\nconst MetricBinanceApiUsedWeight = GlobalPrometheusRegistry.gauge('binance_api_used_weight', '');\nconst MetricBinanceApiCounter = GlobalPrometheusRegistry.counter('binance_api_request_total', '');\nconst terminal = Terminal.fromNodeEnv();\nconst shouldUseHttpProxy = process.env.USE_HTTP_PROXY === 'true';\nconst fetchImpl = shouldUseHttpProxy ? fetch : globalThis.fetch ?? fetch;\nconst MISSING_PUBLIC_IP_LOG_INTERVAL = 3_600_000;\nconst missingPublicIpLogAtByTerminalId = new Map<string, number>();\n\nif (shouldUseHttpProxy) {\n globalThis.fetch = fetch;\n}\n\ntype HttpMethod = 'GET' | 'POST' | 'DELETE' | 'PUT';\n\ntype RequestParams = Record<string, string | number | boolean | undefined>;\n\nexport interface IRequestContext {\n ip: string;\n}\n\nexport interface ICredential {\n access_key: string;\n secret_key: string;\n}\n\nexport interface IApiError {\n code: number;\n msg: string;\n}\n\nexport const BINANCE_TOKEN_BUCKET_OPTIONS_BY_ID: Record<string, TokenBucketOptions> = {\n 'api.binance.com': {\n capacity: 6000,\n refillInterval: 60_000,\n refillAmount: 6000,\n },\n 'fapi.binance.com': {\n capacity: 2400,\n refillInterval: 60_000,\n refillAmount: 2400,\n },\n 'papi.binance.com': {\n capacity: 6000,\n refillInterval: 60_000,\n refillAmount: 6000,\n },\n 'order/unified/minute': {\n capacity: 1200,\n refillInterval: 60_000,\n refillAmount: 1200,\n },\n};\n\nexport const getTokenBucketOptions = (baseKey: string): TokenBucketOptions | undefined =>\n BINANCE_TOKEN_BUCKET_OPTIONS_BY_ID[baseKey];\n\nexport const spotAPIBucket = tokenBucket(\n 'api.binance.com',\n BINANCE_TOKEN_BUCKET_OPTIONS_BY_ID['api.binance.com'],\n);\n\nexport const futureAPIBucket = tokenBucket(\n 'fapi.binance.com',\n BINANCE_TOKEN_BUCKET_OPTIONS_BY_ID['fapi.binance.com'],\n);\n\nexport const unifiedAPIBucket = tokenBucket(\n 'papi.binance.com',\n BINANCE_TOKEN_BUCKET_OPTIONS_BY_ID['papi.binance.com'],\n);\n\nexport const unifiedOrderAPIBucket = tokenBucket(\n 'order/unified/minute',\n BINANCE_TOKEN_BUCKET_OPTIONS_BY_ID['order/unified/minute'],\n);\n\nexport const buildTokenBucketKey = (baseKey: string, ip: string): string => encodePath([baseKey, ip]);\n\nconst resolveLocalPublicIp = (): string => {\n const ip = terminal.terminalInfo.tags?.public_ip?.trim();\n if (ip) return ip;\n const now = Date.now();\n const lastLoggedAt = missingPublicIpLogAtByTerminalId.get(terminal.terminal_id) ?? 0;\n if (now - lastLoggedAt > MISSING_PUBLIC_IP_LOG_INTERVAL) {\n missingPublicIpLogAtByTerminalId.set(terminal.terminal_id, now);\n console.info(formatTime(Date.now()), 'missing terminal public_ip tag, fallback to public-ip-unknown');\n }\n return 'public-ip-unknown';\n};\n\nexport const createRequestContext = (): IRequestContext => {\n if (shouldUseHttpProxy) {\n const ip = selectHTTPProxyIpRoundRobin(terminal);\n return { ip };\n }\n return { ip: resolveLocalPublicIp() };\n};\n\n// 每个接口单独进行主动限流控制\nconst mapPathToRetryAfterUntil: Record<string, number> = {};\n\nexport const isApiError = <T>(value: T | IApiError): value is IApiError =>\n typeof (value as IApiError)?.code === 'number' && typeof (value as IApiError)?.msg === 'string';\n\nconst appendParams = (url: URL, params?: RequestParams) => {\n if (!params) return;\n const entries = Object.entries(params).filter(([, value]) => value !== undefined);\n const timestampEntry = entries.find(([key]) => key === 'timestamp');\n const restEntries = entries.filter(([key]) => key !== 'timestamp');\n restEntries\n .sort(([keyA], [keyB]) => keyA.localeCompare(keyB))\n .forEach(([key, value]) => {\n url.searchParams.set(key, String(value));\n });\n if (timestampEntry) {\n url.searchParams.set(timestampEntry[0], String(timestampEntry[1]));\n }\n};\n\nconst callApi = async <T>(\n method: HttpMethod,\n endpoint: string,\n params?: RequestParams,\n credential?: ICredential,\n requestContext?: IRequestContext,\n): Promise<T> => {\n if (shouldUseHttpProxy && !requestContext) {\n throw newError('E_PROXY_TARGET_NOT_FOUND', { reason: 'Missing request context' });\n }\n const url = new URL(endpoint);\n const normalizedParams: RequestParams = { ...params };\n if (credential) {\n if (normalizedParams.recvWindow === undefined) {\n // FYI https://developers.binance.com/docs/derivatives/usds-margined-futures/general-info#timing-security\n normalizedParams.recvWindow = 5000;\n }\n if (normalizedParams.timestamp === undefined) {\n normalizedParams.timestamp = Date.now();\n }\n }\n appendParams(url, normalizedParams);\n\n let headers: Record<string, string> | undefined;\n if (credential) {\n const signData = url.search.slice(1);\n\n const signature = encodeHex(\n await HmacSHA256(new TextEncoder().encode(signData), new TextEncoder().encode(credential.secret_key)),\n );\n url.searchParams.set('signature', signature);\n headers = {\n 'Content-Type': 'application/json;charset=utf-8',\n 'X-MBX-APIKEY': credential.access_key,\n };\n console.info(formatTime(Date.now()), 'request', method, url.host, url.pathname);\n } else {\n console.info(formatTime(Date.now()), 'request', method, url.host, url.pathname);\n }\n\n const retryAfterUntil = mapPathToRetryAfterUntil[endpoint];\n\n if (retryAfterUntil) {\n if (Date.now() <= retryAfterUntil) {\n // 主动限流\n throw newError('ACTIVE_RATE_LIMIT', {\n wait_time: `${retryAfterUntil - Date.now()}ms`,\n retryAfterUntil,\n url: `${url.host}${url.pathname}`,\n });\n }\n delete mapPathToRetryAfterUntil[endpoint];\n }\n\n MetricBinanceApiCounter.labels({ path: url.pathname, terminal_id: terminal.terminal_id }).inc();\n\n const proxyIp = shouldUseHttpProxy\n ? requestContext?.ip ?? selectHTTPProxyIpRoundRobin(terminal)\n : undefined;\n const res = await fetchImpl(\n url.href,\n shouldUseHttpProxy\n ? {\n method,\n headers,\n labels: proxyIp ? { ip: proxyIp } : undefined,\n terminal,\n }\n : {\n method,\n headers,\n },\n );\n const usedWeight1M = res.headers.get('x-mbx-used-weight-1m');\n const retryAfter = res.headers.get('Retry-After');\n if (retryAfter) {\n mapPathToRetryAfterUntil[endpoint] = Date.now() + parseInt(retryAfter, 10) * 1000;\n }\n console.info(\n formatTime(Date.now()),\n 'response',\n method,\n url.host,\n url.pathname,\n `status=${res.status}`,\n retryAfter ? `retryAfter=${retryAfter}` : '',\n `usedWeight1M=${usedWeight1M ?? 'N/A'}`,\n );\n if (usedWeight1M) {\n MetricBinanceApiUsedWeight.labels({ path: endpoint, terminal_id: terminal.terminal_id }).set(\n +usedWeight1M,\n );\n }\n return res.json() as Promise<T>;\n};\n\nexport const requestPublic = <T>(\n method: HttpMethod,\n endpoint: string,\n params?: RequestParams,\n requestContext?: IRequestContext,\n) => callApi<T>(method, endpoint, params, undefined, requestContext);\n\nexport const requestPrivate = <T>(\n credential: ICredential,\n method: HttpMethod,\n endpoint: string,\n params?: RequestParams,\n requestContext?: IRequestContext,\n) => callApi<T>(method, endpoint, params, credential, requestContext);\n\nexport const getDefaultCredential = (): ICredential => {\n const access_key = process.env.ACCESS_KEY;\n const secret_key = process.env.SECRET_KEY;\n if (!access_key || !secret_key) {\n throw new Error('Missing Binance credential: ACCESS_KEY and SECRET_KEY must be set');\n }\n return { access_key, secret_key };\n};\n"]}
|
package/dist/api/private-api.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { scopeError, tokenBucket } from '@yuants/utils';
|
|
2
|
-
import { buildTokenBucketKey, createRequestContext, getDefaultCredential, requestPrivate, } from './client';
|
|
2
|
+
import { buildTokenBucketKey, createRequestContext, getDefaultCredential, getTokenBucketOptions, requestPrivate, } from './client';
|
|
3
3
|
/**
|
|
4
4
|
* 查询账户信息(USER_DATA)
|
|
5
5
|
*
|
|
@@ -15,7 +15,7 @@ export const getUnifiedAccountInfo = (credential) => {
|
|
|
15
15
|
const weight = 20;
|
|
16
16
|
const requestContext = createRequestContext();
|
|
17
17
|
const bucketKey = buildTokenBucketKey(url.host, requestContext.ip);
|
|
18
|
-
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
18
|
+
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey, getTokenBucketOptions('order/unified/minute')).acquireSync(weight));
|
|
19
19
|
return requestPrivate(credential, 'GET', endpoint, undefined, requestContext);
|
|
20
20
|
};
|
|
21
21
|
/**
|
|
@@ -33,7 +33,7 @@ export const getUnifiedUmAccount = (credential) => {
|
|
|
33
33
|
const weight = 5;
|
|
34
34
|
const requestContext = createRequestContext();
|
|
35
35
|
const bucketKey = buildTokenBucketKey(url.host, requestContext.ip);
|
|
36
|
-
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
36
|
+
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey, getTokenBucketOptions('order/unified/minute')).acquireSync(weight));
|
|
37
37
|
return requestPrivate(credential, 'GET', endpoint, undefined, requestContext);
|
|
38
38
|
};
|
|
39
39
|
/**
|
|
@@ -59,7 +59,7 @@ export const getUnifiedUmOpenOrders = (credential, params) => {
|
|
|
59
59
|
bucketId: bucketKey,
|
|
60
60
|
weight,
|
|
61
61
|
hasSymbol: !!(params === null || params === void 0 ? void 0 : params.symbol),
|
|
62
|
-
}, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
62
|
+
}, () => tokenBucket(bucketKey, getTokenBucketOptions(url.host)).acquireSync(weight));
|
|
63
63
|
return requestPrivate(credential, 'GET', endpoint, params, requestContext);
|
|
64
64
|
};
|
|
65
65
|
/**
|
|
@@ -77,7 +77,7 @@ export const getUnifiedAccountBalance = (credential, params) => {
|
|
|
77
77
|
const weight = 20;
|
|
78
78
|
const requestContext = createRequestContext();
|
|
79
79
|
const bucketKey = buildTokenBucketKey(url.host, requestContext.ip);
|
|
80
|
-
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
80
|
+
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey, getTokenBucketOptions(url.host)).acquireSync(weight));
|
|
81
81
|
return requestPrivate(credential, 'GET', endpoint, params, requestContext);
|
|
82
82
|
};
|
|
83
83
|
/**
|
|
@@ -93,7 +93,7 @@ export const getSpotAccountInfo = (credential, params) => {
|
|
|
93
93
|
const weight = 20;
|
|
94
94
|
const requestContext = createRequestContext();
|
|
95
95
|
const bucketKey = buildTokenBucketKey(url.host, requestContext.ip);
|
|
96
|
-
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
96
|
+
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey, getTokenBucketOptions(url.host)).acquireSync(weight));
|
|
97
97
|
return requestPrivate(credential, 'GET', endpoint, params, requestContext);
|
|
98
98
|
};
|
|
99
99
|
/**
|
|
@@ -117,7 +117,7 @@ export const getSpotOpenOrders = (credential, params) => {
|
|
|
117
117
|
bucketId: bucketKey,
|
|
118
118
|
weight,
|
|
119
119
|
hasSymbol: !!(params === null || params === void 0 ? void 0 : params.symbol),
|
|
120
|
-
}, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
120
|
+
}, () => tokenBucket(bucketKey, getTokenBucketOptions(url.host)).acquireSync(weight));
|
|
121
121
|
return requestPrivate(credential, 'GET', endpoint, params, requestContext);
|
|
122
122
|
};
|
|
123
123
|
/**
|
|
@@ -133,7 +133,7 @@ export const postSpotOrder = (credential, params) => {
|
|
|
133
133
|
const weight = 1;
|
|
134
134
|
const requestContext = createRequestContext();
|
|
135
135
|
const bucketKey = buildTokenBucketKey(url.host, requestContext.ip);
|
|
136
|
-
scopeError('BINANCE_API_RATE_LIMIT', { method: 'POST', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
136
|
+
scopeError('BINANCE_API_RATE_LIMIT', { method: 'POST', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey, getTokenBucketOptions(url.host)).acquireSync(weight));
|
|
137
137
|
return requestPrivate(credential, 'POST', endpoint, params, requestContext);
|
|
138
138
|
};
|
|
139
139
|
/**
|
|
@@ -149,7 +149,7 @@ export const deleteSpotOrder = (credential, params) => {
|
|
|
149
149
|
const weight = 1;
|
|
150
150
|
const requestContext = createRequestContext();
|
|
151
151
|
const bucketKey = buildTokenBucketKey(url.host, requestContext.ip);
|
|
152
|
-
scopeError('BINANCE_API_RATE_LIMIT', { method: 'DELETE', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
152
|
+
scopeError('BINANCE_API_RATE_LIMIT', { method: 'DELETE', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey, getTokenBucketOptions(url.host)).acquireSync(weight));
|
|
153
153
|
return requestPrivate(credential, 'DELETE', endpoint, params, requestContext);
|
|
154
154
|
};
|
|
155
155
|
/**
|
|
@@ -169,7 +169,7 @@ export const postAssetTransfer = (credential, params) => {
|
|
|
169
169
|
const weight = 900;
|
|
170
170
|
const requestContext = createRequestContext();
|
|
171
171
|
const bucketKey = buildTokenBucketKey(url.host, requestContext.ip);
|
|
172
|
-
scopeError('BINANCE_API_RATE_LIMIT', { method: 'POST', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
172
|
+
scopeError('BINANCE_API_RATE_LIMIT', { method: 'POST', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey, getTokenBucketOptions(url.host)).acquireSync(weight));
|
|
173
173
|
return requestPrivate(credential, 'POST', endpoint, params, requestContext);
|
|
174
174
|
};
|
|
175
175
|
/**
|
|
@@ -189,7 +189,7 @@ export const postUnifiedAccountAutoCollection = (credential) => {
|
|
|
189
189
|
const weight = 750;
|
|
190
190
|
const requestContext = createRequestContext();
|
|
191
191
|
const bucketKey = buildTokenBucketKey(url.host, requestContext.ip);
|
|
192
|
-
scopeError('BINANCE_API_RATE_LIMIT', { method: 'POST', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
192
|
+
scopeError('BINANCE_API_RATE_LIMIT', { method: 'POST', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey, getTokenBucketOptions(url.host)).acquireSync(weight));
|
|
193
193
|
return requestPrivate(credential, 'POST', endpoint, undefined, requestContext);
|
|
194
194
|
};
|
|
195
195
|
/**
|
|
@@ -207,7 +207,7 @@ export const getDepositAddress = (credential, params) => {
|
|
|
207
207
|
const weight = 10;
|
|
208
208
|
const requestContext = createRequestContext();
|
|
209
209
|
const bucketKey = buildTokenBucketKey(url.host, requestContext.ip);
|
|
210
|
-
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
210
|
+
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey, getTokenBucketOptions(url.host)).acquireSync(weight));
|
|
211
211
|
return requestPrivate(credential, 'GET', endpoint, params, requestContext);
|
|
212
212
|
};
|
|
213
213
|
/**
|
|
@@ -223,7 +223,7 @@ export const getSubAccountList = (credential, params) => {
|
|
|
223
223
|
const weight = 1;
|
|
224
224
|
const requestContext = createRequestContext();
|
|
225
225
|
const bucketKey = buildTokenBucketKey(url.host, requestContext.ip);
|
|
226
|
-
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
226
|
+
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey, getTokenBucketOptions(url.host)).acquireSync(weight));
|
|
227
227
|
return requestPrivate(credential, 'GET', endpoint, params, requestContext);
|
|
228
228
|
};
|
|
229
229
|
/**
|
|
@@ -239,7 +239,7 @@ export const postWithdraw = (credential, params) => {
|
|
|
239
239
|
const weight = 600;
|
|
240
240
|
const requestContext = createRequestContext();
|
|
241
241
|
const bucketKey = buildTokenBucketKey(url.host, requestContext.ip);
|
|
242
|
-
scopeError('BINANCE_API_RATE_LIMIT', { method: 'POST', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
242
|
+
scopeError('BINANCE_API_RATE_LIMIT', { method: 'POST', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey, getTokenBucketOptions(url.host)).acquireSync(weight));
|
|
243
243
|
return requestPrivate(credential, 'POST', endpoint, params, requestContext);
|
|
244
244
|
};
|
|
245
245
|
/**
|
|
@@ -260,7 +260,7 @@ export const getWithdrawHistory = (credential, params) => {
|
|
|
260
260
|
const weight = 1;
|
|
261
261
|
const requestContext = createRequestContext();
|
|
262
262
|
const bucketKey = buildTokenBucketKey(url.host, requestContext.ip);
|
|
263
|
-
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
263
|
+
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey, getTokenBucketOptions(url.host)).acquireSync(weight));
|
|
264
264
|
return requestPrivate(credential, 'GET', endpoint, params, requestContext);
|
|
265
265
|
};
|
|
266
266
|
/**
|
|
@@ -276,7 +276,7 @@ export const getDepositHistory = (credential, params) => {
|
|
|
276
276
|
const weight = 1;
|
|
277
277
|
const requestContext = createRequestContext();
|
|
278
278
|
const bucketKey = buildTokenBucketKey(url.host, requestContext.ip);
|
|
279
|
-
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
279
|
+
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey, getTokenBucketOptions(url.host)).acquireSync(weight));
|
|
280
280
|
return requestPrivate(credential, 'GET', endpoint, params, requestContext);
|
|
281
281
|
};
|
|
282
282
|
/**
|
|
@@ -292,7 +292,7 @@ export const postUmOrder = (credential, params) => {
|
|
|
292
292
|
const weight = 1;
|
|
293
293
|
const requestContext = createRequestContext();
|
|
294
294
|
const bucketKey = buildTokenBucketKey('order/unified/minute', requestContext.ip);
|
|
295
|
-
scopeError('BINANCE_UNIFIED_ORDER_API_RATE_LIMIT', { method: 'POST', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
295
|
+
scopeError('BINANCE_UNIFIED_ORDER_API_RATE_LIMIT', { method: 'POST', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey, getTokenBucketOptions(url.host)).acquireSync(weight));
|
|
296
296
|
return requestPrivate(credential, 'POST', endpoint, params, requestContext);
|
|
297
297
|
};
|
|
298
298
|
export const deleteUmOrder = (credential, params) => {
|
|
@@ -301,7 +301,7 @@ export const deleteUmOrder = (credential, params) => {
|
|
|
301
301
|
const weight = 1;
|
|
302
302
|
const requestContext = createRequestContext();
|
|
303
303
|
const bucketKey = buildTokenBucketKey('order/unified/minute', requestContext.ip);
|
|
304
|
-
scopeError('BINANCE_UNIFIED_ORDER_API_RATE_LIMIT', { method: 'DELETE', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
304
|
+
scopeError('BINANCE_UNIFIED_ORDER_API_RATE_LIMIT', { method: 'DELETE', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey, getTokenBucketOptions(url.host)).acquireSync(weight));
|
|
305
305
|
return requestPrivate(credential, 'DELETE', endpoint, params, requestContext);
|
|
306
306
|
};
|
|
307
307
|
/**
|
|
@@ -317,7 +317,7 @@ export const getUMIncome = (credential, params) => {
|
|
|
317
317
|
const weight = 30;
|
|
318
318
|
const requestContext = createRequestContext();
|
|
319
319
|
const bucketKey = buildTokenBucketKey(url.host, requestContext.ip);
|
|
320
|
-
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
320
|
+
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey, getTokenBucketOptions(url.host)).acquireSync(weight));
|
|
321
321
|
return requestPrivate(credential, 'GET', endpoint, params, requestContext);
|
|
322
322
|
};
|
|
323
323
|
/**
|
|
@@ -333,7 +333,7 @@ export const getAccountIncome = (credential, params) => {
|
|
|
333
333
|
const weight = 30;
|
|
334
334
|
const requestContext = createRequestContext();
|
|
335
335
|
const bucketKey = buildTokenBucketKey(url.host, requestContext.ip);
|
|
336
|
-
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
336
|
+
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey, getTokenBucketOptions(url.host)).acquireSync(weight));
|
|
337
337
|
return requestPrivate(credential, 'GET', endpoint, params, requestContext);
|
|
338
338
|
};
|
|
339
339
|
/**
|
|
@@ -349,7 +349,7 @@ export const postSpotOrderCancelReplace = (credential, params) => {
|
|
|
349
349
|
const weight = 1;
|
|
350
350
|
const requestContext = createRequestContext();
|
|
351
351
|
const bucketKey = buildTokenBucketKey(url.host, requestContext.ip);
|
|
352
|
-
scopeError('BINANCE_API_RATE_LIMIT', { method: 'POST', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
352
|
+
scopeError('BINANCE_API_RATE_LIMIT', { method: 'POST', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey, getTokenBucketOptions(url.host)).acquireSync(weight));
|
|
353
353
|
return requestPrivate(credential, 'POST', endpoint, params, requestContext);
|
|
354
354
|
};
|
|
355
355
|
/**
|
|
@@ -365,7 +365,7 @@ export const putUmOrder = (credential, params) => {
|
|
|
365
365
|
const weight = 1;
|
|
366
366
|
const requestContext = createRequestContext();
|
|
367
367
|
const bucketKey = buildTokenBucketKey(url.host, requestContext.ip);
|
|
368
|
-
scopeError('BINANCE_API_RATE_LIMIT', { method: 'PUT', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
368
|
+
scopeError('BINANCE_API_RATE_LIMIT', { method: 'PUT', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey, getTokenBucketOptions(url.host)).acquireSync(weight));
|
|
369
369
|
return requestPrivate(credential, 'PUT', endpoint, params, requestContext);
|
|
370
370
|
};
|
|
371
371
|
/**
|
|
@@ -382,7 +382,7 @@ export const getMarginAllPairs = (params) => {
|
|
|
382
382
|
const weight = 1;
|
|
383
383
|
const requestContext = createRequestContext();
|
|
384
384
|
const bucketKey = buildTokenBucketKey(url.host, requestContext.ip);
|
|
385
|
-
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
385
|
+
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey, getTokenBucketOptions(url.host)).acquireSync(weight));
|
|
386
386
|
return requestPrivate(credential, 'GET', endpoint, params, requestContext);
|
|
387
387
|
};
|
|
388
388
|
/**
|
|
@@ -397,7 +397,7 @@ export const getMarginNextHourlyInterestRate = (params) => {
|
|
|
397
397
|
const weight = 1;
|
|
398
398
|
const requestContext = createRequestContext();
|
|
399
399
|
const bucketKey = buildTokenBucketKey(url.host, requestContext.ip);
|
|
400
|
-
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
400
|
+
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey, getTokenBucketOptions(url.host)).acquireSync(weight));
|
|
401
401
|
return requestPrivate(credential, 'GET', endpoint, params, requestContext);
|
|
402
402
|
};
|
|
403
403
|
/**
|
|
@@ -412,7 +412,7 @@ export const getMarginInterestRateHistory = (params) => {
|
|
|
412
412
|
const weight = 1;
|
|
413
413
|
const requestContext = createRequestContext();
|
|
414
414
|
const bucketKey = buildTokenBucketKey(url.host, requestContext.ip);
|
|
415
|
-
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
415
|
+
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey, getTokenBucketOptions(url.host)).acquireSync(weight));
|
|
416
416
|
return requestPrivate(credential, 'GET', endpoint, params, requestContext);
|
|
417
417
|
};
|
|
418
418
|
/**
|
|
@@ -428,7 +428,7 @@ export const getUserAsset = (credential, params) => {
|
|
|
428
428
|
const weight = 1;
|
|
429
429
|
const requestContext = createRequestContext();
|
|
430
430
|
const bucketKey = buildTokenBucketKey(url.host, requestContext.ip);
|
|
431
|
-
scopeError('BINANCE_API_RATE_LIMIT', { method: 'POST', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
431
|
+
scopeError('BINANCE_API_RATE_LIMIT', { method: 'POST', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey, getTokenBucketOptions(url.host)).acquireSync(weight));
|
|
432
432
|
return requestPrivate(credential, 'POST', endpoint, params, requestContext);
|
|
433
433
|
};
|
|
434
434
|
/**
|
|
@@ -444,7 +444,7 @@ export const getFundingAsset = (credential, params) => {
|
|
|
444
444
|
const weight = 1;
|
|
445
445
|
const requestContext = createRequestContext();
|
|
446
446
|
const bucketKey = buildTokenBucketKey(url.host, requestContext.ip);
|
|
447
|
-
scopeError('BINANCE_API_RATE_LIMIT', { method: 'POST', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
447
|
+
scopeError('BINANCE_API_RATE_LIMIT', { method: 'POST', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey, getTokenBucketOptions(url.host)).acquireSync(weight));
|
|
448
448
|
return requestPrivate(credential, 'POST', endpoint, params, requestContext);
|
|
449
449
|
};
|
|
450
450
|
/**
|
|
@@ -460,7 +460,7 @@ export const getUmAccountTradeList = (credential, params) => {
|
|
|
460
460
|
const weight = 5;
|
|
461
461
|
const requestContext = createRequestContext();
|
|
462
462
|
const bucketKey = buildTokenBucketKey(url.host, requestContext.ip);
|
|
463
|
-
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey).acquireSync(weight));
|
|
463
|
+
scopeError('BINANCE_API_RATE_LIMIT', { method: 'GET', endpoint, host: url.host, path: url.pathname, bucketId: bucketKey, weight }, () => tokenBucket(bucketKey, getTokenBucketOptions(url.host)).acquireSync(weight));
|
|
464
464
|
return requestPrivate(credential, 'GET', endpoint, params, requestContext);
|
|
465
465
|
};
|
|
466
466
|
//# sourceMappingURL=private-api.js.map
|