@yuants/vendor-gate 0.4.21 → 0.4.23
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/services/accounts/unified.js +53 -7
- package/dist/services/accounts/unified.js.map +1 -1
- package/dist/services/exchange.js +3 -16
- package/dist/services/exchange.js.map +1 -1
- package/lib/services/accounts/unified.d.ts.map +1 -1
- package/lib/services/accounts/unified.js +53 -7
- package/lib/services/accounts/unified.js.map +1 -1
- package/lib/services/exchange.js +3 -16
- package/lib/services/exchange.js.map +1 -1
- package/package.json +3 -3
- package/temp/package-deps.json +8 -10
- package/dist/services/accounts/future.js +0 -45
- package/dist/services/accounts/future.js.map +0 -1
- package/dist/services/accounts/spot.js +0 -38
- package/dist/services/accounts/spot.js.map +0 -1
- package/lib/services/accounts/future.d.ts +0 -5
- package/lib/services/accounts/future.d.ts.map +0 -1
- package/lib/services/accounts/future.js +0 -50
- package/lib/services/accounts/future.js.map +0 -1
- package/lib/services/accounts/spot.d.ts +0 -3
- package/lib/services/accounts/spot.d.ts.map +0 -1
- package/lib/services/accounts/spot.js +0 -42
- package/lib/services/accounts/spot.js.map +0 -1
|
@@ -1,19 +1,65 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { getSpotTickers } from '../../api/public-api';
|
|
3
|
-
import { loadFuturePositions } from './future';
|
|
1
|
+
import { createCache } from '@yuants/cache';
|
|
4
2
|
import { encodePath } from '@yuants/utils';
|
|
3
|
+
import { getUnifiedAccounts, getFuturePositions } from '../../api/private-api';
|
|
4
|
+
import { getSpotTickers } from '../../api/public-api';
|
|
5
|
+
import { listProducts } from '../markets/product';
|
|
6
|
+
const spotProductMapCache = createCache(async () => {
|
|
7
|
+
const products = await listProducts();
|
|
8
|
+
const map = new Map();
|
|
9
|
+
for (const product of products !== null && products !== void 0 ? products : []) {
|
|
10
|
+
const [, instType] = product.product_id.split('/');
|
|
11
|
+
if (instType === 'SPOT') {
|
|
12
|
+
map.set(product.base_currency, product.product_id);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return map;
|
|
16
|
+
}, { expire: 86400000 });
|
|
17
|
+
const loadFuturePositions = async (credential) => {
|
|
18
|
+
const positions = [];
|
|
19
|
+
const positionsRes = await getFuturePositions(credential, 'usdt');
|
|
20
|
+
for (const position of Array.isArray(positionsRes) ? positionsRes : []) {
|
|
21
|
+
if (!(Math.abs(position.size) > 0))
|
|
22
|
+
continue;
|
|
23
|
+
const product_id = encodePath('GATE', 'FUTURE', position.contract);
|
|
24
|
+
const volume = Math.abs(position.size);
|
|
25
|
+
const closable_price = Number(position.mark_price);
|
|
26
|
+
const valuation = volume * closable_price;
|
|
27
|
+
positions.push({
|
|
28
|
+
datasource_id: 'GATE',
|
|
29
|
+
position_id: `${position.contract}-${position.leverage}-${position.mode}`,
|
|
30
|
+
product_id,
|
|
31
|
+
direction: position.mode === 'dual_long'
|
|
32
|
+
? 'LONG'
|
|
33
|
+
: position.mode === 'dual_short'
|
|
34
|
+
? 'SHORT'
|
|
35
|
+
: position.size > 0
|
|
36
|
+
? 'LONG'
|
|
37
|
+
: 'SHORT',
|
|
38
|
+
volume,
|
|
39
|
+
free_volume: Math.abs(position.size),
|
|
40
|
+
position_price: Number(position.entry_price),
|
|
41
|
+
closable_price,
|
|
42
|
+
floating_profit: Number(position.unrealised_pnl),
|
|
43
|
+
liquidation_price: position.liq_price,
|
|
44
|
+
valuation,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return positions;
|
|
48
|
+
};
|
|
5
49
|
export const getUnifiedAccountInfo = async (credential) => {
|
|
6
50
|
var _a;
|
|
7
|
-
const [futurePositions, unifiedAccount, spotTickers] = await Promise.all([
|
|
51
|
+
const [futurePositions, unifiedAccount, spotTickers, spotProductMap] = await Promise.all([
|
|
8
52
|
loadFuturePositions(credential),
|
|
9
53
|
getUnifiedAccounts(credential, {}),
|
|
10
54
|
getSpotTickers({}),
|
|
55
|
+
spotProductMapCache.query('').catch(() => undefined),
|
|
11
56
|
]);
|
|
57
|
+
const resolvedSpotProductMap = spotProductMap !== null && spotProductMap !== void 0 ? spotProductMap : new Map();
|
|
12
58
|
const balances = (_a = unifiedAccount === null || unifiedAccount === void 0 ? void 0 : unifiedAccount.balances) !== null && _a !== void 0 ? _a : {};
|
|
13
59
|
const spotTickerList = Array.isArray(spotTickers) ? spotTickers : [];
|
|
14
60
|
const spotPositions = Object.keys(balances)
|
|
15
61
|
.map((currency) => {
|
|
16
|
-
var _a, _b;
|
|
62
|
+
var _a, _b, _c;
|
|
17
63
|
let currency_pair = `${currency}_USDT`;
|
|
18
64
|
if (currency === 'SOL2') {
|
|
19
65
|
currency_pair = 'SOL_USDT';
|
|
@@ -28,9 +74,9 @@ export const getUnifiedAccountInfo = async (credential) => {
|
|
|
28
74
|
if (Math.abs(volume) === 0)
|
|
29
75
|
return undefined;
|
|
30
76
|
return {
|
|
31
|
-
datasource_id: '
|
|
77
|
+
datasource_id: 'GATE',
|
|
32
78
|
position_id: currency,
|
|
33
|
-
product_id: encodePath('GATE', 'SPOT', currency),
|
|
79
|
+
product_id: (_c = resolvedSpotProductMap.get(currency)) !== null && _c !== void 0 ? _c : encodePath('GATE', 'SPOT', currency),
|
|
34
80
|
direction: 'LONG',
|
|
35
81
|
volume,
|
|
36
82
|
free_volume: volume,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unified.js","sourceRoot":"","sources":["../../../src/services/accounts/unified.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"unified.js","sourceRoot":"","sources":["../../../src/services/accounts/unified.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAe,MAAM,uBAAuB,CAAC;AAC5F,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EA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AAc;YAC9B,eAAe,EAAE,cAAc,GAAG,MAAM;YACxC,SAAS,EAAE,cAAc,GAAG,MAAM;SACtB,CAAC;IACjB,CAAC,CAAC;SACD,MAAM,CAAC,CAAC,IAAI,EAAqB,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IAE/C,OAAO,CAAC,GAAG,eAAe,EAAE,GAAG,aAAa,CAAC,CAAC;AAChD,CAAC,CAAC","sourcesContent":["import { createCache } from '@yuants/cache';\nimport type { IPosition } from '@yuants/data-account';\nimport { encodePath } from '@yuants/utils';\nimport { getUnifiedAccounts, getFuturePositions, ICredential } from '../../api/private-api';\nimport { getSpotTickers } from '../../api/public-api';\nimport { listProducts } from '../markets/product';\n\nconst spotProductMapCache = createCache(\n async () => {\n const products = await listProducts();\n const map = new Map<string, string>();\n for (const product of products ?? []) {\n const [, instType] = product.product_id.split('/');\n if (instType === 'SPOT') {\n map.set(product.base_currency, product.product_id);\n }\n }\n return map;\n },\n { expire: 86_400_000 },\n);\n\nconst loadFuturePositions = async (credential: ICredential): Promise<IPosition[]> => {\n const positions: IPosition[] = [];\n const positionsRes = await getFuturePositions(credential, 'usdt');\n\n for (const position of Array.isArray(positionsRes) ? positionsRes : []) {\n if (!(Math.abs(position.size) > 0)) continue;\n\n const product_id = encodePath('GATE', 'FUTURE', position.contract);\n const volume = Math.abs(position.size);\n const closable_price = Number(position.mark_price);\n const valuation = volume * closable_price;\n positions.push({\n datasource_id: 'GATE',\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 liquidation_price: position.liq_price,\n valuation,\n });\n }\n\n return positions;\n};\n\nexport const getUnifiedAccountInfo = async (credential: ICredential) => {\n const [futurePositions, unifiedAccount, spotTickers, spotProductMap] = await Promise.all([\n loadFuturePositions(credential),\n getUnifiedAccounts(credential, {}),\n getSpotTickers({}),\n spotProductMapCache.query('').catch(() => undefined),\n ]);\n\n const resolvedSpotProductMap = spotProductMap ?? new Map<string, string>();\n const balances = unifiedAccount?.balances ?? {};\n const spotTickerList = Array.isArray(spotTickers) ? spotTickers : [];\n const spotPositions: IPosition[] = Object.keys(balances)\n .map((currency) => {\n let currency_pair = `${currency}_USDT`;\n if (currency === 'SOL2') {\n currency_pair = 'SOL_USDT';\n }\n if (currency === 'GTSOL') {\n currency_pair = 'SOL_USDT';\n }\n const closable_price =\n currency === 'USDT'\n ? 1\n : Number(spotTickerList.find((ticker) => ticker.currency_pair === currency_pair)?.last || 0);\n const volume = Number(balances[currency]?.available || 0);\n if (Math.abs(volume) === 0) return undefined;\n return {\n datasource_id: 'GATE',\n position_id: currency,\n product_id: resolvedSpotProductMap.get(currency) ?? encodePath('GATE', 'SPOT', currency),\n direction: 'LONG',\n volume,\n free_volume: volume,\n closable_price,\n position_price: closable_price,\n floating_profit: closable_price * volume,\n valuation: closable_price * volume,\n } as IPosition;\n })\n .filter((item): item is IPosition => !!item);\n\n return [...futurePositions, ...spotPositions];\n};\n"]}
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import { provideExchangeServices } from '@yuants/exchange';
|
|
2
2
|
import { Terminal } from '@yuants/protocol';
|
|
3
|
-
import { decodePath } from '@yuants/utils';
|
|
4
3
|
import { getCredentialId } from './accounts/profile';
|
|
5
4
|
import { getUnifiedAccountInfo } from './accounts/unified';
|
|
6
5
|
import { cancelOrder } from './orders/cancelOrder';
|
|
7
6
|
import { submitOrder } from './orders/submitOrder';
|
|
8
|
-
import { getFutureAccountInfo } from './accounts/future';
|
|
9
7
|
import { listOrders, getOrdersByProductId } from './orders/listOrders';
|
|
10
8
|
import { listProducts } from './markets/product';
|
|
11
9
|
const terminal = Terminal.fromNodeEnv();
|
|
@@ -22,10 +20,7 @@ provideExchangeServices(terminal, {
|
|
|
22
20
|
getCredentialId,
|
|
23
21
|
listProducts,
|
|
24
22
|
getPositions: async function (credential) {
|
|
25
|
-
const
|
|
26
|
-
// getFutureAccountInfo(credential),
|
|
27
|
-
getUnifiedAccountInfo(credential),
|
|
28
|
-
]);
|
|
23
|
+
const unifiedPositions = await getUnifiedAccountInfo(credential);
|
|
29
24
|
return [...unifiedPositions];
|
|
30
25
|
},
|
|
31
26
|
getOrders: async function (credential) {
|
|
@@ -36,16 +31,8 @@ provideExchangeServices(terminal, {
|
|
|
36
31
|
return [...umOrders];
|
|
37
32
|
},
|
|
38
33
|
getPositionsByProductId: async function (credential, product_id) {
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
// const positions = await getSpotAccountInfoSnapshot(credential);
|
|
42
|
-
// return positions.filter((position) => position.product_id === product_id);
|
|
43
|
-
// }
|
|
44
|
-
if (instType === 'FUTURE') {
|
|
45
|
-
const positions = await getFutureAccountInfo(credential);
|
|
46
|
-
return positions.filter((position) => position.product_id === product_id);
|
|
47
|
-
}
|
|
48
|
-
throw new Error(`Unsupported instType: ${instType}`);
|
|
34
|
+
const positions = await getUnifiedAccountInfo(credential);
|
|
35
|
+
return positions.filter((position) => position.product_id === product_id);
|
|
49
36
|
},
|
|
50
37
|
getOrdersByProductId,
|
|
51
38
|
submitOrder,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exchange.js","sourceRoot":"","sources":["../../src/services/exchange.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAC3D,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"exchange.js","sourceRoot":"","sources":["../../src/services/exchange.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAC3D,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CA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,EAAE,EAAE,CAAC,QAAQ,CAAC,UAAU,KAAK,UAAU,CAAC,CAAC;IAC5E,CAAC;IACD,oBAAoB;IACpB,WAAW;IACX,WAAW,EAAE,GAAG,EAAE;QAChB,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;IACrC,CAAC;IACD,WAAW;CACZ,CAAC,CAAC","sourcesContent":["import { IPosition } from '@yuants/data-account';\nimport { IOrder } from '@yuants/data-order';\nimport { provideExchangeServices } from '@yuants/exchange';\nimport { Terminal } from '@yuants/protocol';\nimport { getCredentialId } from './accounts/profile';\nimport { getUnifiedAccountInfo } from './accounts/unified';\nimport { cancelOrder } from './orders/cancelOrder';\nimport { submitOrder } from './orders/submitOrder';\nimport { ICredential } from '../api/private-api';\nimport { listOrders, getOrdersByProductId } from './orders/listOrders';\nimport { listProducts } from './markets/product';\n\nconst terminal = Terminal.fromNodeEnv();\n\nprovideExchangeServices<ICredential>(terminal, {\n name: 'GATE',\n credentialSchema: {\n type: 'object',\n required: ['access_key', 'secret_key'],\n properties: {\n access_key: { type: 'string' },\n secret_key: { type: 'string' },\n },\n },\n getCredentialId,\n listProducts,\n getPositions: async function (credential: ICredential): Promise<IPosition[]> {\n const unifiedPositions = await getUnifiedAccountInfo(credential);\n return [...unifiedPositions];\n },\n getOrders: async function (credential: ICredential): Promise<IOrder[]> {\n const [umOrders] = await Promise.all([\n listOrders(credential),\n // listSpotOrders(credential),\n ]);\n return [...umOrders];\n },\n getPositionsByProductId: async function (\n credential: ICredential,\n product_id: string,\n ): Promise<IPosition[]> {\n const positions = await getUnifiedAccountInfo(credential);\n return positions.filter((position) => position.product_id === product_id);\n },\n getOrdersByProductId,\n submitOrder,\n modifyOrder: () => {\n throw new Error('Not implemented');\n },\n cancelOrder,\n});\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unified.d.ts","sourceRoot":"","sources":["../../../src/services/accounts/unified.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"unified.d.ts","sourceRoot":"","sources":["../../../src/services/accounts/unified.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEtD,OAAO,EAA0C,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAuD5F,eAAO,MAAM,qBAAqB,eAAsB,WAAW,yBA0ClE,CAAC"}
|
|
@@ -1,22 +1,68 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getUnifiedAccountInfo = void 0;
|
|
4
|
+
const cache_1 = require("@yuants/cache");
|
|
5
|
+
const utils_1 = require("@yuants/utils");
|
|
4
6
|
const private_api_1 = require("../../api/private-api");
|
|
5
7
|
const public_api_1 = require("../../api/public-api");
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
+
const product_1 = require("../markets/product");
|
|
9
|
+
const spotProductMapCache = (0, cache_1.createCache)(async () => {
|
|
10
|
+
const products = await (0, product_1.listProducts)();
|
|
11
|
+
const map = new Map();
|
|
12
|
+
for (const product of products !== null && products !== void 0 ? products : []) {
|
|
13
|
+
const [, instType] = product.product_id.split('/');
|
|
14
|
+
if (instType === 'SPOT') {
|
|
15
|
+
map.set(product.base_currency, product.product_id);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return map;
|
|
19
|
+
}, { expire: 86400000 });
|
|
20
|
+
const loadFuturePositions = async (credential) => {
|
|
21
|
+
const positions = [];
|
|
22
|
+
const positionsRes = await (0, private_api_1.getFuturePositions)(credential, 'usdt');
|
|
23
|
+
for (const position of Array.isArray(positionsRes) ? positionsRes : []) {
|
|
24
|
+
if (!(Math.abs(position.size) > 0))
|
|
25
|
+
continue;
|
|
26
|
+
const product_id = (0, utils_1.encodePath)('GATE', 'FUTURE', position.contract);
|
|
27
|
+
const volume = Math.abs(position.size);
|
|
28
|
+
const closable_price = Number(position.mark_price);
|
|
29
|
+
const valuation = volume * closable_price;
|
|
30
|
+
positions.push({
|
|
31
|
+
datasource_id: 'GATE',
|
|
32
|
+
position_id: `${position.contract}-${position.leverage}-${position.mode}`,
|
|
33
|
+
product_id,
|
|
34
|
+
direction: position.mode === 'dual_long'
|
|
35
|
+
? 'LONG'
|
|
36
|
+
: position.mode === 'dual_short'
|
|
37
|
+
? 'SHORT'
|
|
38
|
+
: position.size > 0
|
|
39
|
+
? 'LONG'
|
|
40
|
+
: 'SHORT',
|
|
41
|
+
volume,
|
|
42
|
+
free_volume: Math.abs(position.size),
|
|
43
|
+
position_price: Number(position.entry_price),
|
|
44
|
+
closable_price,
|
|
45
|
+
floating_profit: Number(position.unrealised_pnl),
|
|
46
|
+
liquidation_price: position.liq_price,
|
|
47
|
+
valuation,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
return positions;
|
|
51
|
+
};
|
|
8
52
|
const getUnifiedAccountInfo = async (credential) => {
|
|
9
53
|
var _a;
|
|
10
|
-
const [futurePositions, unifiedAccount, spotTickers] = await Promise.all([
|
|
11
|
-
|
|
54
|
+
const [futurePositions, unifiedAccount, spotTickers, spotProductMap] = await Promise.all([
|
|
55
|
+
loadFuturePositions(credential),
|
|
12
56
|
(0, private_api_1.getUnifiedAccounts)(credential, {}),
|
|
13
57
|
(0, public_api_1.getSpotTickers)({}),
|
|
58
|
+
spotProductMapCache.query('').catch(() => undefined),
|
|
14
59
|
]);
|
|
60
|
+
const resolvedSpotProductMap = spotProductMap !== null && spotProductMap !== void 0 ? spotProductMap : new Map();
|
|
15
61
|
const balances = (_a = unifiedAccount === null || unifiedAccount === void 0 ? void 0 : unifiedAccount.balances) !== null && _a !== void 0 ? _a : {};
|
|
16
62
|
const spotTickerList = Array.isArray(spotTickers) ? spotTickers : [];
|
|
17
63
|
const spotPositions = Object.keys(balances)
|
|
18
64
|
.map((currency) => {
|
|
19
|
-
var _a, _b;
|
|
65
|
+
var _a, _b, _c;
|
|
20
66
|
let currency_pair = `${currency}_USDT`;
|
|
21
67
|
if (currency === 'SOL2') {
|
|
22
68
|
currency_pair = 'SOL_USDT';
|
|
@@ -31,9 +77,9 @@ const getUnifiedAccountInfo = async (credential) => {
|
|
|
31
77
|
if (Math.abs(volume) === 0)
|
|
32
78
|
return undefined;
|
|
33
79
|
return {
|
|
34
|
-
datasource_id: '
|
|
80
|
+
datasource_id: 'GATE',
|
|
35
81
|
position_id: currency,
|
|
36
|
-
product_id: (0, utils_1.encodePath)('GATE', 'SPOT', currency),
|
|
82
|
+
product_id: (_c = resolvedSpotProductMap.get(currency)) !== null && _c !== void 0 ? _c : (0, utils_1.encodePath)('GATE', 'SPOT', currency),
|
|
37
83
|
direction: 'LONG',
|
|
38
84
|
volume,
|
|
39
85
|
free_volume: volume,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unified.js","sourceRoot":"","sources":["../../../src/services/accounts/unified.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"unified.js","sourceRoot":"","sources":["../../../src/services/accounts/unified.ts"],"names":[],"mappings":";;;AAAA,yCAA4C;AAE5C,yCAA2C;AAC3C,uDAA4F;AAC5F,qDAAsD;AACtD,gDAAkD;AAElD,MAAM,mBAAmB,GAAG,IAAA,mBAAW,EACrC,KAAK,IAAI,EAAE;IACT,MAAM,QAAQ,GAAG,MAAM,IAAA,sBAAY,GAAE,CAAC;IACtC,MAAM,GAAG,GAAG,IAAI,GAAG,EAAkB,CAAC;IACtC,KAAK,MAAM,OAAO,IAAI,QAAQ,aAAR,QAAQ,cAAR,QAAQ,GAAI,EAAE,EAAE;QACpC,MAAM,CAAC,EAAE,QAAQ,CAAC,GAAG,OAAO,CAAC,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACnD,IAAI,QAAQ,KAAK,MAAM,EAAE;YACvB,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,aAAa,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;SACpD;KACF;IACD,OAAO,GAAG,CAAC;AACb,CAAC,EACD,EAAE,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D,MAAM,cAAc,GAClB,QAAQ,KAAK,MAAM;YACjB,CAAC,CAAC,CAAC;YACH,CAAC,CAAC,MAAM,CAAC,CAAA,MAAA,cAAc,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,aAAa,KAAK,aAAa,CAAC,0CAAE,IAAI,KAAI,CAAC,CAAC,CAAC;QACjG,MAAM,MAAM,GAAG,MAAM,CAAC,CAAA,MAAA,QAAQ,CAAC,QAAQ,CAAC,0CAAE,SAAS,KAAI,CAAC,CAAC,CAAC;QAC1D,IAAI,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC;YAAE,OAAO,SAAS,CAAC;QAC7C,OAAO;YACL,aAAa,EAAE,MAAM;YACrB,WAAW,EAAE,QAAQ;YACrB,UAAU,EAAE,MAAA,sBAAsB,CAAC,GAAG,CAAC,QAAQ,CAAC,mCAAI,IAAA,kBAAU,EAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAC;YACxF,SAAS,EAAE,MAAM;YACjB,MAAM;YACN,WAAW,EAAE,MAAM;YACnB,cAAc;YACd,cAAc,EAAE,cAAc;YAC9B,eAAe,EAAE,cAAc,GAAG,MAAM;YACxC,SAAS,EAAE,cAAc,GAAG,MAAM;SACtB,CAAC;IACjB,CAAC,CAAC;SACD,MAAM,CAAC,CAAC,IAAI,EAAqB,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IAE/C,OAAO,CAAC,GAAG,eAAe,EAAE,GAAG,aAAa,CAAC,CAAC;AAChD,CAAC,CAAC;AA1CW,QAAA,qBAAqB,yBA0ChC","sourcesContent":["import { createCache } from '@yuants/cache';\nimport type { IPosition } from '@yuants/data-account';\nimport { encodePath } from '@yuants/utils';\nimport { getUnifiedAccounts, getFuturePositions, ICredential } from '../../api/private-api';\nimport { getSpotTickers } from '../../api/public-api';\nimport { listProducts } from '../markets/product';\n\nconst spotProductMapCache = createCache(\n async () => {\n const products = await listProducts();\n const map = new Map<string, string>();\n for (const product of products ?? []) {\n const [, instType] = product.product_id.split('/');\n if (instType === 'SPOT') {\n map.set(product.base_currency, product.product_id);\n }\n }\n return map;\n },\n { expire: 86_400_000 },\n);\n\nconst loadFuturePositions = async (credential: ICredential): Promise<IPosition[]> => {\n const positions: IPosition[] = [];\n const positionsRes = await getFuturePositions(credential, 'usdt');\n\n for (const position of Array.isArray(positionsRes) ? positionsRes : []) {\n if (!(Math.abs(position.size) > 0)) continue;\n\n const product_id = encodePath('GATE', 'FUTURE', position.contract);\n const volume = Math.abs(position.size);\n const closable_price = Number(position.mark_price);\n const valuation = volume * closable_price;\n positions.push({\n datasource_id: 'GATE',\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 liquidation_price: position.liq_price,\n valuation,\n });\n }\n\n return positions;\n};\n\nexport const getUnifiedAccountInfo = async (credential: ICredential) => {\n const [futurePositions, unifiedAccount, spotTickers, spotProductMap] = await Promise.all([\n loadFuturePositions(credential),\n getUnifiedAccounts(credential, {}),\n getSpotTickers({}),\n spotProductMapCache.query('').catch(() => undefined),\n ]);\n\n const resolvedSpotProductMap = spotProductMap ?? new Map<string, string>();\n const balances = unifiedAccount?.balances ?? {};\n const spotTickerList = Array.isArray(spotTickers) ? spotTickers : [];\n const spotPositions: IPosition[] = Object.keys(balances)\n .map((currency) => {\n let currency_pair = `${currency}_USDT`;\n if (currency === 'SOL2') {\n currency_pair = 'SOL_USDT';\n }\n if (currency === 'GTSOL') {\n currency_pair = 'SOL_USDT';\n }\n const closable_price =\n currency === 'USDT'\n ? 1\n : Number(spotTickerList.find((ticker) => ticker.currency_pair === currency_pair)?.last || 0);\n const volume = Number(balances[currency]?.available || 0);\n if (Math.abs(volume) === 0) return undefined;\n return {\n datasource_id: 'GATE',\n position_id: currency,\n product_id: resolvedSpotProductMap.get(currency) ?? encodePath('GATE', 'SPOT', currency),\n direction: 'LONG',\n volume,\n free_volume: volume,\n closable_price,\n position_price: closable_price,\n floating_profit: closable_price * volume,\n valuation: closable_price * volume,\n } as IPosition;\n })\n .filter((item): item is IPosition => !!item);\n\n return [...futurePositions, ...spotPositions];\n};\n"]}
|
package/lib/services/exchange.js
CHANGED
|
@@ -2,12 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const exchange_1 = require("@yuants/exchange");
|
|
4
4
|
const protocol_1 = require("@yuants/protocol");
|
|
5
|
-
const utils_1 = require("@yuants/utils");
|
|
6
5
|
const profile_1 = require("./accounts/profile");
|
|
7
6
|
const unified_1 = require("./accounts/unified");
|
|
8
7
|
const cancelOrder_1 = require("./orders/cancelOrder");
|
|
9
8
|
const submitOrder_1 = require("./orders/submitOrder");
|
|
10
|
-
const future_1 = require("./accounts/future");
|
|
11
9
|
const listOrders_1 = require("./orders/listOrders");
|
|
12
10
|
const product_1 = require("./markets/product");
|
|
13
11
|
const terminal = protocol_1.Terminal.fromNodeEnv();
|
|
@@ -24,10 +22,7 @@ const terminal = protocol_1.Terminal.fromNodeEnv();
|
|
|
24
22
|
getCredentialId: profile_1.getCredentialId,
|
|
25
23
|
listProducts: product_1.listProducts,
|
|
26
24
|
getPositions: async function (credential) {
|
|
27
|
-
const
|
|
28
|
-
// getFutureAccountInfo(credential),
|
|
29
|
-
(0, unified_1.getUnifiedAccountInfo)(credential),
|
|
30
|
-
]);
|
|
25
|
+
const unifiedPositions = await (0, unified_1.getUnifiedAccountInfo)(credential);
|
|
31
26
|
return [...unifiedPositions];
|
|
32
27
|
},
|
|
33
28
|
getOrders: async function (credential) {
|
|
@@ -38,16 +33,8 @@ const terminal = protocol_1.Terminal.fromNodeEnv();
|
|
|
38
33
|
return [...umOrders];
|
|
39
34
|
},
|
|
40
35
|
getPositionsByProductId: async function (credential, product_id) {
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
// const positions = await getSpotAccountInfoSnapshot(credential);
|
|
44
|
-
// return positions.filter((position) => position.product_id === product_id);
|
|
45
|
-
// }
|
|
46
|
-
if (instType === 'FUTURE') {
|
|
47
|
-
const positions = await (0, future_1.getFutureAccountInfo)(credential);
|
|
48
|
-
return positions.filter((position) => position.product_id === product_id);
|
|
49
|
-
}
|
|
50
|
-
throw new Error(`Unsupported instType: ${instType}`);
|
|
36
|
+
const positions = await (0, unified_1.getUnifiedAccountInfo)(credential);
|
|
37
|
+
return positions.filter((position) => position.product_id === product_id);
|
|
51
38
|
},
|
|
52
39
|
getOrdersByProductId: listOrders_1.getOrdersByProductId,
|
|
53
40
|
submitOrder: submitOrder_1.submitOrder,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exchange.js","sourceRoot":"","sources":["../../src/services/exchange.ts"],"names":[],"mappings":";;AAEA,+CAA2D;AAC3D,+CAA4C;AAC5C,
|
|
1
|
+
{"version":3,"file":"exchange.js","sourceRoot":"","sources":["../../src/services/exchange.ts"],"names":[],"mappings":";;AAEA,+CAA2D;AAC3D,+CAA4C;AAC5C,gDAAqD;AACrD,gDAA2D;AAC3D,sDAAmD;AACnD,sDAAmD;AAEnD,oDAAuE;AACvE,+CAAiD;AAEjD,MAAM,QAAQ,GAAG,mBAAQ,CAAC,WAAW,EAAE,CAAC;AAExC,IAAA,kCAAuB,EAAc,QAAQ,EAAE;IAC7C,IAAI,EAAE,MAAM;IACZ,gBAAgB,EAAE;QAChB,IAAI,EAAE,QAAQ;QACd,QAAQ,EAAE,CAAC,YAAY,EAAE,YAAY,CAAC;QACtC,UAAU,EAAE;YACV,UAAU,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;YAC9B,UAAU,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;SAC/B;KACF;IACD,eAAe,EAAf,yBAAe;IACf,YAAY,EAAZ,sBAAY;IACZ,YAAY,EAAE,KAAK,WAAW,UAAuB;QACnD,MAAM,gBAAgB,GAAG,MAAM,IAAA,+BAAqB,EAAC,UAAU,CAAC,CAAC;QACjE,OAAO,CAAC,GAAG,gBAAgB,CAAC,CAAC;IAC/B,CAAC;IACD,SAAS,EAAE,KAAK,WAAW,UAAuB;QAChD,MAAM,CAAC,QAAQ,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;YACnC,IAAA,uBAAU,EAAC,UAAU,CAAC;YACtB,gCAAgC;SACjC,CAAC,CAAC;QACH,OAAO,CAAC,GAAG,QAAQ,CAAC,CAAC;IACvB,CAAC;IACD,uBAAuB,EAAE,KAAK,WAC5B,UAAuB,EACvB,UAAkB;QAElB,MAAM,SAAS,GAAG,MAAM,IAAA,+BAAqB,EAAC,UAAU,CAAC,CAAC;QAC1D,OAAO,SAAS,CAAC,MAAM,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,UAAU,KAAK,UAAU,CAAC,CAAC;IAC5E,CAAC;IACD,oBAAoB,EAApB,iCAAoB;IACpB,WAAW,EAAX,yBAAW;IACX,WAAW,EAAE,GAAG,EAAE;QAChB,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;IACrC,CAAC;IACD,WAAW,EAAX,yBAAW;CACZ,CAAC,CAAC","sourcesContent":["import { IPosition } from '@yuants/data-account';\nimport { IOrder } from '@yuants/data-order';\nimport { provideExchangeServices } from '@yuants/exchange';\nimport { Terminal } from '@yuants/protocol';\nimport { getCredentialId } from './accounts/profile';\nimport { getUnifiedAccountInfo } from './accounts/unified';\nimport { cancelOrder } from './orders/cancelOrder';\nimport { submitOrder } from './orders/submitOrder';\nimport { ICredential } from '../api/private-api';\nimport { listOrders, getOrdersByProductId } from './orders/listOrders';\nimport { listProducts } from './markets/product';\n\nconst terminal = Terminal.fromNodeEnv();\n\nprovideExchangeServices<ICredential>(terminal, {\n name: 'GATE',\n credentialSchema: {\n type: 'object',\n required: ['access_key', 'secret_key'],\n properties: {\n access_key: { type: 'string' },\n secret_key: { type: 'string' },\n },\n },\n getCredentialId,\n listProducts,\n getPositions: async function (credential: ICredential): Promise<IPosition[]> {\n const unifiedPositions = await getUnifiedAccountInfo(credential);\n return [...unifiedPositions];\n },\n getOrders: async function (credential: ICredential): Promise<IOrder[]> {\n const [umOrders] = await Promise.all([\n listOrders(credential),\n // listSpotOrders(credential),\n ]);\n return [...umOrders];\n },\n getPositionsByProductId: async function (\n credential: ICredential,\n product_id: string,\n ): Promise<IPosition[]> {\n const positions = await getUnifiedAccountInfo(credential);\n return positions.filter((position) => position.product_id === product_id);\n },\n getOrdersByProductId,\n submitOrder,\n modifyOrder: () => {\n throw new Error('Not implemented');\n },\n cancelOrder,\n});\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yuants/vendor-gate",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.23",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"files": [
|
|
6
6
|
"lib",
|
|
@@ -11,14 +11,14 @@
|
|
|
11
11
|
"@yuants/cache": "0.3.3",
|
|
12
12
|
"@yuants/protocol": "0.53.2",
|
|
13
13
|
"@yuants/transfer": "0.2.39",
|
|
14
|
-
"@yuants/data-account": "0.
|
|
14
|
+
"@yuants/data-account": "0.10.0",
|
|
15
15
|
"@yuants/data-order": "0.6.6",
|
|
16
16
|
"@yuants/sql": "0.9.30",
|
|
17
17
|
"@yuants/data-product": "0.5.0",
|
|
18
18
|
"@yuants/utils": "0.14.0",
|
|
19
19
|
"@yuants/data-series": "0.3.51",
|
|
20
20
|
"@yuants/data-interest-rate": "0.1.48",
|
|
21
|
-
"@yuants/exchange": "0.
|
|
21
|
+
"@yuants/exchange": "0.5.0",
|
|
22
22
|
"@yuants/data-quote": "0.2.43",
|
|
23
23
|
"rxjs": "~7.5.6"
|
|
24
24
|
},
|
package/temp/package-deps.json
CHANGED
|
@@ -1,26 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"apps/vendor-gate/AGENTS.md": "59be8d734f8a91ae5f7377e7c688f1fccb8641b7",
|
|
3
|
-
"apps/vendor-gate/CHANGELOG.json": "
|
|
4
|
-
"apps/vendor-gate/CHANGELOG.md": "
|
|
3
|
+
"apps/vendor-gate/CHANGELOG.json": "5c77c09330ab94d10c89d8b3c0551e00e72a908d",
|
|
4
|
+
"apps/vendor-gate/CHANGELOG.md": "a8dc5cab6d94c963aacb004ef0b711d4337f0724",
|
|
5
5
|
"apps/vendor-gate/README.md": "ea5ee3a61cd87d9104d729902dfaccac9b9913e0",
|
|
6
|
-
"apps/vendor-gate/SESSION_NOTES.md": "
|
|
6
|
+
"apps/vendor-gate/SESSION_NOTES.md": "eefeaa9e02b039bde7180b6a479fc3fa0e72321c",
|
|
7
7
|
"apps/vendor-gate/api-extractor.json": "62f4fd324425b9a235f0c117975967aab09ced0c",
|
|
8
8
|
"apps/vendor-gate/config/jest.config.json": "4bb17bde3ee911163a3edb36a6eb71491d80b1bd",
|
|
9
9
|
"apps/vendor-gate/config/rig.json": "f6c7b5537dc77a3170ba9f008bae3b6c3ee11956",
|
|
10
10
|
"apps/vendor-gate/config/typescript.json": "854907e8a821f2050f6533368db160c649c25348",
|
|
11
11
|
"apps/vendor-gate/etc/vendor-gate.api.md": "970682cd589432de553cc5cee2a0abe73814a515",
|
|
12
|
-
"apps/vendor-gate/package.json": "
|
|
12
|
+
"apps/vendor-gate/package.json": "286cadb50dc8059d94a1ef563160390123fa7cd1",
|
|
13
13
|
"apps/vendor-gate/src/api/http-client.ts": "97e0e70454c6072f388f90442ec4afabe6feb93b",
|
|
14
14
|
"apps/vendor-gate/src/api/private-api.ts": "d8e7c486135bce076f691dd247d0e2eb40543fa5",
|
|
15
15
|
"apps/vendor-gate/src/api/public-api.ts": "ee1a246a8833a7d9bb06e829b09a3bd80e0d86bf",
|
|
16
16
|
"apps/vendor-gate/src/api/rate-limiter.ts": "047c51029ffcaf98190cfd04d96816bbced5fa7c",
|
|
17
17
|
"apps/vendor-gate/src/index.ts": "31888627bccab532d55bd91833d80c72a4afae9b",
|
|
18
|
-
"apps/vendor-gate/src/services/accounts/future.ts": "d67d4926fe22f8e28c1a4928285ecaa319b58be8",
|
|
19
18
|
"apps/vendor-gate/src/services/accounts/profile.ts": "4132c7c3d9760b9a6694d5b3eae7e8b2847fe6c1",
|
|
20
|
-
"apps/vendor-gate/src/services/accounts/
|
|
21
|
-
"apps/vendor-gate/src/services/accounts/unified.ts": "f49b87a161553fd2abcce20ec76f8df3ae98c2bc",
|
|
19
|
+
"apps/vendor-gate/src/services/accounts/unified.ts": "8baba2c3f726d9733f8afbc860d9225d75ed3bd6",
|
|
22
20
|
"apps/vendor-gate/src/services/default-credential.ts": "b9ffa157b818148450bfaaf4851597e91901190c",
|
|
23
|
-
"apps/vendor-gate/src/services/exchange.ts": "
|
|
21
|
+
"apps/vendor-gate/src/services/exchange.ts": "3a8fbaecaf7ea0c5a03f3a61f21fb8d2d4588394",
|
|
24
22
|
"apps/vendor-gate/src/services/markets/interest-rate.ts": "e669eb1d664448f309bb062907b273157d1c111e",
|
|
25
23
|
"apps/vendor-gate/src/services/markets/product.ts": "217bf64b307adca4c251333462b75f73d0d85e83",
|
|
26
24
|
"apps/vendor-gate/src/services/markets/quote.ts": "1624d58b9c607ae01300de292de3ed49a2f2e037",
|
|
@@ -33,14 +31,14 @@
|
|
|
33
31
|
"libraries/cache/temp/package-deps.json": "a4afa15e6462983f9d3735d31dc1ed8a683fb4dc",
|
|
34
32
|
"libraries/protocol/temp/package-deps.json": "0bd43721e96039b52d7b59c834dc6df45cf75e3f",
|
|
35
33
|
"libraries/transfer/temp/package-deps.json": "36c58299bd6c841c5ba7252d71881a881570d08c",
|
|
36
|
-
"libraries/data-account/temp/package-deps.json": "
|
|
34
|
+
"libraries/data-account/temp/package-deps.json": "3ee766acef866410612b60dfc1012082c9c24556",
|
|
37
35
|
"libraries/data-order/temp/package-deps.json": "2adac9fc0423f9b1b7ddaa8946ab7af374cc22e1",
|
|
38
36
|
"libraries/sql/temp/package-deps.json": "4a9a7ec55f04b20459e664e81e76fa74b6c77b39",
|
|
39
37
|
"libraries/data-product/temp/package-deps.json": "a03f08f30800d5fb52c5d019bda4f8e7ec04e344",
|
|
40
38
|
"libraries/utils/temp/package-deps.json": "6d58e9b325e8d16de8a878c32010f626b12a01da",
|
|
41
39
|
"libraries/data-series/temp/package-deps.json": "c89ebffe302757903aa54eff78f76cb855486b8c",
|
|
42
40
|
"libraries/data-interest-rate/temp/package-deps.json": "cef1e1cb0116ad593c24635684e0cbf03488d67c",
|
|
43
|
-
"libraries/exchange/temp/package-deps.json": "
|
|
41
|
+
"libraries/exchange/temp/package-deps.json": "d40288d0530bc7e63246f234bdffa10a497776b1",
|
|
44
42
|
"libraries/data-quote/temp/package-deps.json": "34d079eab44d2bf65e07b112ac2099c6e92a015e",
|
|
45
43
|
"libraries/extension/temp/package-deps.json": "9569c553c2f9a7d50b70d8f101fc2d3825aaccb9",
|
|
46
44
|
"tools/toolkit/temp/package-deps.json": "23e053490eb8feade23e4d45de4e54883e322711"
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { encodePath } from '@yuants/utils';
|
|
2
|
-
import { getFuturePositions, getFuturesAccounts } from '../../api/private-api';
|
|
3
|
-
import { productCache } from '../markets/product';
|
|
4
|
-
export const loadFuturePositions = async (credential) => {
|
|
5
|
-
var _a;
|
|
6
|
-
const positions = [];
|
|
7
|
-
const positionsRes = await getFuturePositions(credential, 'usdt');
|
|
8
|
-
for (const position of Array.isArray(positionsRes) ? positionsRes : []) {
|
|
9
|
-
if (!(Math.abs(position.size) > 0))
|
|
10
|
-
continue;
|
|
11
|
-
const product_id = encodePath('GATE', 'FUTURE', position.contract);
|
|
12
|
-
const theProduct = await productCache.query(product_id);
|
|
13
|
-
const volume = Math.abs(position.size);
|
|
14
|
-
const closable_price = Number(position.mark_price);
|
|
15
|
-
const valuation = volume * closable_price * ((_a = theProduct === null || theProduct === void 0 ? void 0 : theProduct.value_scale) !== null && _a !== void 0 ? _a : 1);
|
|
16
|
-
positions.push({
|
|
17
|
-
datasource_id: 'GATE',
|
|
18
|
-
position_id: `${position.contract}-${position.leverage}-${position.mode}`,
|
|
19
|
-
product_id,
|
|
20
|
-
direction: position.mode === 'dual_long'
|
|
21
|
-
? 'LONG'
|
|
22
|
-
: position.mode === 'dual_short'
|
|
23
|
-
? 'SHORT'
|
|
24
|
-
: position.size > 0
|
|
25
|
-
? 'LONG'
|
|
26
|
-
: 'SHORT',
|
|
27
|
-
volume,
|
|
28
|
-
free_volume: Math.abs(position.size),
|
|
29
|
-
position_price: Number(position.entry_price),
|
|
30
|
-
closable_price,
|
|
31
|
-
floating_profit: Number(position.unrealised_pnl),
|
|
32
|
-
liquidation_price: position.liq_price,
|
|
33
|
-
valuation,
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
return positions;
|
|
37
|
-
};
|
|
38
|
-
export const getFutureAccountInfo = async (credential) => {
|
|
39
|
-
const [positions, rawAccount] = await Promise.all([
|
|
40
|
-
loadFuturePositions(credential),
|
|
41
|
-
getFuturesAccounts(credential, 'usdt'),
|
|
42
|
-
]);
|
|
43
|
-
return positions;
|
|
44
|
-
};
|
|
45
|
-
//# sourceMappingURL=future.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"future.js","sourceRoot":"","sources":["../../../src/services/accounts/future.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAe,MAAM,uBAAuB,CAAC;AAC5F,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD,MAAM,CAAC,MAAM,mBAAmB,GAAG,KAAK,EAAE,UAAuB,EAAwB,EAAE;;IACzF,MAAM,SAAS,GAAgB,EAAE,CAAC;IAClC,MA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cAAc,EAAE,MAAM,CAAC,QAAQ,CAAC,WAAW,CAAC;YAC5C,cAAc;YACd,eAAe,EAAE,MAAM,CAAC,QAAQ,CAAC,cAAc,CAAC;YAChD,iBAAiB,EAAE,QAAQ,CAAC,SAAS;YACrC,SAAS;SACV,CAAC,CAAC;KACJ;IAED,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,oBAAoB,GAAG,KAAK,EAAE,UAAuB,EAAE,EAAE;IACpE,MAAM,CAAC,SAAS,EAAE,UAAU,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;QAChD,mBAAmB,CAAC,UAAU,CAAC;QAC/B,kBAAkB,CAAC,UAAU,EAAE,MAAM,CAAC;KACvC,CAAC,CAAC;IAEH,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC","sourcesContent":["import type { IPosition } from '@yuants/data-account';\nimport { encodePath } from '@yuants/utils';\nimport { getFuturePositions, getFuturesAccounts, ICredential } from '../../api/private-api';\nimport { productCache } from '../markets/product';\n\nexport const loadFuturePositions = async (credential: ICredential): Promise<IPosition[]> => {\n const positions: IPosition[] = [];\n const positionsRes = await getFuturePositions(credential, 'usdt');\n\n for (const position of Array.isArray(positionsRes) ? positionsRes : []) {\n if (!(Math.abs(position.size) > 0)) continue;\n\n const product_id = encodePath('GATE', 'FUTURE', position.contract);\n const theProduct = await productCache.query(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 positions.push({\n datasource_id: 'GATE',\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 liquidation_price: position.liq_price,\n valuation,\n });\n }\n\n return positions;\n};\n\nexport const getFutureAccountInfo = async (credential: ICredential) => {\n const [positions, rawAccount] = await Promise.all([\n loadFuturePositions(credential),\n getFuturesAccounts(credential, 'usdt'),\n ]);\n\n return positions;\n};\n"]}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { createCache } from '@yuants/cache';
|
|
2
|
-
import { makeSpotPosition } from '@yuants/data-account';
|
|
3
|
-
import { getSpotAccounts } from '../../api/private-api';
|
|
4
|
-
import { encodePath } from '@yuants/utils';
|
|
5
|
-
import { listProducts } from '../markets/product';
|
|
6
|
-
const spotProductMapCache = createCache(async () => {
|
|
7
|
-
const products = await listProducts();
|
|
8
|
-
const map = new Map();
|
|
9
|
-
for (const product of products) {
|
|
10
|
-
const [, instType] = product.product_id.split('/');
|
|
11
|
-
if (instType === 'SPOT') {
|
|
12
|
-
map.set(product.base_currency, product.product_id);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
return map;
|
|
16
|
-
}, { expire: 86400000 });
|
|
17
|
-
export const getSpotAccountInfo = async (credential) => {
|
|
18
|
-
const [res, spotProductMap] = await Promise.all([
|
|
19
|
-
getSpotAccounts(credential),
|
|
20
|
-
spotProductMapCache.query(''),
|
|
21
|
-
]);
|
|
22
|
-
const resolvedSpotProductMap = spotProductMap !== null && spotProductMap !== void 0 ? spotProductMap : new Map();
|
|
23
|
-
if (!Array.isArray(res)) {
|
|
24
|
-
throw new Error('Failed to load spot balances');
|
|
25
|
-
}
|
|
26
|
-
return res.map((item) => {
|
|
27
|
-
var _a;
|
|
28
|
-
return makeSpotPosition({
|
|
29
|
-
datasource_id: 'GATE',
|
|
30
|
-
position_id: item.currency,
|
|
31
|
-
product_id: (_a = resolvedSpotProductMap.get(item.currency)) !== null && _a !== void 0 ? _a : encodePath('GATE', 'SPOT', `${item.currency}`),
|
|
32
|
-
volume: Number(item.available),
|
|
33
|
-
free_volume: Number(item.available),
|
|
34
|
-
closable_price: 1, // TODO: use real price
|
|
35
|
-
});
|
|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AAI,GAAG,EAAkB,CAAC;IAC3E,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;QACvB,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;KACjD;IACD,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;;QACtB,OAAO,gBAAgB,CAAC;YACtB,aAAa,EAAE,MAAM;YACrB,WAAW,EAAE,IAAI,CAAC,QAAQ;YAC1B,UAAU,EAAE,MAAA,sBAAsB,CAAC,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,mCAAI,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC;YACvG,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC;YAC9B,WAAW,EAAE,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC;YACnC,cAAc,EAAE,CAAC,EAAE,uBAAuB;SAC3C,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC","sourcesContent":["import { createCache } from '@yuants/cache';\nimport { makeSpotPosition } from '@yuants/data-account';\nimport { getSpotAccounts, ICredential } from '../../api/private-api';\nimport { encodePath } from '@yuants/utils';\nimport { listProducts } from '../markets/product';\n\nconst spotProductMapCache = createCache(\n async () => {\n const products = await listProducts();\n const map = new Map<string, string>();\n for (const product of products) {\n const [, instType] = product.product_id.split('/');\n if (instType === 'SPOT') {\n map.set(product.base_currency, product.product_id);\n }\n }\n return map;\n },\n { expire: 86_400_000 },\n);\n\nexport const getSpotAccountInfo = async (credential: ICredential) => {\n const [res, spotProductMap] = await Promise.all([\n getSpotAccounts(credential),\n spotProductMapCache.query(''),\n ]);\n const resolvedSpotProductMap = spotProductMap ?? new Map<string, string>();\n if (!Array.isArray(res)) {\n throw new Error('Failed to load spot balances');\n }\n return res.map((item) => {\n return makeSpotPosition({\n datasource_id: 'GATE',\n position_id: item.currency,\n product_id: resolvedSpotProductMap.get(item.currency) ?? encodePath('GATE', 'SPOT', `${item.currency}`),\n volume: Number(item.available),\n free_volume: Number(item.available),\n closable_price: 1, // TODO: use real price\n });\n });\n};\n"]}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { IPosition } from '@yuants/data-account';
|
|
2
|
-
import { ICredential } from '../../api/private-api';
|
|
3
|
-
export declare const loadFuturePositions: (credential: ICredential) => Promise<IPosition[]>;
|
|
4
|
-
export declare const getFutureAccountInfo: (credential: ICredential) => Promise<IPosition[]>;
|
|
5
|
-
//# sourceMappingURL=future.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"future.d.ts","sourceRoot":"","sources":["../../../src/services/accounts/future.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEtD,OAAO,EAA0C,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAG5F,eAAO,MAAM,mBAAmB,eAAsB,WAAW,KAAG,QAAQ,SAAS,EAAE,CAmCtF,CAAC;AAEF,eAAO,MAAM,oBAAoB,eAAsB,WAAW,yBAOjE,CAAC"}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getFutureAccountInfo = exports.loadFuturePositions = void 0;
|
|
4
|
-
const utils_1 = require("@yuants/utils");
|
|
5
|
-
const private_api_1 = require("../../api/private-api");
|
|
6
|
-
const product_1 = require("../markets/product");
|
|
7
|
-
const loadFuturePositions = async (credential) => {
|
|
8
|
-
var _a;
|
|
9
|
-
const positions = [];
|
|
10
|
-
const positionsRes = await (0, private_api_1.getFuturePositions)(credential, 'usdt');
|
|
11
|
-
for (const position of Array.isArray(positionsRes) ? positionsRes : []) {
|
|
12
|
-
if (!(Math.abs(position.size) > 0))
|
|
13
|
-
continue;
|
|
14
|
-
const product_id = (0, utils_1.encodePath)('GATE', 'FUTURE', position.contract);
|
|
15
|
-
const theProduct = await product_1.productCache.query(product_id);
|
|
16
|
-
const volume = Math.abs(position.size);
|
|
17
|
-
const closable_price = Number(position.mark_price);
|
|
18
|
-
const valuation = volume * closable_price * ((_a = theProduct === null || theProduct === void 0 ? void 0 : theProduct.value_scale) !== null && _a !== void 0 ? _a : 1);
|
|
19
|
-
positions.push({
|
|
20
|
-
datasource_id: 'GATE',
|
|
21
|
-
position_id: `${position.contract}-${position.leverage}-${position.mode}`,
|
|
22
|
-
product_id,
|
|
23
|
-
direction: position.mode === 'dual_long'
|
|
24
|
-
? 'LONG'
|
|
25
|
-
: position.mode === 'dual_short'
|
|
26
|
-
? 'SHORT'
|
|
27
|
-
: position.size > 0
|
|
28
|
-
? 'LONG'
|
|
29
|
-
: 'SHORT',
|
|
30
|
-
volume,
|
|
31
|
-
free_volume: Math.abs(position.size),
|
|
32
|
-
position_price: Number(position.entry_price),
|
|
33
|
-
closable_price,
|
|
34
|
-
floating_profit: Number(position.unrealised_pnl),
|
|
35
|
-
liquidation_price: position.liq_price,
|
|
36
|
-
valuation,
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
return positions;
|
|
40
|
-
};
|
|
41
|
-
exports.loadFuturePositions = loadFuturePositions;
|
|
42
|
-
const getFutureAccountInfo = async (credential) => {
|
|
43
|
-
const [positions, rawAccount] = await Promise.all([
|
|
44
|
-
(0, exports.loadFuturePositions)(credential),
|
|
45
|
-
(0, private_api_1.getFuturesAccounts)(credential, 'usdt'),
|
|
46
|
-
]);
|
|
47
|
-
return positions;
|
|
48
|
-
};
|
|
49
|
-
exports.getFutureAccountInfo = getFutureAccountInfo;
|
|
50
|
-
//# sourceMappingURL=future.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"future.js","sourceRoot":"","sources":["../../../src/services/accounts/future.ts"],"names":[],"mappings":";;;AACA,yCAA2C;AAC3C,uDAA4F;AAC5F,gDAAkD;AAE3C,MAAM,mBAAmB,GAAG,KAAK,EAA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;YACb,MAAM;YACN,WAAW,EAAE,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC;YACpC,cAAc,EAAE,MAAM,CAAC,QAAQ,CAAC,WAAW,CAAC;YAC5C,cAAc;YACd,eAAe,EAAE,MAAM,CAAC,QAAQ,CAAC,cAAc,CAAC;YAChD,iBAAiB,EAAE,QAAQ,CAAC,SAAS;YACrC,SAAS;SACV,CAAC,CAAC;KACJ;IAED,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC;AAnCW,QAAA,mBAAmB,uBAmC9B;AAEK,MAAM,oBAAoB,GAAG,KAAK,EAAE,UAAuB,EAAE,EAAE;IACpE,MAAM,CAAC,SAAS,EAAE,UAAU,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;QAChD,IAAA,2BAAmB,EAAC,UAAU,CAAC;QAC/B,IAAA,gCAAkB,EAAC,UAAU,EAAE,MAAM,CAAC;KACvC,CAAC,CAAC;IAEH,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC;AAPW,QAAA,oBAAoB,wBAO/B","sourcesContent":["import type { IPosition } from '@yuants/data-account';\nimport { encodePath } from '@yuants/utils';\nimport { getFuturePositions, getFuturesAccounts, ICredential } from '../../api/private-api';\nimport { productCache } from '../markets/product';\n\nexport const loadFuturePositions = async (credential: ICredential): Promise<IPosition[]> => {\n const positions: IPosition[] = [];\n const positionsRes = await getFuturePositions(credential, 'usdt');\n\n for (const position of Array.isArray(positionsRes) ? positionsRes : []) {\n if (!(Math.abs(position.size) > 0)) continue;\n\n const product_id = encodePath('GATE', 'FUTURE', position.contract);\n const theProduct = await productCache.query(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 positions.push({\n datasource_id: 'GATE',\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 liquidation_price: position.liq_price,\n valuation,\n });\n }\n\n return positions;\n};\n\nexport const getFutureAccountInfo = async (credential: ICredential) => {\n const [positions, rawAccount] = await Promise.all([\n loadFuturePositions(credential),\n getFuturesAccounts(credential, 'usdt'),\n ]);\n\n return positions;\n};\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"spot.d.ts","sourceRoot":"","sources":["../../../src/services/accounts/spot.ts"],"names":[],"mappings":"AAEA,OAAO,EAAmB,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAmBrE,eAAO,MAAM,kBAAkB,eAAsB,WAAW,wDAmB/D,CAAC"}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getSpotAccountInfo = void 0;
|
|
4
|
-
const cache_1 = require("@yuants/cache");
|
|
5
|
-
const data_account_1 = require("@yuants/data-account");
|
|
6
|
-
const private_api_1 = require("../../api/private-api");
|
|
7
|
-
const utils_1 = require("@yuants/utils");
|
|
8
|
-
const product_1 = require("../markets/product");
|
|
9
|
-
const spotProductMapCache = (0, cache_1.createCache)(async () => {
|
|
10
|
-
const products = await (0, product_1.listProducts)();
|
|
11
|
-
const map = new Map();
|
|
12
|
-
for (const product of products) {
|
|
13
|
-
const [, instType] = product.product_id.split('/');
|
|
14
|
-
if (instType === 'SPOT') {
|
|
15
|
-
map.set(product.base_currency, product.product_id);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
return map;
|
|
19
|
-
}, { expire: 86400000 });
|
|
20
|
-
const getSpotAccountInfo = async (credential) => {
|
|
21
|
-
const [res, spotProductMap] = await Promise.all([
|
|
22
|
-
(0, private_api_1.getSpotAccounts)(credential),
|
|
23
|
-
spotProductMapCache.query(''),
|
|
24
|
-
]);
|
|
25
|
-
const resolvedSpotProductMap = spotProductMap !== null && spotProductMap !== void 0 ? spotProductMap : new Map();
|
|
26
|
-
if (!Array.isArray(res)) {
|
|
27
|
-
throw new Error('Failed to load spot balances');
|
|
28
|
-
}
|
|
29
|
-
return res.map((item) => {
|
|
30
|
-
var _a;
|
|
31
|
-
return (0, data_account_1.makeSpotPosition)({
|
|
32
|
-
datasource_id: 'GATE',
|
|
33
|
-
position_id: item.currency,
|
|
34
|
-
product_id: (_a = resolvedSpotProductMap.get(item.currency)) !== null && _a !== void 0 ? _a : (0, utils_1.encodePath)('GATE', 'SPOT', `${item.currency}`),
|
|
35
|
-
volume: Number(item.available),
|
|
36
|
-
free_volume: Number(item.available),
|
|
37
|
-
closable_price: 1, // TODO: use real price
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
};
|
|
41
|
-
exports.getSpotAccountInfo = getSpotAccountInfo;
|
|
42
|
-
//# sourceMappingURL=spot.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"spot.js","sourceRoot":"","sources":["../../../src/services/accounts/spot.ts"],"names":[],"mappings":";;;AAAA,yCAA4C;AAC5C,uDAAwD;AACxD,uDAAqE;AACrE,yCAA2C;AAC3C,gDAAkD;AAElD,MAAM,mBAAmB,GAAG,IAAA,mBAAW,EACrC,KAAK,IAAI,EAAE;IACT,MAAM,QAAQ,GAAG,MAAM,IAAA,sBAAY,GAAE,CAAC;IACtC,MAAM,GAAG,GAAG,IAAI,GAAG,EAAkB,CAAC;IACtC,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE;QAC9B,MAAM,CAAC,EAAE,QAAQ,CAAC,GAAG,OAAO,CAAC,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACnD,IAAI,QAAQ,KAAK,MAAM,EAAE;YACvB,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,aAAa,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;SACpD;KACF;IACD,OAAO,GAAG,CAAC;AACb,CAAC,EACD,EAAE,MAAM,EAAE,QAAU,EAAE,CACvB,CAAC;AAEK,MAAM,kBAAkB,GAAG,KAAK,EAAE,UAAuB,EAAE,EAAE;IAClE,MAAM,CAAC,GAAG,EAAE,cAAc,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;QAC9C,IAAA,6BAAe,EAAC,UAAU,CAAC;QAC3B,mBAAmB,CAAC,KAAK,CAAC,EAAE,CAAC;KAC9B,CAAC,CAAC;IACH,MAAM,sBAAsB,GAAG,cAAc,aAAd,cAAc,cAAd,cAAc,GAAI,IAAI,GAAG,EAAkB,CAAC;IAC3E,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;QACvB,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;KACjD;IACD,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;;QACtB,OAAO,IAAA,+BAAgB,EAAC;YACtB,aAAa,EAAE,MAAM;YACrB,WAAW,EAAE,IAAI,CAAC,QAAQ;YAC1B,UAAU,EAAE,MAAA,sBAAsB,CAAC,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,mCAAI,IAAA,kBAAU,EAAC,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC;YACvG,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC;YAC9B,WAAW,EAAE,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC;YACnC,cAAc,EAAE,CAAC,EAAE,uBAAuB;SAC3C,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAnBW,QAAA,kBAAkB,sBAmB7B","sourcesContent":["import { createCache } from '@yuants/cache';\nimport { makeSpotPosition } from '@yuants/data-account';\nimport { getSpotAccounts, ICredential } from '../../api/private-api';\nimport { encodePath } from '@yuants/utils';\nimport { listProducts } from '../markets/product';\n\nconst spotProductMapCache = createCache(\n async () => {\n const products = await listProducts();\n const map = new Map<string, string>();\n for (const product of products) {\n const [, instType] = product.product_id.split('/');\n if (instType === 'SPOT') {\n map.set(product.base_currency, product.product_id);\n }\n }\n return map;\n },\n { expire: 86_400_000 },\n);\n\nexport const getSpotAccountInfo = async (credential: ICredential) => {\n const [res, spotProductMap] = await Promise.all([\n getSpotAccounts(credential),\n spotProductMapCache.query(''),\n ]);\n const resolvedSpotProductMap = spotProductMap ?? new Map<string, string>();\n if (!Array.isArray(res)) {\n throw new Error('Failed to load spot balances');\n }\n return res.map((item) => {\n return makeSpotPosition({\n datasource_id: 'GATE',\n position_id: item.currency,\n product_id: resolvedSpotProductMap.get(item.currency) ?? encodePath('GATE', 'SPOT', `${item.currency}`),\n volume: Number(item.available),\n free_volume: Number(item.available),\n closable_price: 1, // TODO: use real price\n });\n });\n};\n"]}
|