@yuants/vendor-aster 0.1.0 → 0.2.1

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.
@@ -0,0 +1,47 @@
1
+ import { provideAccountInfoService } from '@yuants/data-account';
2
+ import { Terminal } from '@yuants/protocol';
3
+ import { getApiV1Account, getApiV1TickerPrice } from './sapi';
4
+ const ADDRESS = process.env.ADDRESS;
5
+ export const SPOT_ACCOUNT_ID = `ASTER/${ADDRESS}/SPOT`;
6
+ provideAccountInfoService(Terminal.fromNodeEnv(), SPOT_ACCOUNT_ID, async () => {
7
+ const [x, prices] = await Promise.all([getApiV1Account({}), getApiV1TickerPrice({})]);
8
+ const positions = x.balances.map((b) => {
9
+ var _a, _b;
10
+ const thePrice = b.asset === 'USDT' ? 1 : (_b = (_a = prices.find((p) => p.symbol === b.asset + 'USDT')) === null || _a === void 0 ? void 0 : _a.price) !== null && _b !== void 0 ? _b : 0;
11
+ const volume = +b.free + +b.locked;
12
+ const position_price = +thePrice;
13
+ const closable_price = +thePrice;
14
+ const valuation = volume * closable_price;
15
+ const floating_profit = 0;
16
+ return {
17
+ position_id: b.asset,
18
+ datasource_id: 'ASTER',
19
+ product_id: b.asset,
20
+ direction: 'LONG',
21
+ volume,
22
+ free_volume: +b.free,
23
+ position_price,
24
+ closable_price,
25
+ floating_profit,
26
+ valuation,
27
+ };
28
+ });
29
+ const usdtAsset = x.balances.find((b) => b.asset === 'USDT');
30
+ const equity = positions.reduce((a, b) => a + b.valuation, 0);
31
+ const free = usdtAsset ? +usdtAsset.free : 0;
32
+ const balance = usdtAsset ? +usdtAsset.free + +usdtAsset.locked : 0;
33
+ const profit = equity - balance;
34
+ const used = equity - free;
35
+ return {
36
+ money: {
37
+ currency: 'USDT',
38
+ equity,
39
+ balance,
40
+ profit,
41
+ free,
42
+ used,
43
+ },
44
+ positions,
45
+ };
46
+ }, { auto_refresh_interval: 1000 });
47
+ //# sourceMappingURL=account-spot.js.map
@@ -0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WAAW,EAAE,CAAC,CAAC,KAAK;YACpB,aAAa,EAAE,OAAO;YACtB,UAAU,EAAE,CAAC,CAAC,KAAK;YACnB,SAAS,EAAE,MAAM;YACjB,MAAM;YACN,WAAW,EAAE,CAAC,CAAC,CAAC,IAAI;YACpB,cAAc;YACd,cAAc;YACd,eAAe;YACf,SAAS;SACV,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,MAAM,SAAS,GAAG,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,KAAK,MAAM,CAAC,CAAC;IAC7D,MAAM,MAAM,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC;IAC9D,MAAM,IAAI,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;IAC7C,MAAM,OAAO,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,IAAI,GAAG,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;IACpE,MAAM,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;IAChC,MAAM,IAAI,GAAG,MAAM,GAAG,IAAI,CAAC;IAE3B,OAAO;QACL,KAAK,EAAE;YACL,QAAQ,EAAE,MAAM;YAChB,MAAM;YACN,OAAO;YACP,MAAM;YACN,IAAI;YACJ,IAAI;SACL;QACD,SAAS;KACV,CAAC;AACJ,CAAC,EACD,EAAE,qBAAqB,EAAE,IAAI,EAAE,CAChC,CAAC","sourcesContent":["import { IPosition, provideAccountInfoService } from '@yuants/data-account';\nimport { Terminal } from '@yuants/protocol';\nimport { getApiV1Account, getApiV1TickerPrice } from './sapi';\n\nconst ADDRESS = process.env.ADDRESS!;\nexport const SPOT_ACCOUNT_ID = `ASTER/${ADDRESS}/SPOT`;\n\nprovideAccountInfoService(\n Terminal.fromNodeEnv(),\n SPOT_ACCOUNT_ID,\n async () => {\n const [x, prices] = await Promise.all([getApiV1Account({}), getApiV1TickerPrice({})]);\n\n const positions = x.balances.map((b): IPosition => {\n const thePrice = b.asset === 'USDT' ? 1 : prices.find((p) => p.symbol === b.asset + 'USDT')?.price ?? 0;\n\n const volume = +b.free + +b.locked;\n\n const position_price = +thePrice;\n const closable_price = +thePrice;\n const valuation = volume * closable_price;\n const floating_profit = 0;\n\n return {\n position_id: b.asset,\n datasource_id: 'ASTER',\n product_id: b.asset,\n direction: 'LONG',\n volume,\n free_volume: +b.free,\n position_price,\n closable_price,\n floating_profit,\n valuation,\n };\n });\n\n const usdtAsset = x.balances.find((b) => b.asset === 'USDT');\n const equity = positions.reduce((a, b) => a + b.valuation, 0);\n const free = usdtAsset ? +usdtAsset.free : 0;\n const balance = usdtAsset ? +usdtAsset.free + +usdtAsset.locked : 0;\n const profit = equity - balance;\n const used = equity - free;\n\n return {\n money: {\n currency: 'USDT',\n equity,\n balance,\n profit,\n free,\n used,\n },\n positions,\n };\n },\n { auto_refresh_interval: 1000 },\n);\n"]}
package/dist/account.js CHANGED
@@ -4,7 +4,7 @@ import { getFApiV4Account } from './api';
4
4
  const ADDRESS = process.env.ADDRESS;
5
5
  export const ACCOUNT_ID = `ASTER/${ADDRESS}`;
