@pear-protocol/exchanges-sdk 0.0.10 → 0.0.12

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 (60) hide show
  1. package/README.md +59 -42
  2. package/dist/account/account-manager.d.ts +5 -11
  3. package/dist/account/account-manager.js +22 -60
  4. package/dist/account/exchange-account.d.ts +8 -10
  5. package/dist/account/exchange-account.js +30 -57
  6. package/dist/account/exchanges/base.d.ts +13 -15
  7. package/dist/account/exchanges/base.js +5 -27
  8. package/dist/account/exchanges/binance/asset-tracker.d.ts +25 -0
  9. package/dist/account/exchanges/binance/asset-tracker.js +38 -0
  10. package/dist/account/exchanges/binance/mapper.d.ts +4 -3
  11. package/dist/account/exchanges/binance/mapper.js +0 -1
  12. package/dist/account/exchanges/binance/orchestrator.d.ts +9 -12
  13. package/dist/account/exchanges/binance/orchestrator.js +61 -183
  14. package/dist/account/exchanges/binance/rest.d.ts +5 -4
  15. package/dist/account/exchanges/binance/rest.js +4 -20
  16. package/dist/account/exchanges/binance/state-handler.d.ts +32 -0
  17. package/dist/account/exchanges/binance/state-handler.js +113 -0
  18. package/dist/account/exchanges/binance/types.d.ts +3 -76
  19. package/dist/account/exchanges/binance/types.js +6 -0
  20. package/dist/account/exchanges/binance/ws.d.ts +3 -2
  21. package/dist/account/exchanges/binance/ws.js +6 -22
  22. package/dist/account/exchanges/bybit/asset-tracker.d.ts +25 -0
  23. package/dist/account/exchanges/bybit/asset-tracker.js +37 -0
  24. package/dist/account/exchanges/bybit/mapper.d.ts +4 -3
  25. package/dist/account/exchanges/bybit/mapper.js +5 -3
  26. package/dist/account/exchanges/bybit/orchestrator.d.ts +11 -12
  27. package/dist/account/exchanges/bybit/orchestrator.js +48 -134
  28. package/dist/account/exchanges/bybit/rest.d.ts +2 -3
  29. package/dist/account/exchanges/bybit/rest.js +4 -39
  30. package/dist/account/exchanges/bybit/state-handler.d.ts +22 -0
  31. package/dist/account/exchanges/bybit/state-handler.js +70 -0
  32. package/dist/account/exchanges/bybit/types.d.ts +14 -3
  33. package/dist/account/exchanges/bybit/types.js +7 -0
  34. package/dist/account/exchanges/bybit/ws.d.ts +1 -0
  35. package/dist/account/exchanges/bybit/ws.js +21 -12
  36. package/dist/account/exchanges/hyperliquid/asset-tracker.d.ts +34 -0
  37. package/dist/account/exchanges/hyperliquid/asset-tracker.js +83 -0
  38. package/dist/account/exchanges/hyperliquid/mapper.d.ts +4 -4
  39. package/dist/account/exchanges/hyperliquid/mapper.js +1 -0
  40. package/dist/account/exchanges/hyperliquid/orchestrator.d.ts +15 -28
  41. package/dist/account/exchanges/hyperliquid/orchestrator.js +79 -216
  42. package/dist/account/exchanges/hyperliquid/state-handler.d.ts +24 -0
  43. package/dist/account/exchanges/hyperliquid/state-handler.js +104 -0
  44. package/dist/account/exchanges/hyperliquid/types.d.ts +119 -5
  45. package/dist/account/exchanges/hyperliquid/types.js +9 -1
  46. package/dist/account/exchanges/index.d.ts +7 -5
  47. package/dist/account/exchanges/index.js +12 -4
  48. package/dist/account/index.d.ts +3 -1
  49. package/dist/account/types.d.ts +4 -73
  50. package/dist/account/types.js +17 -0
  51. package/dist/account/utils.d.ts +3 -1
  52. package/dist/index.d.ts +3 -1
  53. package/dist/types-D2dbAtin.d.ts +150 -0
  54. package/dist/utils/hmac.d.ts +1 -2
  55. package/dist/utils/hmac.js +1 -8
  56. package/dist/utils/ws.d.ts +6 -3
  57. package/dist/utils/ws.js +24 -7
  58. package/package.json +5 -5
  59. package/dist/account/exchanges/hyperliquid/rest.d.ts +0 -6
  60. package/dist/account/exchanges/hyperliquid/rest.js +0 -11
