@yuants/vendor-gate 0.4.1 → 0.4.3
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/http-client.js +89 -0
- package/dist/api/http-client.js.map +1 -0
- package/dist/api/private-api.js +108 -0
- package/dist/api/private-api.js.map +1 -0
- package/dist/api/public-api.js +37 -0
- package/dist/api/public-api.js.map +1 -0
- package/dist/api/rate-limiter.js +60 -0
- package/dist/api/rate-limiter.js.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/services/account-actions-with-credential.js +37 -0
- package/dist/services/account-actions-with-credential.js.map +1 -0
- package/dist/services/accounts/future.js +56 -0
- package/dist/services/accounts/future.js.map +1 -0
- package/dist/services/accounts/profile.js +25 -0
- package/dist/services/accounts/profile.js.map +1 -0
- package/dist/services/accounts/spot.js +19 -0
- package/dist/services/accounts/spot.js.map +1 -0
- package/dist/services/accounts/unified.js +50 -0
- package/dist/services/accounts/unified.js.map +1 -0
- package/dist/services/default-credential.js +15 -0
- package/dist/services/default-credential.js.map +1 -0
- package/dist/services/legacy.js +53 -0
- package/dist/services/legacy.js.map +1 -0
- package/dist/services/markets/interest-rate.js +30 -0
- package/dist/services/markets/interest-rate.js.map +1 -0
- package/dist/services/markets/product.js +38 -0
- package/dist/services/markets/product.js.map +1 -0
- package/dist/services/order-actions-with-credential.js +18 -0
- package/dist/services/order-actions-with-credential.js.map +1 -0
- package/dist/services/orders/cancelOrder.js +8 -0
- package/dist/services/orders/cancelOrder.js.map +1 -0
- package/dist/services/orders/listOrders.js +31 -0
- package/dist/services/orders/listOrders.js.map +1 -0
- package/dist/services/orders/submitOrder.js +50 -0
- package/dist/services/orders/submitOrder.js.map +1 -0
- package/dist/services/transfer.js +107 -0
- package/dist/services/transfer.js.map +1 -0
- package/dist/vendor-gate.d.ts +1 -0
- package/lib/api/http-client.d.ts +9 -0
- package/lib/api/http-client.d.ts.map +1 -0
- package/lib/api/http-client.js +97 -0
- package/lib/api/http-client.js.map +1 -0
- package/lib/api/private-api.d.ts +343 -0
- package/lib/api/private-api.d.ts.map +1 -0
- package/lib/api/private-api.js +126 -0
- package/lib/api/private-api.js.map +1 -0
- package/lib/api/public-api.d.ts +149 -0
- package/lib/api/public-api.d.ts.map +1 -0
- package/lib/api/public-api.js +45 -0
- package/lib/api/public-api.js.map +1 -0
- package/lib/api/rate-limiter.d.ts +8 -0
- package/lib/api/rate-limiter.d.ts.map +1 -0
- package/lib/api/rate-limiter.js +64 -0
- package/lib/api/rate-limiter.js.map +1 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +9 -0
- package/lib/index.js.map +1 -0
- package/lib/services/account-actions-with-credential.d.ts +2 -0
- package/lib/services/account-actions-with-credential.d.ts.map +1 -0
- package/lib/services/account-actions-with-credential.js +39 -0
- package/lib/services/account-actions-with-credential.js.map +1 -0
- package/lib/services/accounts/future.d.ts +13 -0
- package/lib/services/accounts/future.d.ts.map +1 -0
- package/lib/services/accounts/future.js +61 -0
- package/lib/services/accounts/future.js.map +1 -0
- package/lib/services/accounts/profile.d.ts +10 -0
- package/lib/services/accounts/profile.d.ts.map +1 -0
- package/lib/services/accounts/profile.js +29 -0
- package/lib/services/accounts/profile.js.map +1 -0
- package/lib/services/accounts/spot.d.ts +11 -0
- package/lib/services/accounts/spot.d.ts.map +1 -0
- package/lib/services/accounts/spot.js +23 -0
- package/lib/services/accounts/spot.js.map +1 -0
- package/lib/services/accounts/unified.d.ts +12 -0
- package/lib/services/accounts/unified.d.ts.map +1 -0
- package/lib/services/accounts/unified.js +54 -0
- package/lib/services/accounts/unified.js.map +1 -0
- package/lib/services/default-credential.d.ts +3 -0
- package/lib/services/default-credential.d.ts.map +1 -0
- package/lib/services/default-credential.js +19 -0
- package/lib/services/default-credential.js.map +1 -0
- package/lib/services/legacy.d.ts +2 -0
- package/lib/services/legacy.d.ts.map +1 -0
- package/lib/services/legacy.js +55 -0
- package/lib/services/legacy.js.map +1 -0
- package/lib/services/markets/interest-rate.d.ts +2 -0
- package/lib/services/markets/interest-rate.d.ts.map +1 -0
- package/lib/services/markets/interest-rate.js +32 -0
- package/lib/services/markets/interest-rate.js.map +1 -0
- package/lib/services/markets/product.d.ts +3 -0
- package/lib/services/markets/product.d.ts.map +1 -0
- package/lib/services/markets/product.js +41 -0
- package/lib/services/markets/product.js.map +1 -0
- package/lib/services/order-actions-with-credential.d.ts +2 -0
- package/lib/services/order-actions-with-credential.d.ts.map +1 -0
- package/lib/services/order-actions-with-credential.js +20 -0
- package/lib/services/order-actions-with-credential.js.map +1 -0
- package/lib/services/orders/cancelOrder.d.ts +4 -0
- package/lib/services/orders/cancelOrder.d.ts.map +1 -0
- package/lib/services/orders/cancelOrder.js +12 -0
- package/lib/services/orders/cancelOrder.js.map +1 -0
- package/lib/services/orders/listOrders.d.ts +4 -0
- package/lib/services/orders/listOrders.d.ts.map +1 -0
- package/lib/services/orders/listOrders.js +35 -0
- package/lib/services/orders/listOrders.js.map +1 -0
- package/lib/services/orders/submitOrder.d.ts +4 -0
- package/lib/services/orders/submitOrder.d.ts.map +1 -0
- package/lib/services/orders/submitOrder.js +54 -0
- package/lib/services/orders/submitOrder.js.map +1 -0
- package/lib/services/transfer.d.ts +2 -0
- package/lib/services/transfer.d.ts.map +1 -0
- package/lib/services/transfer.js +109 -0
- package/lib/services/transfer.js.map +1 -0
- package/lib/tsdoc-metadata.json +11 -0
- package/package.json +13 -17
- package/temp/package-deps.json +46 -0
- package/temp/vendor-gate.api.json +177 -0
- package/temp/vendor-gate.api.md +9 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { formatTime } from '@yuants/utils';
|
|
2
|
+
// @ts-ignore
|
|
3
|
+
import CryptoJS from 'crypto-js';
|
|
4
|
+
const BASE_URL = 'https://api.gateio.ws';
|
|
5
|
+
const serializeQueryParams = (params) => {
|
|
6
|
+
if (!params)
|
|
7
|
+
return undefined;
|
|
8
|
+
const normalizedEntries = Object.entries(params).filter(([, value]) => value !== undefined);
|
|
9
|
+
if (normalizedEntries.length === 0) {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
return Object.fromEntries(normalizedEntries.map(([key, value]) => [key, `${value}`]));
|
|
13
|
+
};
|
|
14
|
+
const createRequestArtifacts = (method, path, params) => {
|
|
15
|
+
const url = new URL(BASE_URL);
|
|
16
|
+
url.pathname = path;
|
|
17
|
+
const searchParams = serializeQueryParams(params);
|
|
18
|
+
if (method === 'GET' && searchParams) {
|
|
19
|
+
Object.entries(searchParams).forEach(([key, value]) => url.searchParams.set(key, value));
|
|
20
|
+
}
|
|
21
|
+
const rawBody = method === 'GET' ? '' : JSON.stringify(params);
|
|
22
|
+
const body = rawBody;
|
|
23
|
+
return { url, body };
|
|
24
|
+
};
|
|
25
|
+
const toHeaderObject = (headers) => {
|
|
26
|
+
const iterable = headers;
|
|
27
|
+
return Object.fromEntries(Array.from(iterable));
|
|
28
|
+
};
|
|
29
|
+
const parseJSON = async (response, path, params) => {
|
|
30
|
+
const text = await response.text();
|
|
31
|
+
if (process.env.LOG_LEVEL === 'DEBUG') {
|
|
32
|
+
console.debug(formatTime(Date.now()), 'GateResponse', path, JSON.stringify(params !== null && params !== void 0 ? params : {}), text, {
|
|
33
|
+
status: response.status,
|
|
34
|
+
headers: toHeaderObject(response.headers),
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
return JSON.parse(text);
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
console.error(formatTime(Date.now()), 'GateRequestFailed', path, JSON.stringify(params !== null && params !== void 0 ? params : {}), text, {
|
|
42
|
+
status: response.status,
|
|
43
|
+
headers: toHeaderObject(response.headers),
|
|
44
|
+
});
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
export const requestPublic = async (method, path, params) => {
|
|
49
|
+
const { url, body } = createRequestArtifacts(method, path, params);
|
|
50
|
+
const headers = {
|
|
51
|
+
Accept: 'application/json',
|
|
52
|
+
};
|
|
53
|
+
if (body) {
|
|
54
|
+
headers['Content-Type'] = 'application/json';
|
|
55
|
+
}
|
|
56
|
+
console.info(formatTime(Date.now()), method, url.href);
|
|
57
|
+
const response = await fetch(url.href, {
|
|
58
|
+
method,
|
|
59
|
+
headers,
|
|
60
|
+
body: body || undefined,
|
|
61
|
+
});
|
|
62
|
+
return parseJSON(response, path, params);
|
|
63
|
+
};
|
|
64
|
+
export const requestPrivate = async (credential, method, path, params) => {
|
|
65
|
+
const { url, body } = createRequestArtifacts(method, path, params);
|
|
66
|
+
const timestamp = Date.now() / 1000;
|
|
67
|
+
const bodyDigest = CryptoJS.enc.Hex.stringify(CryptoJS.SHA512(body));
|
|
68
|
+
const signTarget = `${method}\n${url.pathname}\n${url.searchParams}\n${bodyDigest}\n${timestamp}`;
|
|
69
|
+
const sign = CryptoJS.enc.Hex.stringify(CryptoJS.HmacSHA512(signTarget, credential.secret_key));
|
|
70
|
+
const headers = {
|
|
71
|
+
Accept: 'application/json',
|
|
72
|
+
'Content-Type': 'application/json',
|
|
73
|
+
KEY: credential.access_key,
|
|
74
|
+
SIGN: sign,
|
|
75
|
+
Timestamp: `${timestamp}`,
|
|
76
|
+
};
|
|
77
|
+
// Add Channel ID header if exists
|
|
78
|
+
if (process.env.CHANNEL_ID) {
|
|
79
|
+
headers['X-Gate-Channel-Id'] = process.env.CHANNEL_ID;
|
|
80
|
+
}
|
|
81
|
+
console.info(formatTime(Date.now()), method, url.href, JSON.stringify(headers), body);
|
|
82
|
+
const response = await fetch(url.href, {
|
|
83
|
+
method,
|
|
84
|
+
headers,
|
|
85
|
+
body: body || undefined,
|
|
86
|
+
});
|
|
87
|
+
return parseJSON(response, path, params);
|
|
88
|
+
};
|
|
89
|
+
//# sourceMappingURL=http-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http-client.js","sourceRoot":"","sources":["../../src/api/http-client.ts"],"names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,KAAK,GAAG,CAAC,YAAY,KAAK,UAAU,KAAK,SAAS,EAAE,CAAC;IAClG,MAAM,IAAI,GAAG,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,SAAS,CAAC,QAAQ,CAAC,UAAU,CAAC,UAAU,EAAE,UAAU,CAAC,UAAU,CAAC,CAAC,CAAC;IAEhG,MAAM,OAAO,GAA2B;QACtC,MAAM,EAAE,kBAAkB;QAC1B,cAAc,EAAE,kBAAkB;QAClC,GAAG,EAAE,UAAU,CAAC,UAAU;QAC1B,IAAI,EAAE,IAAI;QACV,SAAS,EAAE,GAAG,SAAS,EAAE;KAC1B,CAAC;IAEF,kCAAkC;IAClC,IAAI,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE;QAC1B,OAAO,CAAC,mBAAmB,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC;KACvD;IAED,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,EAAE,IAAI,CAAC,CAAC;IAEtF,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE;QACrC,MAAM;QACN,OAAO;QACP,IAAI,EAAE,IAAI,IAAI,SAAS;KACxB,CAAC,CAAC;IACH,OAAO,SAAS,CAAY,QAAQ,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;AACtD,CAAC,CAAC","sourcesContent":["import { formatTime } from '@yuants/utils';\n// @ts-ignore\nimport CryptoJS from 'crypto-js';\n\nconst BASE_URL = 'https://api.gateio.ws';\n\nexport type HttpMethod = 'GET' | 'POST' | 'DELETE' | 'PUT';\n\nexport type GateParams = Record<string, string | number | boolean | undefined>;\n\nexport interface IGateCredential {\n access_key: string;\n secret_key: string;\n}\n\ninterface IRequestArtifacts {\n url: URL;\n body: string;\n}\n\nconst serializeQueryParams = (params?: GateParams): Record<string, string> | undefined => {\n if (!params) return undefined;\n const normalizedEntries = Object.entries(params).filter(([, value]) => value !== undefined);\n if (normalizedEntries.length === 0) {\n return undefined;\n }\n return Object.fromEntries(normalizedEntries.map(([key, value]) => [key, `${value}`]));\n};\n\nconst createRequestArtifacts = (method: HttpMethod, path: string, params?: GateParams): IRequestArtifacts => {\n const url = new URL(BASE_URL);\n url.pathname = path;\n const searchParams = serializeQueryParams(params);\n if (method === 'GET' && searchParams) {\n Object.entries(searchParams).forEach(([key, value]) => url.searchParams.set(key, value));\n }\n const rawBody = method === 'GET' ? '' : JSON.stringify(params);\n const body = rawBody;\n return { url, body };\n};\n\nconst toHeaderObject = (headers: Headers): Record<string, string> => {\n const iterable = headers as unknown as Iterable<[string, string]>;\n return Object.fromEntries(Array.from(iterable));\n};\n\nconst parseJSON = async <TResponse>(\n response: Response,\n path: string,\n params?: GateParams,\n): Promise<TResponse> => {\n const text = await response.text();\n if (process.env.LOG_LEVEL === 'DEBUG') {\n console.debug(formatTime(Date.now()), 'GateResponse', path, JSON.stringify(params ?? {}), text, {\n status: response.status,\n headers: toHeaderObject(response.headers),\n });\n }\n try {\n return JSON.parse(text) as TResponse;\n } catch (error) {\n console.error(formatTime(Date.now()), 'GateRequestFailed', path, JSON.stringify(params ?? {}), text, {\n status: response.status,\n headers: toHeaderObject(response.headers),\n });\n throw error;\n }\n};\n\nexport const requestPublic = async <TResponse>(\n method: HttpMethod,\n path: string,\n params?: GateParams,\n): Promise<TResponse> => {\n const { url, body } = createRequestArtifacts(method, path, params);\n const headers: Record<string, string> = {\n Accept: 'application/json',\n };\n if (body) {\n headers['Content-Type'] = 'application/json';\n }\n console.info(formatTime(Date.now()), method, url.href);\n const response = await fetch(url.href, {\n method,\n headers,\n body: body || undefined,\n });\n return parseJSON<TResponse>(response, path, params);\n};\n\nexport const requestPrivate = async <TResponse>(\n credential: IGateCredential,\n method: HttpMethod,\n path: string,\n params?: GateParams,\n): Promise<TResponse> => {\n const { url, body } = createRequestArtifacts(method, path, params);\n const timestamp = Date.now() / 1000;\n const bodyDigest = CryptoJS.enc.Hex.stringify(CryptoJS.SHA512(body));\n const signTarget = `${method}\\n${url.pathname}\\n${url.searchParams}\\n${bodyDigest}\\n${timestamp}`;\n const sign = CryptoJS.enc.Hex.stringify(CryptoJS.HmacSHA512(signTarget, credential.secret_key));\n\n const headers: Record<string, string> = {\n Accept: 'application/json',\n 'Content-Type': 'application/json',\n KEY: credential.access_key,\n SIGN: sign,\n Timestamp: `${timestamp}`,\n };\n\n // Add Channel ID header if exists\n if (process.env.CHANNEL_ID) {\n headers['X-Gate-Channel-Id'] = process.env.CHANNEL_ID;\n }\n\n console.info(formatTime(Date.now()), method, url.href, JSON.stringify(headers), body);\n\n const response = await fetch(url.href, {\n method,\n headers,\n body: body || undefined,\n });\n return parseJSON<TResponse>(response, path, params);\n};\n"]}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { requestPrivate } from './http-client';
|
|
2
|
+
const callPrivate = (credential, method, path, params) => requestPrivate(credential, method, path, params);
|
|
3
|
+
/**
|
|
4
|
+
* 获取用户账户信息
|
|
5
|
+
*
|
|
6
|
+
* https://www.gate.io/docs/developers/apiv4/zh_CN/#%E8%8E%B7%E5%8F%96%E7%94%A8%E6%88%B7%E8%B4%A6%E6%88%B7%E4%BF%A1%E6%81%AF
|
|
7
|
+
*/
|
|
8
|
+
export const getAccountDetail = (credential) => callPrivate(credential, 'GET', '/api/v4/account/detail');
|
|
9
|
+
export const getUnifiedAccounts = (credential, params) => callPrivate(credential, 'GET', '/api/v4/unified/accounts', params);
|
|
10
|
+
/**
|
|
11
|
+
* 获取用户仓位列表
|
|
12
|
+
*
|
|
13
|
+
* https://www.gate.io/docs/developers/apiv4/zh_CN/#%E8%8E%B7%E5%8F%96%E7%94%A8%E6%88%B7%E4%BB%93%E4%BD%8D%E5%88%97%E8%A1%A8
|
|
14
|
+
*/
|
|
15
|
+
export const getFuturePositions = (credential, quote_currency, params) => callPrivate(credential, 'GET', `/api/v4/futures/${quote_currency}/positions`, params);
|
|
16
|
+
/**
|
|
17
|
+
* 查询合约订单列表
|
|
18
|
+
*
|
|
19
|
+
* https://www.gate.io/docs/developers/apiv4/zh_CN/#%E6%9F%A5%E8%AF%A2%E5%90%88%E7%BA%A6%E8%AE%A2%E5%8D%95%E5%88%97%E8%A1%A8
|
|
20
|
+
*/
|
|
21
|
+
export const getFuturesOrders = (credential, settle, params) => callPrivate(credential, 'GET', `/api/v4/futures/${settle}/orders`, params);
|
|
22
|
+
/**
|
|
23
|
+
* 获取合约账号
|
|
24
|
+
*
|
|
25
|
+
* https://www.gate.io/docs/developers/apiv4/zh_CN/#%E8%8E%B7%E5%8F%96%E5%90%88%E7%BA%A6%E8%B4%A6%E5%8F%B7
|
|
26
|
+
*/
|
|
27
|
+
export const getFuturesAccounts = (credential, settle) => callPrivate(credential, 'GET', `/api/v4/futures/${settle}/accounts`);
|
|
28
|
+
/**
|
|
29
|
+
* 合约交易下单
|
|
30
|
+
*
|
|
31
|
+
* 下单时指定的是合约张数 size ,而非币的数量,每一张合约对应的币的数量是合约详情接口里返回的 quanto_multiplier
|
|
32
|
+
*
|
|
33
|
+
* 0 成交的订单在撤单 10 分钟之后无法再获取到,会提到订单不存在
|
|
34
|
+
*
|
|
35
|
+
* 设置 reduce_only 为 true 可以防止在减仓的时候穿仓
|
|
36
|
+
*
|
|
37
|
+
* 单仓模式下,如果需要平仓,需要设置 size 为 0 ,close 为 true
|
|
38
|
+
*
|
|
39
|
+
* 双仓模式下,平仓需要使用 auto_size 来设置平仓方向,并同时设置 reduce_only 为 true,size 为 0
|
|
40
|
+
*
|
|
41
|
+
* 设置 stp_act 决定使用限制用户自成交的策略,详细用法参考body参数stp_act
|
|
42
|
+
*
|
|
43
|
+
* https://www.gate.io/docs/developers/apiv4/zh_CN/#%E5%90%88%E7%BA%A6%E4%BA%A4%E6%98%93%E4%B8%8B%E5%8D%95
|
|
44
|
+
*/
|
|
45
|
+
export const postFutureOrders = (credential, settle, params) => callPrivate(credential, 'POST', `/api/v4/futures/${settle}/orders`, params);
|
|
46
|
+
/**
|
|
47
|
+
* 撤销单个订单
|
|
48
|
+
*
|
|
49
|
+
* https://www.gate.io/docs/developers/apiv4/zh_CN/#%E6%92%A4%E9%94%80%E5%8D%95%E4%B8%AA%E8%AE%A2%E5%8D%95-2
|
|
50
|
+
*/
|
|
51
|
+
export const deleteFutureOrders = (credential, settle, order_id) => callPrivate(credential, 'DELETE', `/api/v4/futures/${settle}/orders/${order_id}`);
|
|
52
|
+
/**
|
|
53
|
+
* 提现
|
|
54
|
+
*
|
|
55
|
+
* POST /withdrawals
|
|
56
|
+
*
|
|
57
|
+
* https://www.gate.io/docs/developers/apiv4/zh_CN/#%E6%8F%90%E7%8E%B0
|
|
58
|
+
*/
|
|
59
|
+
export const postWithdrawals = (credential, params) => callPrivate(credential, 'POST', '/api/v4/withdrawals', params);
|
|
60
|
+
/**
|
|
61
|
+
* 获取币种充值地址
|
|
62
|
+
*
|
|
63
|
+
* https://www.gate.io/docs/developers/apiv4/zh_CN/#%E8%8E%B7%E5%8F%96%E5%B8%81%E7%A7%8D%E5%85%85%E5%80%BC%E5%9C%B0%E5%9D%80
|
|
64
|
+
*/
|
|
65
|
+
export const getDepositAddress = (credential, params) => callPrivate(credential, 'GET', '/api/v4/wallet/deposit_address', params);
|
|
66
|
+
/**
|
|
67
|
+
* 创建新的子账户
|
|
68
|
+
*
|
|
69
|
+
* https://www.gate.io/docs/developers/apiv4/zh_CN/#%E5%88%9B%E5%BB%BA%E6%96%B0%E7%9A%84%E5%AD%90%E8%B4%A6%E6%88%B7
|
|
70
|
+
*/
|
|
71
|
+
export const getSubAccountList = (credential, params) => callPrivate(credential, 'GET', '/api/v4/sub_accounts', params);
|
|
72
|
+
/**
|
|
73
|
+
* 获取充值记录
|
|
74
|
+
*
|
|
75
|
+
* 记录查询时间范围不允许超过 30 天
|
|
76
|
+
*
|
|
77
|
+
* https://www.gate.io/docs/developers/apiv4/zh_CN/#%E8%8E%B7%E5%8F%96%E5%85%85%E5%80%BC%E8%AE%B0%E5%BD%95
|
|
78
|
+
*/
|
|
79
|
+
export const getDepositHistory = (credential, params) => callPrivate(credential, 'GET', '/api/v4/wallet/deposits', params);
|
|
80
|
+
/**
|
|
81
|
+
* 获取提现记录
|
|
82
|
+
*
|
|
83
|
+
* 记录查询时间范围不允许超过 30 天
|
|
84
|
+
*
|
|
85
|
+
* https://www.gate.io/docs/developers/apiv4/zh_CN/#%E8%8E%B7%E5%8F%96%E5%B8%81%E7%A7%8D%E5%85%85%E5%80%BC%E5%9C%B0%E5%9D%80
|
|
86
|
+
*/
|
|
87
|
+
export const getWithdrawalHistory = (credential, params) => callPrivate(credential, 'GET', '/api/v4/wallet/withdrawals', params);
|
|
88
|
+
/**
|
|
89
|
+
* 获取现货交易账户列表
|
|
90
|
+
*
|
|
91
|
+
* https://www.gate.io/docs/developers/apiv4/zh_CN/#%E8%8E%B7%E5%8F%96%E7%8E%B0%E8%B4%A7%E4%BA%A4%E6%98%93%E8%B4%A6%E6%88%B7%E5%88%97%E8%A1%A8
|
|
92
|
+
*/
|
|
93
|
+
export const getSpotAccounts = (credential, params) => callPrivate(credential, 'GET', '/api/v4/spot/accounts', params);
|
|
94
|
+
/**
|
|
95
|
+
* 交易账户互转
|
|
96
|
+
*
|
|
97
|
+
* POST /wallet/transfers
|
|
98
|
+
*
|
|
99
|
+
* https://www.gate.io/docs/developers/apiv4/zh_CN/#%E4%BA%A4%E6%98%93%E8%B4%A6%E6%88%B7%E4%BA%92%E8%BD%AC
|
|
100
|
+
*/
|
|
101
|
+
export const postWalletTransfer = (credential, params) => callPrivate(credential, 'POST', '/api/v4/wallet/transfers', params);
|
|
102
|
+
/**
|
|
103
|
+
* 获取统一账户最多可转出
|
|
104
|
+
*
|
|
105
|
+
* https://www.gate.io/docs/developers/apiv4/zh_CN/#%E6%9F%A5%E8%AF%A2%E7%BB%9F%E4%B8%80%E8%B4%A6%E6%88%B7%E6%9C%80%E5%A4%9A%E5%8F%AF%E8%BD%AC%E5%87%BA
|
|
106
|
+
*/
|
|
107
|
+
export const getUnifiedTransferable = (credential, params) => callPrivate(credential, 'GET', `/api/v4/unified/transferable`, params);
|
|
108
|
+
//# sourceMappingURL=private-api.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"private-api.js","sourceRoot":"","sources":["../../src/api/private-api.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAO/C,MAAM,WAAW,GAAG,CAClB,UAAuB,EACvB,MAAkB,EAClB,IAAY,EACZ,MAAmB,EACnB,EAAE,CAAC,cAAc,CAAY,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;AAEjE;;;;GAIG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAC9B,UAAuB,EAStB,EAAE,CAAC,WAAW,CAAC,UAAU,EAAE,KAAK,EAAE,wBAAwB,CAAC,CAAC;AAE/D,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAChC,UAAuB,EACvB,MAA8B,EAiC7B,EAAE,CAAC,WAAW,CAAC,UAAU,EAAE,KAAK,EAAE,0BAA0B,EAAE,MAAM,CAAC,CAAC;AAEzE;;;;GAIG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAChC,UAAuB,EACvB,cAAsB,EACtB,MAA+D,EAkB/D,EAAE,CAAC,WAAW,CAAC,UAAU,EAAE,KAAK,EAAE,mBAAmB,cAAc,YAAY,EAAE,MAAM,CAAC,CAAC;AAE3F;;;;GAIG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAC9B,UAAuB,EACvB,MAAc,EACd,MAMC,EAcD,EAAE,CAAC,WAAW,CAAC,UAAU,EAAE,KAAK,EAAE,mBAAmB,MAAM,SAAS,EAAE,MAAM,CAAC,CAAC;AAEhF;;;;GAIG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAChC,UAAu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ClC,UAAuB,EACvB,MAMC,EAcD,EAAE,CAAC,WAAW,CAAC,UAAU,EAAE,KAAK,EAAE,4BAA4B,EAAE,MAAM,CAAC,CAAC;AAE1E;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAC7B,UAAuB,EACvB,MAEC,EAQD,EAAE,CAAC,WAAW,CAAC,UAAU,EAAE,KAAK,EAAE,uBAAuB,EAAE,MAAM,CAAC,CAAC;AAErE;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAChC,UAAuB,EACvB,MAOC,EAGA,EAAE,CAAC,WAAW,CAAC,UAAU,EAAE,MAAM,EAAE,0BAA0B,EAAE,MAAM,CAAC,CAAC;AAE1E;;;;GAIG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,CACpC,UAAuB,EACvB,MAEC,EAIA,EAAE,CAAC,WAAW,CAAC,UAAU,EAAE,KAAK,EAAE,8BAA8B,EAAE,MAAM,CAAC,CAAC","sourcesContent":["import type { GateParams, HttpMethod } from './http-client';\nimport { requestPrivate } from './http-client';\n\nexport type ICredential = {\n access_key: string;\n secret_key: string;\n};\n\nconst callPrivate = <TResponse>(\n credential: ICredential,\n method: HttpMethod,\n path: string,\n params?: GateParams,\n) => requestPrivate<TResponse>(credential, method, path, params);\n\n/**\n * 获取用户账户信息\n *\n * https://www.gate.io/docs/developers/apiv4/zh_CN/#%E8%8E%B7%E5%8F%96%E7%94%A8%E6%88%B7%E8%B4%A6%E6%88%B7%E4%BF%A1%E6%81%AF\n */\nexport const getAccountDetail = (\n credential: ICredential,\n): Promise<{\n user_id: number;\n ip_whitelist: string[];\n currency_pairs: string[];\n key: {\n mode: number;\n };\n tier: number;\n}> => callPrivate(credential, 'GET', '/api/v4/account/detail');\n\nexport const getUnifiedAccounts = (\n credential: ICredential,\n params?: { currency?: string },\n): Promise<{\n user_id: string;\n refresh_time: number;\n locket: boolean;\n balances: Record<\n string,\n {\n available: string;\n freeze: string;\n borrowed: string;\n negative_liab: string;\n futures_pos_liab: string;\n equity: string;\n total_freeze: string;\n total_liab: string;\n spot_in_use: string;\n }\n >;\n total: string;\n borrowed: string;\n total_initial_margin: string;\n total_margin_balance: string;\n total_maintenance_margin: string;\n total_initial_margin_rate: string;\n total_maintenance_margin_rate: string;\n total_avail_margin: string;\n unified_account_total: string;\n unified_account_total_liab: string;\n unified_account_total_equity: string;\n leverage: string;\n spot_order_loss: string;\n spot_hedge: boolean;\n}> => callPrivate(credential, 'GET', '/api/v4/unified/accounts', params);\n\n/**\n * 获取用户仓位列表\n *\n * https://www.gate.io/docs/developers/apiv4/zh_CN/#%E8%8E%B7%E5%8F%96%E7%94%A8%E6%88%B7%E4%BB%93%E4%BD%8D%E5%88%97%E8%A1%A8\n */\nexport const getFuturePositions = (\n credential: ICredential,\n quote_currency: string,\n params?: { holding?: boolean; limit?: number; offset?: number },\n): Promise<\n {\n user: number;\n contract: string;\n size: number;\n leverage: string;\n risk_limit: string;\n leverage_max: string;\n maintenance_rate: string;\n value: string;\n margin: string;\n entry_price: string;\n mark_price: string;\n unrealised_pnl: string;\n realised_pnl: string;\n mode: string;\n }[]\n> => callPrivate(credential, 'GET', `/api/v4/futures/${quote_currency}/positions`, params);\n\n/**\n * 查询合约订单列表\n *\n * https://www.gate.io/docs/developers/apiv4/zh_CN/#%E6%9F%A5%E8%AF%A2%E5%90%88%E7%BA%A6%E8%AE%A2%E5%8D%95%E5%88%97%E8%A1%A8\n */\nexport const getFuturesOrders = (\n credential: ICredential,\n settle: string,\n params: {\n contract?: string;\n status: string;\n limit?: number;\n offset?: number;\n last_id?: number;\n },\n): Promise<\n {\n id: string;\n contract: string;\n create_time: number;\n size: number;\n price: string;\n is_close: boolean;\n fill_price: string;\n left?: number;\n status?: string;\n text: string;\n }[]\n> => callPrivate(credential, 'GET', `/api/v4/futures/${settle}/orders`, params);\n\n/**\n * 获取合约账号\n *\n * https://www.gate.io/docs/developers/apiv4/zh_CN/#%E8%8E%B7%E5%8F%96%E5%90%88%E7%BA%A6%E8%B4%A6%E5%8F%B7\n */\nexport const getFuturesAccounts = (\n credential: ICredential,\n settle: string,\n): Promise<{\n user: number;\n currency: string;\n total: string;\n unrealised_pnl: string;\n position_margin: string;\n order_margin: string;\n available: string;\n point: string;\n bonus: string;\n in_dual_mode: boolean;\n enable_evolved_classic: boolean;\n history: {\n dnw: string;\n pnl: string;\n fee: string;\n refr: string;\n fund: string;\n point_dnw: string;\n point_fee: string;\n point_refr: string;\n bonus_dnw: string;\n bonus_offset: string;\n };\n}> => callPrivate(credential, 'GET', `/api/v4/futures/${settle}/accounts`);\n\n/**\n * 合约交易下单\n *\n * 下单时指定的是合约张数 size ,而非币的数量,每一张合约对应的币的数量是合约详情接口里返回的 quanto_multiplier\n *\n * 0 成交的订单在撤单 10 分钟之后无法再获取到,会提到订单不存在\n *\n * 设置 reduce_only 为 true 可以防止在减仓的时候穿仓\n *\n * 单仓模式下,如果需要平仓,需要设置 size 为 0 ,close 为 true\n *\n * 双仓模式下,平仓需要使用 auto_size 来设置平仓方向,并同时设置 reduce_only 为 true,size 为 0\n *\n * 设置 stp_act 决定使用限制用户自成交的策略,详细用法参考body参数stp_act\n *\n * https://www.gate.io/docs/developers/apiv4/zh_CN/#%E5%90%88%E7%BA%A6%E4%BA%A4%E6%98%93%E4%B8%8B%E5%8D%95\n */\nexport const postFutureOrders = (\n credential: ICredential,\n settle: string,\n params: {\n contract: string;\n size: number;\n iceberg?: number;\n price?: string;\n close?: boolean;\n reduce_only?: boolean;\n tif?: string;\n text?: string;\n auto_size?: string;\n stp_act?: string;\n },\n): Promise<{\n label?: string;\n message?: string;\n detail?: string;\n id: number;\n user: number;\n contract: string;\n create_time: number;\n size: number;\n iceberg: number;\n left: number;\n price: string;\n fill_price: string;\n mkfr: string;\n tkfr: string;\n tif: string;\n refu: number;\n is_reduce_only: boolean;\n is_close: boolean;\n is_liq: boolean;\n text: string;\n status: string;\n finish_time: number;\n finish_as: string;\n stp_id: number;\n stp_act: string;\n amend_text: string;\n}> => callPrivate(credential, 'POST', `/api/v4/futures/${settle}/orders`, params);\n\n/**\n * 撤销单个订单\n *\n * https://www.gate.io/docs/developers/apiv4/zh_CN/#%E6%92%A4%E9%94%80%E5%8D%95%E4%B8%AA%E8%AE%A2%E5%8D%95-2\n */\nexport const deleteFutureOrders = (credential: ICredential, settle: string, order_id: string): Promise<{}> =>\n callPrivate(credential, 'DELETE', `/api/v4/futures/${settle}/orders/${order_id}`);\n\n/**\n * 提现\n *\n * POST /withdrawals\n *\n * https://www.gate.io/docs/developers/apiv4/zh_CN/#%E6%8F%90%E7%8E%B0\n */\nexport const postWithdrawals = (\n credential: ICredential,\n params: {\n withdraw_order_id?: string;\n amount: string;\n currency: string;\n address?: string;\n memo?: string;\n chain: string;\n },\n): Promise<{\n id: string;\n txid: string;\n withdraw_order_id: string;\n timestamp: number;\n amount: string;\n currency: string;\n address: string;\n memo: string;\n status: string;\n chain: string;\n}> => callPrivate(credential, 'POST', '/api/v4/withdrawals', params);\n\n/**\n * 获取币种充值地址\n *\n * https://www.gate.io/docs/developers/apiv4/zh_CN/#%E8%8E%B7%E5%8F%96%E5%B8%81%E7%A7%8D%E5%85%85%E5%80%BC%E5%9C%B0%E5%9D%80\n */\nexport const getDepositAddress = (\n credential: ICredential,\n params: {\n currency: string;\n },\n): Promise<{\n currency: string;\n address: string;\n multichain_addresses: {\n chain: string;\n address: string;\n payment_id: string;\n payment_name: string;\n obtain_failed: boolean;\n }[];\n}> => callPrivate(credential, 'GET', '/api/v4/wallet/deposit_address', params);\n\n/**\n * 创建新的子账户\n *\n * https://www.gate.io/docs/developers/apiv4/zh_CN/#%E5%88%9B%E5%BB%BA%E6%96%B0%E7%9A%84%E5%AD%90%E8%B4%A6%E6%88%B7\n */\nexport const getSubAccountList = (\n credential: ICredential,\n params?: {\n type?: string;\n },\n): Promise<\n {\n remark: string;\n login_name: string;\n password: string;\n email: string;\n state: number;\n type: number;\n user_id: number;\n create_time: number;\n }[]\n> => callPrivate(credential, 'GET', '/api/v4/sub_accounts', params);\n\n/**\n * 获取充值记录\n *\n * 记录查询时间范围不允许超过 30 天\n *\n * https://www.gate.io/docs/developers/apiv4/zh_CN/#%E8%8E%B7%E5%8F%96%E5%85%85%E5%80%BC%E8%AE%B0%E5%BD%95\n */\nexport const getDepositHistory = (\n credential: ICredential,\n params?: {\n currency?: string;\n from?: number;\n to?: number;\n limit?: number;\n offset?: number;\n },\n): Promise<\n {\n id: string;\n txid: string;\n withdraw_order_id: string;\n timestamp: number;\n amount: string;\n currency: string;\n address: string;\n memo: string;\n status: string;\n chain: string;\n }[]\n> => callPrivate(credential, 'GET', '/api/v4/wallet/deposits', params);\n\n/**\n * 获取提现记录\n *\n * 记录查询时间范围不允许超过 30 天\n *\n * https://www.gate.io/docs/developers/apiv4/zh_CN/#%E8%8E%B7%E5%8F%96%E5%B8%81%E7%A7%8D%E5%85%85%E5%80%BC%E5%9C%B0%E5%9D%80\n */\nexport const getWithdrawalHistory = (\n credential: ICredential,\n params?: {\n currency?: string;\n from?: number;\n to?: number;\n limit?: number;\n offset?: number;\n },\n): Promise<\n {\n id: string;\n txid: string;\n withdraw_order_id: string;\n timestamp: number;\n amount: string;\n currency: string;\n address: string;\n memo: string;\n status: string;\n chain: string;\n }[]\n> => callPrivate(credential, 'GET', '/api/v4/wallet/withdrawals', params);\n\n/**\n * 获取现货交易账户列表\n *\n * https://www.gate.io/docs/developers/apiv4/zh_CN/#%E8%8E%B7%E5%8F%96%E7%8E%B0%E8%B4%A7%E4%BA%A4%E6%98%93%E8%B4%A6%E6%88%B7%E5%88%97%E8%A1%A8\n */\nexport const getSpotAccounts = (\n credential: ICredential,\n params?: {\n currency?: string;\n },\n): Promise<\n {\n currency: string;\n available: string;\n locked: string;\n update_id: string;\n }[]\n> => callPrivate(credential, 'GET', '/api/v4/spot/accounts', params);\n\n/**\n * 交易账户互转\n *\n * POST /wallet/transfers\n *\n * https://www.gate.io/docs/developers/apiv4/zh_CN/#%E4%BA%A4%E6%98%93%E8%B4%A6%E6%88%B7%E4%BA%92%E8%BD%AC\n */\nexport const postWalletTransfer = (\n credential: ICredential,\n params: {\n currency: string;\n from: string;\n to: string;\n amount: string;\n currency_pair?: string;\n settle?: string;\n },\n): Promise<{\n tx_id: string;\n}> => callPrivate(credential, 'POST', '/api/v4/wallet/transfers', params);\n\n/**\n * 获取统一账户最多可转出\n *\n * https://www.gate.io/docs/developers/apiv4/zh_CN/#%E6%9F%A5%E8%AF%A2%E7%BB%9F%E4%B8%80%E8%B4%A6%E6%88%B7%E6%9C%80%E5%A4%9A%E5%8F%AF%E8%BD%AC%E5%87%BA\n */\nexport const getUnifiedTransferable = (\n credential: ICredential,\n params: {\n currency: string;\n },\n): Promise<{\n currency: string;\n amount: string;\n}> => callPrivate(credential, 'GET', `/api/v4/unified/transferable`, params);\n"]}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { rateLimiter } from './rate-limiter';
|
|
2
|
+
import { requestPublic } from './http-client';
|
|
3
|
+
/**
|
|
4
|
+
* 查询所有的合约信息
|
|
5
|
+
*
|
|
6
|
+
* https://www.gate.io/docs/developers/apiv4/zh_CN/#%E6%9F%A5%E8%AF%A2%E6%89%80%E6%9C%89%E7%9A%84%E5%90%88%E7%BA%A6%E4%BF%A1%E6%81%AF
|
|
7
|
+
*/
|
|
8
|
+
export const getFuturesContracts = (settle, params) => requestPublic('GET', `/api/v4/futures/${settle}/contracts`, params);
|
|
9
|
+
/**
|
|
10
|
+
* 合约市场历史资金费率
|
|
11
|
+
*
|
|
12
|
+
* - Note: 该接口返回的数据是按照时间倒序排列的
|
|
13
|
+
* - Note: limit 参数最大值为 1000
|
|
14
|
+
* - Note: t 字段为秒级时间戳 (Unix Second),r 字段为资金费率 (0-1 单位)
|
|
15
|
+
*
|
|
16
|
+
* https://www.gate.io/docs/developers/apiv4/zh_CN/#%E5%90%88%E7%BA%A6%E5%B8%82%E5%9C%BA%E5%8E%86%E5%8F%B2%E8%B5%84%E9%87%91%E8%B4%B9%E7%8E%87
|
|
17
|
+
*/
|
|
18
|
+
export const getFutureFundingRate = (settle, params) => requestPublic('GET', `/api/v4/futures/${settle}/funding_rate`, params);
|
|
19
|
+
/**
|
|
20
|
+
* 查询合约市场深度信息
|
|
21
|
+
*
|
|
22
|
+
* https://www.gate.io/docs/developers/apiv4/zh_CN/#%E6%9F%A5%E8%AF%A2%E5%90%88%E7%BA%A6%E5%B8%82%E5%9C%BA%E6%B7%B1%E5%BA%A6%E4%BF%A1%E6%81%AF
|
|
23
|
+
*/
|
|
24
|
+
export const getFuturesOrderBook = async (settle, params) => rateLimiter.schedule(`futures-order-book:${settle}`, 200, 10000, () => requestPublic('GET', `/api/v4/futures/${settle}/order_book`, params));
|
|
25
|
+
/**
|
|
26
|
+
* 获取所有合约交易行情统计
|
|
27
|
+
*
|
|
28
|
+
* https://www.gate.io/docs/developers/apiv4/zh_CN/#%E8%8E%B7%E5%8F%96%E6%89%80%E6%9C%89%E5%90%88%E7%BA%A6%E4%BA%A4%E6%98%93%E8%A1%8C%E6%83%85%E7%BB%9F%E8%AE%A1
|
|
29
|
+
*/
|
|
30
|
+
export const getFuturesTickers = (settle, params) => requestPublic('GET', `/api/v4/futures/${settle}/tickers`, params);
|
|
31
|
+
/**
|
|
32
|
+
* 获取交易对 ticker 信息
|
|
33
|
+
*
|
|
34
|
+
* https://www.gate.io/docs/developers/apiv4/zh_CN/#%E6%9F%A5%E8%AF%A2%E5%8D%95%E4%B8%AA%E4%BA%A4%E6%98%93%E5%AF%B9%E8%AF%A6%E6%83%85
|
|
35
|
+
*/
|
|
36
|
+
export const getSpotTickers = (params) => requestPublic('GET', `/api/v4/spot/tickers`, params);
|
|
37
|
+
//# sourceMappingURL=public-api.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"public-api.js","sourceRoot":"","sources":["../../src/api/public-api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAE9C;;;;GAIG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CACjC,MAAc,EACd,MAA4C,EA8C5C,EAAE,CAAC,aAAa,CAAC,KAAK,EAAE,mBAAmB,MAAM,YAAY,EAAE,MAAM,CAAC,CAAC;AAEzE;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAClC,MAAc,EACd,MAA4C,EAM5C,EAAE,CAAC,aAAa,CAAC,KAAK,EAAE,mBAAmB,MAAM,eAAe,EAAE,MAAM,CAAC,CAAC;AAE5E;;;;GAIG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,KAAK,EACtC,MAAc,EACd,MAKC,EAaA,EAAE,CACH,WAAW,CAAC,QAAQ,CAAC,sBAAsB,MAAM,EAAE,EAAE,GAAG,EAAE,KAAM,EAAE,GAAG,EAAE,CACrE,aAAa,CAAC,KAAK,EAAE,mBAAmB,MAAM,aAAa,EAAE,MAAM,CAAC,CACrE,CAAC;AAEJ;;;;GAIG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAC/B,MAAc,EACd,MAA8B,EAyB9B,EAAE,CAAC,aAAa,CAAC,KAAK,EAAE,mBAAmB,MAAM,UAAU,EAAE,MAAM,CAAC,CAAC;AAEvE;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,MAG9B,EAoBC,EAAE,CAAC,aAAa,CAAC,KAAK,EAAE,sBAAsB,EAAE,MAAM,CAAC,CAAC","sourcesContent":["import { rateLimiter } from './rate-limiter';\nimport { requestPublic } from './http-client';\n\n/**\n * 查询所有的合约信息\n *\n * https://www.gate.io/docs/developers/apiv4/zh_CN/#%E6%9F%A5%E8%AF%A2%E6%89%80%E6%9C%89%E7%9A%84%E5%90%88%E7%BA%A6%E4%BF%A1%E6%81%AF\n */\nexport const getFuturesContracts = (\n settle: string,\n params?: { limit?: number; offset?: number },\n): Promise<\n {\n name: string;\n type: string;\n quanto_multiplier: string;\n ref_discount_rate: string;\n order_price_deviate: string;\n maintenance_rate: string;\n mark_type: string;\n last_price: string;\n mark_price: string;\n index_price: string;\n funding_rate_indicative: string;\n mark_price_round: string;\n funding_offset: number;\n in_delisting: boolean;\n risk_limit_base: string;\n interest_rate: string;\n order_price_round: string;\n order_size_min: number;\n ref_rebate_rate: string;\n funding_interval: number;\n risk_limit_step: string;\n leverage_min: string;\n leverage_max: string;\n risk_limit_max: string;\n maker_fee_rate: string;\n taker_fee_rate: string;\n funding_rate: string;\n order_size_max: number;\n funding_next_apply: number;\n short_users: number;\n config_change_time: number;\n trade_size: number;\n position_size: number;\n long_users: number;\n funding_impact_value: string;\n orders_limit: number;\n trade_id: number;\n orderbook_id: number;\n enable_bonus: boolean;\n enable_credit: boolean;\n create_time: number;\n funding_cap_ratio: string;\n }[]\n> => requestPublic('GET', `/api/v4/futures/${settle}/contracts`, params);\n\n/**\n * 合约市场历史资金费率\n *\n * - Note: 该接口返回的数据是按照时间倒序排列的\n * - Note: limit 参数最大值为 1000\n * - Note: t 字段为秒级时间戳 (Unix Second),r 字段为资金费率 (0-1 单位)\n *\n * https://www.gate.io/docs/developers/apiv4/zh_CN/#%E5%90%88%E7%BA%A6%E5%B8%82%E5%9C%BA%E5%8E%86%E5%8F%B2%E8%B5%84%E9%87%91%E8%B4%B9%E7%8E%87\n */\nexport const getFutureFundingRate = (\n settle: string,\n params: { contract: string; limit?: number },\n): Promise<\n {\n t: number;\n r: string;\n }[]\n> => requestPublic('GET', `/api/v4/futures/${settle}/funding_rate`, params);\n\n/**\n * 查询合约市场深度信息\n *\n * https://www.gate.io/docs/developers/apiv4/zh_CN/#%E6%9F%A5%E8%AF%A2%E5%90%88%E7%BA%A6%E5%B8%82%E5%9C%BA%E6%B7%B1%E5%BA%A6%E4%BF%A1%E6%81%AF\n */\nexport const getFuturesOrderBook = async (\n settle: string,\n params: {\n contract: string;\n interval?: string;\n limit?: number;\n with_id?: boolean;\n },\n): Promise<{\n id: number;\n current: number;\n update: number;\n asks: {\n p: string;\n s: string;\n }[];\n bids: {\n p: string;\n s: string;\n }[];\n}> =>\n rateLimiter.schedule(`futures-order-book:${settle}`, 200, 10_000, () =>\n requestPublic('GET', `/api/v4/futures/${settle}/order_book`, params),\n );\n\n/**\n * 获取所有合约交易行情统计\n *\n * https://www.gate.io/docs/developers/apiv4/zh_CN/#%E8%8E%B7%E5%8F%96%E6%89%80%E6%9C%89%E5%90%88%E7%BA%A6%E4%BA%A4%E6%98%93%E8%A1%8C%E6%83%85%E7%BB%9F%E8%AE%A1\n */\nexport const getFuturesTickers = (\n settle: string,\n params?: { contract?: string },\n): Promise<\n {\n contract: string;\n last: string;\n change_percentage: string;\n total_size: string;\n low_24h: string;\n high_24h: string;\n volume_24h: string;\n volume_24h_btc: string;\n volume_24h_usd: string;\n volume_24h_base: string;\n volume_24h_quote: string;\n volume_24h_settle: string;\n mark_price: string;\n funding_rate: string;\n funding_rate_indicative: string;\n index_price: string;\n quanto_base_rate: string;\n basis_rate: string;\n basis_value: string;\n lowest_ask: string;\n highest_bid: string;\n }[]\n> => requestPublic('GET', `/api/v4/futures/${settle}/tickers`, params);\n\n/**\n * 获取交易对 ticker 信息\n *\n * https://www.gate.io/docs/developers/apiv4/zh_CN/#%E6%9F%A5%E8%AF%A2%E5%8D%95%E4%B8%AA%E4%BA%A4%E6%98%93%E5%AF%B9%E8%AF%A6%E6%83%85\n */\nexport const getSpotTickers = (params: {\n currency_pair?: string;\n timezone?: string;\n}): Promise<\n Array<{\n currency_pair: string;\n last: string;\n lowest_ask: string;\n lowest_size: string;\n highest_bid: string;\n highest_size: string;\n change_percentage: string;\n change_utc0: string;\n change_utc8: string;\n base_volume: string;\n quote_volume: string;\n high_24h: string;\n low_24h: string;\n etf_net_value: string;\n etf_pre_net_value: string;\n etf_pre_timestamp: string;\n etf_leverage: string;\n }>\n> => requestPublic('GET', `/api/v4/spot/tickers`, params);\n"]}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Subject, filter, mergeMap, timer } from 'rxjs';
|
|
2
|
+
export class RateLimiter {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.channels = new Map();
|
|
5
|
+
}
|
|
6
|
+
schedule(key, limit, periodMs, execute) {
|
|
7
|
+
if (!Number.isFinite(limit) || limit <= 0) {
|
|
8
|
+
return execute();
|
|
9
|
+
}
|
|
10
|
+
const channel = this.ensureChannel(key, limit, periodMs);
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
channel.queue.push({
|
|
13
|
+
execute,
|
|
14
|
+
resolve: (value) => resolve(value),
|
|
15
|
+
reject,
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
ensureChannel(key, limit, periodMs) {
|
|
20
|
+
if (this.channels.has(key)) {
|
|
21
|
+
return this.channels.get(key);
|
|
22
|
+
}
|
|
23
|
+
const subject = new Subject();
|
|
24
|
+
const channel = {
|
|
25
|
+
queue: [],
|
|
26
|
+
limit,
|
|
27
|
+
subject,
|
|
28
|
+
queueSubscription: subject.subscribe((task) => {
|
|
29
|
+
channel.activeCount++;
|
|
30
|
+
task
|
|
31
|
+
.execute()
|
|
32
|
+
.then((value) => task.resolve(value))
|
|
33
|
+
.catch((error) => task.reject(error))
|
|
34
|
+
.finally(() => {
|
|
35
|
+
channel.activeCount--;
|
|
36
|
+
if (channel.queue.length === 0 && channel.activeCount === 0) {
|
|
37
|
+
this.disposeChannel(key);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}),
|
|
41
|
+
timerSubscription: timer(0, periodMs)
|
|
42
|
+
.pipe(filter(() => channel.queue.length > 0), mergeMap(() => channel.queue.splice(0, channel.limit)))
|
|
43
|
+
.subscribe(subject),
|
|
44
|
+
activeCount: 0,
|
|
45
|
+
};
|
|
46
|
+
this.channels.set(key, channel);
|
|
47
|
+
return channel;
|
|
48
|
+
}
|
|
49
|
+
disposeChannel(key) {
|
|
50
|
+
const channel = this.channels.get(key);
|
|
51
|
+
if (!channel)
|
|
52
|
+
return;
|
|
53
|
+
channel.queueSubscription.unsubscribe();
|
|
54
|
+
channel.timerSubscription.unsubscribe();
|
|
55
|
+
channel.subject.complete();
|
|
56
|
+
this.channels.delete(key);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export const rateLimiter = new RateLimiter();
|
|
60
|
+
//# sourceMappingURL=rate-limiter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rate-limiter.js","sourceRoot":"","sources":["../../src/api/rate-limiter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAgB,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,MAAM,CAAC;AAiBtE,MAAM,OAAO,WAAW;IAAxB;QACU,aAAQ,GAAG,IAAI,GAAG,EAA6B,CAAC;IA4D1D,CAAC;IA1DC,QAAQ,CAAI,GAAW,EAAE,KAAa,EAAE,QAAgB,EAAE,OAAyB;QACjF,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC,EAAE;YACzC,OAAO,OAAO,EAAE,CAAC;SAClB;QACD,MAAM,OAAO,GAAG,IAAI,CAAC,aAAa,CAAC,GAAG,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC;QAEzD,OAAO,IAAI,OAAO,CAAI,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACxC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC;gBACjB,OAAO;gBACP,OAAO,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,KAAU,CAAC;gBACvC,MAAM;aACP,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;IAEO,aAAa,CAAC,GAAW,EAAE,KAAa,EAAE,QAAgB;QAChE,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;YAC1B,OAAO,IAAI,CAAC,QAAQ,CAAC,GAA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IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACvC,IAAI,CAAC,OAAO;YAAE,OAAO;QACrB,OAAO,CAAC,iBAAiB,CAAC,WAAW,EAAE,CAAC;QACxC,OAAO,CAAC,iBAAiB,CAAC,WAAW,EAAE,CAAC;QACxC,OAAO,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;QAC3B,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAC5B,CAAC;CACF;AAED,MAAM,CAAC,MAAM,WAAW,GAAG,IAAI,WAAW,EAAE,CAAC","sourcesContent":["import { Subject, Subscription, filter, mergeMap, timer } from 'rxjs';\n\ntype QueueTask = {\n execute: () => Promise<unknown>;\n resolve: (value: unknown) => void;\n reject: (error: unknown) => void;\n};\n\ninterface IRateLimitChannel {\n queue: QueueTask[];\n limit: number;\n subject: Subject<QueueTask>;\n queueSubscription: Subscription;\n timerSubscription: Subscription;\n activeCount: number;\n}\n\nexport class RateLimiter {\n private channels = new Map<string, IRateLimitChannel>();\n\n schedule<T>(key: string, limit: number, periodMs: number, execute: () => Promise<T>): Promise<T> {\n if (!Number.isFinite(limit) || limit <= 0) {\n return execute();\n }\n const channel = this.ensureChannel(key, limit, periodMs);\n\n return new Promise<T>((resolve, reject) => {\n channel.queue.push({\n execute,\n resolve: (value) => resolve(value as T),\n reject,\n });\n });\n }\n\n private ensureChannel(key: string, limit: number, periodMs: number): IRateLimitChannel {\n if (this.channels.has(key)) {\n return this.channels.get(key)!;\n }\n const subject = new Subject<QueueTask>();\n const channel: IRateLimitChannel = {\n queue: [],\n limit,\n subject,\n queueSubscription: subject.subscribe((task) => {\n channel.activeCount++;\n task\n .execute()\n .then((value) => task.resolve(value))\n .catch((error) => task.reject(error))\n .finally(() => {\n channel.activeCount--;\n if (channel.queue.length === 0 && channel.activeCount === 0) {\n this.disposeChannel(key);\n }\n });\n }),\n timerSubscription: timer(0, periodMs)\n .pipe(\n filter(() => channel.queue.length > 0),\n mergeMap(() => channel.queue.splice(0, channel.limit)),\n )\n .subscribe(subject),\n activeCount: 0,\n };\n\n this.channels.set(key, channel);\n return channel;\n }\n\n private disposeChannel(key: string) {\n const channel = this.channels.get(key);\n if (!channel) return;\n channel.queueSubscription.unsubscribe();\n channel.timerSubscription.unsubscribe();\n channel.subject.complete();\n this.channels.delete(key);\n }\n}\n\nexport const rateLimiter = new RateLimiter();\n"]}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import './services/legacy';
|
|
2
|
+
import './services/account-actions-with-credential';
|
|
3
|
+
import './services/order-actions-with-credential';
|
|
4
|
+
import './services/markets/product';
|
|
5
|
+
import './services/markets/interest-rate';
|
|
6
|
+
import './services/transfer';
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,mBAAmB,CAAC;AAC3B,OAAO,4CAA4C,CAAC;AACpD,OAAO,0CAA0C,CAAC;AAClD,OAAO,4BAA4B,CAAC;AACpC,OAAO,kCAAkC,CAAC;AAC1C,OAAO,qBAAqB,CAAC","sourcesContent":["import './services/legacy';\nimport './services/account-actions-with-credential';\nimport './services/order-actions-with-credential';\nimport './services/markets/product';\nimport './services/markets/interest-rate';\nimport './services/transfer';\n"]}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { provideAccountActionsWithCredential } from '@yuants/data-account';
|
|
2
|
+
import { Terminal } from '@yuants/protocol';
|
|
3
|
+
import { getFutureAccountInfo } from './accounts/future';
|
|
4
|
+
import { resolveAccountProfile } from './accounts/profile';
|
|
5
|
+
import { getSpotAccountInfo } from './accounts/spot';
|
|
6
|
+
import { getUnifiedAccountInfo } from './accounts/unified';
|
|
7
|
+
provideAccountActionsWithCredential(Terminal.fromNodeEnv(), 'GATE', {
|
|
8
|
+
type: 'object',
|
|
9
|
+
required: ['access_key', 'secret_key'],
|
|
10
|
+
properties: {
|
|
11
|
+
access_key: { type: 'string' },
|
|
12
|
+
secret_key: { type: 'string' },
|
|
13
|
+
},
|
|
14
|
+
}, {
|
|
15
|
+
listAccounts: async (credential) => {
|
|
16
|
+
const accountIds = await resolveAccountProfile(credential);
|
|
17
|
+
return [
|
|
18
|
+
{ account_id: accountIds.future },
|
|
19
|
+
{ account_id: accountIds.unified },
|
|
20
|
+
{ account_id: accountIds.spot },
|
|
21
|
+
];
|
|
22
|
+
},
|
|
23
|
+
getAccountInfo: async (credential, account_id) => {
|
|
24
|
+
const accountIds = await resolveAccountProfile(credential);
|
|
25
|
+
if (account_id === accountIds.future) {
|
|
26
|
+
return getFutureAccountInfo(credential, account_id);
|
|
27
|
+
}
|
|
28
|
+
if (account_id === accountIds.unified) {
|
|
29
|
+
return getUnifiedAccountInfo(credential, account_id);
|
|
30
|
+
}
|
|
31
|
+
if (account_id === accountIds.spot) {
|
|
32
|
+
return getSpotAccountInfo(credential, account_id);
|
|
33
|
+
}
|
|
34
|
+
throw new Error(`Unsupported account_id: ${account_id}`);
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
//# sourceMappingURL=account-actions-with-credential.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"account-actions-with-credential.js","sourceRoot":"","sources":["../../src/services/account-actions-with-credential.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mCAAmC,EAAE,MAAM,sBAAsB,CAAC;AAC3E,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAE5C,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AACzD,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAE3D,mCAAmC,CACjC,QAAQ,CAAC,WAAW,EAAE,EACtB,MAAM,EACN;IACE,IAAI,EAAE,QAAQ;IACd,QAAQ,EAAE,CAAC,YAAY,EAAE,YAAY,CAAC;IACtC,UAAU,EAAE;QACV,UAAU,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QAC9B,UAAU,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;KAC/B;CACF,EACD;IACE,YAAY,EAAE,KAAK,EAAE,UAAU,EAAE,EAAE;QACjC,MAAM,UAAU,GAAG,MAAM,qBAAqB,CAAC,UAAU,CAAC,CAAC;QAC3D,OAAO;YACL,EAAE,UAAU,EAAE,UAAU,CAAC,MAAM,EAAE;YACjC,EAAE,UAAU,EAAE,UAAU,CAAC,OAAO,EAAE;YAClC,EAAE,UAAU,EAAE,UAAU,CAAC,IAAI,EAAE;SAChC,CAAC;IACJ,CAAC;IACD,cAAc,EAAE,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,EAAE;QAC/C,MAAM,UAAU,GAAG,MAAM,qBAAqB,CAAC,UAAU,CAAC,CAAC;QAC3D,IAAI,UAAU,KAAK,UAAU,CAAC,MAAM,EAAE;YACpC,OAAO,oBAAoB,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;SACrD;QACD,IAAI,UAAU,KAAK,UAAU,CAAC,OAAO,EAAE;YACrC,OAAO,qBAAqB,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;SACtD;QACD,IAAI,UAAU,KAAK,UAAU,CAAC,IAAI,EAAE;YAClC,OAAO,kBAAkB,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;SACnD;QACD,MAAM,IAAI,KAAK,CAAC,2BAA2B,UAAU,EAAE,CAAC,CAAC;IAC3D,CAAC;CACF,CACF,CAAC","sourcesContent":["import { provideAccountActionsWithCredential } from '@yuants/data-account';\nimport { Terminal } from '@yuants/protocol';\nimport { ICredential } from '../api/private-api';\nimport { getFutureAccountInfo } from './accounts/future';\nimport { resolveAccountProfile } from './accounts/profile';\nimport { getSpotAccountInfo } from './accounts/spot';\nimport { getUnifiedAccountInfo } from './accounts/unified';\n\nprovideAccountActionsWithCredential<ICredential>(\n Terminal.fromNodeEnv(),\n 'GATE',\n {\n type: 'object',\n required: ['access_key', 'secret_key'],\n properties: {\n access_key: { type: 'string' },\n secret_key: { type: 'string' },\n },\n },\n {\n listAccounts: async (credential) => {\n const accountIds = await resolveAccountProfile(credential);\n return [\n { account_id: accountIds.future },\n { account_id: accountIds.unified },\n { account_id: accountIds.spot },\n ];\n },\n getAccountInfo: async (credential, account_id) => {\n const accountIds = await resolveAccountProfile(credential);\n if (account_id === accountIds.future) {\n return getFutureAccountInfo(credential, account_id);\n }\n if (account_id === accountIds.unified) {\n return getUnifiedAccountInfo(credential, account_id);\n }\n if (account_id === accountIds.spot) {\n return getSpotAccountInfo(credential, account_id);\n }\n throw new Error(`Unsupported account_id: ${account_id}`);\n },\n },\n);\n"]}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { firstValueFrom } from 'rxjs';
|
|
2
|
+
import { getFuturePositions, getFuturesAccounts } from '../../api/private-api';
|
|
3
|
+
import { mapProductIdToUsdtFutureProduct$ } from '../markets/product';
|
|
4
|
+
export const loadFuturePositions = async (credential) => {
|
|
5
|
+
const [positionsRes, productMap] = await Promise.all([
|
|
6
|
+
getFuturePositions(credential, 'usdt'),
|
|
7
|
+
firstValueFrom(mapProductIdToUsdtFutureProduct$),
|
|
8
|
+
]);
|
|
9
|
+
const positions = Array.isArray(positionsRes) ? positionsRes : [];
|
|
10
|
+
return positions.map((position) => {
|
|
11
|
+
var _a;
|
|
12
|
+
const product_id = position.contract;
|
|
13
|
+
const theProduct = productMap.get(product_id);
|
|
14
|
+
const volume = Math.abs(position.size);
|
|
15
|
+
const closable_price = Number(position.mark_price);
|
|
16
|
+
const valuation = volume * closable_price * ((_a = theProduct === null || theProduct === void 0 ? void 0 : theProduct.value_scale) !== null && _a !== void 0 ? _a : 1);
|
|
17
|
+
return {
|
|
18
|
+
datasource_id: 'GATE-FUTURE',
|
|
19
|
+
position_id: `${position.contract}-${position.leverage}-${position.mode}`,
|
|
20
|
+
product_id,
|
|
21
|
+
direction: position.mode === 'dual_long'
|
|
22
|
+
? 'LONG'
|
|
23
|
+
: position.mode === 'dual_short'
|
|
24
|
+
? 'SHORT'
|
|
25
|
+
: position.size > 0
|
|
26
|
+
? 'LONG'
|
|
27
|
+
: 'SHORT',
|
|
28
|
+
volume,
|
|
29
|
+
free_volume: Math.abs(position.size),
|
|
30
|
+
position_price: Number(position.entry_price),
|
|
31
|
+
closable_price,
|
|
32
|
+
floating_profit: Number(position.unrealised_pnl),
|
|
33
|
+
valuation,
|
|
34
|
+
};
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
export const getFutureAccountInfo = async (credential, account_id) => {
|
|
38
|
+
var _a, _b, _c;
|
|
39
|
+
const [positions, rawAccount] = await Promise.all([
|
|
40
|
+
loadFuturePositions(credential),
|
|
41
|
+
getFuturesAccounts(credential, 'usdt'),
|
|
42
|
+
]);
|
|
43
|
+
const account = (rawAccount === null || rawAccount === void 0 ? void 0 : rawAccount.available) ? rawAccount : { available: '0', total: '0', unrealised_pnl: '0' };
|
|
44
|
+
const free = Number((_a = account.available) !== null && _a !== void 0 ? _a : 0);
|
|
45
|
+
const equity = Number((_b = account.total) !== null && _b !== void 0 ? _b : 0) + Number((_c = account.unrealised_pnl) !== null && _c !== void 0 ? _c : 0);
|
|
46
|
+
return {
|
|
47
|
+
account_id,
|
|
48
|
+
money: {
|
|
49
|
+
currency: 'USDT',
|
|
50
|
+
equity,
|
|
51
|
+
free,
|
|
52
|
+
},
|
|
53
|
+
positions,
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
//# sourceMappingURL=future.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"future.js","sourceRoot":"","sources":["../../../src/services/accounts/future.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AACtC,OAAO,E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js';\nimport { getFuturePositions, getFuturesAccounts, ICredential } from '../../api/private-api';\nimport { mapProductIdToUsdtFutureProduct$ } from '../markets/product';\n\nexport const loadFuturePositions = async (credential: ICredential): Promise<IPosition[]> => {\n const [positionsRes, productMap] = await Promise.all([\n getFuturePositions(credential, 'usdt'),\n firstValueFrom(mapProductIdToUsdtFutureProduct$),\n ]);\n const positions = Array.isArray(positionsRes) ? positionsRes : [];\n return positions.map((position): IPosition => {\n const product_id = position.contract;\n const theProduct = productMap.get(product_id);\n const volume = Math.abs(position.size);\n const closable_price = Number(position.mark_price);\n const valuation = volume * closable_price * (theProduct?.value_scale ?? 1);\n return {\n datasource_id: 'GATE-FUTURE',\n position_id: `${position.contract}-${position.leverage}-${position.mode}`,\n product_id,\n direction:\n position.mode === 'dual_long'\n ? 'LONG'\n : position.mode === 'dual_short'\n ? 'SHORT'\n : position.size > 0\n ? 'LONG'\n : 'SHORT',\n volume,\n free_volume: Math.abs(position.size),\n position_price: Number(position.entry_price),\n closable_price,\n floating_profit: Number(position.unrealised_pnl),\n valuation,\n };\n });\n};\n\nexport const getFutureAccountInfo = async (credential: ICredential, account_id: string) => {\n const [positions, rawAccount] = await Promise.all([\n loadFuturePositions(credential),\n getFuturesAccounts(credential, 'usdt'),\n ]);\n\n const account = rawAccount?.available ? rawAccount : { available: '0', total: '0', unrealised_pnl: '0' };\n const free = Number(account.available ?? 0);\n const equity = Number(account.total ?? 0) + Number(account.unrealised_pnl ?? 0);\n\n return {\n account_id,\n money: {\n currency: 'USDT',\n equity,\n free,\n },\n positions,\n };\n};\n"]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { createCache } from '@yuants/cache';
|
|
2
|
+
import { getAccountDetail } from '../../api/private-api';
|
|
3
|
+
const PROFILE_TTL = 60000;
|
|
4
|
+
const serializeCredential = (credential) => JSON.stringify(credential);
|
|
5
|
+
const deserializeCredential = (key) => JSON.parse(key);
|
|
6
|
+
const accountProfileCache = createCache(async (key) => {
|
|
7
|
+
const credential = deserializeCredential(key);
|
|
8
|
+
const detail = await getAccountDetail(credential);
|
|
9
|
+
const uid = `${detail.user_id}`;
|
|
10
|
+
return {
|
|
11
|
+
uid,
|
|
12
|
+
future: `gate/${uid}/future/USDT`,
|
|
13
|
+
spot: `gate/${uid}/spot/USDT`,
|
|
14
|
+
unified: `gate/${uid}/unified/USDT`,
|
|
15
|
+
};
|
|
16
|
+
}, { expire: PROFILE_TTL });
|
|
17
|
+
export const resolveAccountProfile = async (credential) => {
|
|
18
|
+
const profile = await accountProfileCache.query(serializeCredential(credential));
|
|
19
|
+
if (!profile) {
|
|
20
|
+
throw new Error('Unable to resolve Gate account profile');
|
|
21
|
+
}
|
|
22
|
+
return profile;
|
|
23
|
+
};
|
|
24
|
+
export const getAccountIds = resolveAccountProfile;
|
|
25
|
+
//# sourceMappingURL=profile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"profile.js","sourceRoot":"","sources":["../../../src/services/accounts/profile.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,gBAAgB,EAAe,MAAM,uBAAuB,CAAC;AAStE,MAAM,WAAW,GAAG,KAAM,CAAC;AAE3B,MAAM,mBAAmB,GAAG,CAAC,UAAuB,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;AACpF,MAAM,qBAAqB,GAAG,CAAC,GAAW,EAAe,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAgB,CAAC;AAE3F,MAAM,mBAAmB,GAAG,WAAW,CACrC,KAAK,EAAE,GAAW,EAAE,EAAE;IACpB,MAAM,UAAU,GAAG,qBAAqB,CAAC,GAAG,CAAC,CAAC;IAC9C,MAAM,MAAM,GAAG,MAAM,gBAAgB,CAAC,UAAU,CAAC,CAAC;IAClD,MAAM,GAAG,GAAG,GAAG,MAAM,CAAC,OAAO,EAAE,CAAC;IAChC,OAAO;QACL,GAAG;QACH,MAAM,EAAE,QAAQ,GAAG,cAAc;QACjC,IAAI,EAAE,QAAQ,GAAG,YAAY;QAC7B,OAAO,EAAE,QAAQ,GAAG,eAAe;KACpC,CAAC;AACJ,CAAC,EACD,EAAE,MAAM,EAAE,WAAW,EAAE,CACxB,CAAC;AAEF,MAAM,CAAC,MAAM,qBAAqB,GAAG,KAAK,EAAE,UAAuB,EAA4B,EAAE;IAC/F,MAAM,OAAO,GAAG,MAAM,mBAAmB,CAAC,KAAK,CAAC,mBAAmB,CAAC,UAAU,CAAC,CAAC,CAAC;IACjF,IAAI,CAAC,OAAO,EAAE;QACZ,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;KAC3D;IACD,OAAO,OAAO,CAAC;AACjB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,aAAa,GAAG,qBAAqB,CAAC","sourcesContent":["import { createCache } from '@yuants/cache';\nimport { getAccountDetail, ICredential } from '../../api/private-api';\n\nexport interface IAccountProfile {\n uid: string;\n future: string;\n spot: string;\n unified: string;\n}\n\nconst PROFILE_TTL = 60_000;\n\nconst serializeCredential = (credential: ICredential) => JSON.stringify(credential);\nconst deserializeCredential = (key: string): ICredential => JSON.parse(key) as ICredential;\n\nconst accountProfileCache = createCache<IAccountProfile>(\n async (key: string) => {\n const credential = deserializeCredential(key);\n const detail = await getAccountDetail(credential);\n const uid = `${detail.user_id}`;\n return {\n uid,\n future: `gate/${uid}/future/USDT`,\n spot: `gate/${uid}/spot/USDT`,\n unified: `gate/${uid}/unified/USDT`,\n };\n },\n { expire: PROFILE_TTL },\n);\n\nexport const resolveAccountProfile = async (credential: ICredential): Promise<IAccountProfile> => {\n const profile = await accountProfileCache.query(serializeCredential(credential));\n if (!profile) {\n throw new Error('Unable to resolve Gate account profile');\n }\n return profile;\n};\n\nexport const getAccountIds = resolveAccountProfile;\n"]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { getSpotAccounts } from '../../api/private-api';
|
|
2
|
+
export const getSpotAccountInfo = async (credential, account_id) => {
|
|
3
|
+
var _a, _b;
|
|
4
|
+
const res = await getSpotAccounts(credential);
|
|
5
|
+
if (!Array.isArray(res)) {
|
|
6
|
+
throw new Error('Failed to load spot balances');
|
|
7
|
+
}
|
|
8
|
+
const balance = Number((_b = (_a = res.find((item) => item.currency === 'USDT')) === null || _a === void 0 ? void 0 : _a.available) !== null && _b !== void 0 ? _b : '0');
|
|
9
|
+
return {
|
|
10
|
+
account_id,
|
|
11
|
+
money: {
|
|
12
|
+
currency: 'USDT',
|
|
13
|
+
equity: balance,
|
|
14
|
+
free: balance,
|
|
15
|
+
},
|
|
16
|
+
positions: [],
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=spot.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"spot.js","sourceRoot":"","sources":["../../../src/services/accounts/spot.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAe,MAAM,uBAAuB,CAAC;AAErE,MAAM,CAAC,MAAM,kBAAkB,GAAG,KAAK,EAAE,UAAuB,EAAE,UAAkB,EAAE,EAAE;;IACtF,MAAM,GAAG,GAAG,MAAM,eAAe,CAAC,UAAU,CAAC,CAAC;IAC9C,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;QACvB,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;KACjD;IACD,MAAM,OAAO,GAAG,MAAM,CAAC,MAAA,MAAA,GAAG,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,KAAK,MAAM,CAAC,0CAAE,SAAS,mCAAI,GAAG,CAAC,CAAC;IACvF,OAAO;QACL,UAAU;QACV,KAAK,EAAE;YACL,QAAQ,EAAE,MAAM;YAChB,MAAM,EAAE,OAAO;YACf,IAAI,EAAE,OAAO;SACd;QACD,SAAS,EAAE,EAAE;KACd,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { getSpotAccounts, ICredential } from '../../api/private-api';\n\nexport const getSpotAccountInfo = async (credential: ICredential, account_id: string) => {\n const res = await getSpotAccounts(credential);\n if (!Array.isArray(res)) {\n throw new Error('Failed to load spot balances');\n }\n const balance = Number(res.find((item) => item.currency === 'USDT')?.available ?? '0');\n return {\n account_id,\n money: {\n currency: 'USDT',\n equity: balance,\n free: balance,\n },\n positions: [],\n };\n};\n"]}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { getSpotTickers } from '../../api/public-api';
|
|
2
|
+
import { getUnifiedAccounts } from '../../api/private-api';
|
|
3
|
+
import { loadFuturePositions } from './future';
|
|
4
|
+
export const getUnifiedAccountInfo = async (credential, account_id) => {
|
|
5
|
+
var _a, _b;
|
|
6
|
+
const [futurePositions, unifiedAccount, spotTickers] = await Promise.all([
|
|
7
|
+
loadFuturePositions(credential),
|
|
8
|
+
getUnifiedAccounts(credential, {}),
|
|
9
|
+
getSpotTickers({}),
|
|
10
|
+
]);
|
|
11
|
+
const balances = (_a = unifiedAccount === null || unifiedAccount === void 0 ? void 0 : unifiedAccount.balances) !== null && _a !== void 0 ? _a : {};
|
|
12
|
+
const spotTickerList = Array.isArray(spotTickers) ? spotTickers : [];
|
|
13
|
+
const spotPositions = Object.keys(balances)
|
|
14
|
+
.map((currency) => {
|
|
15
|
+
var _a, _b;
|
|
16
|
+
if (currency === 'USDT')
|
|
17
|
+
return undefined;
|
|
18
|
+
let currency_pair = `${currency}_USDT`;
|
|
19
|
+
if (currency === 'SOL2') {
|
|
20
|
+
currency_pair = 'SOL_USDT';
|
|
21
|
+
}
|
|
22
|
+
const closable_price = Number(((_a = spotTickerList.find((ticker) => ticker.currency_pair === currency_pair)) === null || _a === void 0 ? void 0 : _a.last) || 0);
|
|
23
|
+
const volume = Number(((_b = balances[currency]) === null || _b === void 0 ? void 0 : _b.available) || 0);
|
|
24
|
+
return {
|
|
25
|
+
datasource_id: 'gate/spot',
|
|
26
|
+
position_id: currency,
|
|
27
|
+
product_id: currency,
|
|
28
|
+
direction: 'LONG',
|
|
29
|
+
volume,
|
|
30
|
+
free_volume: volume,
|
|
31
|
+
closable_price,
|
|
32
|
+
position_price: closable_price,
|
|
33
|
+
floating_profit: 0,
|
|
34
|
+
valuation: closable_price * volume,
|
|
35
|
+
};
|
|
36
|
+
})
|
|
37
|
+
.filter((item) => !!item);
|
|
38
|
+
const free = Number(((_b = balances['USDT']) === null || _b === void 0 ? void 0 : _b.available) || 0);
|
|
39
|
+
const equity = Number((unifiedAccount === null || unifiedAccount === void 0 ? void 0 : unifiedAccount.unified_account_total_equity) || 0);
|
|
40
|
+
return {
|
|
41
|
+
account_id,
|
|
42
|
+
money: {
|
|
43
|
+
currency: 'USDT',
|
|
44
|
+
equity,
|
|
45
|
+
free,
|
|
46
|
+
},
|
|
47
|
+
positions: [...futurePositions, ...spotPositions],
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
//# sourceMappingURL=unified.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unified.js","sourceRoot":"","sources":["../../../src/services/accounts/unified.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,kBAAkB,EAAe,MAAM,uBAAuB,CAAC;AACxE,OAAO,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC;AAE/C,MAAM,CAAC,MAAM,qBAAqB,GAAG,KAAK,EAAE,UAAuB,EAAE,UAAkB,EAAE,EAAE;;IACzF,MAAM,CAAC,eAAe,EAAE,cAAc,EAAE,WAAW,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;QACvE,mBAAmB,CAAC,UAAU,CAAC;QAC/B,kBAAkB,CAAC,UAAU,EAAE,EAAE,CAAC;QAClC,cAAc,CAAC,EAAE,CAAC;KACnB,CAAC,CAAC;IAEH,MAAM,QAAQ,GAAG,MAAA,cAAc,aAAd,cAAc,uB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AAA,cAAc,aAAd,cAAc,uBAAd,cAAc,CAAE,4BAA4B,KAAI,CAAC,CAAC,CAAC;IAEzE,OAAO;QACL,UAAU;QACV,KAAK,EAAE;YACL,QAAQ,EAAE,MAAM;YAChB,MAAM;YACN,IAAI;SACL;QACD,SAAS,EAAE,CAAC,GAAG,eAAe,EAAE,GAAG,aAAa,CAAC;KAClD,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import type { IPosition } from '@yuants/data-account';\nimport { getSpotTickers } from '../../api/public-api';\nimport { getUnifiedAccounts, ICredential } from '../../api/private-api';\nimport { loadFuturePositions } from './future';\n\nexport const getUnifiedAccountInfo = async (credential: ICredential, account_id: string) => {\n const [futurePositions, unifiedAccount, spotTickers] = await Promise.all([\n loadFuturePositions(credential),\n getUnifiedAccounts(credential, {}),\n getSpotTickers({}),\n ]);\n\n const balances = unifiedAccount?.balances ?? {};\n const spotTickerList = Array.isArray(spotTickers) ? spotTickers : [];\n\n const spotPositions: IPosition[] = Object.keys(balances)\n .map((currency) => {\n if (currency === 'USDT') return undefined;\n let currency_pair = `${currency}_USDT`;\n if (currency === 'SOL2') {\n currency_pair = 'SOL_USDT';\n }\n const closable_price = Number(\n spotTickerList.find((ticker) => ticker.currency_pair === currency_pair)?.last || 0,\n );\n const volume = Number(balances[currency]?.available || 0);\n return {\n datasource_id: 'gate/spot',\n position_id: currency,\n product_id: currency,\n direction: 'LONG',\n volume,\n free_volume: volume,\n closable_price,\n position_price: closable_price,\n floating_profit: 0,\n valuation: closable_price * volume,\n } as IPosition;\n })\n .filter((item): item is IPosition => !!item);\n\n const free = Number(balances['USDT']?.available || 0);\n const equity = Number(unifiedAccount?.unified_account_total_equity || 0);\n\n return {\n account_id,\n money: {\n currency: 'USDT',\n equity,\n free,\n },\n positions: [...futurePositions, ...spotPositions],\n };\n};\n"]}
|