@pear-protocol/exchanges-sdk 0.0.18 → 0.1.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.
Files changed (178) 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 +18 -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 +37 -24
  67. package/dist/index.js +6 -21
  68. package/dist/sdk.d.ts +43 -0
  69. package/dist/sdk.js +52 -0
  70. package/dist/syncer/base.d.ts +33 -0
  71. package/dist/syncer/base.js +14 -0
  72. package/dist/syncer/index.d.ts +18 -0
  73. package/dist/syncer/index.js +1 -0
  74. package/dist/tracker/base.d.ts +79 -0
  75. package/dist/tracker/base.js +120 -0
  76. package/dist/tracker/binance/const.d.ts +4 -0
  77. package/dist/tracker/binance/const.js +4 -0
  78. package/dist/{account/exchanges → tracker}/binance/mapper.d.ts +4 -1
  79. package/dist/{account/exchanges → tracker}/binance/mapper.js +7 -6
  80. package/dist/tracker/binance/tracker.d.ts +60 -0
  81. package/dist/tracker/binance/tracker.js +181 -0
  82. package/dist/tracker/binance/types.d.ts +6 -0
  83. package/dist/{account/exchanges → tracker}/bybit/mapper.d.ts +5 -3
  84. package/dist/{account/exchanges → tracker}/bybit/mapper.js +11 -10
  85. package/dist/tracker/bybit/tracker.d.ts +51 -0
  86. package/dist/tracker/bybit/tracker.js +138 -0
  87. package/dist/tracker/bybit/types.d.ts +7 -0
  88. package/dist/{account/exchanges → tracker}/hyperliquid/mapper.d.ts +6 -4
  89. package/dist/{account/exchanges → tracker}/hyperliquid/mapper.js +18 -16
  90. package/dist/tracker/hyperliquid/tracker.d.ts +62 -0
  91. package/dist/tracker/hyperliquid/tracker.js +219 -0
  92. package/dist/tracker/hyperliquid/types.d.ts +23 -0
  93. package/dist/tracker/index.d.ts +38 -0
  94. package/dist/tracker/index.js +81 -0
  95. package/dist/tracker/lighter/const.d.ts +3 -0
  96. package/dist/tracker/lighter/const.js +3 -0
  97. package/dist/tracker/lighter/mapper.d.ts +16 -0
  98. package/dist/tracker/lighter/mapper.js +77 -0
  99. package/dist/tracker/lighter/tracker.d.ts +53 -0
  100. package/dist/tracker/lighter/tracker.js +150 -0
  101. package/dist/tracker/lighter/types.d.ts +16 -0
  102. package/dist/tracker/lighter/types.js +6 -0
  103. package/dist/{account/exchanges → tracker}/okx/mapper.d.ts +5 -3
  104. package/dist/{account/exchanges → tracker}/okx/mapper.js +11 -10
  105. package/dist/tracker/okx/tracker.d.ts +51 -0
  106. package/dist/tracker/okx/tracker.js +134 -0
  107. package/dist/tracker/okx/types.d.ts +8 -0
  108. package/dist/tracker/types.d.ts +63 -0
  109. package/dist/{account → tracker}/types.js +6 -4
  110. package/dist/tracker/utils.d.ts +11 -0
  111. package/dist/{account → tracker}/utils.js +0 -2
  112. package/dist/types.d.ts +50 -0
  113. package/dist/types.js +1 -0
  114. package/package.json +8 -4
  115. package/dist/account/account-manager.d.ts +0 -31
  116. package/dist/account/account-manager.js +0 -119
  117. package/dist/account/exchange-account.d.ts +0 -35
  118. package/dist/account/exchange-account.js +0 -158
  119. package/dist/account/exchanges/base.d.ts +0 -48
  120. package/dist/account/exchanges/base.js +0 -41
  121. package/dist/account/exchanges/binance/asset-tracker.d.ts +0 -26
  122. package/dist/account/exchanges/binance/asset-tracker.js +0 -38
  123. package/dist/account/exchanges/binance/const.d.ts +0 -10
  124. package/dist/account/exchanges/binance/const.js +0 -10
  125. package/dist/account/exchanges/binance/orchestrator.d.ts +0 -31
  126. package/dist/account/exchanges/binance/orchestrator.js +0 -91
  127. package/dist/account/exchanges/binance/state-handler.d.ts +0 -33
  128. package/dist/account/exchanges/binance/state-handler.js +0 -113
  129. package/dist/account/exchanges/binance/types.d.ts +0 -5
  130. package/dist/account/exchanges/binance/ws.d.ts +0 -23
  131. package/dist/account/exchanges/binance/ws.js +0 -69
  132. package/dist/account/exchanges/bybit/asset-tracker.d.ts +0 -26
  133. package/dist/account/exchanges/bybit/asset-tracker.js +0 -37
  134. package/dist/account/exchanges/bybit/const.d.ts +0 -7
  135. package/dist/account/exchanges/bybit/const.js +0 -7
  136. package/dist/account/exchanges/bybit/orchestrator.d.ts +0 -22
  137. package/dist/account/exchanges/bybit/orchestrator.js +0 -66
  138. package/dist/account/exchanges/bybit/state-handler.d.ts +0 -23
  139. package/dist/account/exchanges/bybit/state-handler.js +0 -70
  140. package/dist/account/exchanges/bybit/ws.d.ts +0 -19
  141. package/dist/account/exchanges/bybit/ws.js +0 -83
  142. package/dist/account/exchanges/hyperliquid/asset-tracker.d.ts +0 -35
  143. package/dist/account/exchanges/hyperliquid/asset-tracker.js +0 -83
  144. package/dist/account/exchanges/hyperliquid/orchestrator.d.ts +0 -26
  145. package/dist/account/exchanges/hyperliquid/orchestrator.js +0 -102
  146. package/dist/account/exchanges/hyperliquid/state-handler.d.ts +0 -25
  147. package/dist/account/exchanges/hyperliquid/state-handler.js +0 -104
  148. package/dist/account/exchanges/index.d.ts +0 -11
  149. package/dist/account/exchanges/index.js +0 -32
  150. package/dist/account/exchanges/okx/asset-tracker.d.ts +0 -26
  151. package/dist/account/exchanges/okx/asset-tracker.js +0 -34
  152. package/dist/account/exchanges/okx/const.d.ts +0 -7
  153. package/dist/account/exchanges/okx/const.js +0 -7
  154. package/dist/account/exchanges/okx/orchestrator.d.ts +0 -22
  155. package/dist/account/exchanges/okx/orchestrator.js +0 -57
  156. package/dist/account/exchanges/okx/state-handler.d.ts +0 -24
  157. package/dist/account/exchanges/okx/state-handler.js +0 -78
  158. package/dist/account/exchanges/okx/ws.d.ts +0 -20
  159. package/dist/account/exchanges/okx/ws.js +0 -103
  160. package/dist/account/index.d.ts +0 -11
  161. package/dist/account/index.js +0 -4
  162. package/dist/account/types.d.ts +0 -5
  163. package/dist/account/utils.d.ts +0 -10
  164. package/dist/credentials.d.ts +0 -28
  165. package/dist/credentials.js +0 -46
  166. package/dist/types-Caz2X4h0.d.ts +0 -151
  167. package/dist/utils/ws.d.ts +0 -10
  168. package/dist/utils/ws.js +0 -42
  169. /package/dist/{account/exchanges → client}/bybit/rest.d.ts +0 -0
  170. /package/dist/{utils → client}/hmac.d.ts +0 -0
  171. /package/dist/{utils → client}/hmac.js +0 -0
  172. /package/dist/{account/exchanges → client}/okx/rest.d.ts +0 -0
  173. /package/dist/{account/exchanges → tracker}/binance/types.js +0 -0
  174. /package/dist/{account/exchanges → tracker}/bybit/types.js +0 -0
  175. /package/dist/{account/exchanges → tracker}/hyperliquid/const.d.ts +0 -0
  176. /package/dist/{account/exchanges → tracker}/hyperliquid/const.js +0 -0
  177. /package/dist/{account/exchanges → tracker}/hyperliquid/types.js +0 -0
  178. /package/dist/{account/exchanges → tracker}/okx/types.js +0 -0
