@pear-protocol/exchanges-sdk 0.1.7 → 0.1.9

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 (90) hide show
  1. package/README.md +63 -2
  2. package/dist/client/lighter/rest.js +18 -1
  3. package/dist/helper/factory.js +6 -5
  4. package/dist/helper/hyperliquid/helper.js +23 -0
  5. package/dist/helper/hyperliquid/index.js +1 -0
  6. package/dist/helper/hyperliquid/types.js +1 -0
  7. package/dist/helper/index.js +1 -0
  8. package/dist/index.d.ts +817 -49
  9. package/dist/sdk.js +33 -20
  10. package/dist/status/base.js +10 -0
  11. package/dist/status/binance/status.js +14 -0
  12. package/dist/status/bybit/status.js +14 -0
  13. package/dist/status/factory.js +26 -0
  14. package/dist/status/hyperliquid/status.js +37 -0
  15. package/dist/status/index.js +7 -0
  16. package/dist/status/lighter/status.js +33 -0
  17. package/dist/status/okx/status.js +14 -0
  18. package/dist/syncer/base.js +2 -0
  19. package/dist/syncer/hyperliquid/syncer.js +1 -2
  20. package/dist/syncer/lighter/syncer.js +1 -1
  21. package/dist/tracker/hyperliquid/tracker.js +1 -2
  22. package/dist/tracker/index.js +2 -0
  23. package/dist/tracker/lighter/tracker.js +1 -2
  24. package/package.json +4 -4
  25. package/dist/client/base-ws.d.ts +0 -30
  26. package/dist/client/binance/exchange-ws.d.ts +0 -19
  27. package/dist/client/binance/rest.d.ts +0 -13
  28. package/dist/client/binance/types.d.ts +0 -103
  29. package/dist/client/binance/ws.d.ts +0 -30
  30. package/dist/client/bybit/exchange-ws.d.ts +0 -20
  31. package/dist/client/bybit/rest.d.ts +0 -11
  32. package/dist/client/bybit/types.d.ts +0 -75
  33. package/dist/client/bybit/ws.d.ts +0 -17
  34. package/dist/client/exchange-ws.d.ts +0 -12
  35. package/dist/client/hmac.d.ts +0 -4
  36. package/dist/client/hyperliquid/exchange-ws.d.ts +0 -15
  37. package/dist/client/hyperliquid/rest.d.ts +0 -3
  38. package/dist/client/hyperliquid/types.d.ts +0 -188
  39. package/dist/client/hyperliquid/ws.d.ts +0 -10
  40. package/dist/client/index.d.ts +0 -31
  41. package/dist/client/lighter/constants.d.ts +0 -4
  42. package/dist/client/lighter/exchange-ws.d.ts +0 -15
  43. package/dist/client/lighter/rest.d.ts +0 -59
  44. package/dist/client/lighter/types.d.ts +0 -53
  45. package/dist/client/lighter/ws.d.ts +0 -10
  46. package/dist/client/okx/exchange-ws.d.ts +0 -20
  47. package/dist/client/okx/rest.d.ts +0 -10
  48. package/dist/client/okx/types.d.ts +0 -77
  49. package/dist/client/okx/ws.d.ts +0 -17
  50. package/dist/helper/factory.d.ts +0 -11
  51. package/dist/helper/index.d.ts +0 -8
  52. package/dist/helper/lighter/ethereum/abi.d.ts +0 -66
  53. package/dist/helper/lighter/ethereum/index.d.ts +0 -8
  54. package/dist/helper/lighter/ethereum/types.d.ts +0 -23
  55. package/dist/helper/lighter/helper.d.ts +0 -10
  56. package/dist/helper/lighter/index.d.ts +0 -7
  57. package/dist/helper/lighter/types.d.ts +0 -64
  58. package/dist/helper/types.d.ts +0 -12
  59. package/dist/sdk.d.ts +0 -44
  60. package/dist/syncer/base.d.ts +0 -47
  61. package/dist/syncer/binance/syncer.d.ts +0 -31
  62. package/dist/syncer/bybit/syncer.d.ts +0 -28
  63. package/dist/syncer/factory.d.ts +0 -22
  64. package/dist/syncer/hyperliquid/syncer.d.ts +0 -28
  65. package/dist/syncer/index.d.ts +0 -29
  66. package/dist/syncer/lighter/syncer.d.ts +0 -37
  67. package/dist/syncer/okx/syncer.d.ts +0 -29
  68. package/dist/tracker/base.d.ts +0 -79
  69. package/dist/tracker/binance/const.d.ts +0 -4
  70. package/dist/tracker/binance/mapper.d.ts +0 -14
  71. package/dist/tracker/binance/tracker.d.ts +0 -60
  72. package/dist/tracker/binance/types.d.ts +0 -6
  73. package/dist/tracker/bybit/mapper.d.ts +0 -15
  74. package/dist/tracker/bybit/tracker.d.ts +0 -51
  75. package/dist/tracker/bybit/types.d.ts +0 -7
  76. package/dist/tracker/hyperliquid/const.d.ts +0 -3
  77. package/dist/tracker/hyperliquid/mapper.d.ts +0 -12
  78. package/dist/tracker/hyperliquid/tracker.d.ts +0 -62
  79. package/dist/tracker/hyperliquid/types.d.ts +0 -23
  80. package/dist/tracker/index.d.ts +0 -38
  81. package/dist/tracker/lighter/const.d.ts +0 -3
  82. package/dist/tracker/lighter/mapper.d.ts +0 -16
  83. package/dist/tracker/lighter/tracker.d.ts +0 -53
  84. package/dist/tracker/lighter/types.d.ts +0 -16
  85. package/dist/tracker/okx/mapper.d.ts +0 -16
  86. package/dist/tracker/okx/tracker.d.ts +0 -51
  87. package/dist/tracker/okx/types.d.ts +0 -8
  88. package/dist/tracker/types.d.ts +0 -63
  89. package/dist/tracker/utils.d.ts +0 -11
  90. package/dist/types.d.ts +0 -50
