@pear-protocol/exchanges-sdk 0.0.18 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (190) hide show
  1. package/README.md +73 -137
  2. package/dist/client/base-ws.d.ts +30 -0
  3. package/dist/client/base-ws.js +113 -0
  4. package/dist/client/binance/exchange-ws.d.ts +19 -0
  5. package/dist/client/binance/exchange-ws.js +26 -0
  6. package/dist/{account/exchanges → client}/binance/rest.d.ts +2 -6
  7. package/dist/{account/exchanges → client}/binance/rest.js +4 -11
  8. package/dist/client/binance/types.d.ts +68 -0
  9. package/dist/client/binance/types.js +1 -0
  10. package/dist/client/binance/ws.d.ts +30 -0
  11. package/dist/client/binance/ws.js +54 -0
  12. package/dist/client/bybit/exchange-ws.d.ts +20 -0
  13. package/dist/client/bybit/exchange-ws.js +28 -0
  14. package/dist/{account/exchanges → client}/bybit/rest.js +3 -2
  15. package/dist/{account/exchanges → client}/bybit/types.d.ts +1 -9
  16. package/dist/client/bybit/types.js +1 -0
  17. package/dist/client/bybit/ws.d.ts +17 -0
  18. package/dist/client/bybit/ws.js +50 -0
  19. package/dist/client/exchange-ws.d.ts +12 -0
  20. package/dist/client/exchange-ws.js +10 -0
  21. package/dist/client/hyperliquid/exchange-ws.d.ts +15 -0
  22. package/dist/client/hyperliquid/exchange-ws.js +21 -0
  23. package/dist/client/hyperliquid/rest.d.ts +3 -0
  24. package/dist/client/hyperliquid/rest.js +12 -0
  25. package/dist/{account/exchanges → client}/hyperliquid/types.d.ts +93 -39
  26. package/dist/client/hyperliquid/types.js +1 -0
  27. package/dist/client/hyperliquid/ws.d.ts +10 -0
  28. package/dist/client/hyperliquid/ws.js +16 -0
  29. package/dist/client/index.d.ts +31 -0
  30. package/dist/client/index.js +47 -0
  31. package/dist/client/lighter/constants.d.ts +4 -0
  32. package/dist/client/lighter/constants.js +4 -0
  33. package/dist/client/lighter/exchange-ws.d.ts +15 -0
  34. package/dist/client/lighter/exchange-ws.js +21 -0
  35. package/dist/client/lighter/rest.d.ts +56 -0
  36. package/dist/client/lighter/rest.js +27 -0
  37. package/dist/client/lighter/types.d.ts +32 -0
  38. package/dist/client/lighter/types.js +1 -0
  39. package/dist/client/lighter/ws.d.ts +10 -0
  40. package/dist/client/lighter/ws.js +17 -0
  41. package/dist/client/okx/exchange-ws.d.ts +20 -0
  42. package/dist/client/okx/exchange-ws.js +28 -0
  43. package/dist/{account/exchanges → client}/okx/rest.js +2 -2
  44. package/dist/{account/exchanges → client}/okx/types.d.ts +1 -10
  45. package/dist/client/okx/types.js +1 -0
  46. package/dist/client/okx/ws.d.ts +17 -0
  47. package/dist/client/okx/ws.js +61 -0
  48. package/dist/helper/factory.d.ts +10 -0
  49. package/dist/helper/factory.js +12 -0
  50. package/dist/helper/index.d.ts +7 -0
  51. package/dist/helper/index.js +2 -0
  52. package/dist/helper/lighter/ethereum/abi.d.ts +66 -0
  53. package/dist/helper/lighter/ethereum/abi.js +40 -0
  54. package/dist/helper/lighter/ethereum/index.d.ts +8 -0
  55. package/dist/helper/lighter/ethereum/index.js +34 -0
  56. package/dist/helper/lighter/ethereum/types.d.ts +23 -0
  57. package/dist/helper/lighter/ethereum/types.js +1 -0
  58. package/dist/helper/lighter/helper.d.ts +9 -0
  59. package/dist/helper/lighter/helper.js +67 -0
  60. package/dist/helper/lighter/index.d.ts +6 -0
  61. package/dist/helper/lighter/index.js +2 -0
  62. package/dist/helper/lighter/types.d.ts +63 -0
  63. package/dist/helper/lighter/types.js +35 -0
  64. package/dist/helper/types.d.ts +11 -0
  65. package/dist/helper/types.js +1 -0
  66. package/dist/index.d.ts +43 -24
  67. package/dist/index.js +6 -21
  68. package/dist/sdk.d.ts +43 -0
  69. package/dist/sdk.js +53 -0
  70. package/dist/syncer/base.d.ts +40 -0
  71. package/dist/syncer/base.js +50 -0
  72. package/dist/syncer/binance/syncer.d.ts +24 -0
  73. package/dist/syncer/binance/syncer.js +9 -0
  74. package/dist/syncer/bybit/syncer.d.ts +24 -0
  75. package/dist/syncer/bybit/syncer.js +9 -0
  76. package/dist/syncer/factory.d.ts +22 -0
  77. package/dist/syncer/factory.js +26 -0
  78. package/dist/syncer/hyperliquid/syncer.d.ts +26 -0
  79. package/dist/syncer/hyperliquid/syncer.js +43 -0
  80. package/dist/syncer/index.d.ts +24 -0
  81. package/dist/syncer/index.js +7 -0
  82. package/dist/syncer/lighter/syncer.d.ts +24 -0
  83. package/dist/syncer/lighter/syncer.js +9 -0
  84. package/dist/syncer/okx/syncer.d.ts +24 -0
  85. package/dist/syncer/okx/syncer.js +9 -0
  86. package/dist/tracker/base.d.ts +79 -0
  87. package/dist/tracker/base.js +120 -0
  88. package/dist/tracker/binance/const.d.ts +4 -0
  89. package/dist/tracker/binance/const.js +4 -0
  90. package/dist/{account/exchanges → tracker}/binance/mapper.d.ts +4 -1
  91. package/dist/{account/exchanges → tracker}/binance/mapper.js +7 -6
  92. package/dist/tracker/binance/tracker.d.ts +60 -0
  93. package/dist/tracker/binance/tracker.js +181 -0
  94. package/dist/tracker/binance/types.d.ts +6 -0
  95. package/dist/{account/exchanges → tracker}/bybit/mapper.d.ts +5 -3
  96. package/dist/{account/exchanges → tracker}/bybit/mapper.js +11 -10
  97. package/dist/tracker/bybit/tracker.d.ts +51 -0
  98. package/dist/tracker/bybit/tracker.js +138 -0
  99. package/dist/tracker/bybit/types.d.ts +7 -0
  100. package/dist/{account/exchanges → tracker}/hyperliquid/mapper.d.ts +6 -4
  101. package/dist/{account/exchanges → tracker}/hyperliquid/mapper.js +18 -16
  102. package/dist/tracker/hyperliquid/tracker.d.ts +62 -0
  103. package/dist/tracker/hyperliquid/tracker.js +219 -0
  104. package/dist/tracker/hyperliquid/types.d.ts +23 -0
  105. package/dist/tracker/index.d.ts +38 -0
  106. package/dist/tracker/index.js +81 -0
  107. package/dist/tracker/lighter/const.d.ts +3 -0
  108. package/dist/tracker/lighter/const.js +3 -0
  109. package/dist/tracker/lighter/mapper.d.ts +16 -0
  110. package/dist/tracker/lighter/mapper.js +77 -0
  111. package/dist/tracker/lighter/tracker.d.ts +53 -0
  112. package/dist/tracker/lighter/tracker.js +150 -0
  113. package/dist/tracker/lighter/types.d.ts +16 -0
  114. package/dist/tracker/lighter/types.js +6 -0
  115. package/dist/{account/exchanges → tracker}/okx/mapper.d.ts +5 -3
  116. package/dist/{account/exchanges → tracker}/okx/mapper.js +11 -10
  117. package/dist/tracker/okx/tracker.d.ts +51 -0
  118. package/dist/tracker/okx/tracker.js +134 -0
  119. package/dist/tracker/okx/types.d.ts +8 -0
  120. package/dist/tracker/types.d.ts +63 -0
  121. package/dist/{account → tracker}/types.js +6 -4
  122. package/dist/tracker/utils.d.ts +11 -0
  123. package/dist/{account → tracker}/utils.js +0 -2
  124. package/dist/types.d.ts +50 -0
  125. package/dist/types.js +1 -0
  126. package/package.json +7 -5
  127. package/dist/account/account-manager.d.ts +0 -31
  128. package/dist/account/account-manager.js +0 -119
  129. package/dist/account/exchange-account.d.ts +0 -35
  130. package/dist/account/exchange-account.js +0 -158
  131. package/dist/account/exchanges/base.d.ts +0 -48
  132. package/dist/account/exchanges/base.js +0 -41
  133. package/dist/account/exchanges/binance/asset-tracker.d.ts +0 -26
  134. package/dist/account/exchanges/binance/asset-tracker.js +0 -38
  135. package/dist/account/exchanges/binance/const.d.ts +0 -10
  136. package/dist/account/exchanges/binance/const.js +0 -10
  137. package/dist/account/exchanges/binance/orchestrator.d.ts +0 -31
  138. package/dist/account/exchanges/binance/orchestrator.js +0 -91
  139. package/dist/account/exchanges/binance/state-handler.d.ts +0 -33
  140. package/dist/account/exchanges/binance/state-handler.js +0 -113
  141. package/dist/account/exchanges/binance/types.d.ts +0 -5
  142. package/dist/account/exchanges/binance/ws.d.ts +0 -23
  143. package/dist/account/exchanges/binance/ws.js +0 -69
  144. package/dist/account/exchanges/bybit/asset-tracker.d.ts +0 -26
  145. package/dist/account/exchanges/bybit/asset-tracker.js +0 -37
  146. package/dist/account/exchanges/bybit/const.d.ts +0 -7
  147. package/dist/account/exchanges/bybit/const.js +0 -7
  148. package/dist/account/exchanges/bybit/orchestrator.d.ts +0 -22
  149. package/dist/account/exchanges/bybit/orchestrator.js +0 -66
  150. package/dist/account/exchanges/bybit/state-handler.d.ts +0 -23
  151. package/dist/account/exchanges/bybit/state-handler.js +0 -70
  152. package/dist/account/exchanges/bybit/ws.d.ts +0 -19
  153. package/dist/account/exchanges/bybit/ws.js +0 -83
  154. package/dist/account/exchanges/hyperliquid/asset-tracker.d.ts +0 -35
  155. package/dist/account/exchanges/hyperliquid/asset-tracker.js +0 -83
  156. package/dist/account/exchanges/hyperliquid/orchestrator.d.ts +0 -26
  157. package/dist/account/exchanges/hyperliquid/orchestrator.js +0 -102
  158. package/dist/account/exchanges/hyperliquid/state-handler.d.ts +0 -25
  159. package/dist/account/exchanges/hyperliquid/state-handler.js +0 -104
  160. package/dist/account/exchanges/index.d.ts +0 -11
  161. package/dist/account/exchanges/index.js +0 -32
  162. package/dist/account/exchanges/okx/asset-tracker.d.ts +0 -26
  163. package/dist/account/exchanges/okx/asset-tracker.js +0 -34
  164. package/dist/account/exchanges/okx/const.d.ts +0 -7
  165. package/dist/account/exchanges/okx/const.js +0 -7
  166. package/dist/account/exchanges/okx/orchestrator.d.ts +0 -22
  167. package/dist/account/exchanges/okx/orchestrator.js +0 -57
  168. package/dist/account/exchanges/okx/state-handler.d.ts +0 -24
  169. package/dist/account/exchanges/okx/state-handler.js +0 -78
  170. package/dist/account/exchanges/okx/ws.d.ts +0 -20
  171. package/dist/account/exchanges/okx/ws.js +0 -103
  172. package/dist/account/index.d.ts +0 -11
  173. package/dist/account/index.js +0 -4
  174. package/dist/account/types.d.ts +0 -5
  175. package/dist/account/utils.d.ts +0 -10
  176. package/dist/credentials.d.ts +0 -28
  177. package/dist/credentials.js +0 -46
  178. package/dist/types-Caz2X4h0.d.ts +0 -151
  179. package/dist/utils/ws.d.ts +0 -10
  180. package/dist/utils/ws.js +0 -42
  181. /package/dist/{account/exchanges → client}/bybit/rest.d.ts +0 -0
  182. /package/dist/{utils → client}/hmac.d.ts +0 -0
  183. /package/dist/{utils → client}/hmac.js +0 -0
  184. /package/dist/{account/exchanges → client}/okx/rest.d.ts +0 -0
  185. /package/dist/{account/exchanges → tracker}/binance/types.js +0 -0
  186. /package/dist/{account/exchanges → tracker}/bybit/types.js +0 -0
  187. /package/dist/{account/exchanges → tracker}/hyperliquid/const.d.ts +0 -0
  188. /package/dist/{account/exchanges → tracker}/hyperliquid/const.js +0 -0
  189. /package/dist/{account/exchanges → tracker}/hyperliquid/types.js +0 -0
  190. /package/dist/{account/exchanges → tracker}/okx/types.js +0 -0