@@ -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 };
@@ -1,24 +0,0 @@
1
- import { S as StateUpdate, h as TrackedAssetInfo } from '../../../types-Caz2X4h0.js';
2
- import { OkxAccountType, OkxRawBalance, OkxRawPosition, OkxWsMessage } from './types.js';
3
- import '@pear-protocol/types';
4
- import '../bybit/types.js';
5
- import '../hyperliquid/types.js';
6
-
7
- type OnStateUpdate = (update: StateUpdate) => void;
8
- declare class OkxStateHandler {
9
- private onStateUpdate;
10
- private getTrackedAssetInfos;
11
- private rawCoins;
12
- private lastBalance;
13
- private accountType;
14
- constructor(deps: {
15
- onStateUpdate: OnStateUpdate;
16
- getTrackedAssetInfos: () => Map<string, TrackedAssetInfo>;
17
- });
18
- setAccountType(type: OkxAccountType | null): void;
19
- handleInitialData(balance: OkxRawBalance, positions: OkxRawPosition[]): void;
20
- handleMessage(msg: OkxWsMessage): void;
21
- private handleBalanceUpdate;
22
- }
23
-
24
- export { OkxStateHandler };
@@ -1,78 +0,0 @@
1
- import { mergeRawCoins, buildBalance, mapPositions, mapPositionUpdates } from './mapper';
2
-
3
- class OkxStateHandler {
4
- onStateUpdate;
5
- getTrackedAssetInfos;
6
- rawCoins = /* @__PURE__ */ new Map();
7
- lastBalance = null;
8
- accountType = null;
9
- constructor(deps) {
10
- this.onStateUpdate = deps.onStateUpdate;
11
- this.getTrackedAssetInfos = deps.getTrackedAssetInfos;
12
- }
13
- setAccountType(type) {
14
- this.accountType = type;
15
- }
16
- handleInitialData(balance, positions) {
17
- this.lastBalance = balance;
18
- this.rawCoins = mergeRawCoins(/* @__PURE__ */ new Map(), balance.details ?? []);
19
- const built = buildBalance(balance, this.rawCoins, this.accountType);
20
- if (built) this.onStateUpdate({ balance: built });
21
- this.onStateUpdate({ positions: mapPositions(positions) });
22
- }
23
- handleMessage(msg) {
24
- if (msg.arg.channel === "account") {
25
- const raw = msg.data[0];
26
- if (raw) this.handleBalanceUpdate(raw);
27
- return;
28
- }
29
- if (msg.arg.channel === "positions") {
30
- const posMsg = msg;
31
- const action = posMsg.action ?? "update";
32
- const rawPositions = posMsg.data;
33
- const positions = action === "snapshot" ? mapPositions(rawPositions) : mapPositionUpdates(rawPositions);
34
- if (positions.size === 0 && action !== "snapshot") return;
35
- const leverageSettings = /* @__PURE__ */ new Map();
36
- for (const p of rawPositions) {
37
- if (p.instId && p.lever) {
38
- leverageSettings.set(p.instId, p.lever);
39
- }
40
- }
41
- const trackedAssetInfos = this.getTrackedAssetInfos();
42
- const trackedAssets = /* @__PURE__ */ new Map();
43
- for (const p of rawPositions) {
44
- const existing = trackedAssetInfos.get(p.instId);
45
- if (existing) {
46
- const leverage = p.lever || existing.leverage;
47
- const marginType = p.mgnMode;
48
- const updated = { ...existing, leverage, marginType };
49
- trackedAssetInfos.set(p.instId, updated);
50
- trackedAssets.set(p.instId, updated);
51
- }
52
- }
53
- this.onStateUpdate({
54
- positions,
55
- leverageSettings,
56
- ...trackedAssets.size > 0 ? { trackedAssets } : {}
57
- });
58
- }
59
- }
60
- handleBalanceUpdate(raw) {
61
- this.rawCoins = mergeRawCoins(this.rawCoins, raw.details ?? []);
62
- const merged = this.lastBalance ? {
63
- totalEq: raw.totalEq || this.lastBalance.totalEq,
64
- adjEq: raw.adjEq || this.lastBalance.adjEq,
65
- availEq: raw.availEq || this.lastBalance.availEq,
66
- mmr: raw.mmr || this.lastBalance.mmr,
67
- imr: raw.imr || this.lastBalance.imr,
68
- upl: raw.upl || this.lastBalance.upl,
69
- mgnRatio: raw.mgnRatio || this.lastBalance.mgnRatio,
70
- details: raw.details ?? this.lastBalance.details
71
- } : raw;
72
- this.lastBalance = merged;
73
- const built = buildBalance(merged, this.rawCoins, this.accountType);
74
- if (built) this.onStateUpdate({ balance: built });
75
- }
76
- }
77
-
78
- export { OkxStateHandler };
@@ -1,20 +0,0 @@
1
- import { OkxCredentialsDTO } from '@pear-protocol/types';
2
- import { OkxWsHandlers } from './types.js';
3
-
4
- declare class OkxWs {
5
- private ws;
6
- private pingTimer;
7
- private handlers;
8
- private demo;
9
- private creds;
10
- private readonly pingIntervalMs;
11
- constructor(handlers: OkxWsHandlers, demo: boolean, pingIntervalMs?: number);
12
- connect(creds: OkxCredentialsDTO): Promise<void>;
13
- close(): void;
14
- get isConnected(): boolean;
15
- private clearPing;
16
- private subscribe;
17
- private login;
18
- }
19
-
20
- export { OkxWs };
@@ -1,103 +0,0 @@
1
- import { hmacSha256Base64 } from '../../../utils/hmac';
2
- import { createWs, waitForOpen, sendWs, ReconnectingWebSocket, clearIntervalTimer } from '../../../utils/ws';
3
- import { DEFAULT_PING_INTERVAL_MS, WS_PRIVATE_URL_DEMO, WS_PRIVATE_URL, LOGIN_TIMEOUT_MS } from './const';
4
-
5
- class OkxWs {
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_PRIVATE_URL_DEMO : WS_PRIVATE_URL);
20
- this.ws.addEventListener("message", (event) => {
21
- const data = event.data;
22
- if (typeof data !== "string" || data === "pong") return;
23
- try {
24
- const msg = JSON.parse(data);
25
- if (msg.event) return;
26
- if (msg.arg && msg.data) {
27
- this.handlers.onMessage(msg);
28
- }
29
- } catch {
30
- }
31
- });
32
- let initialOpen = true;
33
- this.ws.addEventListener("open", () => {
34
- if (initialOpen) {
35
- initialOpen = false;
36
- return;
37
- }
38
- if (!this.creds) return;
39
- this.login(this.creds).then(() => this.subscribe()).catch(() => {
40
- });
41
- });
42
- await waitForOpen(this.ws);
43
- await this.login(creds);
44
- this.subscribe();
45
- this.pingTimer = setInterval(() => sendWs(this.ws, "ping"), this.pingIntervalMs);
46
- }
47
- close() {
48
- this.clearPing();
49
- this.creds = null;
50
- if (this.ws) {
51
- this.ws.close();
52
- this.ws = null;
53
- }
54
- }
55
- get isConnected() {
56
- return this.ws?.readyState === ReconnectingWebSocket.OPEN;
57
- }
58
- clearPing() {
59
- this.pingTimer = clearIntervalTimer(this.pingTimer);
60
- }
61
- subscribe() {
62
- sendWs(this.ws, {
63
- op: "subscribe",
64
- args: [{ channel: "account" }, { channel: "positions", instType: "SWAP" }]
65
- });
66
- }
67
- async login(creds) {
68
- const timestamp = (Date.now() / 1e3).toString();
69
- const sign = await hmacSha256Base64(creds.api_secret, `${timestamp}GET/users/self/verify`);
70
- return new Promise((resolve, reject) => {
71
- const timeout = setTimeout(() => {
72
- this.ws?.removeEventListener("message", handler);
73
- reject(new Error("OKX login timeout"));
74
- }, LOGIN_TIMEOUT_MS);
75
- const handler = (event) => {
76
- try {
77
- const msg = JSON.parse(event.data);
78
- if (msg.event === "login") {
79
- clearTimeout(timeout);
80
- this.ws?.removeEventListener("message", handler);
81
- if (msg.code === "0") resolve();
82
- else reject(new Error(`OKX login failed: ${msg.msg ?? "unknown"}`));
83
- }
84
- } catch {
85
- }
86
- };
87
- this.ws?.addEventListener("message", handler);
88
- sendWs(this.ws, {
89
- op: "login",
90
- args: [
91
- {
92
- apiKey: creds.api_key,
93
- passphrase: creds.api_pass,
94
- timestamp,
95
- sign
96
- }
97
- ]
98
- });
99
- });
100
- }
101
- }
102
-
103
- export { OkxWs };
@@ -1,11 +0,0 @@
1
- export { AccountManager } from './account-manager.js';
2
- export { ExchangeAccount } from './exchange-account.js';
3
- export { createOrchestrator } from './exchanges/index.js';
4
- export { A as ACCOUNT_TYPE_LABELS, a as AccountBalance, b as AccountConfig, c as AccountPosition, d as AccountType, e as AssetBalance, B as BalanceCallback, f as BinanceAccountType, E as ExchangeState, H as HyperliquidAccountCredentials, M as MarginMode, P as PositionsCallback, S as StateUpdate, g as SupportedCredentials, T as TrackedAssetCallback, h as TrackedAssetInfo, i as Tracker } from '../types-Caz2X4h0.js';
5
- export { BybitAccountType } from './exchanges/bybit/types.js';
6
- export { HyperliquidAccountType } from './exchanges/hyperliquid/types.js';
7
- export { OkxAccountType } from './exchanges/okx/types.js';
8
- export { Orchestrator, OrchestratorConfig } from './exchanges/base.js';
9
- import '@pear-protocol/core-sdk';
10
- import '@pear-protocol/types';
11
- import '../credentials.js';
@@ -1,4 +0,0 @@
1
- export * from './account-manager';
2
- export * from './exchange-account';
3
- export * from './exchanges';
4
- export * from './types';
@@ -1,5 +0,0 @@
1
- import '@pear-protocol/types';
2
- export { A as ACCOUNT_TYPE_LABELS, a as AccountBalance, b as AccountConfig, c as AccountPosition, d as AccountType, e as AssetBalance, B as BalanceCallback, E as ExchangeState, H as HyperliquidAccountCredentials, M as MarginMode, P as PositionsCallback, S as StateUpdate, g as SupportedCredentials, T as TrackedAssetCallback, h as TrackedAssetInfo, i as Tracker } from '../types-Caz2X4h0.js';
3
- import './exchanges/bybit/types.js';
4
- import './exchanges/hyperliquid/types.js';
5
- import './exchanges/okx/types.js';
@@ -1,10 +0,0 @@
1
- import { E as ExchangeState } from '../types-Caz2X4h0.js';
2
- import '@pear-protocol/types';
3
- import './exchanges/bybit/types.js';
4
- import './exchanges/hyperliquid/types.js';
5
- import './exchanges/okx/types.js';
6
-
7
- declare function createEmptyState(): ExchangeState;
8
- declare function positionKey(symbol: string, side: string): string;
9
-
10
- export { createEmptyState, positionKey };
@@ -1,28 +0,0 @@
1
- import PearSDK from '@pear-protocol/core-sdk';
2
- import { Connector, ConnectorCredentialsDTO } from '@pear-protocol/types';
3
-
4
- declare class Credentials {
5
- private sdk;
6
- private cache;
7
- constructor(sdk: PearSDK);
8
- /**
9
- * Fetch exchange credentials from the backend and cache them in memory.
10
- */
11
- fetch<T extends Connector>(tradeAccountId: string): Promise<ConnectorCredentialsDTO[T]>;
12
- /**
13
- * Get previously fetched credentials from the in-memory cache.
14
- * Returns null if not yet fetched in this SDK instance.
15
- */
16
- get<T extends Connector>(tradeAccountId: string): ConnectorCredentialsDTO[T] | null;
17
- /**
18
- * Get credentials from in-memory cache or fetch from the backend if not available.
19
- */
20
- getOrFetch<T extends Connector>(tradeAccountId: string): Promise<ConnectorCredentialsDTO[T]>;
21
- /**
22
- * Clear in-memory credentials for a specific trade account,
23
- * or all cached credentials if no ID is provided.
24
- */
25
- clear(tradeAccountId?: string): void;
26
- }
27
-
28
- export { Credentials };
@@ -1,46 +0,0 @@
1
- class Credentials {
2
- sdk;
3
- cache = /* @__PURE__ */ new Map();
4
- constructor(sdk) {
5
- this.sdk = sdk;
6
- }
7
- /**
8
- * Fetch exchange credentials from the backend and cache them in memory.
9
- */
10
- async fetch(tradeAccountId) {
11
- const response = await this.sdk.core.accounts.credentials(tradeAccountId);
12
- const credentials = response.credentials;
13
- this.cache.set(tradeAccountId, { credentials, connector: response.connector });
14
- return credentials;
15
- }
16
- /**
17
- * Get previously fetched credentials from the in-memory cache.
18
- * Returns null if not yet fetched in this SDK instance.
19
- */
20
- get(tradeAccountId) {
21
- const cached = this.cache.get(tradeAccountId);
22
- if (!cached) return null;
23
- return cached.credentials;
24
- }
25
- /**
26
- * Get credentials from in-memory cache or fetch from the backend if not available.
27
- */
28
- async getOrFetch(tradeAccountId) {
29
- const cached = this.get(tradeAccountId);
30
- if (cached) return cached;
31
- return await this.fetch(tradeAccountId);
32
- }
33
- /**
34
- * Clear in-memory credentials for a specific trade account,
35
- * or all cached credentials if no ID is provided.
36
- */
37
- clear(tradeAccountId) {
38
- if (tradeAccountId) {
39
- this.cache.delete(tradeAccountId);
40
- } else {
41
- this.cache.clear();
42
- }
43
- }
44
- }
45
-
46
- export { Credentials };
@@ -1,151 +0,0 @@
1
- import { Connector, BinanceCredentialsDTO, BybitCredentialsDTO, HyperliquidCredentialsDTO, OkxCredentialsDTO } from '@pear-protocol/types';
2
- import { BybitAccountType } from './account/exchanges/bybit/types.js';
3
- import { HyperliquidAccountType } from './account/exchanges/hyperliquid/types.js';
4
- import { OkxAccountType } from './account/exchanges/okx/types.js';
5
-
6
- declare enum BinanceAccountType {
7
- MULTI_ASSET = "MULTI_ASSET",
8
- SINGLE_ASSET = "SINGLE_ASSET"
9
- }
10
- interface BinanceRawPositionRisk {
11
- symbol: string;
12
- leverage: string;
13
- marginType: MarginMode;
14
- isAutoAddMargin: string;
15
- isolatedMargin: string;
16
- positionAmt: string;
17
- entryPrice: string;
18
- breakEvenPrice: string;
19
- unRealizedProfit: string;
20
- liquidationPrice: string;
21
- markPrice: string;
22
- maxNotionalValue: string;
23
- positionSide: string;
24
- notional: string;
25
- isolatedWallet: string;
26
- updateTime: number;
27
- }
28
- interface BinanceRawAsset {
29
- asset: string;
30
- walletBalance: string;
31
- availableBalance: string;
32
- unrealizedProfit: string;
33
- marginBalance: string;
34
- initialMargin: string;
35
- maintMargin: string;
36
- marginAvailable: boolean;
37
- }
38
- interface BinanceRawAccountStatus {
39
- totalWalletBalance: string;
40
- totalMarginBalance: string;
41
- availableBalance: string;
42
- totalUnrealizedProfit: string;
43
- totalInitialMargin: string;
44
- totalMaintMargin: string;
45
- assets?: BinanceRawAsset[];
46
- }
47
- interface BinanceMultiAssetsMarginResponse {
48
- multiAssetsMargin: boolean;
49
- }
50
- interface BinanceAssetIndex {
51
- symbol: string;
52
- time: number;
53
- index: string;
54
- bidBuffer: string;
55
- askBuffer: string;
56
- bidRate: string;
57
- askRate: string;
58
- autoExchangeBidBuffer: string;
59
- autoExchangeAskBuffer: string;
60
- autoExchangeBidRate: string;
61
- autoExchangeAskRate: string;
62
- }
63
- interface BinanceRawUserDataEvent {
64
- e?: string;
65
- ac?: {
66
- s: string;
67
- l: string;
68
- };
69
- a?: {
70
- B?: unknown[];
71
- P?: unknown[];
72
- };
73
- }
74
- interface BinanceWsHandlers {
75
- onUserDataEvent: (event: unknown) => void;
76
- onWsApiResponse: (id: number, result: unknown) => void;
77
- }
78
-
79
- type MarginMode = 'cross' | 'isolated';
80
- type AccountType = BinanceAccountType | BybitAccountType | HyperliquidAccountType | OkxAccountType;
81
- declare const ACCOUNT_TYPE_LABELS: Record<AccountType, string>;
82
- interface AssetBalance {
83
- asset: string;
84
- free: string;
85
- used: string;
86
- total: string;
87
- walletBalance: string;
88
- usdValue: string;
89
- }
90
- interface AccountBalance {
91
- accountType: AccountType;
92
- free: Record<string, string>;
93
- used: Record<string, string>;
94
- total: Record<string, string>;
95
- availableToTrade: string;
96
- totalEquity: string;
97
- walletBalance: string;
98
- unrealizedPnl: string;
99
- initialMarginUsed: string;
100
- maintenanceMargin: string;
101
- marginRatio: string;
102
- assets: AssetBalance[];
103
- timestamp: number;
104
- }
105
- interface AccountPosition {
106
- symbol: string;
107
- side: 'long' | 'short' | 'both';
108
- size: string;
109
- entryPrice: string;
110
- unrealizedPnl: string;
111
- leverage: string;
112
- marginType: MarginMode;
113
- liquidationPrice: string | null;
114
- }
115
- interface TrackedAssetInfo {
116
- coin: string;
117
- leverage: string;
118
- marginType: MarginMode;
119
- }
120
- interface ExchangeState {
121
- balance: AccountBalance | null;
122
- positions: Map<string, AccountPosition>;
123
- leverageSettings: Map<string, string>;
124
- trackedAssets: Map<string, TrackedAssetInfo>;
125
- lastUpdated: number;
126
- initialized: boolean;
127
- }
128
- type BalanceCallback = (balance: AccountBalance) => void;
129
- type PositionsCallback = (positions: AccountPosition[]) => void;
130
- type TrackedAssetCallback = (info: TrackedAssetInfo) => void;
131
- interface Tracker<T> {
132
- get(): T;
133
- untrack(): void;
134
- }
135
- interface AccountConfig {
136
- exchange: Connector;
137
- demo?: boolean;
138
- onAuthError?: () => void;
139
- }
140
- type HyperliquidAccountCredentials = HyperliquidCredentialsDTO & {
141
- address: string;
142
- };
143
- type SupportedCredentials = BinanceCredentialsDTO | BybitCredentialsDTO | HyperliquidAccountCredentials | OkxCredentialsDTO;
144
- interface StateUpdate {
145
- balance?: AccountBalance;
146
- positions?: Map<string, AccountPosition>;
147
- leverageSettings?: Map<string, string>;
148
- trackedAssets?: Map<string, TrackedAssetInfo>;
149
- }
150
-
151
- export { ACCOUNT_TYPE_LABELS as A, type BalanceCallback as B, type ExchangeState as E, type HyperliquidAccountCredentials as H, type MarginMode as M, type PositionsCallback as P, type StateUpdate as S, type TrackedAssetCallback as T, type AccountBalance as a, type AccountConfig as b, type AccountPosition as c, type AccountType as d, type AssetBalance as e, BinanceAccountType as f, type SupportedCredentials as g, type TrackedAssetInfo as h, type Tracker as i, type BinanceRawAccountStatus as j, type BinanceRawPositionRisk as k, type BinanceAssetIndex as l, type BinanceRawUserDataEvent as m, type BinanceWsHandlers as n, type BinanceMultiAssetsMarginResponse as o, type BinanceRawAsset as p };
@@ -1,10 +0,0 @@
1
- import { WebSocket } from 'partysocket';
2
- export { WebSocket as ReconnectingWebSocket } from 'partysocket';
3
-
4
- declare function createWs(url: string): WebSocket;
5
- declare function waitForOpen(ws: WebSocket): Promise<void>;
6
- declare function sendWs(ws: WebSocket | null, msg: Record<string, unknown> | string): void;
7
- declare function clearTimer(timer: ReturnType<typeof setTimeout> | null): null;
8
- declare function clearIntervalTimer(timer: ReturnType<typeof setInterval> | null): null;
9
-
10
- export { clearIntervalTimer, clearTimer, createWs, sendWs, waitForOpen };