package/dist/index.d.ts CHANGED
@@ -1,49 +1,817 @@
1
- export { CreateExchangeWsConfig, ExchangeWsByConnector, createExchangeWs } from './client/index.js';
2
- export { createLighterHelper } from './helper/lighter/helper.js';
3
- export { ASSET_DECIMALS, ApproveIntegratorParams, ApproveIntegratorResult, AssetId, ConnectWalletParams, ConnectWalletResult, DepositParams, LighterHelper, LighterLookupResult, RouteType, WithdrawParams, getAssetDecimals } from './helper/lighter/types.js';
4
- export { ExchangeHelperExchange, ExchangeHelperMap } from './helper/types.js';
5
- export { ExchangesSDK } from './sdk.js';
6
- export { AccountSyncer, AccountSyncerConfig, BaseAccountSyncer, ListExternalFillsParams } from './syncer/base.js';
7
- export { BinanceAccountSyncer } from './syncer/binance/syncer.js';
8
- export { BybitAccountSyncer } from './syncer/bybit/syncer.js';
9
- export { createAccountSyncer } from './syncer/factory.js';
10
- export { HyperliquidAccountSyncer } from './syncer/hyperliquid/syncer.js';
11
- export { LighterAccountSyncer } from './syncer/lighter/syncer.js';
12
- export { OkxAccountSyncer } from './syncer/okx/syncer.js';
13
- export { CreateAccountTrackerConfig, createAccountTracker } from './tracker/index.js';
14
- export { ExchangeConnection, ExchangesSDKConfig, HyperliquidAccountCredentials, LighterAccountCredentials, SupportedCredentials } from './types.js';
15
- export { ACCOUNT_TYPE_LABELS, AccountBalance, AccountPosition, AccountType, AssetBalance, BalanceCallback, ExchangeState, MarginMode, PositionsCallback, StateUpdate, TrackedAssetCallback, TrackedAssetInfo } from './tracker/types.js';
16
- export { AccountTracker, AccountTrackerConfig, BaseAccountTracker } from './tracker/base.js';
17
- export { ApiKey } from './client/lighter/rest.js';
18
- export { BaseWs, MessageListener, OpenListener, WsMessage } from './client/base-ws.js';
19
- export { BinanceAccountTracker, BinanceAccountTrackerConfig } from './tracker/binance/tracker.js';
20
- export { BinanceAccountType } from './tracker/binance/types.js';
21
- export { BinanceExchangeWs } from './client/binance/exchange-ws.js';
22
- export { BinanceUserDataWs, BinanceUserDataWsConfig, BinanceWsApi, BinanceWsApiConfig } from './client/binance/ws.js';
23
- export { BybitAccountTracker, BybitAccountTrackerConfig } from './tracker/bybit/tracker.js';
24
- export { BybitAccountType } from './tracker/bybit/types.js';
25
- export { BybitExchangeWs } from './client/bybit/exchange-ws.js';
26
- export { BybitWs, BybitWsConfig } from './client/bybit/ws.js';
27
- export { ExchangeWs } from './client/exchange-ws.js';
28
- export { HyperliquidAccountTracker, HyperliquidAccountTrackerConfig } from './tracker/hyperliquid/tracker.js';
29
- export { HyperliquidAccountType } from './tracker/hyperliquid/types.js';
30
- export { HyperliquidExchangeWs } from './client/hyperliquid/exchange-ws.js';
31
- export { HyperliquidWs } from './client/hyperliquid/ws.js';
32
- export { LighterAccountTracker, LighterAccountTrackerConfig } from './tracker/lighter/tracker.js';
33
- export { LighterAccountType } from './tracker/lighter/types.js';
34
- export { LighterExchangeWs } from './client/lighter/exchange-ws.js';
35
- export { LighterWs } from './client/lighter/ws.js';
36
- export { OkxAccountTracker, OkxAccountTrackerConfig } from './tracker/okx/tracker.js';
37
- export { OkxAccountType } from './tracker/okx/types.js';
38
- export { OkxExchangeWs } from './client/okx/exchange-ws.js';
39
- export { OkxWs, OkxWsConfig } from './client/okx/ws.js';
40
- export { WalletClientWithAccount } from './helper/lighter/ethereum/types.js';
41
- import '@pear-protocol/types';
42
- import '@pear-protocol/core-sdk';
43
- import './client/binance/types.js';
44
- import './client/bybit/types.js';
45
- import './client/hyperliquid/types.js';
46
- import './client/lighter/types.js';
47
- import './client/okx/types.js';
48
- import 'partysocket';
49
- import 'viem';
1
+ import { HttpTypes, Connector, BinanceCredentialsDTO, BybitCredentialsDTO, OkxCredentialsDTO, TradeAccount } from '@pear-protocol/types';
2
+ import PearSDK from '@pear-protocol/core-sdk';
3
+ import { WebSocket } from 'partysocket';
4
+ import { WalletClient, Transport, Chain, Account } from 'viem';
5
+
6
+ type SupportedCredentials = HttpTypes.ExchangeCredentialsResponse['credentials'];
7
+ type ExchangesSDKConfig = {
8
+ sdk: PearSDK;
9
+ demo?: boolean;
10
+ options?: ConnectOptions;
11
+ };
12
+ type AccountHealthStatus = 'active' | 'warning';
13
+ type AccountWarningReason = 'lighter_api_key_invalid' | 'lighter_integrator_not_approved' | 'hyperliquid_api_wallet_invalid' | 'hyperliquid_builder_fee_not_approved' | 'api_key_invalid';
14
+ type AccountStatus = {
15
+ status: AccountHealthStatus;
16
+ reasons: AccountWarningReason[];
17
+ };
18
+ type ConnectOptions = {
19
+ builderAddress?: `0x${string}`;
20
+ integratorAccountIndex?: number;
21
+ };
22
+ type ExchangeConnection = HttpTypes.ExchangeCredentialsResponse & {
23
+ ws: ExchangeWsByConnector[Connector];
24
+ };
25
+
26
+ interface WsMessage {
27
+ [key: string]: unknown;
28
+ }
29
+ type MessageListener = (data: string) => void;
30
+ type OpenListener = () => void;
31
+ declare abstract class BaseWs {
32
+ protected ws: WebSocket | null;
33
+ protected pingTimer: ReturnType<typeof setInterval> | null;
34
+ private messageListeners;
35
+ private openListeners;
36
+ private nextListenerId;
37
+ protected abstract getWsUrl(): string;
38
+ protected abstract getPingIntervalMs(): number;
39
+ protected abstract buildPingMessage(): WsMessage | string | null;
40
+ connect(): Promise<void>;
41
+ disconnect(): void;
42
+ get connected(): boolean;
43
+ send(msg: WsMessage | string): void;
44
+ addMessageListener(listener: MessageListener): string;
45
+ removeMessageListener(id: string): void;
46
+ addOpenListener(listener: OpenListener): string;
47
+ removeOpenListener(id: string): void;
48
+ destroy(): void;
49
+ private startPing;
50
+ private stopPing;
51
+ }
52
+
53
+ declare abstract class ExchangeWs {
54
+ abstract readonly exchange: Connector;
55
+ protected abstract connections(): BaseWs[];
56
+ destroy(): void;
57
+ get isConnected(): boolean;
58
+ }
59
+
60
+ type BinanceUserDataWsConfig = {
61
+ listenKey: string;
62
+ demo: boolean;
63
+ };
64
+ declare class BinanceUserDataWs extends BaseWs {
65
+ private readonly listenKey;
66
+ private readonly demo;
67
+ constructor(config: BinanceUserDataWsConfig);
68
+ protected getWsUrl(): string;
69
+ protected getPingIntervalMs(): number;
70
+ protected buildPingMessage(): null;
71
+ }
72
+ type BinanceWsApiConfig = {
73
+ demo: boolean;
74
+ };
75
+ declare class BinanceWsApi extends BaseWs {
76
+ private readonly demo;
77
+ private requestId;
78
+ constructor(config: BinanceWsApiConfig);
79
+ protected getWsUrl(): string;
80
+ protected getPingIntervalMs(): number;
81
+ protected buildPingMessage(): null;
82
+ sendSignedRequest(creds: BinanceCredentialsDTO, method: string, params?: Record<string, unknown>): Promise<number>;
83
+ }
84
+
85
+ declare class BinanceExchangeWs extends ExchangeWs {
86
+ userData: BinanceUserDataWs;
87
+ wsApi: BinanceWsApi;
88
+ readonly exchange: "binance";
89
+ constructor(userData: BinanceUserDataWs, wsApi: BinanceWsApi);
90
+ protected connections(): BaseWs[];
91
+ static create(config: {
92
+ demo: boolean;
93
+ credentials: BinanceCredentialsDTO;
94
+ }): Promise<BinanceExchangeWs>;
95
+ }
96
+
97
+ type BybitWsConfig = {
98
+ demo: boolean;
99
+ };
100
+ declare class BybitWs extends BaseWs {
101
+ private readonly demo;
102
+ constructor(config: BybitWsConfig);
103
+ protected getWsUrl(): string;
104
+ protected getPingIntervalMs(): number;
105
+ protected buildPingMessage(): WsMessage;
106
+ authenticate(creds: BybitCredentialsDTO): Promise<void>;
107
+ }
108
+
109
+ declare class BybitExchangeWs extends ExchangeWs {
110
+ main: BybitWs;
111
+ private readonly creds;
112
+ readonly exchange: "bybit";
113
+ constructor(main: BybitWs, creds: BybitCredentialsDTO);
114
+ protected connections(): BaseWs[];
115
+ authenticate(): Promise<void>;
116
+ static create(config: {
117
+ demo: boolean;
118
+ credentials: BybitCredentialsDTO;
119
+ }): Promise<BybitExchangeWs>;
120
+ }
121
+
122
+ declare class HyperliquidWs extends BaseWs {
123
+ protected getWsUrl(): string;
124
+ protected getPingIntervalMs(): number;
125
+ protected buildPingMessage(): null;
126
+ }
127
+
128
+ declare class HyperliquidExchangeWs extends ExchangeWs {
129
+ main: HyperliquidWs;
130
+ readonly exchange: "hyperliquid";
131
+ constructor(main: HyperliquidWs);
132
+ protected connections(): BaseWs[];
133
+ static create(): Promise<HyperliquidExchangeWs>;
134
+ }
135
+
136
+ declare class LighterWs extends BaseWs {
137
+ protected getWsUrl(): string;
138
+ protected getPingIntervalMs(): number;
139
+ protected buildPingMessage(): WsMessage;
140
+ }
141
+
142
+ declare class LighterExchangeWs extends ExchangeWs {
143
+ main: LighterWs;
144
+ readonly exchange: "lighter";
145
+ constructor(main: LighterWs);
146
+ protected connections(): BaseWs[];
147
+ static create(): Promise<LighterExchangeWs>;
148
+ }
149
+
150
+ type OkxWsConfig = {
151
+ demo: boolean;
152
+ };
153
+ declare class OkxWs extends BaseWs {
154
+ private readonly demo;
155
+ constructor(config: OkxWsConfig);
156
+ protected getWsUrl(): string;
157
+ protected getPingIntervalMs(): number;
158
+ protected buildPingMessage(): string;
159
+ login(creds: OkxCredentialsDTO): Promise<void>;
160
+ }
161
+
162
+ declare class OkxExchangeWs extends ExchangeWs {
163
+ main: OkxWs;
164
+ private readonly creds;
165
+ readonly exchange: "okx";
166
+ constructor(main: OkxWs, creds: OkxCredentialsDTO);
167
+ protected connections(): BaseWs[];
168
+ login(): Promise<void>;
169
+ static create(config: {
170
+ demo: boolean;
171
+ credentials: OkxCredentialsDTO;
172
+ }): Promise<OkxExchangeWs>;
173
+ }
174
+
175
+ type ExchangeWsByConnector = {
176
+ binance: BinanceExchangeWs;
177
+ bybit: BybitExchangeWs;
178
+ okx: OkxExchangeWs;
179
+ hyperliquid: HyperliquidExchangeWs;
180
+ lighter: LighterExchangeWs;
181
+ };
182
+ type CreateExchangeWsConfig = {
183
+ demo: boolean;
184
+ credentials: SupportedCredentials;
185
+ };
186
+ declare function createExchangeWs<E extends Connector>(exchange: E, config: CreateExchangeWsConfig): Promise<ExchangeWsByConnector[E]>;
187
+
188
+ interface HyperliquidHelper {
189
+ checkApiWallet(params: CheckApiWalletParams): Promise<boolean>;
190
+ checkBuilderFeeApproval(userAddress: `0x${string}`, builderAddress: `0x${string}`): Promise<boolean>;
191
+ }
192
+ type CheckApiWalletParams = {
193
+ userAddress: `0x${string}`;
194
+ agentWalletAddress: `0x${string}`;
195
+ };
196
+
197
+ declare function createHyperliquidHelper(): HyperliquidHelper;
198
+
199
+ type WalletClientWithAccount = WalletClient<Transport, Chain | undefined, Account>;
200
+
201
+ type LighterRawTrade = {
202
+ trade_id: number;
203
+ trade_id_str?: string;
204
+ type: 'trade' | 'liquidation' | 'deleverage' | 'market-settlement' | string;
205
+ market_id: number;
206
+ size: string;
207
+ price: string;
208
+ usd_amount: string;
209
+ ask_client_id_str?: string;
210
+ bid_client_id_str?: string;
211
+ ask_account_id: number;
212
+ bid_account_id: number;
213
+ is_maker_ask: boolean;
214
+ timestamp: number;
215
+ taker_fee?: number;
216
+ taker_position_size_before?: string;
217
+ maker_fee?: number;
218
+ maker_position_size_before?: string;
219
+ integrator_taker_fee?: number;
220
+ integrator_maker_fee?: number;
221
+ };
222
+
223
+ type ApiKey = {
224
+ account_index: number;
225
+ api_key_index: number;
226
+ nonce: number;
227
+ public_key: string;
228
+ transaction_time: number;
229
+ };
230
+
231
+ declare const AssetId: {
232
+ readonly ETH: 1;
233
+ readonly LIT: 2;
234
+ readonly USDC: 3;
235
+ readonly LINK: 5;
236
+ readonly UNI: 6;
237
+ readonly AAVE: 7;
238
+ readonly SKY: 8;
239
+ readonly LDO: 9;
240
+ };
241
+ type AssetId = (typeof AssetId)[keyof typeof AssetId];
242
+ declare const RouteType: {
243
+ readonly PERP: 0;
244
+ readonly SPOT: 1;
245
+ };
246
+ type RouteType = (typeof RouteType)[keyof typeof RouteType];
247
+ declare const ASSET_DECIMALS: Record<number, number>;
248
+ declare function getAssetDecimals(assetId: number): number;
249
+ type ConnectWalletParams = {
250
+ accountIndex: number;
251
+ };
252
+ type DepositParams = {
253
+ to: `0x${string}`;
254
+ assetId: number;
255
+ routeType: number;
256
+ amount: string;
257
+ nativeValue?: string;
258
+ };
259
+ type WithdrawParams = {
260
+ accountIndex: number;
261
+ assetId: number;
262
+ routeType: number;
263
+ amount: string;
264
+ };
265
+ type ConnectWalletResult = {
266
+ txHash: string;
267
+ apiKeyIndex: number;
268
+ apiPrivateKey: string;
269
+ };
270
+ type ApproveIntegratorParams = {
271
+ accountIndex: number;
272
+ apiKeyIndex: number;
273
+ apiPrivateKey: string;
274
+ };
275
+ type ApproveIntegratorResult = {
276
+ txHash: string;
277
+ };
278
+ interface LighterHelper {
279
+ lookupAccount(l1Address: string): Promise<LighterLookupResult>;
280
+ checkIntegratorApproval(accountIndex: number, integratorAccountIndex: number): Promise<boolean>;
281
+ connectWallet(walletClient: WalletClientWithAccount, params: ConnectWalletParams): Promise<ConnectWalletResult>;
282
+ approveIntegrator(walletClient: WalletClientWithAccount, params: ApproveIntegratorParams): Promise<ApproveIntegratorResult>;
283
+ }
284
+ type LighterLookupResult = {
285
+ mainAccountIndex: number;
286
+ subAccountIndexes: number[];
287
+ };
288
+
289
+ declare function createLighterHelper(sdk: PearSDK): LighterHelper;
290
+
291
+ type ExchangeHelperMap = {
292
+ hyperliquid: HyperliquidHelper;
293
+ lighter: LighterHelper;
294
+ };
295
+ type ExchangeHelperExchange = keyof ExchangeHelperMap;
296
+
297
+ interface AccountSyncerConfig {
298
+ sdk: PearSDK;
299
+ connector: Connector;
300
+ account: TradeAccount;
301
+ credentials: SupportedCredentials;
302
+ ws: ExchangeWsByConnector[Connector];
303
+ demo: boolean;
304
+ }
305
+ interface AccountSyncer {
306
+ synchronize(): Promise<void>;
307
+ }
308
+ interface ListExternalFillsParams {
309
+ since: Date;
310
+ instruments: HttpTypes.Instrument[];
311
+ openPositions: HttpTypes.OpenPositionLite[];
312
+ provisionalFills: HttpTypes.ProvisionalFill[];
313
+ }
314
+ declare abstract class BaseAccountSyncer<ExternalFill> implements AccountSyncer {
315
+ protected sdk: PearSDK;
316
+ protected connector: Connector;
317
+ protected account: TradeAccount;
318
+ protected credentials: SupportedCredentials;
319
+ protected ws: ExchangeWsByConnector[Connector];
320
+ protected demo: boolean;
321
+ constructor(config: AccountSyncerConfig);
322
+ protected abstract listExternalFills(params: ListExternalFillsParams): Promise<HttpTypes.UnknownExternalFill[]>;
323
+ protected abstract isReduceOnly(fill: ExternalFill): boolean;
324
+ synchronize(): Promise<void>;
325
+ }
326
+
327
+ interface BinanceRawUserTrade {
328
+ buyer: boolean;
329
+ commission: string;
330
+ commissionAsset: string;
331
+ id: number;
332
+ maker: boolean;
333
+ orderId: number;
334
+ price: string;
335
+ qty: string;
336
+ quoteQty: string;
337
+ realizedPnl: string;
338
+ side: 'BUY' | 'SELL';
339
+ positionSide: string;
340
+ symbol: string;
341
+ time: number;
342
+ }
343
+
344
+ declare class BinanceAccountSyncer extends BaseAccountSyncer<BinanceRawUserTrade> {
345
+ private readonly creds;
346
+ constructor(config: AccountSyncerConfig);
347
+ protected listExternalFills({ since, instruments, openPositions, provisionalFills, }: ListExternalFillsParams): Promise<HttpTypes.UnknownExternalFill[]>;
348
+ protected isReduceOnly(trade: BinanceRawUserTrade): boolean;
349
+ private collectRequestedInstrumentIds;
350
+ private mapOrdersBySymbolOrderId;
351
+ private orderKey;
352
+ }
353
+
354
+ interface BybitRawExecution {
355
+ symbol: string;
356
+ orderId: string;
357
+ orderLinkId: string;
358
+ side: 'Buy' | 'Sell';
359
+ execFee: string;
360
+ execId: string;
361
+ execPrice: string;
362
+ execQty: string;
363
+ execTime: string;
364
+ execType: string;
365
+ closedSize: string;
366
+ }
367
+
368
+ declare class BybitAccountSyncer extends BaseAccountSyncer<BybitRawExecution> {
369
+ private readonly creds;
370
+ constructor(config: AccountSyncerConfig);
371
+ protected listExternalFills({ since }: ListExternalFillsParams): Promise<HttpTypes.UnknownExternalFill[]>;
372
+ protected isReduceOnly(execution: BybitRawExecution): boolean;
373
+ }
374
+
375
+ declare function createAccountSyncer(exchange: Connector, config: AccountSyncerConfig): AccountSyncer;
376
+
377
+ type UserFillsByTimeResponse = {
378
+ /** Asset symbol. */
379
+ coin: string;
380
+ /**
381
+ * Price.
382
+ * @pattern ^[0-9]+(\.[0-9]+)?$
383
+ */
384
+ px: string;
385
+ /**
386
+ * Size.
387
+ * @pattern ^[0-9]+(\.[0-9]+)?$
388
+ */
389
+ sz: string;
390
+ /** Order side ("B" = Bid/Buy, "A" = Ask/Sell). */
391
+ side: 'B' | 'A';
392
+ /** Timestamp when the trade occurred (in ms since epoch). */
393
+ time: number;
394
+ /**
395
+ * Start position size.
396
+ * @pattern ^-?[0-9]+(\.[0-9]+)?$
397
+ */
398
+ startPosition: string;
399
+ /** Direction indicator for frontend display. */
400
+ dir: string;
401
+ /**
402
+ * Realized PnL.
403
+ * @pattern ^-?[0-9]+(\.[0-9]+)?$
404
+ */
405
+ closedPnl: string;
406
+ /**
407
+ * L1 transaction hash.
408
+ * @pattern ^0x[a-fA-F0-9]{64}$
409
+ */
410
+ hash: `0x${string}`;
411
+ /** Order ID. */
412
+ oid: number;
413
+ /** Indicates if the fill was a taker order. */
414
+ crossed: boolean;
415
+ /**
416
+ * Fee charged or rebate received (negative indicates rebate).
417
+ * @pattern ^-?[0-9]+(\.[0-9]+)?$
418
+ */
419
+ fee: string;
420
+ /**
421
+ * Optional fee charged by the UI builder.
422
+ * @pattern ^-?[0-9]+(\.[0-9]+)?$
423
+ */
424
+ builderFee?: string;
425
+ /** Unique transaction identifier for a partial fill of an order. */
426
+ tid: number;
427
+ /** Token in which the fee is denominated (e.g., "USDC"). */
428
+ feeToken: string;
429
+ /** ID of the TWAP. */
430
+ twapId: number | null;
431
+ /**
432
+ * Client Order ID.
433
+ * @pattern ^0x[a-fA-F0-9]{32}$
434
+ */
435
+ cloid?: `0x${string}`;
436
+ /** Liquidation details. */
437
+ liquidation?: {
438
+ /**
439
+ * Address of the liquidated user.
440
+ * @pattern ^0x[a-fA-F0-9]{40}$
441
+ */
442
+ liquidatedUser: `0x${string}`;
443
+ /**
444
+ * Mark price at the time of liquidation.
445
+ * @pattern ^[0-9]+(\.[0-9]+)?$
446
+ */
447
+ markPx: string;
448
+ /** Liquidation method. */
449
+ method: 'market' | 'backstop';
450
+ };
451
+ };
452
+
453
+ declare class HyperliquidAccountSyncer extends BaseAccountSyncer<UserFillsByTimeResponse> {
454
+ private readonly address;
455
+ constructor(config: AccountSyncerConfig);
456
+ protected listExternalFills({ since, instruments, }: ListExternalFillsParams): Promise<HttpTypes.UnknownExternalFill[]>;
457
+ protected isReduceOnly(fill: UserFillsByTimeResponse): boolean;
458
+ }
459
+
460
+ declare class LighterAccountSyncer extends BaseAccountSyncer<LighterRawTrade> {
461
+ private readonly accountIndex;
462
+ private readonly apiKeyIndex;
463
+ private readonly apiPrivateKey;
464
+ constructor(config: AccountSyncerConfig);
465
+ protected listExternalFills({ since, instruments, }: ListExternalFillsParams): Promise<HttpTypes.UnknownExternalFill[]>;
466
+ protected isReduceOnly(trade: LighterRawTrade): boolean;
467
+ private getAccountSide;
468
+ private getClientOrderId;
469
+ private isMaker;
470
+ private getPositionSizeBefore;
471
+ private getTradeFee;
472
+ private getIntegratorFee;
473
+ private feeTickToDecimal;
474
+ }
475
+
476
+ interface OkxRawFill {
477
+ instType: string;
478
+ instId: string;
479
+ tradeId: string;
480
+ ordId: string;
481
+ clOrdId: string;
482
+ billId: string;
483
+ fillPx: string;
484
+ fillSz: string;
485
+ side: 'buy' | 'sell';
486
+ posSide: string;
487
+ execType: string;
488
+ fee: string;
489
+ feeCcy: string;
490
+ fillTime: string;
491
+ ts: string;
492
+ }
493
+
494
+ declare class OkxAccountSyncer extends BaseAccountSyncer<OkxRawFill> {
495
+ private readonly creds;
496
+ constructor(config: AccountSyncerConfig);
497
+ protected listExternalFills({ since, instruments, }: ListExternalFillsParams): Promise<HttpTypes.UnknownExternalFill[]>;
498
+ protected isReduceOnly(fill: OkxRawFill): boolean;
499
+ private contractsToBase;
500
+ }
501
+
502
+ declare enum BinanceAccountType {
503
+ MULTI_ASSET = "MULTI_ASSET",
504
+ SINGLE_ASSET = "SINGLE_ASSET"
505
+ }
506
+
507
+ declare enum BybitAccountType {
508
+ ISOLATED_MARGIN = "ISOLATED_MARGIN",
509
+ REGULAR_MARGIN = "REGULAR_MARGIN",
510
+ PORTFOLIO_MARGIN = "PORTFOLIO_MARGIN"
511
+ }
512
+
513
+ declare enum HyperliquidAccountType {
514
+ DEFAULT = "default",
515
+ DEX_ABSTRACTION = "dexAbstraction",
516
+ DISABLED = "disabled",
517
+ PORTFOLIO_MARGIN = "portfolioMargin",
518
+ UNIFIED_ACCOUNT = "unifiedAccount"
519
+ }
520
+
521
+ declare enum LighterAccountType {
522
+ BASIC = "basic"
523
+ }
524
+
525
+ declare enum OkxAccountType {
526
+ SPOT = "OKX_SPOT",
527
+ SINGLE_CURRENCY_MARGIN = "OKX_SINGLE",
528
+ MULTI_CURRENCY_MARGIN = "OKX_MULTI",
529
+ PORTFOLIO_MARGIN = "OKX_PORTFOLIO"
530
+ }
531
+
532
+ type MarginMode = 'cross' | 'isolated';
533
+ type AccountType = BinanceAccountType | BybitAccountType | HyperliquidAccountType | LighterAccountType | OkxAccountType;
534
+ declare const ACCOUNT_TYPE_LABELS: Record<AccountType, string>;
535
+ interface AssetBalance {
536
+ asset: string;
537
+ free: string;
538
+ used: string;
539
+ total: string;
540
+ walletBalance: string;
541
+ usdValue: string;
542
+ }
543
+ interface AccountBalance {
544
+ accountType: AccountType;
545
+ free: Record<string, string>;
546
+ used: Record<string, string>;
547
+ total: Record<string, string>;
548
+ availableToTrade: string;
549
+ totalEquity: string;
550
+ walletBalance: string;
551
+ unrealizedPnl: string;
552
+ initialMarginUsed: string;
553
+ maintenanceMargin: string;
554
+ marginRatio: string;
555
+ assets: AssetBalance[];
556
+ timestamp: number;
557
+ }
558
+ interface AccountPosition {
559
+ symbol: string;
560
+ side: 'long' | 'short' | 'both';
561
+ size: string;
562
+ entryPrice: string;
563
+ unrealizedPnl: string;
564
+ leverage: string;
565
+ marginType: MarginMode;
566
+ liquidationPrice: string | null;
567
+ }
568
+ interface TrackedAssetInfo {
569
+ coin: string;
570
+ leverage: string;
571
+ marginType: MarginMode;
572
+ }
573
+ interface ExchangeState {
574
+ balance: AccountBalance | null;
575
+ positions: Map<string, AccountPosition>;
576
+ lastUpdated: number;
577
+ initialized: boolean;
578
+ }
579
+ type BalanceCallback = (balance: AccountBalance) => void;
580
+ type PositionsCallback = (positions: AccountPosition[]) => void;
581
+ type TrackedAssetCallback = (info: TrackedAssetInfo) => void;
582
+ interface StateUpdate {
583
+ balance?: AccountBalance;
584
+ positions?: Map<string, AccountPosition>;
585
+ trackedAssets?: Map<string, TrackedAssetInfo>;
586
+ }
587
+
588
+ type ErrorHandler = (error: Error) => void;
589
+ type AccountTrackerConfig = {
590
+ demo: boolean;
591
+ onError?: ErrorHandler;
592
+ };
593
+ interface AccountTracker {
594
+ start(): Promise<void>;
595
+ disconnect(): Promise<void>;
596
+ readonly isConnected: boolean;
597
+ readonly isInitialized: boolean;
598
+ readonly hasListeners: boolean;
599
+ trackBalance(cb: BalanceCallback): () => void;
600
+ trackPosition(cb: PositionsCallback): () => void;
601
+ trackAsset(asset: string, cb: TrackedAssetCallback): () => void;
602
+ getBalance(): AccountBalance | null;
603
+ getPositions(): AccountPosition[];
604
+ getTrackedAsset(coin: string): TrackedAssetInfo | null;
605
+ }
606
+ declare abstract class BaseAccountTracker implements AccountTracker {
607
+ protected onError: ErrorHandler;
608
+ protected demo: boolean;
609
+ protected credentials: SupportedCredentials | null;
610
+ private balance;
611
+ private positions;
612
+ private initialized;
613
+ private balanceListeners;
614
+ private positionListeners;
615
+ private assetEntries;
616
+ constructor(config: AccountTrackerConfig);
617
+ disconnect(): Promise<void>;
618
+ get isInitialized(): boolean;
619
+ get hasListeners(): boolean;
620
+ trackBalance(cb: BalanceCallback): () => void;
621
+ trackPosition(cb: PositionsCallback): () => void;
622
+ trackAsset(asset: string, cb: TrackedAssetCallback): () => void;
623
+ private releaseAssetListener;
624
+ protected get trackedCoins(): ReadonlySet<string>;
625
+ getBalance(): AccountBalance | null;
626
+ getPositions(): AccountPosition[];
627
+ getTrackedAsset(coin: string): TrackedAssetInfo | null;
628
+ protected onStateUpdate(update: StateUpdate): void;
629
+ private applyBalanceUpdate;
630
+ private applyPositionsUpdate;
631
+ private applyTrackedAssetsUpdate;
632
+ private getOrCreateAssetEntry;
633
+ private positionSnapshot;
634
+ private notify;
635
+ private broadcast;
636
+ abstract start(): Promise<void>;
637
+ abstract get isConnected(): boolean;
638
+ protected abstract ensureAssetTracked(asset: string): void;
639
+ protected abstract releaseAsset(asset: string): void;
640
+ protected abstract cleanupConnections(): void | Promise<void>;
641
+ }
642
+
643
+ type BinanceAccountTrackerConfig = AccountTrackerConfig & {
644
+ credentials: SupportedCredentials;
645
+ userDataWs: BinanceUserDataWs;
646
+ wsApiWs: BinanceWsApi;
647
+ listenKeyRefreshMs?: number;
648
+ accountPollMs?: number;
649
+ };
650
+ declare class BinanceAccountTracker extends BaseAccountTracker {
651
+ private readonly creds;
652
+ private readonly userDataWs;
653
+ private readonly wsApiWs;
654
+ private readonly listenKeyRefreshMs;
655
+ private readonly accountPollMs;
656
+ private userDataListenerId;
657
+ private wsApiListenerId;
658
+ private listenKeyTimer;
659
+ private pollTimer;
660
+ private currentPositions;
661
+ private accountType;
662
+ private trackedAssetInfos;
663
+ constructor(config: BinanceAccountTrackerConfig);
664
+ start(): Promise<void>;
665
+ protected cleanupConnections(): Promise<void>;
666
+ get isConnected(): boolean;
667
+ protected ensureAssetTracked(coin: string): void;
668
+ protected releaseAsset(coin: string): void;
669
+ private pollAccountStatus;
670
+ private routeUserData;
671
+ private routeWsApi;
672
+ private handleAccountStatus;
673
+ private handlePositionRisk;
674
+ private handleUserDataEvent;
675
+ private refreshAllPositions;
676
+ }
677
+
678
+ type BybitAccountTrackerConfig = AccountTrackerConfig & {
679
+ credentials: SupportedCredentials;
680
+ ws: BybitExchangeWs;
681
+ };
682
+ declare class BybitAccountTracker extends BaseAccountTracker {
683
+ private readonly creds;
684
+ private readonly ws;
685
+ private messageListenerId;
686
+ private openListenerId;
687
+ private rawCoins;
688
+ private lastWallet;
689
+ private accountType;
690
+ private trackedAssetInfos;
691
+ constructor(config: BybitAccountTrackerConfig);
692
+ start(): Promise<void>;
693
+ protected cleanupConnections(): Promise<void>;
694
+ get isConnected(): boolean;
695
+ protected ensureAssetTracked(coin: string): void;
696
+ protected releaseAsset(coin: string): void;
697
+ private subscribe;
698
+ private handleInitialData;
699
+ private routeRaw;
700
+ private handleMessage;
701
+ private handleWalletUpdate;
702
+ }
703
+
704
+ type HyperliquidAccountTrackerConfig = AccountTrackerConfig & {
705
+ account: TradeAccount;
706
+ credentials: SupportedCredentials;
707
+ ws: HyperliquidWs;
708
+ };
709
+ declare class HyperliquidAccountTracker extends BaseAccountTracker {
710
+ private readonly address;
711
+ private readonly ws;
712
+ private messageListenerId;
713
+ private openListenerId;
714
+ private subscribedCoins;
715
+ private lastSummary;
716
+ private spotCoinBalances;
717
+ private accountType;
718
+ private symbolToCollateralToken;
719
+ private availableToTradeByCollateral;
720
+ private trackedAssetData;
721
+ constructor(config: HyperliquidAccountTrackerConfig);
722
+ start(): Promise<void>;
723
+ protected cleanupConnections(): Promise<void>;
724
+ get isConnected(): boolean;
725
+ protected ensureAssetTracked(coin: string): void;
726
+ protected releaseAsset(coin: string): void;
727
+ private isLocked;
728
+ private addrLower;
729
+ private sendStaticSubs;
730
+ private resubscribeAll;
731
+ private routeRaw;
732
+ private handle;
733
+ private loadPerpMetas;
734
+ private onClearinghouseState;
735
+ private onActiveAssetData;
736
+ private onWebData3;
737
+ private onSpotState;
738
+ private buildBalance;
739
+ private buildTrackedAssetsMap;
740
+ }
741
+
742
+ type LighterAccountTrackerConfig = AccountTrackerConfig & {
743
+ account: TradeAccount;
744
+ credentials: SupportedCredentials;
745
+ ws: LighterWs;
746
+ };
747
+ declare class LighterAccountTracker extends BaseAccountTracker {
748
+ private readonly accountIdentifier;
749
+ private readonly ws;
750
+ private messageListenerId;
751
+ private openListenerId;
752
+ private pollTimer;
753
+ private assetsByAssetId;
754
+ private summary;
755
+ private hasAssets;
756
+ constructor(config: LighterAccountTrackerConfig);
757
+ start(): Promise<void>;
758
+ protected cleanupConnections(): Promise<void>;
759
+ get isConnected(): boolean;
760
+ protected ensureAssetTracked(_coin: string): void;
761
+ protected releaseAsset(_coin: string): void;
762
+ private sendStaticSubs;
763
+ private routeRaw;
764
+ private handle;
765
+ private onAccountAll;
766
+ private onUserStats;
767
+ private buildBalance;
768
+ private pollAccount;
769
+ }
770
+
771
+ type OkxAccountTrackerConfig = AccountTrackerConfig & {
772
+ credentials: SupportedCredentials;
773
+ ws: OkxExchangeWs;
774
+ };
775
+ declare class OkxAccountTracker extends BaseAccountTracker {
776
+ private readonly creds;
777
+ private readonly ws;
778
+ private messageListenerId;
779
+ private openListenerId;
780
+ private rawCoins;
781
+ private lastBalance;
782
+ private accountType;
783
+ private trackedAssetInfos;
784
+ constructor(config: OkxAccountTrackerConfig);
785
+ start(): Promise<void>;
786
+ protected cleanupConnections(): Promise<void>;
787
+ get isConnected(): boolean;
788
+ protected ensureAssetTracked(instId: string): void;
789
+ protected releaseAsset(instId: string): void;
790
+ private subscribe;
791
+ private handleInitialData;
792
+ private routeRaw;
793
+ private handleMessage;
794
+ private handleBalanceUpdate;
795
+ }
796
+
797
+ type CreateAccountTrackerConfig<E extends Connector> = AccountTrackerConfig & {
798
+ account: TradeAccount;
799
+ credentials: SupportedCredentials;
800
+ ws: ExchangeWsByConnector[E];
801
+ };
802
+ declare function createAccountTracker<E extends Connector>(exchange: E, config: CreateAccountTrackerConfig<E>): AccountTracker;
803
+
804
+ declare class ExchangesSDK {
805
+ private sdk;
806
+ private demo;
807
+ private options?;
808
+ constructor(config: ExchangesSDKConfig);
809
+ connect(tradeAccountId: string): Promise<ExchangeConnection>;
810
+ status(connection: ExchangeConnection): Promise<AccountStatus>;
811
+ createTracker(connection: ExchangeConnection): AccountTracker;
812
+ createHelper<E extends ExchangeHelperExchange>(exchange: E): ExchangeHelperMap[E];
813
+ createSyncer(connection: ExchangeConnection): AccountSyncer;
814
+ private fetchCredentials;
815
+ }
816
+
817
+ export { ACCOUNT_TYPE_LABELS, ASSET_DECIMALS, type AccountBalance, type AccountHealthStatus, type AccountPosition, type AccountStatus, type AccountSyncer, type AccountSyncerConfig, type AccountTracker, type AccountTrackerConfig, type AccountType, type AccountWarningReason, type ApiKey, type ApproveIntegratorParams, type ApproveIntegratorResult, type AssetBalance, AssetId, type BalanceCallback, BaseAccountSyncer, BaseAccountTracker, BaseWs, BinanceAccountSyncer, BinanceAccountTracker, type BinanceAccountTrackerConfig, BinanceAccountType, BinanceExchangeWs, BinanceUserDataWs, type BinanceUserDataWsConfig, BinanceWsApi, type BinanceWsApiConfig, BybitAccountSyncer, BybitAccountTracker, type BybitAccountTrackerConfig, BybitAccountType, BybitExchangeWs, BybitWs, type BybitWsConfig, type CheckApiWalletParams, type ConnectOptions, type ConnectWalletParams, type ConnectWalletResult, type CreateAccountTrackerConfig, type CreateExchangeWsConfig, type DepositParams, type ExchangeConnection, type ExchangeHelperExchange, type ExchangeHelperMap, type ExchangeState, ExchangeWs, type ExchangeWsByConnector, ExchangesSDK, type ExchangesSDKConfig, HyperliquidAccountSyncer, HyperliquidAccountTracker, type HyperliquidAccountTrackerConfig, HyperliquidAccountType, HyperliquidExchangeWs, type HyperliquidHelper, HyperliquidWs, LighterAccountSyncer, LighterAccountTracker, type LighterAccountTrackerConfig, LighterAccountType, LighterExchangeWs, type LighterHelper, type LighterLookupResult, LighterWs, type ListExternalFillsParams, type MarginMode, type MessageListener, OkxAccountSyncer, OkxAccountTracker, type OkxAccountTrackerConfig, OkxAccountType, OkxExchangeWs, OkxWs, type OkxWsConfig, type OpenListener, type PositionsCallback, RouteType, type StateUpdate, type SupportedCredentials, type TrackedAssetCallback, type TrackedAssetInfo, type WalletClientWithAccount, type WithdrawParams, type WsMessage, createAccountSyncer, createAccountTracker, createExchangeWs, createHyperliquidHelper, createLighterHelper, getAssetDecimals };