@@ -1,73 +1,4 @@
1
- import { Connector, BinanceCredentialsDTO, BybitCredentialsDTO, HyperliquidCredentialsDTO } from '@pear-protocol/types';
2
-
3
- type MarginMode = 'cross' | 'isolated';
4
- interface AssetBalance {
5
- asset: string;
6
- free: string;
7
- used: string;
8
- total: string;
9
- walletBalance: string;
10
- usdValue: string;
11
- }
12
- interface AccountBalance {
13
- accountType: string;
14
- free: Record<string, string>;
15
- used: Record<string, string>;
16
- total: Record<string, string>;
17
- availableToTrade: string;
18
- totalEquity: string;
19
- walletBalance: string;
20
- unrealizedPnl: string;
21
- initialMarginUsed: string;
22
- maintenanceMargin: string;
23
- marginRatio: string;
24
- assets: AssetBalance[];
25
- timestamp: number;
26
- }
27
- interface AccountPosition {
28
- symbol: string;
29
- side: 'long' | 'short' | 'both';
30
- size: string;
31
- entryPrice: string;
32
- unrealizedPnl: string;
33
- leverage: string;
34
- marginType: MarginMode;
35
- liquidationPrice: string | null;
36
- }
37
- interface TrackedAssetInfo {
38
- coin: string;
39
- leverage: string;
40
- marginType: MarginMode;
41
- }
42
- interface ExchangeState {
43
- balance: AccountBalance | null;
44
- positions: Map<string, AccountPosition>;
45
- leverageSettings: Map<string, string>;
46
- trackedAssets: Map<string, TrackedAssetInfo>;
47
- lastUpdated: number;
48
- initialized: boolean;
49
- }
50
- type BalanceCallback = (balance: AccountBalance) => void;
51
- type PositionsCallback = (positions: AccountPosition[]) => void;
52
- type TrackedAssetCallback = (info: TrackedAssetInfo) => void;
53
- interface Tracker<T> {
54
- get(): T;
55
- untrack(): void;
56
- }
57
- interface AccountConfig {
58
- exchange: Connector;
59
- demo?: boolean;
60
- onAuthError?: () => void;
61
- }
62
- type HyperliquidAccountCredentials = HyperliquidCredentialsDTO & {
63
- address: string;
64
- };
65
- type SupportedCredentials = BinanceCredentialsDTO | BybitCredentialsDTO | HyperliquidAccountCredentials;
66
- interface StateUpdate {
67
- balance?: AccountBalance;
68
- positions?: Map<string, AccountPosition>;
69
- leverageSettings?: Map<string, string>;
70
- trackedAssets?: Map<string, TrackedAssetInfo>;
71
- }
72
-
73
- export type { AccountBalance, AccountConfig, AccountPosition, AssetBalance, BalanceCallback, ExchangeState, HyperliquidAccountCredentials, MarginMode, PositionsCallback, StateUpdate, SupportedCredentials, TrackedAssetCallback, TrackedAssetInfo, Tracker };
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-D2dbAtin.js';
3
+ import './exchanges/bybit/types.js';
4
+ import './exchanges/hyperliquid/types.js';
@@ -1 +1,18 @@
1
+ import { BinanceAccountType } from './exchanges/binance/types';
2
+ import { BybitAccountType } from './exchanges/bybit/types';
3
+ import { HyperliquidAccountType } from './exchanges/hyperliquid/types';
1
4
 
5
+ const ACCOUNT_TYPE_LABELS = {
6
+ [BinanceAccountType.SINGLE_ASSET]: "Single-Asset Mode",
7
+ [BinanceAccountType.MULTI_ASSET]: "Multi-Asset Mode",
8
+ [BybitAccountType.ISOLATED_MARGIN]: "Isolated Margin",
9
+ [BybitAccountType.REGULAR_MARGIN]: "Cross Margin",
10
+ [BybitAccountType.PORTFOLIO_MARGIN]: "Portfolio Margin",
11
+ [HyperliquidAccountType.DEFAULT]: "Standard",
12
+ [HyperliquidAccountType.DISABLED]: "Standard",
13
+ [HyperliquidAccountType.DEX_ABSTRACTION]: "Standard (Shared HIP-3 Balance)",
14
+ [HyperliquidAccountType.PORTFOLIO_MARGIN]: "Portfolio Margin",
15
+ [HyperliquidAccountType.UNIFIED_ACCOUNT]: "Unified Account"
16
+ };
17
+
18
+ export { ACCOUNT_TYPE_LABELS };
@@ -1,5 +1,7 @@
1
- import { ExchangeState } from './types.js';
1
+ import { E as ExchangeState } from '../types-D2dbAtin.js';
2
2
  import '@pear-protocol/types';