6
6
  provideAccountInfoService(Terminal.fromNodeEnv(), ACCOUNT_ID, async () => {
7
- const a = await getFApiV4Account({});
7
+ const [a] = await Promise.all([getFApiV4Account({})]);
8
8
  const profit = +a.totalUnrealizedProfit;
9
9
  const balance = +a.totalWalletBalance;
10
10
  const equity = balance + profit;
@@ -1 +1 @@
1
- {"version":3,"file":"account.js","sourceRoot":"","sources":["../src/account.ts"],"names":[],"mappings":"AAAA,OAAO,EAA4B,yBAAyB,EAAE,MAAM,sBAAsB,CAAC;AAC3F,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OA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;QAC/F,MAAM,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC;QAChC,WAAW,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC;QACrC,cAAc,EAAE,CAAC,CAAC,CAAC,UAAU;QAC7B,cAAc,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAC,WAAW,CAAC;QACtD,eAAe,EAAE,CAAC,CAAC,CAAC,gBAAgB;QACpC,SAAS,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;KACjC,CAAC,CACH,CAAC;IACJ,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;AAC9B,CAAC,EACD,EAAE,qBAAqB,EAAE,IAAI,EAAE,CAChC,CAAC","sourcesContent":["import { IAccountMoney, IPosition, provideAccountInfoService } from '@yuants/data-account';\nimport { Terminal } from '@yuants/protocol';\nimport { getFApiV4Account } from './api';\n\nconst ADDRESS = process.env.ADDRESS!;\nexport const ACCOUNT_ID = `ASTER/${ADDRESS}`;\n\nprovideAccountInfoService(\n Terminal.fromNodeEnv(),\n ACCOUNT_ID,\n async () => {\n const a = await getFApiV4Account({});\n\n const profit = +a.totalUnrealizedProfit;\n const balance = +a.totalWalletBalance;\n const equity = balance + profit;\n const free = +a.availableBalance;\n const used = equity - free;\n\n const money: IAccountMoney = {\n currency: 'USD',\n equity: equity,\n balance: balance,\n profit: profit,\n free: free,\n used: used,\n };\n const positions = a.positions\n .filter((p) => +p.positionAmt !== 0)\n .map(\n (p): IPosition => ({\n position_id: p.symbol,\n product_id: p.symbol,\n datasource_id: 'ASTER',\n direction: p.positionSide === 'BOTH' ? (+p.positionAmt > 0 ? 'LONG' : 'SHORT') : p.positionSide,\n volume: Math.abs(+p.positionAmt),\n free_volume: Math.abs(+p.positionAmt),\n position_price: +p.entryPrice,\n closable_price: Math.abs(+p.notional / +p.positionAmt),\n floating_profit: +p.unrealizedProfit,\n valuation: Math.abs(+p.notional),\n }),\n );\n return { money, positions };\n },\n { auto_refresh_interval: 1000 },\n);\n"]}
1
+ {"version":3,"file":"account.js","sourceRoot":"","sources":["../src/account.ts"],"names":[],"mappings":"AAAA,OAAO,EAA4B,yBAAyB,EAAE,MAAM,sBAAsB,CAAC;AAC3F,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAoB,gBAAgB,EAAE,MAAM,OAAO,CAAC;AAE3D,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,OAAQ,CAAC;AACrC,MAAM,CAAC,MAAM,UAAU,GAAG,SAAS,OAAO,EAAE,CAAC;AAE7C,yBAAyB,CACvB,QAAQ,CAAC,WAAW,EAAE,EACtB,UAAU,EACV,KAAK,IAAI,EAAE;IACT,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC,gBAAgB,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IAEtD,MAAM,MAAM,GAAG,CAAC,CAAC,CAAC,qBAAqB,CAAC;IACxC,MAAM,OAAO,GAAG,CAAC,CAAC,CAAC,kBAAkB,CAAC;IACtC,MAAM,MAAM,GAAG,OAAO,GAAG,MAAM,CAAC;IAChC,MAAM,IAAI,GAAG,CAAC,CAAC,CAAC,gBAAgB,CAAC;IACjC,MAAM,IAAI,GAAG,MAAM,GAAG,IAAI,CAAC;IAE3B,MAAM,KAAK,GAAkB;QAC3B,QAAQ,EAAE,KAAK;QACf,MAAM,EAAE,MAAM;QACd,OAAO,EAAE,OAAO;QAChB,MAAM,EAAE,MAAM;QACd,IAAI,EAAE,IAAI;QACV,IAAI,EAAE,IAAI;KACX,CAAC;IACF,MAAM,SAAS,GAAG,CAAC,CAAC,SAAS;SAC1B,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,WAAW,KAAK,CAAC,CAAC;SACnC,GAAG,CACF,CAAC,CAAC,EAAa,EAAE,CAAC,CAAC;QACjB,WAAW,EAAE,CAAC,CAAC,MAAM;QACrB,UAAU,EAAE,CAAC,CAAC,MAAM;QACpB,aAAa,EAAE,OAAO;QACtB,SAAS,EAAE,CAAC,CAAC,YAAY,KAAK,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,YAAY;QAC/F,MAAM,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC;QAChC,WAAW,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC;QACrC,cAAc,EAAE,CAAC,CAAC,CAAC,UAAU;QAC7B,cAAc,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAC,WAAW,CAAC;QACtD,eAAe,EAAE,CAAC,CAAC,CAAC,gBAAgB;QACpC,SAAS,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;KACjC,CAAC,CACH,CAAC;IACJ,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;AAC9B,CAAC,EACD,EAAE,qBAAqB,EAAE,IAAI,EAAE,CAChC,CAAC","sourcesContent":["import { IAccountMoney, IPosition, provideAccountInfoService } from '@yuants/data-account';\nimport { Terminal } from '@yuants/protocol';\nimport { getFApiV2Balance, getFApiV4Account } from './api';\n\nconst ADDRESS = process.env.ADDRESS!;\nexport const ACCOUNT_ID = `ASTER/${ADDRESS}`;\n\nprovideAccountInfoService(\n Terminal.fromNodeEnv(),\n ACCOUNT_ID,\n async () => {\n const [a] = await Promise.all([getFApiV4Account({})]);\n\n const profit = +a.totalUnrealizedProfit;\n const balance = +a.totalWalletBalance;\n const equity = balance + profit;\n const free = +a.availableBalance;\n const used = equity - free;\n\n const money: IAccountMoney = {\n currency: 'USD',\n equity: equity,\n balance: balance,\n profit: profit,\n free: free,\n used: used,\n };\n const positions = a.positions\n .filter((p) => +p.positionAmt !== 0)\n .map(\n (p): IPosition => ({\n position_id: p.symbol,\n product_id: p.symbol,\n datasource_id: 'ASTER',\n direction: p.positionSide === 'BOTH' ? (+p.positionAmt > 0 ? 'LONG' : 'SHORT') : p.positionSide,\n volume: Math.abs(+p.positionAmt),\n free_volume: Math.abs(+p.positionAmt),\n position_price: +p.entryPrice,\n closable_price: Math.abs(+p.notional / +p.positionAmt),\n floating_profit: +p.unrealizedProfit,\n valuation: Math.abs(+p.notional),\n }),\n );\n return { money, positions };\n },\n { auto_refresh_interval: 1000 },\n);\n"]}
package/dist/api.js CHANGED
@@ -1,33 +1,7 @@
1
+ import { opensslEquivalentHMAC } from './utils';
1
2
  const API_KEY = process.env.API_KEY;
2
3
  const SECRET_KEY = process.env.SECRET_KEY;
3
4
  const BASE_URL = 'https://fapi.asterdex.com';
4
- function arrayBufferToHex(buffer) {
5
- const bytes = new Uint8Array(buffer);
6
- const hexArray = [];
7
- for (const byte of bytes) {
8
- const hex = byte.toString(16).padStart(2, '0');
9
- hexArray.push(hex);
10
- }
11
- return hexArray.join('');
12
- }
13
- async function opensslEquivalentHMAC(message, secretKey) {
14
- try {
15
- const keyBuffer = new TextEncoder().encode(secretKey);
16
- const messageBuffer = new TextEncoder().encode(message);
17
- // 导入密钥
18
- const cryptoKey = await crypto.subtle.importKey('raw', keyBuffer, {
19
- name: 'HMAC',
20
- hash: { name: 'SHA-256' },
21
- }, false, ['sign']);
22
- // 进行 HMAC-SHA256 签名
23
- const signature = await crypto.subtle.sign('HMAC', cryptoKey, messageBuffer);
24
- // 转换为16进制小写(与 openssl 输出格式一致)
25
- return arrayBufferToHex(signature);
26
- }
27
- catch (error) {
28
- throw new Error(`HMAC-SHA256 签名失败: ${error}`);
29
- }
30
- }
31
5
  const request = async (type, method, endpoint, params = {}) => {
32
6
  const needApiKey = type !== 'NONE';
33
7
  const needSign = type === 'TRADE' || type === 'USER_DATA';
@@ -45,7 +19,7 @@ const request = async (type, method, endpoint, params = {}) => {
45
19
  url.searchParams.set('signature', signature);
46
20
  }
47
21
  console.info(url.toString());
48
- return fetch(url.toString(), {
22
+ const res = await fetch(url.toString(), {
49
23
  method,
50
24
  headers: needApiKey
51
25
  ? {
@@ -53,6 +27,10 @@ const request = async (type, method, endpoint, params = {}) => {
53
27
  }
54
28
  : {},
55
29
  }).then((response) => response.json());
30
+ if (res.code && res.code !== 0) {
31
+ throw JSON.stringify(res);
32
+ }
33
+ return res;
56
34
  };
57
35
  const createApi = (type, method, endpoint) => (params) => request(type, method, endpoint, params);
58
36
  /**
@@ -61,5 +39,6 @@ const createApi = (type, method, endpoint) => (params) => request(type, method,
61
39
  * https://github.com/asterdex/api-docs/blob/master/aster-finance-futures-api_CN.md#%E8%B4%A6%E6%88%B7%E4%BF%A1%E6%81%AFv4-user_data
62
40
  */
63
41
  export const getFApiV4Account = createApi('USER_DATA', 'GET', '/fapi/v4/account');
42
+ export const getFApiV2Balance = createApi('USER_DATA', 'GET', '/fapi/v2/balance');
64
43
  export const postFApiV1Order = createApi('TRADE', 'POST', '/fapi/v1/order');
65
44
  //# sourceMappingURL=api.js.map
package/dist/api.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,OAAQ,CAAC;AACrC,MAAM,UAAU,GAAG,OAAO,CAAC,GAAG,CAAC,UAAW,CAAC;AAE3C,MAAM,QAAQ,GAAG,2BAA2B,CAAC;AAE7C,SAAS,gBAAgB,CAAC,MAAmB;IAC3C,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;IACrC,MAAM,QAAQ,GAAa,EAAE,CAAC;IAE9B,KAAK,MAAM,IAAI,IA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AM,gBAAgB,GAAG,SAAS,CAqDvC,WAAW,EAAE,KAAK,EAAE,kBAAkB,CAAC,CAAC;AAE1C,MAAM,CAAC,MAAM,eAAe,GAAG,SAAS,CAmBtC,OAAO,EAAE,MAAM,EAAE,gBAAgB,CAAC,CAAC","sourcesContent":["const API_KEY = process.env.API_KEY!;\nconst SECRET_KEY = process.env.SECRET_KEY!;\n\nconst BASE_URL = 'https://fapi.asterdex.com';\n\nfunction arrayBufferToHex(buffer: ArrayBuffer): string {\n const bytes = new Uint8Array(buffer);\n const hexArray: string[] = [];\n\n for (const byte of bytes) {\n const hex = byte.toString(16).padStart(2, '0');\n hexArray.push(hex);\n }\n\n return hexArray.join('');\n}\n\nasync function opensslEquivalentHMAC(message: string, secretKey: string): Promise<string> {\n try {\n const keyBuffer = new TextEncoder().encode(secretKey);\n const messageBuffer = new TextEncoder().encode(message);\n\n // 导入密钥\n const cryptoKey = await crypto.subtle.importKey(\n 'raw',\n keyBuffer,\n {\n name: 'HMAC',\n hash: { name: 'SHA-256' },\n },\n false,\n ['sign'],\n );\n\n // 进行 HMAC-SHA256 签名\n const signature = await crypto.subtle.sign('HMAC', cryptoKey, messageBuffer);\n\n // 转换为16进制小写(与 openssl 输出格式一致)\n return arrayBufferToHex(signature);\n } catch (error) {\n throw new Error(`HMAC-SHA256 签名失败: ${error}`);\n }\n}\n\nconst request = async <T>(\n type: 'NONE' | 'TRADE' | 'USER_DATA' | 'USER_STREAM' | 'MARKET_DATA',\n method: string,\n endpoint: string,\n params: any = {},\n): Promise<T> => {\n const needApiKey = type !== 'NONE';\n const needSign = type === 'TRADE' || type === 'USER_DATA';\n\n const url = new URL(BASE_URL);\n url.pathname = endpoint;\n for (const [key, value] of Object.entries(params)) {\n if (value === undefined) continue;\n url.searchParams.set(key, `${value}`);\n }\n\n if (needSign) {\n url.searchParams.set('timestamp', `${Date.now()}`);\n const msg = url.search.slice(1); // 去掉开头的 '?'\n const signature = await opensslEquivalentHMAC(msg, SECRET_KEY);\n url.searchParams.set('signature', signature);\n }\n\n console.info(url.toString());\n return fetch(url.toString(), {\n method,\n headers: needApiKey\n ? {\n 'X-MBX-APIKEY': API_KEY,\n }\n : {},\n }).then((response) => response.json() as any as T);\n};\n\nconst createApi =\n <TReq, TRes>(\n type: 'NONE' | 'TRADE' | 'USER_DATA' | 'USER_STREAM' | 'MARKET_DATA',\n method: string,\n endpoint: string,\n ) =>\n (params: TReq) =>\n request<TRes>(type, method, endpoint, params);\n\n/**\n * 获取账户信息\n *\n * https://github.com/asterdex/api-docs/blob/master/aster-finance-futures-api_CN.md#%E8%B4%A6%E6%88%B7%E4%BF%A1%E6%81%AFv4-user_data\n */\nexport const getFApiV4Account = createApi<\n {},\n {\n feeTier: number;\n canTrade: boolean;\n canDeposit: boolean;\n canWithdraw: boolean;\n updateTime: number;\n totalInitialMargin: string;\n totalMaintMargin: string;\n totalWalletBalance: string;\n totalUnrealizedProfit: string;\n totalMarginBalance: string;\n totalPositionInitialMargin: string;\n totalOpenOrderInitialMargin: string;\n totalCrossWalletBalance: string;\n totalCrossUnPnl: string;\n availableBalance: string;\n maxWithdrawAmount: string;\n assets: {\n asset: string;\n walletBalance: string;\n unrealizedProfit: string;\n marginBalance: string;\n maintMargin: string;\n initialMargin: string;\n positionInitialMargin: string;\n openOrderInitialMargin: string;\n maxWithdrawAmount: string;\n crossWalletBalance: string;\n crossUnPnl: string;\n availableBalance: string;\n marginAvailable: boolean;\n updateTime: number;\n }[];\n positions: {\n symbol: string;\n initialMargin: string;\n maintMargin: string;\n unrealizedProfit: string;\n positionInitialMargin: string;\n openOrderInitialMargin: string;\n leverage: string;\n isolated: boolean;\n entryPrice: string;\n maxNotional: string;\n positionSide: 'BOTH' | 'LONG' | 'SHORT';\n positionAmt: string;\n notional: string;\n isolatedWallet: string;\n updateTime: number;\n }[];\n }\n>('USER_DATA', 'GET', '/fapi/v4/account');\n\nexport const postFApiV1Order = createApi<\n {\n symbol: string;\n side: 'BUY' | 'SELL';\n positionSide?: 'BOTH' | 'LONG' | 'SHORT';\n type:\n | 'MARKET'\n | 'LIMIT'\n | 'STOP'\n | 'STOP_MARKET'\n | 'TAKE_PROFIT'\n | 'TAKE_PROFIT_MARKET'\n | 'TRAILING_STOP_MARKET';\n reduceOnly?: 'true' | 'false';\n quantity?: number;\n price?: number;\n timeInForce?: 'GTC' | 'IOC' | 'FOK' | 'GTX' | 'HIDDEN';\n },\n {}\n>('TRADE', 'POST', '/fapi/v1/order');\n"]}
1
+ {"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAC;AAEhD,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,OAAQ,CAAC;AACrC,MAAM,UAAU,GAAG,OAAO,CAAC,GAAG,CAAC,UAAW,CAAC;AAE3C,MAAM,QAAQ,GAAG,2BAA2B,CAAC;AAE7C,MAAM,OAAO,GAAG,KAAK,EACnB,IAAoE,EACpE,MAAc,EACd,QAAgB,EAChB,SAAc,EAAE,EACJ,EAAE;IACd,MAAM,UAAU,GAAG,IAAI,KAAK,MAAM,CAAC;IACnC,MAAM,QAAQ,GAAG,IAAI,KAAK,OAAO,IAAI,IAAI,KAAK,WAAW,CAAC;IAE1D,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC9B,GAAG,CAAC,QAAQ,GAAG,QAAQ,CAAC;IACxB,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;QACjD,IAAI,KAAK,KAAK,SAAS;YAAE,SAAS;QAClC,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,KAAK,EAAE,CAAC,CAAC;KACvC;IAED,IAAI,QAAQ,EAAE;QACZ,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,WAAW,EAAE,GAAG,IAAI,CAAC,GAAG,EAA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AAM,EAAE,gBAAgB,CAAC,CAAC","sourcesContent":["import { opensslEquivalentHMAC } from './utils';\n\nconst API_KEY = process.env.API_KEY!;\nconst SECRET_KEY = process.env.SECRET_KEY!;\n\nconst BASE_URL = 'https://fapi.asterdex.com';\n\nconst request = async <T>(\n type: 'NONE' | 'TRADE' | 'USER_DATA' | 'USER_STREAM' | 'MARKET_DATA',\n method: string,\n endpoint: string,\n params: any = {},\n): Promise<T> => {\n const needApiKey = type !== 'NONE';\n const needSign = type === 'TRADE' || type === 'USER_DATA';\n\n const url = new URL(BASE_URL);\n url.pathname = endpoint;\n for (const [key, value] of Object.entries(params)) {\n if (value === undefined) continue;\n url.searchParams.set(key, `${value}`);\n }\n\n if (needSign) {\n url.searchParams.set('timestamp', `${Date.now()}`);\n const msg = url.search.slice(1); // 去掉开头的 '?'\n const signature = await opensslEquivalentHMAC(msg, SECRET_KEY);\n url.searchParams.set('signature', signature);\n }\n\n console.info(url.toString());\n const res = await fetch(url.toString(), {\n method,\n headers: needApiKey\n ? {\n 'X-MBX-APIKEY': API_KEY,\n }\n : {},\n }).then((response) => response.json());\n if (res.code && res.code !== 0) {\n throw JSON.stringify(res);\n }\n return res;\n};\n\nconst createApi =\n <TReq, TRes>(\n type: 'NONE' | 'TRADE' | 'USER_DATA' | 'USER_STREAM' | 'MARKET_DATA',\n method: string,\n endpoint: string,\n ) =>\n (params: TReq) =>\n request<TRes>(type, method, endpoint, params);\n\n/**\n * 获取账户信息\n *\n * https://github.com/asterdex/api-docs/blob/master/aster-finance-futures-api_CN.md#%E8%B4%A6%E6%88%B7%E4%BF%A1%E6%81%AFv4-user_data\n */\nexport const getFApiV4Account = createApi<\n {},\n {\n feeTier: number;\n canTrade: boolean;\n canDeposit: boolean;\n canWithdraw: boolean;\n updateTime: number;\n totalInitialMargin: string;\n totalMaintMargin: string;\n totalWalletBalance: string;\n totalUnrealizedProfit: string;\n totalMarginBalance: string;\n totalPositionInitialMargin: string;\n totalOpenOrderInitialMargin: string;\n totalCrossWalletBalance: string;\n totalCrossUnPnl: string;\n availableBalance: string;\n maxWithdrawAmount: string;\n assets: {\n asset: string;\n walletBalance: string;\n unrealizedProfit: string;\n marginBalance: string;\n maintMargin: string;\n initialMargin: string;\n positionInitialMargin: string;\n openOrderInitialMargin: string;\n maxWithdrawAmount: string;\n crossWalletBalance: string;\n crossUnPnl: string;\n availableBalance: string;\n marginAvailable: boolean;\n updateTime: number;\n }[];\n positions: {\n symbol: string;\n initialMargin: string;\n maintMargin: string;\n unrealizedProfit: string;\n positionInitialMargin: string;\n openOrderInitialMargin: string;\n leverage: string;\n isolated: boolean;\n entryPrice: string;\n maxNotional: string;\n positionSide: 'BOTH' | 'LONG' | 'SHORT';\n positionAmt: string;\n notional: string;\n isolatedWallet: string;\n updateTime: number;\n }[];\n }\n>('USER_DATA', 'GET', '/fapi/v4/account');\n\nexport const getFApiV2Balance = createApi<\n {},\n {\n accountAlias: string; // 账户唯一识别码\n asset: string; // 资产\n balance: string; // 总余额\n crossWalletBalance: string; // 全仓余额\n crossUnPnl: string; // 全仓持仓未实现盈亏\n availableBalance: string; // 下单可用余额\n maxWithdrawAmount: string; // 最大可转出余额\n marginAvailable: boolean; // 是否可用作联合保证金\n updateTime: number;\n }[]\n>('USER_DATA', 'GET', '/fapi/v2/balance');\n\nexport const postFApiV1Order = createApi<\n {\n symbol: string;\n side: 'BUY' | 'SELL';\n positionSide?: 'BOTH' | 'LONG' | 'SHORT';\n type:\n | 'MARKET'\n | 'LIMIT'\n | 'STOP'\n | 'STOP_MARKET'\n | 'TAKE_PROFIT'\n | 'TAKE_PROFIT_MARKET'\n | 'TRAILING_STOP_MARKET';\n reduceOnly?: 'true' | 'false';\n quantity?: number;\n price?: number;\n timeInForce?: 'GTC' | 'IOC' | 'FOK' | 'GTX' | 'HIDDEN';\n },\n {}\n>('TRADE', 'POST', '/fapi/v1/order');\n"]}
package/dist/index.js CHANGED
@@ -1,3 +1,5 @@
1
1
  import './account';
2
+ import './account-spot';
2
3
  import './order';
4
+ import './order-spot';
3
5
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,WAAW,CAAC;AACnB,OAAO,SAAS,CAAC","sourcesContent":["import './account';\nimport './order';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,WAAW,CAAC;AACnB,OAAO,gBAAgB,CAAC;AACxB,OAAO,SAAS,CAAC;AACjB,OAAO,cAAc,CAAC","sourcesContent":["import './account';\nimport './account-spot';\nimport './order';\nimport './order-spot';\n"]}
@@ -0,0 +1,38 @@
1
+ import { Terminal } from '@yuants/protocol';
2
+ import { roundToStep } from '@yuants/utils';
3
+ import { SPOT_ACCOUNT_ID } from './account-spot';
4
+ import { getApiV1TickerPrice, postApiV1Order } from './sapi';
5
+ Terminal.fromNodeEnv().server.provideService('SubmitOrder', { required: ['account_id'], properties: { account_id: { type: 'string', const: SPOT_ACCOUNT_ID } } }, async (msg) => {
6
+ var _a;
7
+ const order = msg.req;
8
+ const symbol = order.product_id;
9
+ const type = { MARKET: 'MARKET', LIMIT: 'LIMIT', MAKER: 'LIMIT' }[order.order_type];
10
+ if (!type)
11
+ throw new Error(`Unsupported order_type: ${order.order_type}`);
12
+ const side = { OPEN_LONG: 'BUY', OPEN_SHORT: 'SELL', CLOSE_LONG: 'SELL', CLOSE_SHORT: 'BUY' }[order.order_direction];
13
+ if (!side)
14
+ throw new Error(`Unsupported order_direction: ${order.order_direction}`);
15
+ const timeInForce = order.order_type === 'MAKER' ? 'GTX' : order.order_type === 'LIMIT' ? 'GTC' : undefined;
16
+ const price = order.price;
17
+ let quantity = order.volume;
18
+ let quoteOrderQty;
19
+ if (type === 'MARKET' && side === 'BUY') {
20
+ const spotPrice = await getApiV1TickerPrice({});
21
+ const thePrice = (_a = spotPrice.find((x) => x.symbol === symbol)) === null || _a === void 0 ? void 0 : _a.price;
22
+ if (!thePrice)
23
+ throw new Error(`Cannot get price for symbol ${symbol}`);
24
+ quantity = undefined;
25
+ quoteOrderQty = roundToStep(order.volume * +thePrice, 0.01);
26
+ }
27
+ await postApiV1Order({
28
+ symbol,
29
+ type,
30
+ side,
31
+ timeInForce,
32
+ price,
33
+ quantity,
34
+ quoteOrderQty,
35
+ });
36
+ return { res: { code: 0, message: 'OK' } };
37
+ });
38
+ //# sourceMappingURL=order-spot.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"order-spot.js","sourceRoot":"","sources":["../src/order-spot.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACjD,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAC;AAE7D,QAAQ,CAAC,WAAW,EAAE,CAAC,MAAM,CAAC,cAAc,CAC1C,aAAa,EACb,EAAE,QAAQ,EAAE,CAAC,YAAY,CAAC,EAAE,UAAU,EAAE,EAAE,UAAU,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,eAAe,EAAE,EAAE,EAAE,EACpG,KAAK,EAAE,GAAG,EAAE,EAAE;;IACZ,MAAM,KAAK,GAAG,GAAG,CAAC,GAAG,CAAC;IACtB,MAAM,MAAM,GAAG,KAAK,CAAC,UAAU,CAAC;IAEhC,MAAM,IAAI,GAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAY,CAAC,KAAK,CAAC,UAAW,CAAC,CAAC;IAChG,IAAI,CAAC,IAAI;QAAE,MAAM,IAAI,KAAK,CAAC,2BAA2B,KAAK,CAAC,UAAU,EAAE,CAAC,CAAC;IAE1E,MAAM,IAAI,GAAI,EAAE,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CAAC;AAC7C,CAAC,CACF,CAAC","sourcesContent":["import { IOrder } from '@yuants/data-order';\nimport { Terminal } from '@yuants/protocol';\nimport { roundToStep } from '@yuants/utils';\nimport { SPOT_ACCOUNT_ID } from './account-spot';\nimport { getApiV1TickerPrice, postApiV1Order } from './sapi';\n\nTerminal.fromNodeEnv().server.provideService<IOrder>(\n 'SubmitOrder',\n { required: ['account_id'], properties: { account_id: { type: 'string', const: SPOT_ACCOUNT_ID } } },\n async (msg) => {\n const order = msg.req;\n const symbol = order.product_id;\n\n const type = ({ MARKET: 'MARKET', LIMIT: 'LIMIT', MAKER: 'LIMIT' } as const)[order.order_type!];\n if (!type) throw new Error(`Unsupported order_type: ${order.order_type}`);\n\n const side = ({ OPEN_LONG: 'BUY', OPEN_SHORT: 'SELL', CLOSE_LONG: 'SELL', CLOSE_SHORT: 'BUY' } as const)[\n order.order_direction!\n ];\n if (!side) throw new Error(`Unsupported order_direction: ${order.order_direction}`);\n\n const timeInForce =\n order.order_type === 'MAKER' ? 'GTX' : order.order_type === 'LIMIT' ? 'GTC' : undefined;\n\n const price = order.price;\n\n let quantity: number | undefined = order.volume;\n let quoteOrderQty: number | undefined;\n\n if (type === 'MARKET' && side === 'BUY') {\n const spotPrice = await getApiV1TickerPrice({});\n const thePrice = spotPrice.find((x) => x.symbol === symbol)?.price;\n if (!thePrice) throw new Error(`Cannot get price for symbol ${symbol}`);\n quantity = undefined;\n quoteOrderQty = roundToStep(order.volume * +thePrice, 0.01);\n }\n\n await postApiV1Order({\n symbol,\n type,\n side,\n timeInForce,\n price,\n quantity,\n quoteOrderQty,\n });\n\n return { res: { code: 0, message: 'OK' } };\n },\n);\n"]}
package/dist/sapi.js ADDED
@@ -0,0 +1,48 @@
1
+ import { opensslEquivalentHMAC } from './utils';
2
+ const API_KEY = process.env.API_KEY;
3
+ const SECRET_KEY = process.env.SECRET_KEY;
4
+ const BASE_URL = 'https://sapi.asterdex.com';
5
+ const request = async (type, method, endpoint, params = {}) => {
6
+ const needApiKey = type !== 'NONE';
7
+ const needSign = type === 'TRADE' || type === 'USER_DATA';
8
+ const url = new URL(BASE_URL);
9
+ url.pathname = endpoint;
10
+ for (const [key, value] of Object.entries(params)) {
11
+ if (value === undefined)
12
+ continue;
13
+ url.searchParams.set(key, `${value}`);
14
+ }
15
+ if (needSign) {
16
+ url.searchParams.set('timestamp', `${Date.now()}`);
17
+ const msg = url.search.slice(1); // 去掉开头的 '?'
18
+ const signature = await opensslEquivalentHMAC(msg, SECRET_KEY);
19
+ url.searchParams.set('signature', signature);
20
+ }
21
+ console.info(url.toString());
22
+ const response = await fetch(url.toString(), {
23
+ method,
24
+ headers: needApiKey
25
+ ? {
26
+ 'X-MBX-APIKEY': API_KEY,
27
+ }
28
+ : {},
29
+ }).then((response) => response.json());
30
+ if (response.code && response.code !== 0) {
31
+ throw JSON.stringify(response);
32
+ }
33
+ return response;
34
+ };
35
+ const createApi = (type, method, endpoint) => (params) => request(type, method, endpoint, params);
36
+ /**
37
+ * 获取账户信息 (现货)
38
+ *
39
+ * https://github.com/asterdex/api-docs/blob/master/aster-finance-spot-api_CN.md#%E8%B4%A6%E6%88%B7%E4%BF%A1%E6%81%AF-user_data
40
+ */
41
+ export const getApiV1Account = createApi('USER_DATA', 'GET', '/api/v1/account');
42
+ /**
43
+ * 获取最新价格
44
+ * https://github.com/asterdex/api-docs/blob/master/aster-finance-spot-api_CN.md#%E6%9C%80%E6%96%B0%E4%BB%B7%E6%A0%BC
45
+ */
46
+ export const getApiV1TickerPrice = createApi('MARKET_DATA', 'GET', '/api/v1/ticker/price');
47
+ export const postApiV1Order = createApi('TRADE', 'POST', '/api/v1/order');
48
+ //# sourceMappingURL=sapi.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sapi.js","sourceRoot":"","sources":["../src/sapi.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAC;AAEhD,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC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C,CAAC;gBACE,cAAc,EAAE,OAAO;aACxB;YACH,CAAC,CAAC,EAAE;KACP,CAAC,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAS,CAAC,CAAC;IAE9C,IAAI,QAAQ,CAAC,IAAI,IAAI,QAAQ,CAAC,IAAI,KAAK,CAAC,EAAE;QACxC,MAAM,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;KAChC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC,CAAC;AAEF,MAAM,SAAS,GACb,CACE,IAAoE,EACpE,MAAc,EACd,QAAgB,EAChB,EAAE,CACJ,CAAC,MAAY,EAAE,EAAE,CACf,OAAO,CAAO,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;AAElD;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,SAAS,CAetC,WAAW,EAAE,KAAK,EAAE,iBAAiB,CAAC,CAAC;AAEzC;;;GAGG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,SAAS,CAO1C,aAAa,EAAE,KAAK,EAAE,sBAAsB,CAAC,CAAC;AAEhD,MAAM,CAAC,MAAM,cAAc,GAAG,SAAS,CAWrC,OAAO,EAAE,MAAM,EAAE,eAAe,CAAC,CAAC","sourcesContent":["import { opensslEquivalentHMAC } from './utils';\n\nconst API_KEY = process.env.API_KEY!;\nconst SECRET_KEY = process.env.SECRET_KEY!;\n\nconst BASE_URL = 'https://sapi.asterdex.com';\n\nconst request = async <T>(\n type: 'NONE' | 'TRADE' | 'USER_DATA' | 'USER_STREAM' | 'MARKET_DATA',\n method: string,\n endpoint: string,\n params: any = {},\n): Promise<T> => {\n const needApiKey = type !== 'NONE';\n const needSign = type === 'TRADE' || type === 'USER_DATA';\n\n const url = new URL(BASE_URL);\n url.pathname = endpoint;\n for (const [key, value] of Object.entries(params)) {\n if (value === undefined) continue;\n url.searchParams.set(key, `${value}`);\n }\n\n if (needSign) {\n url.searchParams.set('timestamp', `${Date.now()}`);\n const msg = url.search.slice(1); // 去掉开头的 '?'\n const signature = await opensslEquivalentHMAC(msg, SECRET_KEY);\n url.searchParams.set('signature', signature);\n }\n\n console.info(url.toString());\n\n const response = await fetch(url.toString(), {\n method,\n headers: needApiKey\n ? {\n 'X-MBX-APIKEY': API_KEY,\n }\n : {},\n }).then((response) => response.json() as any);\n\n if (response.code && response.code !== 0) {\n throw JSON.stringify(response);\n }\n\n return response;\n};\n\nconst createApi =\n <TReq, TRes>(\n type: 'NONE' | 'TRADE' | 'USER_DATA' | 'USER_STREAM' | 'MARKET_DATA',\n method: string,\n endpoint: string,\n ) =>\n (params: TReq) =>\n request<TRes>(type, method, endpoint, params);\n\n/**\n * 获取账户信息 (现货)\n *\n * https://github.com/asterdex/api-docs/blob/master/aster-finance-spot-api_CN.md#%E8%B4%A6%E6%88%B7%E4%BF%A1%E6%81%AF-user_data\n */\nexport const getApiV1Account = createApi<\n {},\n {\n feeTier: number;\n canTrade: boolean;\n canDeposit: boolean;\n canWithdraw: boolean;\n canBurnAsset: boolean;\n updateTime: number;\n balances: {\n asset: string;\n free: string;\n locked: string;\n }[];\n }\n>('USER_DATA', 'GET', '/api/v1/account');\n\n/**\n * 获取最新价格\n * https://github.com/asterdex/api-docs/blob/master/aster-finance-spot-api_CN.md#%E6%9C%80%E6%96%B0%E4%BB%B7%E6%A0%BC\n */\nexport const getApiV1TickerPrice = createApi<\n {},\n {\n symbol: string;\n price: string;\n time: number;\n }[]\n>('MARKET_DATA', 'GET', '/api/v1/ticker/price');\n\nexport const postApiV1Order = createApi<\n {\n symbol: string;\n side: 'BUY' | 'SELL';\n type: 'MARKET' | 'LIMIT' | 'STOP' | 'STOP_MARKET' | 'TAKE_PROFIT' | 'TAKE_PROFIT_MARKET';\n timeInForce?: 'GTC' | 'IOC' | 'FOK' | 'GTX';\n quantity?: number;\n quoteOrderQty?: number;\n price?: number;\n },\n {}\n>('TRADE', 'POST', '/api/v1/order');\n"]}
package/dist/utils.js ADDED
@@ -0,0 +1,28 @@
1
+ export function arrayBufferToHex(buffer) {
2
+ const bytes = new Uint8Array(buffer);
3
+ const hexArray = [];
4
+ for (const byte of bytes) {
5
+ const hex = byte.toString(16).padStart(2, '0');
6
+ hexArray.push(hex);
7
+ }
8
+ return hexArray.join('');
9
+ }
10
+ export async function opensslEquivalentHMAC(message, secretKey) {
11
+ try {
12
+ const keyBuffer = new TextEncoder().encode(secretKey);
13
+ const messageBuffer = new TextEncoder().encode(message);
14
+ // 导入密钥
15
+ const cryptoKey = await crypto.subtle.importKey('raw', keyBuffer, {
16
+ name: 'HMAC',
17
+ hash: { name: 'SHA-256' },
18
+ }, false, ['sign']);
19
+ // 进行 HMAC-SHA256 签名
20
+ const signature = await crypto.subtle.sign('HMAC', cryptoKey, messageBuffer);
21
+ // 转换为16进制小写(与 openssl 输出格式一致)
22
+ return arrayBufferToHex(signature);
23
+ }
24
+ catch (error) {
25
+ throw new Error(`HMAC-SHA256 签名失败: ${error}`);
26
+ }
27
+ }
28
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,gBAAgB,CAAC,MAAmB;IAClD,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;IACrC,MAAM,QAAQ,GAAa,EAAE,CAAC;IAE9B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE;QACxB,MAAM,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;QAC/C,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;KACpB;IAED,OAAO,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAC3B,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,qBAAqB,CAAC,OAAe,EAAE,SAAiB;IAC5E,IAAI;QACF,MAAM,SAAS,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;QACtD,MAAM,aAAa,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAExD,OAAO;QACP,MAAM,SAAS,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,SAAS,CAC7C,KAAK,EACL,SAAS,EACT;YACE,IAAI,EAAE,MAAM;YACZ,IAAI,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;SAC1B,EACD,KAAK,EACL,CAAC,MAAM,CAAC,CACT,CAAC;QAEF,oBAAoB;QACpB,MAAM,SAAS,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,SAAS,EAAE,aAAa,CAAC,CAAC;QAE7E,8BAA8B;QAC9B,OAAO,gBAAgB,CAAC,SAAS,CAAC,CAAC;KACpC;IAAC,OAAO,KAAK,EAAE;QACd,MAAM,IAAI,KAAK,CAAC,qBAAqB,KAAK,EAAE,CAAC,CAAC;KAC/C;AACH,CAAC","sourcesContent":["export function arrayBufferToHex(buffer: ArrayBuffer): string {\n const bytes = new Uint8Array(buffer);\n const hexArray: string[] = [];\n\n for (const byte of bytes) {\n const hex = byte.toString(16).padStart(2, '0');\n hexArray.push(hex);\n }\n\n return hexArray.join('');\n}\n\nexport async function opensslEquivalentHMAC(message: string, secretKey: string): Promise<string> {\n try {\n const keyBuffer = new TextEncoder().encode(secretKey);\n const messageBuffer = new TextEncoder().encode(message);\n\n // 导入密钥\n const cryptoKey = await crypto.subtle.importKey(\n 'raw',\n keyBuffer,\n {\n name: 'HMAC',\n hash: { name: 'SHA-256' },\n },\n false,\n ['sign'],\n );\n\n // 进行 HMAC-SHA256 签名\n const signature = await crypto.subtle.sign('HMAC', cryptoKey, messageBuffer);\n\n // 转换为16进制小写(与 openssl 输出格式一致)\n return arrayBufferToHex(signature);\n } catch (error) {\n throw new Error(`HMAC-SHA256 签名失败: ${error}`);\n }\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export declare const SPOT_ACCOUNT_ID: string;
2
+ //# sourceMappingURL=account-spot.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"account-spot.d.ts","sourceRoot":"","sources":["../src/account-spot.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,eAAe,QAA0B,CAAC"}
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SPOT_ACCOUNT_ID = void 0;
4
+ const data_account_1 = require("@yuants/data-account");
5
+ const protocol_1 = require("@yuants/protocol");
6
+ const sapi_1 = require("./sapi");
7
+ const ADDRESS = process.env.ADDRESS;
8
+ exports.SPOT_ACCOUNT_ID = `ASTER/${ADDRESS}/SPOT`;
9
+ (0, data_account_1.provideAccountInfoService)(protocol_1.Terminal.fromNodeEnv(), exports.SPOT_ACCOUNT_ID, async () => {
10
+ const [x, prices] = await Promise.all([(0, sapi_1.getApiV1Account)({}), (0, sapi_1.getApiV1TickerPrice)({})]);
11
+ const positions = x.balances.map((b) => {
12
+ var _a, _b;
13
+ const thePrice = b.asset === 'USDT' ? 1 : (_b = (_a = prices.find((p) => p.symbol === b.asset + 'USDT')) === null || _a === void 0 ? void 0 : _a.price) !== null && _b !== void 0 ? _b : 0;
14
+ const volume = +b.free + +b.locked;
15
+ const position_price = +thePrice;
16
+ const closable_price = +thePrice;
17
+ const valuation = volume * closable_price;
18
+ const floating_profit = 0;
19
+ return {
20
+ position_id: b.asset,
21
+ datasource_id: 'ASTER',
22
+ product_id: b.asset,
23
+ direction: 'LONG',
24
+ volume,
25
+ free_volume: +b.free,
26
+ position_price,
27
+ closable_price,
28
+ floating_profit,
29
+ valuation,
30
+ };
31
+ });
32
+ const usdtAsset = x.balances.find((b) => b.asset === 'USDT');
33
+ const equity = positions.reduce((a, b) => a + b.valuation, 0);
34
+ const free = usdtAsset ? +usdtAsset.free : 0;
35
+ const balance = usdtAsset ? +usdtAsset.free + +usdtAsset.locked : 0;
36
+ const profit = equity - balance;
37
+ const used = equity - free;
38
+ return {
39
+ money: {
40
+ currency: 'USDT',
41
+ equity,
42
+ balance,
43
+ profit,
44
+ free,
45
+ used,
46
+ },
47
+ positions,
48
+ };
49
+ }, { auto_refresh_interval: 1000 });
50
+ //# sourceMappingURL=account-spot.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"account-spot.js","sourceRoot":"","sources":["../src/account-spot.ts"],"names":[],"mappings":";;;AAAA,uDAA4E;AAC5E,+CAA4C;AAC5C,iCAA8D;AAE9D,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,OAAQ,CAAC;AACxB,QAAA,eAAe,GAAG,SAAS,OAAO,OAAO,CAAC;AAEvD,IAAA,wCAAyB,EACvB,mBAAQ,CAAC,WAAW,EAAE,EACtB,uBAAe,EACf,KAAK,IAAI,EAAE;IACT,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC,IAAA,sBAAe,EAAC,EAAE,CAAC,EAAE,IAAA,0BAAmB,EAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IAEtF,MAAM,SAAS,GAAG,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,EAAa,EAAE;;QAChD,MAAM,QAAQ,GAAG,CAAC,CAAC,KAAK,KAAK,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAA,MAAA,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,KAAK,GAAG,MAAM,CAAC,0CAAE,KAAK,mCAAI,CAAC,CAAC;QAExG,MAAM,MAAM,GAAG,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC;QAEnC,MAAM,cAAc,GAAG,CAAC,QAAQ,CAAC;QACjC,MAAM,cAAc,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import { Terminal } from '@yuants/protocol';\nimport { getApiV1Account, getApiV1TickerPrice } from './sapi';\n\nconst ADDRESS = process.env.ADDRESS!;\nexport const SPOT_ACCOUNT_ID = `ASTER/${ADDRESS}/SPOT`;\n\nprovideAccountInfoService(\n Terminal.fromNodeEnv(),\n SPOT_ACCOUNT_ID,\n async () => {\n const [x, prices] = await Promise.all([getApiV1Account({}), getApiV1TickerPrice({})]);\n\n const positions = x.balances.map((b): IPosition => {\n const thePrice = b.asset === 'USDT' ? 1 : prices.find((p) => p.symbol === b.asset + 'USDT')?.price ?? 0;\n\n const volume = +b.free + +b.locked;\n\n const position_price = +thePrice;\n const closable_price = +thePrice;\n const valuation = volume * closable_price;\n const floating_profit = 0;\n\n return {\n position_id: b.asset,\n datasource_id: 'ASTER',\n product_id: b.asset,\n direction: 'LONG',\n volume,\n free_volume: +b.free,\n position_price,\n closable_price,\n floating_profit,\n valuation,\n };\n });\n\n const usdtAsset = x.balances.find((b) => b.asset === 'USDT');\n const equity = positions.reduce((a, b) => a + b.valuation, 0);\n const free = usdtAsset ? +usdtAsset.free : 0;\n const balance = usdtAsset ? +usdtAsset.free + +usdtAsset.locked : 0;\n const profit = equity - balance;\n const used = equity - free;\n\n return {\n money: {\n currency: 'USDT',\n equity,\n balance,\n profit,\n free,\n used,\n },\n positions,\n };\n },\n { auto_refresh_interval: 1000 },\n);\n"]}
package/lib/account.js CHANGED
@@ -7,7 +7,7 @@ const api_1 = require("./api");
7
7
  const ADDRESS = process.env.ADDRESS;
8
8
  exports.ACCOUNT_ID = `ASTER/${ADDRESS}`;
9
9
  (0, data_account_1.provideAccountInfoService)(protocol_1.Terminal.fromNodeEnv(), exports.ACCOUNT_ID, async () => {
10
- const a = await (0, api_1.getFApiV4Account)({});
10
+ const [a] = await Promise.all([(0, api_1.getFApiV4Account)({})]);
11
11
  const profit = +a.totalUnrealizedProfit;
12
12
  const balance = +a.totalWalletBalance;
13
13
  const equity = balance + profit;
@@ -1 +1 @@
1
- {"version":3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CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,YAAY;QAC/F,MAAM,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC;QAChC,WAAW,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC;QACrC,cAAc,EAAE,CAAC,CAAC,CAAC,UAAU;QAC7B,cAAc,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAC,WAAW,CAAC;QACtD,eAAe,EAAE,CAAC,CAAC,CAAC,gBAAgB;QACpC,SAAS,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;KACjC,CAAC,CACH,CAAC;IACJ,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;AAC9B,CAAC,EACD,EAAE,qBAAqB,EAAE,IAAI,EAAE,CAChC,CAAC","sourcesContent":["import { IAccountMoney, IPosition, provideAccountInfoService } from '@yuants/data-account';\nimport { Terminal } from '@yuants/protocol';\nimport { getFApiV4Account } from './api';\n\nconst ADDRESS = process.env.ADDRESS!;\nexport const ACCOUNT_ID = `ASTER/${ADDRESS}`;\n\nprovideAccountInfoService(\n Terminal.fromNodeEnv(),\n ACCOUNT_ID,\n async () => {\n const a = await getFApiV4Account({});\n\n const profit = +a.totalUnrealizedProfit;\n const balance = +a.totalWalletBalance;\n const equity = balance + profit;\n const free = +a.availableBalance;\n const used = equity - free;\n\n const money: IAccountMoney = {\n currency: 'USD',\n equity: equity,\n balance: balance,\n profit: profit,\n free: free,\n used: used,\n };\n const positions = a.positions\n .filter((p) => +p.positionAmt !== 0)\n .map(\n (p): IPosition => ({\n position_id: p.symbol,\n product_id: p.symbol,\n datasource_id: 'ASTER',\n direction: p.positionSide === 'BOTH' ? (+p.positionAmt > 0 ? 'LONG' : 'SHORT') : p.positionSide,\n volume: Math.abs(+p.positionAmt),\n free_volume: Math.abs(+p.positionAmt),\n position_price: +p.entryPrice,\n closable_price: Math.abs(+p.notional / +p.positionAmt),\n floating_profit: +p.unrealizedProfit,\n valuation: Math.abs(+p.notional),\n }),\n );\n return { money, positions };\n },\n { auto_refresh_interval: 1000 },\n);\n"]}
1
+ {"version":3,"file":"account.js","sourceRoot":"","sources":["../src/account.ts"],"names":[],"mappings":";;;AAAA,uDAA2F;AAC3F,+CAA4C;AAC5C,+BAA2D;AAE3D,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,OAAQ,CAAC;AACxB,QAAA,UAAU,GAAG,SAAS,OAAO,EAAE,CAAC;AAE7C,IAAA,wCAAyB,EACvB,mBAAQ,CAAC,WAAW,EAAE,EACtB,kBAAU,EACV,KAAK,IAAI,EAAE;IACT,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC,IAAA,sBAAgB,EAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IAEtD,MAAM,MAAM,GAAG,CAAC,CAAC,CAAC,qBAAqB,CAAC;IACxC,MAAM,OAAO,GAAG,CAAC,CAAC,CAAC,kBAAkB,CAAC;IACtC,MAAM,MAAM,GAAG,OAAO,GAAG,MAAM,CAAC;IAChC,MAAM,IAAI,GAAG,CAAC,CAAC,CAAC,gBAAgB,CAAC;IACjC,MAAM,IAAI,GAAG,MAAM,GAAG,IAAI,CAAC;IAE3B,MAAM,KAAK,GAAkB;QAC3B,QAAQ,EAAE,KAAK;QACf,MAAM,EAAE,MAAM;QACd,OAAO,EAAE,OAAO;QAChB,MAAM,EAAE,MAAM;QACd,IAAI,EAAE,IAAI;QACV,IAAI,EAAE,IAAI;KACX,CAAC;IACF,MAAM,SAAS,GAAG,CAAC,CAAC,SAAS;SAC1B,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,WAAW,KAAK,CAAC,CAAC;SACnC,GAAG,CACF,CAAC,CAAC,EAAa,EAAE,CAAC,CAAC;QACjB,WAAW,EAAE,CAAC,CAAC,MAAM;QACrB,UAAU,EAAE,CAAC,CAAC,MAAM;QACpB,aAAa,EAAE,OAAO;QACtB,SAAS,EAAE,CAAC,CAAC,YAAY,KAAK,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,YAAY;QAC/F,MAAM,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC;QAChC,WAAW,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC;QACrC,cAAc,EAAE,CAAC,CAAC,CAAC,UAAU;QAC7B,cAAc,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAC,WAAW,CAAC;QACtD,eAAe,EAAE,CAAC,CAAC,CAAC,gBAAgB;QACpC,SAAS,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;KACjC,CAAC,CACH,CAAC;IACJ,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;AAC9B,CAAC,EACD,EAAE,qBAAqB,EAAE,IAAI,EAAE,CAChC,CAAC","sourcesContent":["import { IAccountMoney, IPosition, provideAccountInfoService } from '@yuants/data-account';\nimport { Terminal } from '@yuants/protocol';\nimport { getFApiV2Balance, getFApiV4Account } from './api';\n\nconst ADDRESS = process.env.ADDRESS!;\nexport const ACCOUNT_ID = `ASTER/${ADDRESS}`;\n\nprovideAccountInfoService(\n Terminal.fromNodeEnv(),\n ACCOUNT_ID,\n async () => {\n const [a] = await Promise.all([getFApiV4Account({})]);\n\n const profit = +a.totalUnrealizedProfit;\n const balance = +a.totalWalletBalance;\n const equity = balance + profit;\n const free = +a.availableBalance;\n const used = equity - free;\n\n const money: IAccountMoney = {\n currency: 'USD',\n equity: equity,\n balance: balance,\n profit: profit,\n free: free,\n used: used,\n };\n const positions = a.positions\n .filter((p) => +p.positionAmt !== 0)\n .map(\n (p): IPosition => ({\n position_id: p.symbol,\n product_id: p.symbol,\n datasource_id: 'ASTER',\n direction: p.positionSide === 'BOTH' ? (+p.positionAmt > 0 ? 'LONG' : 'SHORT') : p.positionSide,\n volume: Math.abs(+p.positionAmt),\n free_volume: Math.abs(+p.positionAmt),\n position_price: +p.entryPrice,\n closable_price: Math.abs(+p.notional / +p.positionAmt),\n floating_profit: +p.unrealizedProfit,\n valuation: Math.abs(+p.notional),\n }),\n );\n return { money, positions };\n },\n { auto_refresh_interval: 1000 },\n);\n"]}
package/lib/api.d.ts CHANGED
@@ -54,10 +54,21 @@ export declare const getFApiV4Account: (params: {}) => Promise<{
54
54
  updateTime: number;
55
55
  }[];
56
56
  }>;
57
+ export declare const getFApiV2Balance: (params: {}) => Promise<{
58
+ accountAlias: string;
59
+ asset: string;
60
+ balance: string;
61
+ crossWalletBalance: string;
62
+ crossUnPnl: string;
63
+ availableBalance: string;
64
+ maxWithdrawAmount: string;
65
+ marginAvailable: boolean;
66
+ updateTime: number;
67
+ }[]>;
57
68
  export declare const postFApiV1Order: (params: {
58
69
  symbol: string;
59
70
  side: 'BUY' | 'SELL';
60
- positionSide?: "BOTH" | "LONG" | "SHORT" | undefined;
71
+ positionSide?: "LONG" | "BOTH" | "SHORT" | undefined;
61
72
  type: 'MARKET' | 'LIMIT' | 'STOP' | 'STOP_MARKET' | 'TAKE_PROFIT' | 'TAKE_PROFIT_MARKET' | 'TRAILING_STOP_MARKET';
62
73
  reduceOnly?: "true" | "false" | undefined;
63
74
  quantity?: number | undefined;
package/lib/api.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAuFA;;;;GAIG;AACH,eAAO,MAAM,gBAAgB;aAGhB,MAAM;cACL,OAAO;gBACL,OAAO;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AAQ,EAAE,MAAM,CAAC;QACjB,cAAc,EAAE,MAAM,CAAC;QACvB,UAAU,EAAE,MAAM,CAAC;KACpB,EAAE;EAEkC,CAAC;AAE1C,eAAO,MAAM,gBAAgB;kBAGX,MAAM;WACb,MAAM;aACJ,MAAM;wBACK,MAAM;gBACd,MAAM;sBACA,MAAM;uBACL,MAAM;qBACR,OAAO;gBACZ,MAAM;IAEmB,CAAC;AAE1C,eAAO,MAAM,eAAe;YAEhB,MAAM;UACR,KAAK,GAAG,MAAM;;UAGhB,QAAQ,GACR,OAAO,GACP,MAAM,GACN,aAAa,GACb,aAAa,GACb,oBAAoB,GACpB,sBAAsB;;;;;iBAOM,CAAC"}
package/lib/api.js CHANGED
@@ -1,36 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.postFApiV1Order = exports.getFApiV4Account = void 0;
3
+ exports.postFApiV1Order = exports.getFApiV2Balance = exports.getFApiV4Account = void 0;
4
+ const utils_1 = require("./utils");
4
5
  const API_KEY = process.env.API_KEY;
5
6
  const SECRET_KEY = process.env.SECRET_KEY;
6
7
  const BASE_URL = 'https://fapi.asterdex.com';
7
- function arrayBufferToHex(buffer) {
8
- const bytes = new Uint8Array(buffer);
9
- const hexArray = [];
10
- for (const byte of bytes) {
11
- const hex = byte.toString(16).padStart(2, '0');
12
- hexArray.push(hex);
13
- }
14
- return hexArray.join('');
15
- }
16
- async function opensslEquivalentHMAC(message, secretKey) {
17
- try {
18
- const keyBuffer = new TextEncoder().encode(secretKey);
19
- const messageBuffer = new TextEncoder().encode(message);
20
- // 导入密钥
21
- const cryptoKey = await crypto.subtle.importKey('raw', keyBuffer, {
22
- name: 'HMAC',
23
- hash: { name: 'SHA-256' },
24
- }, false, ['sign']);
25
- // 进行 HMAC-SHA256 签名
26
- const signature = await crypto.subtle.sign('HMAC', cryptoKey, messageBuffer);
27
- // 转换为16进制小写(与 openssl 输出格式一致)
28
- return arrayBufferToHex(signature);
29
- }
30
- catch (error) {
31
- throw new Error(`HMAC-SHA256 签名失败: ${error}`);
32
- }
33
- }
34
8
  const request = async (type, method, endpoint, params = {}) => {
35
9
  const needApiKey = type !== 'NONE';
36
10
  const needSign = type === 'TRADE' || type === 'USER_DATA';
@@ -44,11 +18,11 @@ const request = async (type, method, endpoint, params = {}) => {
44
18
  if (needSign) {
45
19
  url.searchParams.set('timestamp', `${Date.now()}`);
46
20
  const msg = url.search.slice(1); // 去掉开头的 '?'
47
- const signature = await opensslEquivalentHMAC(msg, SECRET_KEY);
21
+ const signature = await (0, utils_1.opensslEquivalentHMAC)(msg, SECRET_KEY);
48
22
  url.searchParams.set('signature', signature);
49
23
  }
50
24
  console.info(url.toString());
51
- return fetch(url.toString(), {
25
+ const res = await fetch(url.toString(), {
52
26
  method,
53
27
  headers: needApiKey
54
28
  ? {
@@ -56,6 +30,10 @@ const request = async (type, method, endpoint, params = {}) => {
56
30
  }
57
31
  : {},
58
32
  }).then((response) => response.json());
33
+ if (res.code && res.code !== 0) {
34
+ throw JSON.stringify(res);
35
+ }
36
+ return res;
59
37
  };
60
38
  const createApi = (type, method, endpoint) => (params) => request(type, method, endpoint, params);
61
39
  /**
@@ -64,5 +42,6 @@ const createApi = (type, method, endpoint) => (params) => request(type, method,
64
42
  * https://github.com/asterdex/api-docs/blob/master/aster-finance-futures-api_CN.md#%E8%B4%A6%E6%88%B7%E4%BF%A1%E6%81%AFv4-user_data
65
43
  */
66
44
  exports.getFApiV4Account = createApi('USER_DATA', 'GET', '/fapi/v4/account');
45
+ exports.getFApiV2Balance = createApi('USER_DATA', 'GET', '/fapi/v2/balance');
67
46
  exports.postFApiV1Order = createApi('TRADE', 'POST', '/fapi/v1/order');
68
47
  //# sourceMappingURL=api.js.map
package/lib/api.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":";;;AAAA,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,OAAQ,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AAC,QAAQ,CAAC,IAAI,EAAc,CAAC,CAAC;AACrD,CAAC,CAAC;AAEF,MAAM,SAAS,GACb,CACE,IAAoE,EACpE,MAAc,EACd,QAAgB,EAChB,EAAE,CACJ,CAAC,MAAY,EAAE,EAAE,CACf,OAAO,CAAO,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;AAElD;;;;GAIG;AACU,QAAA,gBAAgB,GAAG,SAAS,CAqDvC,WAAW,EAAE,KAAK,EAAE,kBAAkB,CAAC,CAAC;AAE7B,QAAA,eAAe,GAAG,SAAS,CAmBtC,OAAO,EAAE,MAAM,EAAE,gBAAgB,CAAC,CAAC","sourcesContent":["const API_KEY = process.env.API_KEY!;\nconst SECRET_KEY = process.env.SECRET_KEY!;\n\nconst BASE_URL = 'https://fapi.asterdex.com';\n\nfunction arrayBufferToHex(buffer: ArrayBuffer): string {\n const bytes = new Uint8Array(buffer);\n const hexArray: string[] = [];\n\n for (const byte of bytes) {\n const hex = byte.toString(16).padStart(2, '0');\n hexArray.push(hex);\n }\n\n return hexArray.join('');\n}\n\nasync function opensslEquivalentHMAC(message: string, secretKey: string): Promise<string> {\n try {\n const keyBuffer = new TextEncoder().encode(secretKey);\n const messageBuffer = new TextEncoder().encode(message);\n\n // 导入密钥\n const cryptoKey = await crypto.subtle.importKey(\n 'raw',\n keyBuffer,\n {\n name: 'HMAC',\n hash: { name: 'SHA-256' },\n },\n false,\n ['sign'],\n );\n\n // 进行 HMAC-SHA256 签名\n const signature = await crypto.subtle.sign('HMAC', cryptoKey, messageBuffer);\n\n // 转换为16进制小写(与 openssl 输出格式一致)\n return arrayBufferToHex(signature);\n } catch (error) {\n throw new Error(`HMAC-SHA256 签名失败: ${error}`);\n }\n}\n\nconst request = async <T>(\n type: 'NONE' | 'TRADE' | 'USER_DATA' | 'USER_STREAM' | 'MARKET_DATA',\n method: string,\n endpoint: string,\n params: any = {},\n): Promise<T> => {\n const needApiKey = type !== 'NONE';\n const needSign = type === 'TRADE' || type === 'USER_DATA';\n\n const url = new URL(BASE_URL);\n url.pathname = endpoint;\n for (const [key, value] of Object.entries(params)) {\n if (value === undefined) continue;\n url.searchParams.set(key, `${value}`);\n }\n\n if (needSign) {\n url.searchParams.set('timestamp', `${Date.now()}`);\n const msg = url.search.slice(1); // 去掉开头的 '?'\n const signature = await opensslEquivalentHMAC(msg, SECRET_KEY);\n url.searchParams.set('signature', signature);\n }\n\n console.info(url.toString());\n return fetch(url.toString(), {\n method,\n headers: needApiKey\n ? {\n 'X-MBX-APIKEY': API_KEY,\n }\n : {},\n }).then((response) => response.json() as any as T);\n};\n\nconst createApi =\n <TReq, TRes>(\n type: 'NONE' | 'TRADE' | 'USER_DATA' | 'USER_STREAM' | 'MARKET_DATA',\n method: string,\n endpoint: string,\n ) =>\n (params: TReq) =>\n request<TRes>(type, method, endpoint, params);\n\n/**\n * 获取账户信息\n *\n * https://github.com/asterdex/api-docs/blob/master/aster-finance-futures-api_CN.md#%E8%B4%A6%E6%88%B7%E4%BF%A1%E6%81%AFv4-user_data\n */\nexport const getFApiV4Account = createApi<\n {},\n {\n feeTier: number;\n canTrade: boolean;\n canDeposit: boolean;\n canWithdraw: boolean;\n updateTime: number;\n totalInitialMargin: string;\n totalMaintMargin: string;\n totalWalletBalance: string;\n totalUnrealizedProfit: string;\n totalMarginBalance: string;\n totalPositionInitialMargin: string;\n totalOpenOrderInitialMargin: string;\n totalCrossWalletBalance: string;\n totalCrossUnPnl: string;\n availableBalance: string;\n maxWithdrawAmount: string;\n assets: {\n asset: string;\n walletBalance: string;\n unrealizedProfit: string;\n marginBalance: string;\n maintMargin: string;\n initialMargin: string;\n positionInitialMargin: string;\n openOrderInitialMargin: string;\n maxWithdrawAmount: string;\n crossWalletBalance: string;\n crossUnPnl: string;\n availableBalance: string;\n marginAvailable: boolean;\n updateTime: number;\n }[];\n positions: {\n symbol: string;\n initialMargin: string;\n maintMargin: string;\n unrealizedProfit: string;\n positionInitialMargin: string;\n openOrderInitialMargin: string;\n leverage: string;\n isolated: boolean;\n entryPrice: string;\n maxNotional: string;\n positionSide: 'BOTH' | 'LONG' | 'SHORT';\n positionAmt: string;\n notional: string;\n isolatedWallet: string;\n updateTime: number;\n }[];\n }\n>('USER_DATA', 'GET', '/fapi/v4/account');\n\nexport const postFApiV1Order = createApi<\n {\n symbol: string;\n side: 'BUY' | 'SELL';\n positionSide?: 'BOTH' | 'LONG' | 'SHORT';\n type:\n | 'MARKET'\n | 'LIMIT'\n | 'STOP'\n | 'STOP_MARKET'\n | 'TAKE_PROFIT'\n | 'TAKE_PROFIT_MARKET'\n | 'TRAILING_STOP_MARKET';\n reduceOnly?: 'true' | 'false';\n quantity?: number;\n price?: number;\n timeInForce?: 'GTC' | 'IOC' | 'FOK' | 'GTX' | 'HIDDEN';\n },\n {}\n>('TRADE', 'POST', '/fapi/v1/order');\n"]}
1
+ {"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":";;;AAAA,mCAAgD;AAEhD,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,OAAQ,CAAC;AACrC,MAAM,UAAU,GAAG,OAAO,CAAC,GAAG,CAAC,UAAW,CAAC;AAE3C,MAAM,QAAQ,GAAG,2BAA2B,CAAC;AAE7C,MAAM,OAAO,GAAG,KAAK,EACnB,IAAoE,EACpE,MAAc,EACd,QAAgB,EAChB,SAAc,EAAE,EACJ,EAAE;IACd,MAAM,UAAU,GAAG,IAAI,KAAK,MAAM,CAAC;IACnC,MAAM,QAAQ,GAAG,IAAI,KAAK,OAAO,IAAI,IAAI,KAAK,WAAW,CAAC;IAE1D,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC9B,GAAG,CAAC,QAAQ,GAAG,QAAQ,CAAC;IACxB,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;QACjD,IAAI,KAAK,KAAK,SAA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AAW,EAAE,KAAK,EAAE,kBAAkB,CAAC,CAAC;AAE7B,QAAA,gBAAgB,GAAG,SAAS,CAavC,WAAW,EAAE,KAAK,EAAE,kBAAkB,CAAC,CAAC;AAE7B,QAAA,eAAe,GAAG,SAAS,CAmBtC,OAAO,EAAE,MAAM,EAAE,gBAAgB,CAAC,CAAC","sourcesContent":["import { opensslEquivalentHMAC } from './utils';\n\nconst API_KEY = process.env.API_KEY!;\nconst SECRET_KEY = process.env.SECRET_KEY!;\n\nconst BASE_URL = 'https://fapi.asterdex.com';\n\nconst request = async <T>(\n type: 'NONE' | 'TRADE' | 'USER_DATA' | 'USER_STREAM' | 'MARKET_DATA',\n method: string,\n endpoint: string,\n params: any = {},\n): Promise<T> => {\n const needApiKey = type !== 'NONE';\n const needSign = type === 'TRADE' || type === 'USER_DATA';\n\n const url = new URL(BASE_URL);\n url.pathname = endpoint;\n for (const [key, value] of Object.entries(params)) {\n if (value === undefined) continue;\n url.searchParams.set(key, `${value}`);\n }\n\n if (needSign) {\n url.searchParams.set('timestamp', `${Date.now()}`);\n const msg = url.search.slice(1); // 去掉开头的 '?'\n const signature = await opensslEquivalentHMAC(msg, SECRET_KEY);\n url.searchParams.set('signature', signature);\n }\n\n console.info(url.toString());\n const res = await fetch(url.toString(), {\n method,\n headers: needApiKey\n ? {\n 'X-MBX-APIKEY': API_KEY,\n }\n : {},\n }).then((response) => response.json());\n if (res.code && res.code !== 0) {\n throw JSON.stringify(res);\n }\n return res;\n};\n\nconst createApi =\n <TReq, TRes>(\n type: 'NONE' | 'TRADE' | 'USER_DATA' | 'USER_STREAM' | 'MARKET_DATA',\n method: string,\n endpoint: string,\n ) =>\n (params: TReq) =>\n request<TRes>(type, method, endpoint, params);\n\n/**\n * 获取账户信息\n *\n * https://github.com/asterdex/api-docs/blob/master/aster-finance-futures-api_CN.md#%E8%B4%A6%E6%88%B7%E4%BF%A1%E6%81%AFv4-user_data\n */\nexport const getFApiV4Account = createApi<\n {},\n {\n feeTier: number;\n canTrade: boolean;\n canDeposit: boolean;\n canWithdraw: boolean;\n updateTime: number;\n totalInitialMargin: string;\n totalMaintMargin: string;\n totalWalletBalance: string;\n totalUnrealizedProfit: string;\n totalMarginBalance: string;\n totalPositionInitialMargin: string;\n totalOpenOrderInitialMargin: string;\n totalCrossWalletBalance: string;\n totalCrossUnPnl: string;\n availableBalance: string;\n maxWithdrawAmount: string;\n assets: {\n asset: string;\n walletBalance: string;\n unrealizedProfit: string;\n marginBalance: string;\n maintMargin: string;\n initialMargin: string;\n positionInitialMargin: string;\n openOrderInitialMargin: string;\n maxWithdrawAmount: string;\n crossWalletBalance: string;\n crossUnPnl: string;\n availableBalance: string;\n marginAvailable: boolean;\n updateTime: number;\n }[];\n positions: {\n symbol: string;\n initialMargin: string;\n maintMargin: string;\n unrealizedProfit: string;\n positionInitialMargin: string;\n openOrderInitialMargin: string;\n leverage: string;\n isolated: boolean;\n entryPrice: string;\n maxNotional: string;\n positionSide: 'BOTH' | 'LONG' | 'SHORT';\n positionAmt: string;\n notional: string;\n isolatedWallet: string;\n updateTime: number;\n }[];\n }\n>('USER_DATA', 'GET', '/fapi/v4/account');\n\nexport const getFApiV2Balance = createApi<\n {},\n {\n accountAlias: string; // 账户唯一识别码\n asset: string; // 资产\n balance: string; // 总余额\n crossWalletBalance: string; // 全仓余额\n crossUnPnl: string; // 全仓持仓未实现盈亏\n availableBalance: string; // 下单可用余额\n maxWithdrawAmount: string; // 最大可转出余额\n marginAvailable: boolean; // 是否可用作联合保证金\n updateTime: number;\n }[]\n>('USER_DATA', 'GET', '/fapi/v2/balance');\n\nexport const postFApiV1Order = createApi<\n {\n symbol: string;\n side: 'BUY' | 'SELL';\n positionSide?: 'BOTH' | 'LONG' | 'SHORT';\n type:\n | 'MARKET'\n | 'LIMIT'\n | 'STOP'\n | 'STOP_MARKET'\n | 'TAKE_PROFIT'\n | 'TAKE_PROFIT_MARKET'\n | 'TRAILING_STOP_MARKET';\n reduceOnly?: 'true' | 'false';\n quantity?: number;\n price?: number;\n timeInForce?: 'GTC' | 'IOC' | 'FOK' | 'GTX' | 'HIDDEN';\n },\n {}\n>('TRADE', 'POST', '/fapi/v1/order');\n"]}
package/lib/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
1
  import './account';
2
+ import './account-spot';
2
3
  import './order';
4
+ import './order-spot';
3
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,WAAW,CAAC;AACnB,OAAO,SAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,WAAW,CAAC;AACnB,OAAO,gBAAgB,CAAC;AACxB,OAAO,SAAS,CAAC;AACjB,OAAO,cAAc,CAAC"}
package/lib/index.js CHANGED
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  require("./account");
4
+ require("./account-spot");
4
5
  require("./order");
6
+ require("./order-spot");
5
7
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,qBAAmB;AACnB,mBAAiB","sourcesContent":["import './account';\nimport './order';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,qBAAmB;AACnB,0BAAwB;AACxB,mBAAiB;AACjB,wBAAsB","sourcesContent":["import './account';\nimport './account-spot';\nimport './order';\nimport './order-spot';\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=order-spot.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"order-spot.d.ts","sourceRoot":"","sources":["../src/order-spot.ts"],"names":[],"mappings":""}
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const protocol_1 = require("@yuants/protocol");
4
+ const utils_1 = require("@yuants/utils");
5
+ const account_spot_1 = require("./account-spot");
6
+ const sapi_1 = require("./sapi");
7
+ protocol_1.Terminal.fromNodeEnv().server.provideService('SubmitOrder', { required: ['account_id'], properties: { account_id: { type: 'string', const: account_spot_1.SPOT_ACCOUNT_ID } } }, async (msg) => {
8
+ var _a;
9
+ const order = msg.req;
10
+ const symbol = order.product_id;
11
+ const type = { MARKET: 'MARKET', LIMIT: 'LIMIT', MAKER: 'LIMIT' }[order.order_type];
12
+ if (!type)
13
+ throw new Error(`Unsupported order_type: ${order.order_type}`);
14
+ const side = { OPEN_LONG: 'BUY', OPEN_SHORT: 'SELL', CLOSE_LONG: 'SELL', CLOSE_SHORT: 'BUY' }[order.order_direction];
15
+ if (!side)
16
+ throw new Error(`Unsupported order_direction: ${order.order_direction}`);
17
+ const timeInForce = order.order_type === 'MAKER' ? 'GTX' : order.order_type === 'LIMIT' ? 'GTC' : undefined;
18
+ const price = order.price;
19
+ let quantity = order.volume;
20
+ let quoteOrderQty;
21
+ if (type === 'MARKET' && side === 'BUY') {
22
+ const spotPrice = await (0, sapi_1.getApiV1TickerPrice)({});
23
+ const thePrice = (_a = spotPrice.find((x) => x.symbol === symbol)) === null || _a === void 0 ? void 0 : _a.price;
24
+ if (!thePrice)
25
+ throw new Error(`Cannot get price for symbol ${symbol}`);
26
+ quantity = undefined;
27
+ quoteOrderQty = (0, utils_1.roundToStep)(order.volume * +thePrice, 0.01);
28
+ }
29
+ await (0, sapi_1.postApiV1Order)({
30
+ symbol,
31
+ type,
32
+ side,
33
+ timeInForce,
34
+ price,
35
+ quantity,
36
+ quoteOrderQty,
37
+ });
38
+ return { res: { code: 0, message: 'OK' } };
39
+ });
40
+ //# sourceMappingURL=order-spot.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"order-spot.js","sourceRoot":"","sources":["../src/order-spot.ts"],"names":[],"mappings":";;AACA,+CAA4C;AAC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,IAAI,IAAI,KAAK,KAAK,EAAE;QACvC,MAAM,SAAS,GAAG,MAAM,IAAA,0BAAmB,EAAC,EAAE,CAAC,CAAC;QAChD,MAAM,QAAQ,GAAG,MAAA,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,KAAK,MAAM,CAAC,0CAAE,KAAK,CAAC;QACnE,IAAI,CAAC,QAAQ;YAAE,MAAM,IAAI,KAAK,CAAC,+BAA+B,MAAM,EAAE,CAAC,CAAC;QACxE,QAAQ,GAAG,SAAS,CAAC;QACrB,aAAa,GAAG,IAAA,mBAAW,EAAC,KAAK,CAAC,MAAM,GAAG,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;KAC7D;IAED,MAAM,IAAA,qBAAc,EAAC;QACnB,MAAM;QACN,IAAI;QACJ,IAAI;QACJ,WAAW;QACX,KAAK;QACL,QAAQ;QACR,aAAa;KACd,CAAC,CAAC;IAEH,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,CAAC;AAC7C,CAAC,CACF,CAAC","sourcesContent":["import { IOrder } from '@yuants/data-order';\nimport { Terminal } from '@yuants/protocol';\nimport { roundToStep } from '@yuants/utils';\nimport { SPOT_ACCOUNT_ID } from './account-spot';\nimport { getApiV1TickerPrice, postApiV1Order } from './sapi';\n\nTerminal.fromNodeEnv().server.provideService<IOrder>(\n 'SubmitOrder',\n { required: ['account_id'], properties: { account_id: { type: 'string', const: SPOT_ACCOUNT_ID } } },\n async (msg) => {\n const order = msg.req;\n const symbol = order.product_id;\n\n const type = ({ MARKET: 'MARKET', LIMIT: 'LIMIT', MAKER: 'LIMIT' } as const)[order.order_type!];\n if (!type) throw new Error(`Unsupported order_type: ${order.order_type}`);\n\n const side = ({ OPEN_LONG: 'BUY', OPEN_SHORT: 'SELL', CLOSE_LONG: 'SELL', CLOSE_SHORT: 'BUY' } as const)[\n order.order_direction!\n ];\n if (!side) throw new Error(`Unsupported order_direction: ${order.order_direction}`);\n\n const timeInForce =\n order.order_type === 'MAKER' ? 'GTX' : order.order_type === 'LIMIT' ? 'GTC' : undefined;\n\n const price = order.price;\n\n let quantity: number | undefined = order.volume;\n let quoteOrderQty: number | undefined;\n\n if (type === 'MARKET' && side === 'BUY') {\n const spotPrice = await getApiV1TickerPrice({});\n const thePrice = spotPrice.find((x) => x.symbol === symbol)?.price;\n if (!thePrice) throw new Error(`Cannot get price for symbol ${symbol}`);\n quantity = undefined;\n quoteOrderQty = roundToStep(order.volume * +thePrice, 0.01);\n }\n\n await postApiV1Order({\n symbol,\n type,\n side,\n timeInForce,\n price,\n quantity,\n quoteOrderQty,\n });\n\n return { res: { code: 0, message: 'OK' } };\n },\n);\n"]}
package/lib/sapi.d.ts ADDED
@@ -0,0 +1,37 @@
1
+ /**
2
+ * 获取账户信息 (现货)
3
+ *
4
+ * https://github.com/asterdex/api-docs/blob/master/aster-finance-spot-api_CN.md#%E8%B4%A6%E6%88%B7%E4%BF%A1%E6%81%AF-user_data
5
+ */
6
+ export declare const getApiV1Account: (params: {}) => Promise<{
7
+ feeTier: number;
8
+ canTrade: boolean;
9
+ canDeposit: boolean;
10
+ canWithdraw: boolean;
11
+ canBurnAsset: boolean;
12
+ updateTime: number;
13
+ balances: {
14
+ asset: string;
15
+ free: string;
16
+ locked: string;
17
+ }[];
18
+ }>;
19
+ /**
20
+ * 获取最新价格
21
+ * https://github.com/asterdex/api-docs/blob/master/aster-finance-spot-api_CN.md#%E6%9C%80%E6%96%B0%E4%BB%B7%E6%A0%BC
22
+ */
23
+ export declare const getApiV1TickerPrice: (params: {}) => Promise<{
24
+ symbol: string;
25
+ price: string;
26
+ time: number;
27
+ }[]>;
28
+ export declare const postApiV1Order: (params: {
29
+ symbol: string;
30
+ side: 'BUY' | 'SELL';
31
+ type: 'MARKET' | 'LIMIT' | 'STOP' | 'STOP_MARKET' | 'TAKE_PROFIT' | 'TAKE_PROFIT_MARKET';
32
+ timeInForce?: "GTC" | "IOC" | "FOK" | "GTX" | undefined;
33
+ quantity?: number | undefined;
34
+ quoteOrderQty?: number | undefined;
35
+ price?: number | undefined;
36
+ }) => Promise<{}>;
37
+ //# sourceMappingURL=sapi.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sapi.d.ts","sourceRoot":"","sources":["../src/sapi.ts"],"names":[],"mappings":"AAyDA;;;;GAIG;AACH,eAAO,MAAM,eAAe;aAGf,MAAM;cACL,OAAO;gBACL,OAAO;iBACN,OAAO;kBACN,OAAO;gBACT,MAAM;cACR;QACR,KAAK,EAAE,MAAM,CAAC;QACd,IAAI,EAAE,MAAM,CAAC;QACb,MAAM,EAAE,MAAM,CAAC;KAChB,EAAE;EAEiC,CAAC;AAEzC;;;GAGG;AACH,eAAO,MAAM,mBAAmB;YAGpB,MAAM;WACP,MAAM;UACP,MAAM;IAE+B,CAAC;AAEhD,eAAO,MAAM,cAAc;YAEf,MAAM;UACR,KAAK,GAAG,MAAM;UACd,QAAQ,GAAG,OAAO,GAAG,MAAM,GAAG,aAAa,GAAG,aAAa,GAAG,oBAAoB;;;;;iBAOzD,CAAC"}
package/lib/sapi.js ADDED
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.postApiV1Order = exports.getApiV1TickerPrice = exports.getApiV1Account = void 0;
4
+ const utils_1 = require("./utils");
5
+ const API_KEY = process.env.API_KEY;
6
+ const SECRET_KEY = process.env.SECRET_KEY;
7
+ const BASE_URL = 'https://sapi.asterdex.com';
8
+ const request = async (type, method, endpoint, params = {}) => {
9
+ const needApiKey = type !== 'NONE';
10
+ const needSign = type === 'TRADE' || type === 'USER_DATA';
11
+ const url = new URL(BASE_URL);
12
+ url.pathname = endpoint;
13
+ for (const [key, value] of Object.entries(params)) {
14
+ if (value === undefined)
15
+ continue;
16
+ url.searchParams.set(key, `${value}`);
17
+ }
18
+ if (needSign) {
19
+ url.searchParams.set('timestamp', `${Date.now()}`);
20
+ const msg = url.search.slice(1); // 去掉开头的 '?'
21
+ const signature = await (0, utils_1.opensslEquivalentHMAC)(msg, SECRET_KEY);
22
+ url.searchParams.set('signature', signature);
23
+ }
24
+ console.info(url.toString());
25
+ const response = await fetch(url.toString(), {
26
+ method,
27
+ headers: needApiKey
28
+ ? {
29
+ 'X-MBX-APIKEY': API_KEY,
30
+ }
31
+ : {},
32
+ }).then((response) => response.json());
33
+ if (response.code && response.code !== 0) {
34
+ throw JSON.stringify(response);
35
+ }
36
+ return response;
37
+ };
38
+ const createApi = (type, method, endpoint) => (params) => request(type, method, endpoint, params);
39
+ /**
40
+ * 获取账户信息 (现货)
41
+ *
42
+ * https://github.com/asterdex/api-docs/blob/master/aster-finance-spot-api_CN.md#%E8%B4%A6%E6%88%B7%E4%BF%A1%E6%81%AF-user_data
43
+ */
44
+ exports.getApiV1Account = createApi('USER_DATA', 'GET', '/api/v1/account');
45
+ /**
46
+ * 获取最新价格
47
+ * https://github.com/asterdex/api-docs/blob/master/aster-finance-spot-api_CN.md#%E6%9C%80%E6%96%B0%E4%BB%B7%E6%A0%BC
48
+ */
49
+ exports.getApiV1TickerPrice = createApi('MARKET_DATA', 'GET', '/api/v1/ticker/price');
50
+ exports.postApiV1Order = createApi('TRADE', 'POST', '/api/v1/order');
51
+ //# sourceMappingURL=sapi.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sapi.js","sourceRoot":"","sources":["../src/sapi.ts"],"names":[],"mappings":";;;AAAA,mCAAgD;AAEhD,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,OAAQ,CAAC;AACrC,MAAM,UAAU,GAAG,OAAO,CAAC,GAAG,CAAC,UAAW,CAAC;AAE3C,MAAM,QAAQ,GAAG,2BAA2B,CAAC;AAE7C,MAAM,OAAO,GAAG,KAAK,EACnB,IAAoE,EACpE,MAAc,EACd,QAAgB,EAChB,SAAc,EAAE,EACJ,EAAE;IACd,MAAM,UAAU,GAAG,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AE;QACxC,MAAM,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;KAChC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC,CAAC;AAEF,MAAM,SAAS,GACb,CACE,IAAoE,EACpE,MAAc,EACd,QAAgB,EAChB,EAAE,CACJ,CAAC,MAAY,EAAE,EAAE,CACf,OAAO,CAAO,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;AAElD;;;;GAIG;AACU,QAAA,eAAe,GAAG,SAAS,CAetC,WAAW,EAAE,KAAK,EAAE,iBAAiB,CAAC,CAAC;AAEzC;;;GAGG;AACU,QAAA,mBAAmB,GAAG,SAAS,CAO1C,aAAa,EAAE,KAAK,EAAE,sBAAsB,CAAC,CAAC;AAEnC,QAAA,cAAc,GAAG,SAAS,CAWrC,OAAO,EAAE,MAAM,EAAE,eAAe,CAAC,CAAC","sourcesContent":["import { opensslEquivalentHMAC } from './utils';\n\nconst API_KEY = process.env.API_KEY!;\nconst SECRET_KEY = process.env.SECRET_KEY!;\n\nconst BASE_URL = 'https://sapi.asterdex.com';\n\nconst request = async <T>(\n type: 'NONE' | 'TRADE' | 'USER_DATA' | 'USER_STREAM' | 'MARKET_DATA',\n method: string,\n endpoint: string,\n params: any = {},\n): Promise<T> => {\n const needApiKey = type !== 'NONE';\n const needSign = type === 'TRADE' || type === 'USER_DATA';\n\n const url = new URL(BASE_URL);\n url.pathname = endpoint;\n for (const [key, value] of Object.entries(params)) {\n if (value === undefined) continue;\n url.searchParams.set(key, `${value}`);\n }\n\n if (needSign) {\n url.searchParams.set('timestamp', `${Date.now()}`);\n const msg = url.search.slice(1); // 去掉开头的 '?'\n const signature = await opensslEquivalentHMAC(msg, SECRET_KEY);\n url.searchParams.set('signature', signature);\n }\n\n console.info(url.toString());\n\n const response = await fetch(url.toString(), {\n method,\n headers: needApiKey\n ? {\n 'X-MBX-APIKEY': API_KEY,\n }\n : {},\n }).then((response) => response.json() as any);\n\n if (response.code && response.code !== 0) {\n throw JSON.stringify(response);\n }\n\n return response;\n};\n\nconst createApi =\n <TReq, TRes>(\n type: 'NONE' | 'TRADE' | 'USER_DATA' | 'USER_STREAM' | 'MARKET_DATA',\n method: string,\n endpoint: string,\n ) =>\n (params: TReq) =>\n request<TRes>(type, method, endpoint, params);\n\n/**\n * 获取账户信息 (现货)\n *\n * https://github.com/asterdex/api-docs/blob/master/aster-finance-spot-api_CN.md#%E8%B4%A6%E6%88%B7%E4%BF%A1%E6%81%AF-user_data\n */\nexport const getApiV1Account = createApi<\n {},\n {\n feeTier: number;\n canTrade: boolean;\n canDeposit: boolean;\n canWithdraw: boolean;\n canBurnAsset: boolean;\n updateTime: number;\n balances: {\n asset: string;\n free: string;\n locked: string;\n }[];\n }\n>('USER_DATA', 'GET', '/api/v1/account');\n\n/**\n * 获取最新价格\n * https://github.com/asterdex/api-docs/blob/master/aster-finance-spot-api_CN.md#%E6%9C%80%E6%96%B0%E4%BB%B7%E6%A0%BC\n */\nexport const getApiV1TickerPrice = createApi<\n {},\n {\n symbol: string;\n price: string;\n time: number;\n }[]\n>('MARKET_DATA', 'GET', '/api/v1/ticker/price');\n\nexport const postApiV1Order = createApi<\n {\n symbol: string;\n side: 'BUY' | 'SELL';\n type: 'MARKET' | 'LIMIT' | 'STOP' | 'STOP_MARKET' | 'TAKE_PROFIT' | 'TAKE_PROFIT_MARKET';\n timeInForce?: 'GTC' | 'IOC' | 'FOK' | 'GTX';\n quantity?: number;\n quoteOrderQty?: number;\n price?: number;\n },\n {}\n>('TRADE', 'POST', '/api/v1/order');\n"]}
package/lib/utils.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export declare function arrayBufferToHex(buffer: ArrayBuffer): string;
2
+ export declare function opensslEquivalentHMAC(message: string, secretKey: string): Promise<string>;
3
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,WAAW,GAAG,MAAM,CAU5D;AAED,wBAAsB,qBAAqB,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAyB/F"}
package/lib/utils.js ADDED
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.opensslEquivalentHMAC = exports.arrayBufferToHex = void 0;
4
+ function arrayBufferToHex(buffer) {
5
+ const bytes = new Uint8Array(buffer);
6
+ const hexArray = [];
7
+ for (const byte of bytes) {
8
+ const hex = byte.toString(16).padStart(2, '0');
9
+ hexArray.push(hex);
10
+ }
11
+ return hexArray.join('');
12
+ }
13
+ exports.arrayBufferToHex = arrayBufferToHex;
14
+ async function opensslEquivalentHMAC(message, secretKey) {
15
+ try {
16
+ const keyBuffer = new TextEncoder().encode(secretKey);
17
+ const messageBuffer = new TextEncoder().encode(message);
18
+ // 导入密钥
19
+ const cryptoKey = await crypto.subtle.importKey('raw', keyBuffer, {
20
+ name: 'HMAC',
21
+ hash: { name: 'SHA-256' },
22
+ }, false, ['sign']);
23
+ // 进行 HMAC-SHA256 签名
24
+ const signature = await crypto.subtle.sign('HMAC', cryptoKey, messageBuffer);
25
+ // 转换为16进制小写(与 openssl 输出格式一致)
26
+ return arrayBufferToHex(signature);
27
+ }
28
+ catch (error) {
29
+ throw new Error(`HMAC-SHA256 签名失败: ${error}`);
30
+ }
31
+ }
32
+ exports.opensslEquivalentHMAC = opensslEquivalentHMAC;
33
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAAA,SAAgB,gBAAgB,CAAC,MAAmB;IAClD,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;IACrC,MAAM,QAAQ,GAAa,EAAE,CAAC;IAE9B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE;QACxB,MAAM,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;QAC/C,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;KACpB;IAED,OAAO,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAC3B,CAAC;AAVD,4CAUC;AAEM,KAAK,UAAU,qBAAqB,CAAC,OAAe,EAAE,SAAiB;IAC5E,IAAI;QACF,MAAM,SAAS,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;QACtD,MAAM,aAAa,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAExD,OAAO;QACP,MAAM,SAAS,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,SAAS,CAC7C,KAAK,EACL,SAAS,EACT;YACE,IAAI,EAAE,MAAM;YACZ,IAAI,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;SAC1B,EACD,KAAK,EACL,CAAC,MAAM,CAAC,CACT,CAAC;QAEF,oBAAoB;QACpB,MAAM,SAAS,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,SAAS,EAAE,aAAa,CAAC,CAAC;QAE7E,8BAA8B;QAC9B,OAAO,gBAAgB,CAAC,SAAS,CAAC,CAAC;KACpC;IAAC,OAAO,KAAK,EAAE;QACd,MAAM,IAAI,KAAK,CAAC,qBAAqB,KAAK,EAAE,CAAC,CAAC;KAC/C;AACH,CAAC;AAzBD,sDAyBC","sourcesContent":["export function arrayBufferToHex(buffer: ArrayBuffer): string {\n const bytes = new Uint8Array(buffer);\n const hexArray: string[] = [];\n\n for (const byte of bytes) {\n const hex = byte.toString(16).padStart(2, '0');\n hexArray.push(hex);\n }\n\n return hexArray.join('');\n}\n\nexport async function opensslEquivalentHMAC(message: string, secretKey: string): Promise<string> {\n try {\n const keyBuffer = new TextEncoder().encode(secretKey);\n const messageBuffer = new TextEncoder().encode(message);\n\n // 导入密钥\n const cryptoKey = await crypto.subtle.importKey(\n 'raw',\n keyBuffer,\n {\n name: 'HMAC',\n hash: { name: 'SHA-256' },\n },\n false,\n ['sign'],\n );\n\n // 进行 HMAC-SHA256 签名\n const signature = await crypto.subtle.sign('HMAC', cryptoKey, messageBuffer);\n\n // 转换为16进制小写(与 openssl 输出格式一致)\n return arrayBufferToHex(signature);\n } catch (error) {\n throw new Error(`HMAC-SHA256 签名失败: ${error}`);\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yuants/vendor-aster",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
4
  "bin": "lib/cli.js",
5
5
  "main": "lib/index.js",
6
6
  "files": [
@@ -1,15 +1,19 @@
1
1
  {
2
- "apps/vendor-aster/CHANGELOG.json": "07b18186b91af11462a3a316972d38045954a69c",
3
- "apps/vendor-aster/CHANGELOG.md": "fab26eb54b6446219c669e713d8f6167565d51ba",
2
+ "apps/vendor-aster/CHANGELOG.json": "9289afd530e76166c0a21a4a4639202fb46cf641",
3
+ "apps/vendor-aster/CHANGELOG.md": "d0c350f21aaa8dc4f8e0278a3f306706c02c74a0",
4
4
  "apps/vendor-aster/config/jest.config.json": "4bb17bde3ee911163a3edb36a6eb71491d80b1bd",
5
5
  "apps/vendor-aster/config/rig.json": "f6c7b5537dc77a3170ba9f008bae3b6c3ee11956",
6
6
  "apps/vendor-aster/config/typescript.json": "854907e8a821f2050f6533368db160c649c25348",
7
- "apps/vendor-aster/package.json": "3ec36d5d191790fdaa1cc3eebb28ada6576410fa",
8
- "apps/vendor-aster/src/account.ts": "dfef4baeb4a7f28726cd789e5a2e481e97d3a905",
9
- "apps/vendor-aster/src/api.ts": "b50b5e0f4a2679eeb1aa53c8109ddced464a6293",
7
+ "apps/vendor-aster/package.json": "c8de094343a9713a63d613fe4c6c3037858a205e",
8
+ "apps/vendor-aster/src/account-spot.ts": "f2d77bba520a13b124968aa1169bda0c58a06b18",
9
+ "apps/vendor-aster/src/account.ts": "4f0421aa90255f6b9d225905dcb33933bad0c43a",
10
+ "apps/vendor-aster/src/api.ts": "0d71d7db31f62e2dbb0d6241fcdd5dc16798570e",
10
11
  "apps/vendor-aster/src/cli.ts": "9bf6b5559a6c6f33da20e74cc6c5d702c60ec891",
11
- "apps/vendor-aster/src/index.ts": "dc5f7588eba080e7098c267fd1218575bb2eb239",
12
+ "apps/vendor-aster/src/index.ts": "bde17011380b14dbeaf830d4caaa081db7c65a8b",
13
+ "apps/vendor-aster/src/order-spot.ts": "e17f728347f3c49d4311493a47b1ed72d5a29f37",
12
14
  "apps/vendor-aster/src/order.ts": "546900e63e6e1c72d01086ea5eecc3c418ba7b67",
15
+ "apps/vendor-aster/src/sapi.ts": "40214de87500f9fb6647c11d3999e0e449a6fa0b",
16
+ "apps/vendor-aster/src/utils.ts": "f732922ea47951ddf0889bba9a70fd8e270cae2f",
13
17
  "apps/vendor-aster/tsconfig.json": "81da8f78196974b5d15da0edb6b2d9f48641063c",
14
18
  "apps/vendor-aster/.rush/temp/shrinkwrap-deps.json": "1f86f5175c8714c481b272c219e0d95bc6e72755",
15
19
  "libraries/protocol/temp/package-deps.json": "3b8c7d38aeee5cc1b2858530c0bff8904d8326e4",