@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,83 +0,0 @@
1
- import { hmacSha256Hex } from '../../../utils/hmac';
2
- import { createWs, sendWs, waitForOpen, ReconnectingWebSocket, clearIntervalTimer } from '../../../utils/ws';
3
- import { DEFAULT_PING_INTERVAL_MS, WS_URL_DEMO, WS_URL } from './const';
4
-
5
- class BybitWs {
6
- ws = null;
7
- pingTimer = null;
8
- handlers;
9
- demo;
10
- creds = null;
11
- pingIntervalMs;
12
- constructor(handlers, demo, pingIntervalMs = DEFAULT_PING_INTERVAL_MS) {
13
- this.handlers = handlers;
14
- this.demo = demo;
15
- this.pingIntervalMs = pingIntervalMs;
16
- }
17
- async connect(creds) {
18
- this.creds = creds;
19
- this.ws = createWs(this.demo ? WS_URL_DEMO : WS_URL);
20
- this.ws.addEventListener("message", (event) => {
21
- try {
22
- const msg = JSON.parse(event.data);
23
- if (msg.topic && msg.data) {
24
- this.handlers.onMessage({ topic: msg.topic, data: msg.data });
25
- }
26
- } catch {
27
- }
28
- });
29
- let initialOpen = true;
30
- this.ws.addEventListener("open", () => {
31
- if (initialOpen) {
32
- initialOpen = false;
33
- return;
34
- }
35
- if (!this.creds) return;
36
- this.authenticate(this.creds).then(() => {
37
- sendWs(this.ws, { op: "subscribe", args: ["wallet", "position"] });
38
- }).catch(() => {
39
- });
40
- });
41
- await waitForOpen(this.ws);
42
- await this.authenticate(creds);
43
- sendWs(this.ws, { op: "subscribe", args: ["wallet", "position"] });
44
- this.pingTimer = setInterval(() => sendWs(this.ws, { op: "ping" }), this.pingIntervalMs);
45
- }
46
- close() {
47
- this.clearPing();
48
- this.creds = null;
49
- if (this.ws) {
50
- this.ws.close();
51
- this.ws = null;
52
- }
53
- }
54
- get isConnected() {
55
- return this.ws?.readyState === ReconnectingWebSocket.OPEN;
56
- }
57
- clearPing() {
58
- this.pingTimer = clearIntervalTimer(this.pingTimer);
59
- }
60
- async authenticate(creds) {
61
- const expires = Date.now() + 1e4;
62
- const signature = await hmacSha256Hex(creds.api_secret, `GET/realtime${expires}`);
63
- return new Promise((resolve, reject) => {
64
- const timeout = setTimeout(() => reject(new Error("Bybit auth timeout")), 1e4);
65
- const handler = (event) => {
66
- try {
67
- const msg = JSON.parse(event.data);
68
- if (msg.op === "auth") {
69
- clearTimeout(timeout);
70
- this.ws?.removeEventListener("message", handler);
71
- if (msg.success) resolve();
72
- else reject(new Error(`Bybit auth failed: ${msg.ret_msg}`));
73
- }
74
- } catch {
75
- }
76
- };
77
- this.ws?.addEventListener("message", handler);
78
- sendWs(this.ws, { op: "auth", args: [creds.api_key, expires, signature] });
79
- });
80
- }
81
- }
82
-
83
- export { BybitWs };
@@ -1,35 +0,0 @@
1
- import { WebSocket } from 'partysocket';
2
- import { S as StateUpdate } from '../../../types-Caz2X4h0.js';
3
- import { ActiveAssetDataEvent } from './types.js';
4
- import '@pear-protocol/types';
5
- import '../bybit/types.js';
6
- import '../okx/types.js';
7
-
8
- type OnStateUpdate = (update: StateUpdate) => void;
9
- type OnActiveAssetData = (data: ActiveAssetDataEvent) => void;
10
- declare class HyperliquidAssetTracker {
11
- private onStateUpdate;
12
- private onActiveAssetData;
13
- private getWs;
14
- private getAddress;
15
- private isStopped;
16
- private subscribedCoins;
17
- private trackedAssetData;
18
- constructor(deps: {
19
- onStateUpdate: OnStateUpdate;
20
- onActiveAssetData: OnActiveAssetData;
21
- getWs: () => WebSocket | null;
22
- getAddress: () => `0x${string}` | null;
23
- isStopped: () => boolean;
24
- });
25
- onAssetTracked(coin: string): void;
26
- onAssetUntracked(coin: string): void;
27
- canUntrack(coin: string): boolean;
28
- handleActiveAssetDataMessage(data: ActiveAssetDataEvent): void;
29
- subscribeAll(coins: ReadonlySet<string>): void;
30
- private subscribeActiveAssetData;
31
- private unsubscribeActiveAssetData;
32
- private buildTrackedAssetsMap;
33
- }
34
-
35
- export { HyperliquidAssetTracker };
@@ -1,83 +0,0 @@
1
- import { sendWs } from '../../../utils/ws';
2
- import { LOCKED_TRACKED_COINS } from './const';
3
- import { buildTrackedAssetInfo } from './state-handler';
4
-
5
- class HyperliquidAssetTracker {
6
- onStateUpdate;
7
- onActiveAssetData;
8
- getWs;
9
- getAddress;
10
- isStopped;
11
- subscribedCoins = /* @__PURE__ */ new Set();
12
- trackedAssetData = /* @__PURE__ */ new Map();
13
- constructor(deps) {
14
- this.onStateUpdate = deps.onStateUpdate;
15
- this.onActiveAssetData = deps.onActiveAssetData;
16
- this.getWs = deps.getWs;
17
- this.getAddress = deps.getAddress;
18
- this.isStopped = deps.isStopped;
19
- }
20
- onAssetTracked(coin) {
21
- this.subscribeActiveAssetData(coin);
22
- }
23
- onAssetUntracked(coin) {
24
- this.unsubscribeActiveAssetData(coin);
25
- this.trackedAssetData.delete(coin);
26
- this.onStateUpdate({ trackedAssets: this.buildTrackedAssetsMap() });
27
- }
28
- canUntrack(coin) {
29
- return !LOCKED_TRACKED_COINS.includes(coin);
30
- }
31
- handleActiveAssetDataMessage(data) {
32
- if (!data.coin) return;
33
- if (!this.subscribedCoins.has(data.coin)) return;
34
- this.trackedAssetData.set(data.coin, data);
35
- this.onActiveAssetData(data);
36
- }
37
- subscribeAll(coins) {
38
- for (const coin of coins) {
39
- if (!LOCKED_TRACKED_COINS.includes(coin)) {
40
- this.subscribeActiveAssetData(coin);
41
- }
42
- }
43
- }
44
- subscribeActiveAssetData(coin) {
45
- const ws = this.getWs();
46
- const address = this.getAddress();
47
- if (!ws || !address || this.isStopped()) return;
48
- if (this.subscribedCoins.has(coin)) return;
49
- this.subscribedCoins.add(coin);
50
- sendWs(ws, {
51
- method: "subscribe",
52
- subscription: {
53
- type: "activeAssetData",
54
- coin,
55
- user: address.toLowerCase()
56
- }
57
- });
58
- }
59
- unsubscribeActiveAssetData(coin) {
60
- if (!this.subscribedCoins.has(coin)) return;
61
- this.subscribedCoins.delete(coin);
62
- const ws = this.getWs();
63
- const address = this.getAddress();
64
- if (!ws || !address) return;
65
- sendWs(ws, {
66
- method: "unsubscribe",
67
- subscription: {
68
- type: "activeAssetData",
69
- coin,
70
- user: address.toLowerCase()
71
- }
72
- });
73
- }
74
- buildTrackedAssetsMap() {
75
- const result = /* @__PURE__ */ new Map();
76
- for (const [coin, data] of this.trackedAssetData) {
77
- result.set(coin, buildTrackedAssetInfo(data));
78
- }
79
- return result;
80
- }
81
- }
82
-
83
- export { HyperliquidAssetTracker };
@@ -1,26 +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 '../bybit/types.js';
5
- import './types.js';
6
- import '../okx/types.js';
7
-
8
- declare class HyperliquidOrchestrator extends BaseOrchestrator {
9
- private ws;
10
- private address;
11
- private stateHandler;
12
- private assetTracker;
13
- constructor(config: OrchestratorConfig, credentials: SupportedCredentials);
14
- protected initAsync(): Promise<void>;
15
- private subscribe;
16
- private handleMessage;
17
- private fetchInfo;
18
- protected cleanupConnections(): void;
19
- stop(): Promise<void>;
20
- get isConnected(): boolean;
21
- onAssetTracked(coin: string): void;
22
- onAssetUntracked(coin: string): void;
23
- protected canUntrackAsset(coin: string): boolean;
24
- }
25
-
26
- export { HyperliquidOrchestrator };
@@ -1,102 +0,0 @@
1
- import { createWs, waitForOpen, sendWs } from '../../../utils/ws';
2
- import { BaseOrchestrator } from '../base';
3
- import { HyperliquidAssetTracker } from './asset-tracker';
4
- import { LOCKED_TRACKED_COINS } from './const';
5
- import { HyperliquidStateHandler } from './state-handler';
6
-
7
- const HL_WS_URL = "wss://api.hyperliquid.xyz/ws";
8
- const HL_INFO_URL = "https://api.hyperliquid.xyz/info";
9
- class HyperliquidOrchestrator extends BaseOrchestrator {
10
- ws = null;
11
- address;
12
- stateHandler;
13
- assetTracker;
14
- constructor(config, credentials) {
15
- super(config);
16
- const creds = credentials;
17
- this.credentials = credentials;
18
- this.address = creds.address;
19
- this.stateHandler = new HyperliquidStateHandler((update) => this.onStateUpdate(update));
20
- this.assetTracker = new HyperliquidAssetTracker({
21
- onStateUpdate: (update) => this.onStateUpdate(update),
22
- onActiveAssetData: (data) => this.stateHandler.handleActiveAssetData(data),
23
- getWs: () => this.ws,
24
- getAddress: () => this.address,
25
- isStopped: () => this.stopped
26
- });
27
- }
28
- async initAsync() {
29
- const allMetas = await this.fetchInfo({ type: "allPerpMetas" });
30
- this.stateHandler.loadPerpMetas(allMetas);
31
- this.ws = createWs(HL_WS_URL);
32
- await waitForOpen(this.ws);
33
- this.ws.addEventListener("message", (event) => {
34
- this.handleMessage(event.data);
35
- });
36
- const userAddr = this.address.toLowerCase();
37
- this.subscribe({ type: "allDexsClearinghouseState", user: userAddr });
38
- this.subscribe({ type: "spotState", user: userAddr });
39
- this.subscribe({ type: "webData3", user: userAddr });
40
- for (const coin of LOCKED_TRACKED_COINS) {
41
- this.trackAsset(coin);
42
- }
43
- this.assetTracker.subscribeAll(this.trackedCoins);
44
- }
45
- subscribe(subscription) {
46
- sendWs(this.ws, { method: "subscribe", subscription });
47
- }
48
- handleMessage(raw) {
49
- try {
50
- const text = typeof raw === "string" ? raw : void 0;
51
- if (!text) return;
52
- const msg = JSON.parse(text);
53
- if (!msg.channel || !msg.data) return;
54
- switch (msg.channel) {
55
- case "allDexsClearinghouseState":
56
- this.stateHandler.handleClearinghouseState(msg.data);
57
- break;
58
- case "spotState":
59
- this.stateHandler.handleSpotState(msg.data);
60
- break;
61
- case "webData3":
62
- this.stateHandler.handleWebData3(msg.data);
63
- break;
64
- case "activeAssetData":
65
- this.assetTracker.handleActiveAssetDataMessage(msg.data);
66
- break;
67
- }
68
- } catch {
69
- }
70
- }
71
- async fetchInfo(body) {
72
- const res = await fetch(HL_INFO_URL, {
73
- method: "POST",
74
- headers: { "Content-Type": "application/json" },
75
- body: JSON.stringify(body)
76
- });
77
- if (!res.ok) throw new Error(`Hyperliquid info request failed: ${res.status}`);
78
- return res.json();
79
- }
80
- cleanupConnections() {
81
- this.ws?.close();
82
- this.ws = null;
83
- }
84
- async stop() {
85
- this.stopped = true;
86
- this.cleanupConnections();
87
- }
88
- get isConnected() {
89
- return this.ws !== null && !this.stopped;
90
- }
91
- onAssetTracked(coin) {
92
- this.assetTracker.onAssetTracked(coin);
93
- }
94
- onAssetUntracked(coin) {
95
- this.assetTracker.onAssetUntracked(coin);
96
- }
97
- canUntrackAsset(coin) {
98
- return this.assetTracker.canUntrack(coin);
99
- }
100
- }
101
-
102
- export { HyperliquidOrchestrator };
@@ -1,25 +0,0 @@
1
- import { S as StateUpdate, h as TrackedAssetInfo } from '../../../types-Caz2X4h0.js';
2
- import { AllPerpMetasResponse, AllDexsClearinghouseStateEvent, ActiveAssetDataEvent, WebData3Event, SpotStateEvent } from './types.js';
3
- import '@pear-protocol/types';
4
- import '../bybit/types.js';
5
- import '../okx/types.js';
6
-
7
- type OnStateUpdate = (update: StateUpdate) => void;
8
- declare class HyperliquidStateHandler {
9
- private onStateUpdate;
10
- private lastSummary;
11
- private spotCoinBalances;
12
- private accountType;
13
- private symbolToCollateralToken;
14
- private availableToTradeByCollateral;
15
- constructor(onStateUpdate: OnStateUpdate);
16
- loadPerpMetas(allMetas: AllPerpMetasResponse): void;
17
- handleClearinghouseState(data: AllDexsClearinghouseStateEvent): void;
18
- handleActiveAssetData(data: ActiveAssetDataEvent): void;
19
- handleWebData3(data: WebData3Event): void;
20
- handleSpotState(data: SpotStateEvent): void;
21
- private buildBalance;
22
- }
23
- declare function buildTrackedAssetInfo(data: ActiveAssetDataEvent): TrackedAssetInfo;
24
-
25
- export { HyperliquidStateHandler, buildTrackedAssetInfo };
@@ -1,104 +0,0 @@
1
- import { mapPositions, mapBalance } from './mapper';
2
- import { COLLATERAL_TOKEN_MAP } from './types';
3
-
4
- class HyperliquidStateHandler {
5
- onStateUpdate;
6
- lastSummary = {
7
- accountValue: 0,
8
- totalMarginUsed: 0,
9
- totalRawUsd: 0,
10
- crossMaintenanceMarginUsed: 0,
11
- withdrawable: 0,
12
- totalUnrealizedPnl: 0
13
- };
14
- spotCoinBalances = /* @__PURE__ */ new Map();
15
- accountType = null;
16
- symbolToCollateralToken = /* @__PURE__ */ new Map();
17
- availableToTradeByCollateral = /* @__PURE__ */ new Map();
18
- constructor(onStateUpdate) {
19
- this.onStateUpdate = onStateUpdate;
20
- }
21
- loadPerpMetas(allMetas) {
22
- allMetas.forEach((dexMeta) => {
23
- const collateralToken = COLLATERAL_TOKEN_MAP[dexMeta.collateralToken];
24
- dexMeta.universe.forEach((asset) => {
25
- if (!asset.name) return;
26
- if (collateralToken && !this.symbolToCollateralToken.has(asset.name)) {
27
- this.symbolToCollateralToken.set(asset.name, collateralToken);
28
- }
29
- });
30
- });
31
- }
32
- handleClearinghouseState(data) {
33
- const states = (data.clearinghouseStates || []).map(([, s]) => s).filter(Boolean);
34
- const sumStr = (values) => values.reduce((acc, v) => acc + (Number.parseFloat(v || "0") || 0), 0);
35
- const allAssetPositions = states.flatMap((s) => s.assetPositions || []);
36
- const totalUnrealizedPnl = allAssetPositions.reduce(
37
- (acc, ap) => acc + (Number.parseFloat(ap.position.unrealizedPnl || "0") || 0),
38
- 0
39
- );
40
- this.lastSummary = {
41
- accountValue: sumStr(states.map((s) => s.crossMarginSummary.accountValue)),
42
- totalMarginUsed: sumStr(states.map((s) => s.crossMarginSummary.totalMarginUsed)),
43
- totalRawUsd: sumStr(states.map((s) => s.crossMarginSummary.totalRawUsd)),
44
- crossMaintenanceMarginUsed: sumStr(states.map((s) => s.crossMaintenanceMarginUsed)),
45
- withdrawable: sumStr(states.map((s) => s.withdrawable)),
46
- totalUnrealizedPnl
47
- };
48
- const balance = this.buildBalance();
49
- const positions = mapPositions(allAssetPositions);
50
- const leverageSettings = /* @__PURE__ */ new Map();
51
- for (const ap of allAssetPositions) {
52
- const coin = ap.position.coin;
53
- if (coin && ap.position.leverage?.value) {
54
- leverageSettings.set(coin, ap.position.leverage.value.toString());
55
- }
56
- }
57
- this.onStateUpdate({ balance: balance ?? void 0, positions, leverageSettings });
58
- }
59
- handleActiveAssetData(data) {
60
- if (!data.coin) return;
61
- const collateral = this.symbolToCollateralToken.get(data.coin);
62
- if (collateral && data.availableToTrade) {
63
- const long = Number.parseFloat(data.availableToTrade[0] || "0");
64
- const short = Number.parseFloat(data.availableToTrade[1] || "0");
65
- const minAvailable = Math.min(long, short);
66
- this.availableToTradeByCollateral.set(collateral, minAvailable);
67
- const balance = this.buildBalance();
68
- if (balance) this.onStateUpdate({ balance });
69
- }
70
- const trackedAssets = /* @__PURE__ */ new Map();
71
- trackedAssets.set(data.coin, buildTrackedAssetInfo(data));
72
- this.onStateUpdate({ trackedAssets });
73
- }
74
- handleWebData3(data) {
75
- this.accountType = data.userState.abstraction ?? null;
76
- }
77
- handleSpotState(data) {
78
- const balances = data?.spotState?.balances;
79
- if (!balances) return;
80
- for (const b of balances) {
81
- const total = Number.parseFloat(b.total || "0");
82
- const hold = Number.parseFloat(b.hold || "0");
83
- if (total !== 0 || hold !== 0) {
84
- this.spotCoinBalances.set(b.coin, { coin: b.coin, total, hold });
85
- } else {
86
- this.spotCoinBalances.delete(b.coin);
87
- }
88
- }
89
- const balance = this.buildBalance();
90
- if (balance) this.onStateUpdate({ balance });
91
- }
92
- buildBalance() {
93
- return mapBalance(this.lastSummary, this.spotCoinBalances, this.accountType, this.availableToTradeByCollateral);
94
- }
95
- }
96
- function buildTrackedAssetInfo(data) {
97
- return {
98
- coin: data.coin,
99
- leverage: (data.leverage?.value ?? 1).toString(),
100
- marginType: data.leverage?.type
101
- };
102
- }
103
-
104
- export { HyperliquidStateHandler, buildTrackedAssetInfo };
@@ -1,11 +0,0 @@
1
- import { Connector } from '@pear-protocol/types';
2
- import { g as SupportedCredentials } from '../../types-Caz2X4h0.js';
3
- export { f as BinanceAccountType } from '../../types-Caz2X4h0.js';
4
- import { OrchestratorConfig, Orchestrator } from './base.js';
5
- export { BybitAccountType } from './bybit/types.js';
6
- export { HyperliquidAccountType } from './hyperliquid/types.js';
7
- export { OkxAccountType } from './okx/types.js';
8
-
9
- declare function createOrchestrator(exchange: Connector, config: OrchestratorConfig, credentials: SupportedCredentials): Orchestrator;
10
-
11
- export { Orchestrator, OrchestratorConfig, createOrchestrator };
@@ -1,32 +0,0 @@
1
- import './base';
2
- import { BinanceOrchestrator } from './binance/orchestrator';
3
- import { BybitOrchestrator } from './bybit/orchestrator';
4
- import { HyperliquidOrchestrator } from './hyperliquid/orchestrator';
5
- import { OkxOrchestrator } from './okx/orchestrator';
6
- export { BinanceAccountType } from './binance/types';
7
- export { BybitAccountType } from './bybit/types';
8
- export { HyperliquidAccountType } from './hyperliquid/types';
9
- export { OkxAccountType } from './okx/types';
10
-
11
- function createOrchestrator(exchange, config, credentials) {
12
- let orchestrator;
13
- switch (exchange) {
14
- case "binance":
15
- orchestrator = new BinanceOrchestrator(config, credentials);
16
- break;
17
- case "bybit":
18
- orchestrator = new BybitOrchestrator(config, credentials);
19
- break;
20
- case "hyperliquid":
21
- orchestrator = new HyperliquidOrchestrator(config, credentials);
22
- break;
23
- case "okx":
24
- orchestrator = new OkxOrchestrator(config, credentials);
25
- break;
26
- default:
27
- throw new Error(`Unsupported exchange: ${exchange}`);
28
- }
29
- return orchestrator;
30
- }
31
-
32
- export { createOrchestrator };
@@ -1,26 +0,0 @@
1
- import { OkxCredentialsDTO } from '@pear-protocol/types';
2
- import { S as StateUpdate, h as TrackedAssetInfo } from '../../../types-Caz2X4h0.js';
3
- import '../bybit/types.js';
4
- import '../hyperliquid/types.js';
5
- import './types.js';
6
-
7
- type OnStateUpdate = (update: StateUpdate) => void;
8
- type OnError = (error: Error) => void;
9
- declare class OkxAssetTracker {
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: () => OkxCredentialsDTO;
19
- getDemo: () => boolean;
20
- });
21
- getInfos(): Map<string, TrackedAssetInfo>;
22
- onAssetTracked(instId: string): void;
23
- onAssetUntracked(instId: string): void;
24
- }
25
-
26
- export { OkxAssetTracker };
@@ -1,34 +0,0 @@
1
- import { fetchLeverageInfo } from './rest';
2
-
3
- class OkxAssetTracker {
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(instId) {
19
- fetchLeverageInfo(this.getCreds(), instId, this.getDemo()).then((info) => {
20
- const leverage = info?.lever || "0";
21
- const marginType = info?.mgnMode;
22
- const trackedAssetInfo = { coin: instId, leverage, marginType };
23
- this.trackedAssetInfos.set(instId, trackedAssetInfo);
24
- const trackedAssets = /* @__PURE__ */ new Map([[instId, trackedAssetInfo]]);
25
- this.onStateUpdate({ trackedAssets });
26
- }).catch((err) => this.onError(err instanceof Error ? err : new Error(String(err))));
27
- }
28
- onAssetUntracked(instId) {
29
- this.trackedAssetInfos.delete(instId);
30
- this.onStateUpdate({ trackedAssets: new Map(this.trackedAssetInfos) });
31
- }
32
- }
33
-
34
- export { OkxAssetTracker };
@@ -1,7 +0,0 @@
1
- declare const REST_BASE_URL = "https://www.okx.com";
2
- declare const WS_PRIVATE_URL = "wss://ws.okx.com:8443/ws/v5/private";
3
- declare const WS_PRIVATE_URL_DEMO = "wss://wspap.okx.com:8443/ws/v5/private?brokerId=9999";
4
- declare const DEFAULT_PING_INTERVAL_MS = 25000;
5
- declare const LOGIN_TIMEOUT_MS = 10000;
6
-
7
- export { DEFAULT_PING_INTERVAL_MS, LOGIN_TIMEOUT_MS, REST_BASE_URL, WS_PRIVATE_URL, WS_PRIVATE_URL_DEMO };
@@ -1,7 +0,0 @@
1
- const REST_BASE_URL = "https://www.okx.com";
2
- const WS_PRIVATE_URL = "wss://ws.okx.com:8443/ws/v5/private";
3
- const WS_PRIVATE_URL_DEMO = "wss://wspap.okx.com:8443/ws/v5/private?brokerId=9999";
4
- const DEFAULT_PING_INTERVAL_MS = 25e3;
5
- const LOGIN_TIMEOUT_MS = 1e4;
6
-
7
- export { DEFAULT_PING_INTERVAL_MS, LOGIN_TIMEOUT_MS, REST_BASE_URL, WS_PRIVATE_URL, WS_PRIVATE_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 '../bybit/types.js';
5
- import '../hyperliquid/types.js';
6
- import './types.js';
7
-
8
- declare class OkxOrchestrator 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 { OkxOrchestrator };
@@ -1,57 +0,0 @@
1
- import { BaseOrchestrator } from '../base';
2
- import { OkxAssetTracker } from './asset-tracker';
3
- import { acctLvToAccountType } from './mapper';
4
- import { fetchAccountConfig, fetchBalance, fetchPositions } from './rest';
5
- import { OkxStateHandler } from './state-handler';
6
- import { OkxWs } from './ws';
7
-
8
- class OkxOrchestrator 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 OkxAssetTracker({
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 OkxStateHandler({
24
- onStateUpdate: (update) => this.onStateUpdate(update),
25
- getTrackedAssetInfos: () => this.assetTracker.getInfos()
26
- });
27
- }
28
- async initAsync() {
29
- const [config, balance, positions] = await Promise.all([
30
- fetchAccountConfig(this.creds, this.demo),
31
- fetchBalance(this.creds, this.demo),
32
- fetchPositions(this.creds, this.demo)
33
- ]);
34
- this.stateHandler.setAccountType(acctLvToAccountType(config.acctLv));
35
- this.stateHandler.handleInitialData(balance, positions);
36
- this.ws = new OkxWs({ onMessage: (msg) => this.stateHandler.handleMessage(msg) }, this.demo);
37
- await this.ws.connect(this.creds);
38
- }
39
- cleanupConnections() {
40
- this.ws?.close();
41
- }
42
- async stop() {
43
- this.stopped = true;
44
- this.cleanupConnections();
45
- }
46
- get isConnected() {
47
- return this.ws?.isConnected ?? false;
48
- }
49
- onAssetTracked(coin) {
50
- this.assetTracker.onAssetTracked(coin);
51
- }
52
- onAssetUntracked(coin) {
53
- this.assetTracker.onAssetUntracked(coin);
54
- }
55
- }
56
-
57
- export { OkxOrchestrator };