3
+ import './exchanges/bybit/types.js';
4
+ import './exchanges/hyperliquid/types.js';
3
5
 
4
6
  declare function createEmptyState(): ExchangeState;
5
7
  declare function positionKey(symbol: string, side: string): string;
package/dist/index.d.ts CHANGED
@@ -2,8 +2,10 @@ import PearSDK from '@pear-protocol/core-sdk';
2
2
  import { AccountManager } from './account/account-manager.js';
3
3
  export { ExchangeAccount } from './account/exchange-account.js';
4
4
  export { createOrchestrator } from './account/exchanges/index.js';
5
- export { AccountBalance, AccountConfig, AccountPosition, AssetBalance, BalanceCallback, ExchangeState, HyperliquidAccountCredentials, MarginMode, PositionsCallback, StateUpdate, SupportedCredentials, TrackedAssetCallback, TrackedAssetInfo, Tracker } from './account/types.js';
5
+ 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-D2dbAtin.js';
6
6
  import { Credentials } from './credentials.js';
7
+ export { BybitAccountType } from './account/exchanges/bybit/types.js';
8
+ export { HyperliquidAccountType } from './account/exchanges/hyperliquid/types.js';
7
9
  export { Orchestrator, OrchestratorConfig } from './account/exchanges/base.js';
8
10
  import '@pear-protocol/types';
9
11
 