@@ -1,91 +0,0 @@
1
- import { BaseOrchestrator } from '../base';
2
- import { BinanceAssetTracker } from './asset-tracker';
3
- import { DEFAULT_LISTEN_KEY_REFRESH_MS, DEFAULT_ACCOUNT_POLL_MS } from './const';
4
- import { fetchAccountStatus, fetchAllPositionRisk, fetchMultiAssetsMargin, createListenKey, keepAliveListenKey } from './rest';
5
- import { BinanceStateHandler } from './state-handler';
6
- import './types';
7
- import { BinanceWs } from './ws';
8
-
9
- class BinanceOrchestrator extends BaseOrchestrator {
10
- ws = null;
11
- listenKeyTimer = null;
12
- pollTimer = null;
13
- creds;
14
- listenKeyRefreshMs;
15
- accountPollMs;
16
- stateHandler;
17
- assetTracker;
18
- constructor(config, credentials) {
19
- super(config);
20
- this.credentials = credentials;
21
- this.creds = credentials;
22
- this.listenKeyRefreshMs = config.listenKeyRefreshMs ?? DEFAULT_LISTEN_KEY_REFRESH_MS;
23
- this.accountPollMs = config.accountPollMs ?? DEFAULT_ACCOUNT_POLL_MS;
24
- this.assetTracker = new BinanceAssetTracker({
25
- onStateUpdate: (update) => this.onStateUpdate(update),
26
- onError: (err) => this.onError(err),
27
- getCreds: () => this.creds,
28
- getDemo: () => this.demo
29
- });
30
- this.stateHandler = new BinanceStateHandler({
31
- onStateUpdate: (update) => this.onStateUpdate(update),
32
- onError: (err) => this.onError(err),
33
- getCreds: () => this.creds,
34
- getDemo: () => this.demo,
35
- getTrackedCoins: () => this.trackedCoins,
36
- getTrackedAssetInfos: () => this.assetTracker.getInfos()
37
- });
38
- }
39
- async initAsync() {
40
- const [snapshot, positionRisk, multiAssetsMargin] = await Promise.all([
41
- fetchAccountStatus(this.creds, this.demo),
42
- fetchAllPositionRisk(this.creds, this.demo),
43
- fetchMultiAssetsMargin(this.creds, this.demo)
44
- ]);
45
- this.stateHandler.setMultiAssetsMargin(multiAssetsMargin);
46
- this.stateHandler.handleAccountStatus(snapshot);
47
- this.stateHandler.handlePositionRisk(positionRisk, false);
48
- const listenKey = await createListenKey(this.creds.read.api_key, this.demo);
49
- this.ws = new BinanceWs(
50
- {
51
- onUserDataEvent: (event) => this.stateHandler.handleUserDataEvent(event),
52
- onWsApiResponse: (_id, result) => this.stateHandler.handleAccountStatus(result)
53
- },
54
- this.demo
55
- );
56
- await this.ws.connectUserDataStream(listenKey);
57
- this.listenKeyTimer = setInterval(() => {
58
- keepAliveListenKey(this.creds.read.api_key, this.demo).catch(
59
- (err) => this.onError(err instanceof Error ? err : new Error(String(err)))
60
- );
61
- }, this.listenKeyRefreshMs);
62
- await this.ws.connectWsApi(this.creds);
63
- this.pollAccountStatus();
64
- this.pollTimer = setInterval(() => this.pollAccountStatus(), this.accountPollMs);
65
- }
66
- cleanupConnections() {
67
- if (this.listenKeyTimer) clearInterval(this.listenKeyTimer);
68
- if (this.pollTimer) clearInterval(this.pollTimer);
69
- this.ws?.close();
70
- }
71
- async stop() {
72
- this.stopped = true;
73
- this.cleanupConnections();
74
- }
75
- get isConnected() {
76
- return this.ws?.isConnected ?? false;
77
- }
78
- onAssetTracked(coin) {
79
- this.assetTracker.onAssetTracked(coin);
80
- }
81
- onAssetUntracked(coin) {
82
- this.assetTracker.onAssetUntracked(coin);
83
- }
84
- pollAccountStatus() {
85
- if (!this.ws?.isConnected) return;
86
- const id = this.ws.nextRequestId();
87
- this.ws.sendSignedRequest(id, "v2/account.status");
88
- }
89
- }
90
-
91
- export { BinanceOrchestrator };
@@ -1,33 +0,0 @@
1
- import { BinanceCredentialsDTO } from '@pear-protocol/types';
2
- import { S as StateUpdate, h as TrackedAssetInfo, j as BinanceRawAccountStatus, k as BinanceRawPositionRisk, m as BinanceRawUserDataEvent } from '../../../types-Caz2X4h0.js';
3
- import '../bybit/types.js';
4
- import '../hyperliquid/types.js';
5
- import '../okx/types.js';
6
-
7
- type OnStateUpdate = (update: StateUpdate) => void;
8
- type OnError = (error: Error) => void;
9
- declare class BinanceStateHandler {
10
- private onStateUpdate;
11
- private onError;
12
- private getCreds;
13
- private getDemo;
14
- private getTrackedCoins;
15
- private getTrackedAssetInfos;
16
- private currentPositions;
17
- private accountType;
18
- constructor(deps: {
19
- onStateUpdate: OnStateUpdate;
20
- onError: OnError;
21
- getCreds: () => BinanceCredentialsDTO;
22
- getDemo: () => boolean;
23
- getTrackedCoins: () => ReadonlySet<string>;
24
- getTrackedAssetInfos: () => Map<string, TrackedAssetInfo>;
25
- });
26
- setMultiAssetsMargin(value: boolean): void;
27
- handleAccountStatus(result: BinanceRawAccountStatus): void;
28
- handlePositionRisk(risk: BinanceRawPositionRisk[], merge: boolean): void;
29
- handleUserDataEvent(event: BinanceRawUserDataEvent): void;
30
- private refreshAllPositions;
31
- }
32
-
33
- export { BinanceStateHandler };
@@ -1,113 +0,0 @@
1
- import { positionKey } from '../../utils';
2
- import { mapBalance, mapPositions, mergePositions } from './mapper';
3
- import { fetchPositionRisk } from './rest';
4
- import { BinanceAccountType } from './types';
5
-
6
- class BinanceStateHandler {
7
- onStateUpdate;
8
- onError;
9
- getCreds;
10
- getDemo;
11
- getTrackedCoins;
12
- getTrackedAssetInfos;
13
- currentPositions = /* @__PURE__ */ new Map();
14
- accountType = BinanceAccountType.SINGLE_ASSET;
15
- constructor(deps) {
16
- this.onStateUpdate = deps.onStateUpdate;
17
- this.onError = deps.onError;
18
- this.getCreds = deps.getCreds;
19
- this.getDemo = deps.getDemo;
20
- this.getTrackedCoins = deps.getTrackedCoins;
21
- this.getTrackedAssetInfos = deps.getTrackedAssetInfos;
22
- }
23
- setMultiAssetsMargin(value) {
24
- this.accountType = value ? BinanceAccountType.MULTI_ASSET : BinanceAccountType.SINGLE_ASSET;
25
- }
26
- handleAccountStatus(result) {
27
- const balance = { ...mapBalance(result), accountType: this.accountType };
28
- this.onStateUpdate({ balance });
29
- }
30
- handlePositionRisk(risk, merge) {
31
- let positions = mapPositions(risk);
32
- if (merge && this.currentPositions.size > 0) {
33
- positions = mergePositions(positions, this.currentPositions);
34
- }
35
- this.currentPositions = positions;
36
- const leverageSettings = /* @__PURE__ */ new Map();
37
- for (const p of risk) {
38
- if (p.symbol && p.leverage) {
39
- leverageSettings.set(p.symbol, p.leverage);
40
- }
41
- }
42
- this.onStateUpdate({ positions, leverageSettings });
43
- }
44
- handleUserDataEvent(event) {
45
- if (event.e === "ACCOUNT_CONFIG_UPDATE" && event.ac) {
46
- const symbol = event.ac.s ?? "";
47
- const leverage = event.ac.l ?? "0";
48
- if (symbol && leverage !== "0") {
49
- const leverageSettings = /* @__PURE__ */ new Map();
50
- leverageSettings.set(symbol, leverage);
51
- const trackedAssetInfos = this.getTrackedAssetInfos();
52
- const existing = trackedAssetInfos.get(symbol);
53
- if (existing) {
54
- const updated = { ...existing, leverage };
55
- trackedAssetInfos.set(symbol, updated);
56
- const trackedAssets = /* @__PURE__ */ new Map();
57
- trackedAssets.set(symbol, updated);
58
- this.onStateUpdate({ leverageSettings, trackedAssets });
59
- } else {
60
- this.onStateUpdate({ leverageSettings });
61
- }
62
- }
63
- this.refreshAllPositions();
64
- }
65
- if (event.e === "ACCOUNT_UPDATE") {
66
- this.refreshAllPositions();
67
- }
68
- }
69
- refreshAllPositions() {
70
- const creds = this.getCreds();
71
- const demo = this.getDemo();
72
- const symbols = /* @__PURE__ */ new Set();
73
- for (const pos of this.currentPositions.values()) {
74
- symbols.add(pos.symbol);
75
- }
76
- for (const coin of this.getTrackedCoins()) {
77
- symbols.add(coin);
78
- }
79
- if (symbols.size === 0) return;
80
- const promises = Array.from(symbols).map((symbol) => fetchPositionRisk(creds, symbol, demo).catch(() => void 0));
81
- Promise.all(promises).then((results) => {
82
- const freshPositions = /* @__PURE__ */ new Map();
83
- for (const risk of results) {
84
- if (!risk) continue;
85
- const amt = risk.positionAmt ?? "0";
86
- if (amt === "0" || amt === "") continue;
87
- const symbol = risk.symbol ?? "";
88
- const side = amt.startsWith("-") ? "short" : "long";
89
- freshPositions.set(positionKey(symbol, side), {
90
- symbol,
91
- side,
92
- size: amt,
93
- entryPrice: risk.entryPrice ?? "0",
94
- unrealizedPnl: risk.unRealizedProfit ?? "0",
95
- leverage: risk.leverage ?? "0",
96
- marginType: risk.marginType,
97
- liquidationPrice: risk.liquidationPrice || null
98
- });
99
- }
100
- for (const [key, pos] of this.currentPositions) {
101
- if (!symbols.has(pos.symbol) && !freshPositions.has(key)) {
102
- freshPositions.set(key, pos);
103
- }
104
- }
105
- this.currentPositions = freshPositions;
106
- this.onStateUpdate({ positions: freshPositions });
107
- }).catch((err) => {
108
- this.onError(err instanceof Error ? err : new Error(String(err)));
109
- });
110
- }
111
- }
112
-
113
- export { BinanceStateHandler };
@@ -1,5 +0,0 @@
1
- export { f as BinanceAccountType, l as BinanceAssetIndex, o as BinanceMultiAssetsMarginResponse, j as BinanceRawAccountStatus, p as BinanceRawAsset, k as BinanceRawPositionRisk, m as BinanceRawUserDataEvent, n as BinanceWsHandlers } from '../../../types-Caz2X4h0.js';
2
- import '@pear-protocol/types';
3
- import '../bybit/types.js';
4
- import '../hyperliquid/types.js';
5
- import '../okx/types.js';
@@ -1,23 +0,0 @@
1
- import { BinanceCredentialsDTO } from '@pear-protocol/types';
2
- import { n as BinanceWsHandlers } from '../../../types-Caz2X4h0.js';
3
- import '../bybit/types.js';
4
- import '../hyperliquid/types.js';
5
- import '../okx/types.js';
6
-
7
- declare class BinanceWs {
8
- private userDataWs;
9
- private wsApiWs;
10
- private requestId;
11
- private handlers;
12
- private demo;
13
- private creds;
14
- constructor(handlers: BinanceWsHandlers, demo: boolean);
15
- connectUserDataStream(listenKey: string): Promise<void>;
16
- connectWsApi(creds: BinanceCredentialsDTO): Promise<void>;
17
- nextRequestId(): number;
18
- sendSignedRequest(id: number, method: string, params?: Record<string, unknown>): Promise<void>;
19
- close(): void;
20
- get isConnected(): boolean;
21
- }
22
-
23
- export { BinanceWs };
@@ -1,69 +0,0 @@
1
- import { hmacSha256Hex } from '../../../utils/hmac';
2
- import { createWs, waitForOpen, sendWs, ReconnectingWebSocket } from '../../../utils/ws';
3
- import { USER_DATA_STREAM_URL_TESTNET, USER_DATA_STREAM_URL, WS_API_URL_TESTNET, WS_API_URL } from './const';
4
-
5
- class BinanceWs {
6
- userDataWs = null;
7
- wsApiWs = null;
8
- requestId = 1;
9
- handlers;
10
- demo;
11
- creds = null;
12
- constructor(handlers, demo) {
13
- this.handlers = handlers;
14
- this.demo = demo;
15
- }
16
- async connectUserDataStream(listenKey) {
17
- const url = this.demo ? USER_DATA_STREAM_URL_TESTNET : USER_DATA_STREAM_URL;
18
- this.userDataWs = createWs(`${url}/${listenKey}`);
19
- this.userDataWs.addEventListener("message", (event) => {
20
- try {
21
- this.handlers.onUserDataEvent(JSON.parse(event.data));
22
- } catch {
23
- }
24
- });
25
- await waitForOpen(this.userDataWs);
26
- }
27
- async connectWsApi(creds) {
28
- this.creds = creds;
29
- const url = this.demo ? WS_API_URL_TESTNET : WS_API_URL;
30
- this.wsApiWs = createWs(url);
31
- this.wsApiWs.addEventListener("message", (event) => {
32
- try {
33
- const msg = JSON.parse(event.data);
34
- if (msg.id !== void 0 && msg.result !== void 0) {
35
- this.handlers.onWsApiResponse(msg.id, msg.result);
36
- }
37
- } catch {
38
- }
39
- });
40
- await waitForOpen(this.wsApiWs);
41
- }
42
- nextRequestId() {
43
- return this.requestId++;
44
- }
45
- async sendSignedRequest(id, method, params = {}) {
46
- if (!this.creds) return;
47
- const timestamp = Date.now();
48
- const allParams = { ...params, apiKey: this.creds.read.api_key, timestamp };
49
- const queryString = Object.keys(allParams).sort().map((k) => `${k}=${allParams[k]}`).join("&");
50
- const signature = await hmacSha256Hex(this.creds.read.api_secret, queryString);
51
- sendWs(this.wsApiWs, { id, method, params: { ...allParams, signature } });
52
- }
53
- close() {
54
- this.creds = null;
55
- if (this.userDataWs) {
56
- this.userDataWs.close();
57
- this.userDataWs = null;
58
- }
59
- if (this.wsApiWs) {
60
- this.wsApiWs.close();
61
- this.wsApiWs = null;
62
- }
63
- }
64
- get isConnected() {
65
- return this.userDataWs?.readyState === ReconnectingWebSocket.OPEN && this.wsApiWs?.readyState === ReconnectingWebSocket.OPEN;
66
- }
67
- }
68
-
69
- export { BinanceWs };
@@ -1,26 +0,0 @@
1
- import { BybitCredentialsDTO } from '@pear-protocol/types';
2
- import { S as StateUpdate, h as TrackedAssetInfo } from '../../../types-Caz2X4h0.js';
3
- import './types.js';
4
- import '../hyperliquid/types.js';
5
- import '../okx/types.js';
6
-
7
- type OnStateUpdate = (update: StateUpdate) => void;
8
- type OnError = (error: Error) => void;
9
- declare class BybitAssetTracker {
10
- private onStateUpdate;
11
- private onError;
12
- private getCreds;
13
- private getDemo;
14
- private trackedAssetInfos;
15
- constructor(deps: {
16
- onStateUpdate: OnStateUpdate;
17
- onError: OnError;
18
- getCreds: () => BybitCredentialsDTO;
19
- getDemo: () => boolean;
20
- });
21
- getInfos(): Map<string, TrackedAssetInfo>;
22
- onAssetTracked(coin: string): void;
23
- onAssetUntracked(coin: string): void;
24
- }
25
-
26
- export { BybitAssetTracker };
@@ -1,37 +0,0 @@
1
- import { fetchPositionLeverage } from './rest';
2
-
3
- class BybitAssetTracker {
4
- onStateUpdate;
5
- onError;
6
- getCreds;
7
- getDemo;
8
- trackedAssetInfos = /* @__PURE__ */ new Map();
9
- constructor(deps) {
10
- this.onStateUpdate = deps.onStateUpdate;
11
- this.onError = deps.onError;
12
- this.getCreds = deps.getCreds;
13
- this.getDemo = deps.getDemo;
14
- }
15
- getInfos() {
16
- return this.trackedAssetInfos;
17
- }
18
- onAssetTracked(coin) {
19
- fetchPositionLeverage(this.getCreds(), coin, this.getDemo()).then((pos) => {
20
- const leverage = pos?.leverage || "1";
21
- const marginType = pos?.tradeMode === "1" ? "cross" : "isolated";
22
- const info = { coin, leverage, marginType };
23
- this.trackedAssetInfos.set(coin, info);
24
- const trackedAssets = /* @__PURE__ */ new Map();
25
- trackedAssets.set(coin, info);
26
- this.onStateUpdate({ trackedAssets });
27
- }).catch((err) => {
28
- this.onError(err instanceof Error ? err : new Error(String(err)));
29
- });
30
- }
31
- onAssetUntracked(coin) {
32
- this.trackedAssetInfos.delete(coin);
33
- this.onStateUpdate({ trackedAssets: new Map(this.trackedAssetInfos) });
34
- }
35
- }
36
-
37
- export { BybitAssetTracker };
@@ -1,7 +0,0 @@
1
- declare const BASE_URL = "https://api.bybit.com";
2
- declare const BASE_URL_DEMO = "https://api-demo.bybit.com";
3
- declare const WS_URL = "wss://stream.bybit.com/v5/private";
4
- declare const WS_URL_DEMO = "wss://stream-demo.bybit.com/v5/private";
5
- declare const DEFAULT_PING_INTERVAL_MS = 20000;
6
-
7
- export { BASE_URL, BASE_URL_DEMO, DEFAULT_PING_INTERVAL_MS, WS_URL, WS_URL_DEMO };
@@ -1,7 +0,0 @@
1
- const BASE_URL = "https://api.bybit.com";
2
- const BASE_URL_DEMO = "https://api-demo.bybit.com";
3
- const WS_URL = "wss://stream.bybit.com/v5/private";
4
- const WS_URL_DEMO = "wss://stream-demo.bybit.com/v5/private";
5
- const DEFAULT_PING_INTERVAL_MS = 2e4;
6
-
7
- export { BASE_URL, BASE_URL_DEMO, DEFAULT_PING_INTERVAL_MS, WS_URL, WS_URL_DEMO };
@@ -1,22 +0,0 @@
1
- import { g as SupportedCredentials } from '../../../types-Caz2X4h0.js';
2
- import { BaseOrchestrator, OrchestratorConfig } from '../base.js';
3
- import '@pear-protocol/types';
4
- import './types.js';
5
- import '../hyperliquid/types.js';
6
- import '../okx/types.js';
7
-
8
- declare class BybitOrchestrator extends BaseOrchestrator {
9
- private ws;
10
- private creds;
11
- private stateHandler;
12
- private assetTracker;
13
- constructor(config: OrchestratorConfig, credentials: SupportedCredentials);
14
- protected initAsync(): Promise<void>;
15
- protected cleanupConnections(): void;
16
- stop(): Promise<void>;
17
- get isConnected(): boolean;
18
- onAssetTracked(coin: string): void;
19
- onAssetUntracked(coin: string): void;
20
- }
21
-
22
- export { BybitOrchestrator };
@@ -1,66 +0,0 @@
1
- import { BaseOrchestrator } from '../base';
2
- import { BybitAssetTracker } from './asset-tracker';
3
- import { fetchWalletBalance, fetchPositions, fetchAccountInfo } from './rest';
4
- import { BybitStateHandler } from './state-handler';
5
- import './types';
6
- import { BybitWs } from './ws';
7
-
8
- class BybitOrchestrator extends BaseOrchestrator {
9
- ws = null;
10
- creds;
11
- stateHandler;
12
- assetTracker;
13
- constructor(config, credentials) {
14
- super(config);
15
- this.credentials = credentials;
16
- this.creds = credentials;
17
- this.assetTracker = new BybitAssetTracker({
18
- onStateUpdate: (update) => this.onStateUpdate(update),
19
- onError: (err) => this.onError(err),
20
- getCreds: () => this.creds,
21
- getDemo: () => this.demo
22
- });
23
- this.stateHandler = new BybitStateHandler({
24
- onStateUpdate: (update) => this.onStateUpdate(update),
25
- getTrackedAssetInfos: () => this.assetTracker.getInfos()
26
- });
27
- }
28
- async initAsync() {
29
- const [wallet, positionsUsd, positionsUsdc, accountInfo] = await Promise.all([
30
- fetchWalletBalance(this.creds, this.demo),
31
- fetchPositions(this.creds, "USDT", this.demo),
32
- fetchPositions(this.creds, "USDC", this.demo),
33
- fetchAccountInfo(this.creds, this.demo)
34
- ]);
35
- this.stateHandler.handleInitialData(
36
- wallet ?? null,
37
- [positionsUsd, positionsUsdc].flat(),
38
- accountInfo?.marginMode ?? null
39
- );
40
- this.ws = new BybitWs(
41
- {
42
- onMessage: (msg) => this.stateHandler.handleMessage(msg)
43
- },
44
- this.demo
45
- );
46
- await this.ws.connect(this.creds);
47
- }
48
- cleanupConnections() {
49
- this.ws?.close();
50
- }
51
- async stop() {
52
- this.stopped = true;
53
- this.cleanupConnections();
54
- }
55
- get isConnected() {
56
- return this.ws?.isConnected ?? false;
57
- }
58
- onAssetTracked(coin) {
59
- this.assetTracker.onAssetTracked(coin);
60
- }
61
- onAssetUntracked(coin) {
62
- this.assetTracker.onAssetUntracked(coin);
63
- }
64
- }
65
-
66
- export { BybitOrchestrator };
@@ -1,23 +0,0 @@
1
- import { S as StateUpdate, h as TrackedAssetInfo } from '../../../types-Caz2X4h0.js';
2
- import { BybitRawWallet, BybitRawPosition, BybitAccountType, BybitWsMessage } from './types.js';
3
- import '@pear-protocol/types';
4
- import '../hyperliquid/types.js';
5
- import '../okx/types.js';
6
-
7
- type OnStateUpdate = (update: StateUpdate) => void;
8
- declare class BybitStateHandler {
9
- private onStateUpdate;
10
- private getTrackedAssetInfos;
11
- private rawCoins;
12
- private lastWallet;
13
- private accountType;
14
- constructor(deps: {
15
- onStateUpdate: OnStateUpdate;
16
- getTrackedAssetInfos: () => Map<string, TrackedAssetInfo>;
17
- });
18
- handleInitialData(wallet: BybitRawWallet | null, positions: BybitRawPosition[], accountType: BybitAccountType | null): void;
19
- handleMessage(msg: BybitWsMessage): void;
20
- private handleWalletUpdate;
21
- }
22
-
23
- export { BybitStateHandler };
@@ -1,70 +0,0 @@
1
- import { mergeRawCoins, buildBalance, mapPositions, mapPositionUpdates } from './mapper';
2
- import './types';
3
-
4
- class BybitStateHandler {
5
- onStateUpdate;
6
- getTrackedAssetInfos;
7
- rawCoins = /* @__PURE__ */ new Map();
8
- lastWallet = null;
9
- accountType = null;
10
- constructor(deps) {
11
- this.onStateUpdate = deps.onStateUpdate;
12
- this.getTrackedAssetInfos = deps.getTrackedAssetInfos;
13
- }
14
- handleInitialData(wallet, positions, accountType) {
15
- this.accountType = accountType;
16
- if (wallet) {
17
- this.lastWallet = wallet;
18
- this.rawCoins = mergeRawCoins(/* @__PURE__ */ new Map(), wallet.coin ?? []);
19
- const balance = buildBalance(wallet, this.rawCoins, this.accountType);
20
- if (balance) this.onStateUpdate({ balance });
21
- }
22
- this.onStateUpdate({ positions: mapPositions(positions) });
23
- }
24
- handleMessage(msg) {
25
- if (msg.topic === "wallet") {
26
- const wallet = msg.data[0];
27
- if (wallet) this.handleWalletUpdate(wallet);
28
- } else if (msg.topic === "position") {
29
- const rawPositions = msg.data;
30
- const positions = mapPositionUpdates(rawPositions);
31
- if (positions.size > 0) {
32
- const trackedAssetInfos = this.getTrackedAssetInfos();
33
- const trackedAssets = /* @__PURE__ */ new Map();
34
- for (const pos of rawPositions) {
35
- const existing = trackedAssetInfos.get(pos.symbol);
36
- if (existing) {
37
- const leverage = pos.leverage || existing.leverage;
38
- const marginType = pos.tradeMode === "1" ? "isolated" : "cross";
39
- const updated = { ...existing, leverage, marginType };
40
- trackedAssetInfos.set(pos.symbol, updated);
41
- trackedAssets.set(pos.symbol, updated);
42
- }
43
- }
44
- this.onStateUpdate({
45
- positions,
46
- ...trackedAssets.size > 0 ? { trackedAssets } : {}
47
- });
48
- }
49
- }
50
- }
51
- handleWalletUpdate(raw) {
52
- this.rawCoins = mergeRawCoins(this.rawCoins, raw.coin ?? []);
53
- const merged = this.lastWallet ? {
54
- totalEquity: raw.totalEquity || this.lastWallet.totalEquity,
55
- totalWalletBalance: raw.totalWalletBalance || this.lastWallet.totalWalletBalance,
56
- totalMarginBalance: raw.totalMarginBalance || this.lastWallet.totalMarginBalance,
57
- totalAvailableBalance: raw.totalAvailableBalance || this.lastWallet.totalAvailableBalance,
58
- totalPerpUPL: raw.totalPerpUPL || this.lastWallet.totalPerpUPL,
59
- totalInitialMargin: raw.totalInitialMargin || this.lastWallet.totalInitialMargin,
60
- totalMaintenanceMargin: raw.totalMaintenanceMargin || this.lastWallet.totalMaintenanceMargin,
61
- accountMMRate: raw.accountMMRate || this.lastWallet.accountMMRate,
62
- coin: raw.coin ?? this.lastWallet.coin
63
- } : raw;
64
- this.lastWallet = merged;
65
- const balance = buildBalance(merged, this.rawCoins, this.accountType);
66
- if (balance) this.onStateUpdate({ balance });
67
- }
68
- }
69
-
70
- export { BybitStateHandler };
@@ -1,19 +0,0 @@
1
- import { BybitCredentialsDTO } from '@pear-protocol/types';
2
- import { BybitWsHandlers } from './types.js';
3
-
4
- declare class BybitWs {
5
- private ws;
6
- private pingTimer;
7
- private handlers;
8
- private demo;
9
- private creds;
10
- private readonly pingIntervalMs;
11
- constructor(handlers: BybitWsHandlers, demo: boolean, pingIntervalMs?: number);
12
- connect(creds: BybitCredentialsDTO): Promise<void>;
13
- close(): void;
14
- get isConnected(): boolean;
15
- private clearPing;
16
- private authenticate;
17
- }
18
-
19
- export { BybitWs };