@@ -0,0 +1,150 @@
1
+ import { Connector, BinanceCredentialsDTO, BybitCredentialsDTO, HyperliquidCredentialsDTO } from '@pear-protocol/types';
2
+ import { BybitAccountType } from './account/exchanges/bybit/types.js';
3
+ import { HyperliquidAccountType } from './account/exchanges/hyperliquid/types.js';
4
+
5
+ declare enum BinanceAccountType {
6
+ MULTI_ASSET = "MULTI_ASSET",
7
+ SINGLE_ASSET = "SINGLE_ASSET"
8
+ }
9
+ interface BinanceRawPositionRisk {
10
+ symbol: string;
11
+ leverage: string;
12
+ marginType: MarginMode;
13
+ isAutoAddMargin: string;
14
+ isolatedMargin: string;
15
+ positionAmt: string;
16
+ entryPrice: string;
17
+ breakEvenPrice: string;
18
+ unRealizedProfit: string;
19
+ liquidationPrice: string;
20
+ markPrice: string;
21
+ maxNotionalValue: string;
22
+ positionSide: string;
23
+ notional: string;
24
+ isolatedWallet: string;
25
+ updateTime: number;
26
+ }
27
+ interface BinanceRawAsset {
28
+ asset: string;
29
+ walletBalance: string;
30
+ availableBalance: string;
31
+ unrealizedProfit: string;
32
+ marginBalance: string;
33
+ initialMargin: string;
34
+ maintMargin: string;
35
+ marginAvailable: boolean;
36
+ }
37
+ interface BinanceRawAccountStatus {
38
+ totalWalletBalance: string;
39
+ totalMarginBalance: string;
40
+ availableBalance: string;
41
+ totalUnrealizedProfit: string;
42
+ totalInitialMargin: string;
43
+ totalMaintMargin: string;
44
+ assets?: BinanceRawAsset[];
45
+ }
46
+ interface BinanceMultiAssetsMarginResponse {
47
+ multiAssetsMargin: boolean;
48
+ }
49
+ interface BinanceAssetIndex {
50
+ symbol: string;
51
+ time: number;
52
+ index: string;
53
+ bidBuffer: string;
54
+ askBuffer: string;
55
+ bidRate: string;
56
+ askRate: string;
57
+ autoExchangeBidBuffer: string;
58
+ autoExchangeAskBuffer: string;
59
+ autoExchangeBidRate: string;
60
+ autoExchangeAskRate: string;
61
+ }
62
+ interface BinanceRawUserDataEvent {
63
+ e?: string;
64
+ ac?: {
65
+ s: string;
66
+ l: string;
67
+ };
68
+ a?: {
69
+ B?: unknown[];
70
+ P?: unknown[];
71
+ };
72
+ }
73
+ interface BinanceWsHandlers {
74
+ onUserDataEvent: (event: unknown) => void;
75
+ onWsApiResponse: (id: number, result: unknown) => void;
76
+ }
77
+
78
+ type MarginMode = 'cross' | 'isolated';
79
+ type AccountType = BinanceAccountType | BybitAccountType | HyperliquidAccountType;
80
+ declare const ACCOUNT_TYPE_LABELS: Record<AccountType, string>;
81
+ interface AssetBalance {
82
+ asset: string;
83
+ free: string;
84
+ used: string;
85
+ total: string;
86
+ walletBalance: string;
87
+ usdValue: string;
88
+ }
89
+ interface AccountBalance {
90
+ accountType: AccountType;
91
+ free: Record<string, string>;
92
+ used: Record<string, string>;
93
+ total: Record<string, string>;
94
+ availableToTrade: string;
95
+ totalEquity: string;
96
+ walletBalance: string;
97
+ unrealizedPnl: string;
98
+ initialMarginUsed: string;
99
+ maintenanceMargin: string;
100
+ marginRatio: string;
101
+ assets: AssetBalance[];
102
+ timestamp: number;
103
+ }
104
+ interface AccountPosition {
105
+ symbol: string;
106
+ side: 'long' | 'short' | 'both';
107
+ size: string;
108
+ entryPrice: string;
109
+ unrealizedPnl: string;
110
+ leverage: string;
111
+ marginType: MarginMode;
112
+ liquidationPrice: string | null;
113
+ }
114
+ interface TrackedAssetInfo {
115
+ coin: string;
116
+ leverage: string;
117
+ marginType: MarginMode;
118
+ }
119
+ interface ExchangeState {
120
+ balance: AccountBalance | null;
121
+ positions: Map<string, AccountPosition>;
122
+ leverageSettings: Map<string, string>;
123
+ trackedAssets: Map<string, TrackedAssetInfo>;
124
+ lastUpdated: number;
125
+ initialized: boolean;
126
+ }
127
+ type BalanceCallback = (balance: AccountBalance) => void;
128
+ type PositionsCallback = (positions: AccountPosition[]) => void;
129
+ type TrackedAssetCallback = (info: TrackedAssetInfo) => void;
130
+ interface Tracker<T> {
131
+ get(): T;
132
+ untrack(): void;
133
+ }
134
+ interface AccountConfig {
135
+ exchange: Connector;
136
+ demo?: boolean;
137
+ onAuthError?: () => void;
138
+ }
139
+ type HyperliquidAccountCredentials = HyperliquidCredentialsDTO & {
140
+ address: string;
141
+ };
142
+ type SupportedCredentials = BinanceCredentialsDTO | BybitCredentialsDTO | HyperliquidAccountCredentials;
143
+ interface StateUpdate {
144
+ balance?: AccountBalance;
145
+ positions?: Map<string, AccountPosition>;
146
+ leverageSettings?: Map<string, string>;
147
+ trackedAssets?: Map<string, TrackedAssetInfo>;
148
+ }
149
+
150
+ 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,4 +1,3 @@
1
1
  declare function hmacSha256Hex(secret: string, message: string): Promise<string>;
2
- declare function hmacSha256Base64(secret: string, message: string): Promise<string>;
3
2
 
4
- export { hmacSha256Base64, hmacSha256Hex };
3
+ export { hmacSha256Hex };
@@ -6,12 +6,5 @@ async function hmacSha256Hex(secret, message) {
6
6
  const signature = await crypto.subtle.sign("HMAC", key, encoder.encode(message));
7
7
  return Array.from(new Uint8Array(signature)).map((b) => b.toString(16).padStart(2, "0")).join("");
8
8
  }
9
- async function hmacSha256Base64(secret, message) {
10
- const key = await crypto.subtle.importKey("raw", encoder.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, [
11
- "sign"
12
- ]);
13
- const signature = await crypto.subtle.sign("HMAC", key, encoder.encode(message));
14
- return btoa(String.fromCharCode(...new Uint8Array(signature)));
15
- }
16
9
 
17
- export { hmacSha256Base64, hmacSha256Hex };
10
+ export { hmacSha256Hex };
@@ -1,7 +1,10 @@
1
- declare function reconnectDelay(attempt: number): number;
2
- declare function connectWs(url: string): Promise<WebSocket>;
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>;
3
6
  declare function sendWs(ws: WebSocket | null, msg: Record<string, unknown> | string): void;
4
7
  declare function clearTimer(timer: ReturnType<typeof setTimeout> | null): null;
5
8
  declare function clearIntervalTimer(timer: ReturnType<typeof setInterval> | null): null;
6
9
 
7
- export { clearIntervalTimer, clearTimer, connectWs, reconnectDelay, sendWs };
10
+ export { clearIntervalTimer, clearTimer, createWs, sendWs, waitForOpen };
package/dist/utils/ws.js CHANGED
@@ -1,11 +1,28 @@
1
- function reconnectDelay(attempt) {
2
- return Math.min(1e3 * 2 ** (attempt - 1), 3e4);
1
+ import { WebSocket } from 'partysocket';
2
+ export { WebSocket as ReconnectingWebSocket } from 'partysocket';
3
+
4
+ function createWs(url) {
5
+ return new WebSocket(url, [], {
6
+ maxReconnectionDelay: 3e4,
7
+ minReconnectionDelay: 1e3,
8
+ reconnectionDelayGrowFactor: 2,
9
+ connectionTimeout: 1e4
10
+ });
3
11
  }
4
- function connectWs(url) {
12
+ function waitForOpen(ws) {
13
+ if (ws.readyState === WebSocket.OPEN) return Promise.resolve();
5
14
  return new Promise((resolve, reject) => {
6
- const ws = new WebSocket(url);
7
- ws.onopen = () => resolve(ws);
8
- ws.onerror = () => reject(new Error(`WebSocket connection failed: ${url}`));
15
+ const onOpen = () => {
16
+ ws.removeEventListener("error", onError);
17
+ resolve();
18
+ };
19
+ const onError = () => {
20
+ ws.removeEventListener("open", onOpen);
21
+ ws.close();
22
+ reject(new Error("WebSocket connection failed"));
23
+ };
24
+ ws.addEventListener("open", onOpen, { once: true });
25
+ ws.addEventListener("error", onError, { once: true });
9
26
  });
10
27
  }
11
28
  function sendWs(ws, msg) {
@@ -22,4 +39,4 @@ function clearIntervalTimer(timer) {
22
39
  return null;
23
40
  }
24
41
 
25
- export { clearIntervalTimer, clearTimer, connectWs, reconnectDelay, sendWs };
42
+ export { clearIntervalTimer, clearTimer, createWs, sendWs, waitForOpen };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pear-protocol/exchanges-sdk",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "description": "Pear Protocol Exchanges SDK",
5
5
  "private": false,
6
6
  "type": "module",
@@ -25,10 +25,10 @@
25
25
  "typecheck": "tsc --noEmit"
26
26
  },
27
27
  "dependencies": {
28
- "@nktkas/hyperliquid": "0.30.2",
29
- "@pear-protocol/core-sdk": "0.0.11",
30
- "@pear-protocol/types": "0.0.10",
31
- "viem": "^2.43.5"
28
+ "@pear-protocol/core-sdk": "0.0.12",
29
+ "@pear-protocol/types": "0.0.11",
30
+ "@pear-protocol/utils": "0.0.11",
31
+ "partysocket": "^1.0.3"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@backend/typescript-config": "workspace:*",
@@ -1,6 +0,0 @@
1
- import { HyperliquidAccountCredentials } from '../../types.js';
2
- import '@pear-protocol/types';
3
-
4
- declare function setLeverage(creds: HyperliquidAccountCredentials, asset: number, leverage: number, isCross: boolean): Promise<void>;
5
-
6
- export { setLeverage };
@@ -1,11 +0,0 @@
1
- import { HttpTransport, ExchangeClient } from '@nktkas/hyperliquid';
2
- import { privateKeyToAccount } from 'viem/accounts';
3
-
4
- async function setLeverage(creds, asset, leverage, isCross) {
5
- const transport = new HttpTransport();
6
- const wallet = privateKeyToAccount(creds.signer_key);
7
- const exchangeClient = new ExchangeClient({ transport, wallet });
8
- await exchangeClient.updateLeverage({ asset, leverage, isCross });
9
- }
10
-
11
- export { setLeverage };