@pear-protocol/exchanges-sdk 0.1.6 → 0.1.8

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/README.md CHANGED
@@ -27,8 +27,15 @@ import { ExchangesSDK } from '@pear-protocol/exchanges-sdk';
27
27
  const sdk = new PearSDK({ /* ... */ });
28
28
  const exchanges = new ExchangesSDK({ sdk });
29
29
 
30
- const connection = await exchanges.connect(tradeAccountId, 'binance');
30
+ const connection = await exchanges.connect(tradeAccountId);
31
+
32
+ const status = await exchanges.status(connection);
33
+ if (status.status === 'warning') {
34
+ console.warn(status.reasons);
35
+ }
36
+
31
37
  const tracker = exchanges.createTracker(connection);
38
+ const syncer = exchanges.createSyncer(connection);
32
39
  await tracker.start();
33
40
 
34
41
  const offBalance = tracker.trackBalance((balance) => {
@@ -51,6 +58,59 @@ Pass `demo: true` to target testnet:
51
58
  const exchanges = new ExchangesSDK({ sdk, demo: true });
52
59
  ```
53
60
 
61
+ ## Connection
62
+
63
+ `connect(tradeAccountId)` fetches decrypted credentials and the trade account from the core API, then opens the exchange websocket.
64
+
65
+ The connector comes from the backend trade account.
66
+
67
+ ```typescript
68
+ const connection = await exchanges.connect(tradeAccountId);
69
+
70
+ connection.exchange; // "hyperliquid"
71
+ connection.account.exchangeIdentifier;
72
+ connection.credentials;
73
+ connection.ws;
74
+ ```
75
+
76
+ For exchanges that need account health checks, pass status options when creating the SDK:
77
+
78
+ ```typescript
79
+ const exchanges = new ExchangesSDK({
80
+ sdk,
81
+ options: {
82
+ builderAddress: '0x...',
83
+ integratorAccountIndex: 1234,
84
+ },
85
+ });
86
+ ```
87
+
88
+ `builderAddress` is used to check Hyperliquid builder fee approval. `integratorAccountIndex` is used to check Lighter integrator approval.
89
+
90
+ ## Account Status
91
+
92
+ Use `status(connection)` to check whether the account is ready for trading through PEAR.
93
+
94
+ ```typescript
95
+ const status = await exchanges.status(connection);
96
+
97
+ if (status.status === 'warning') {
98
+ for (const reason of status.reasons) {
99
+ console.warn(reason);
100
+ }
101
+ }
102
+ ```
103
+
104
+ Possible warning reasons:
105
+
106
+ | Reason | Meaning |
107
+ |--------|---------|
108
+ | `api_key_invalid` | API credentials failed the exchange account check |
109
+ | `hyperliquid_api_wallet_invalid` | Hyperliquid agent wallet is missing or invalid |
110
+ | `hyperliquid_builder_fee_not_approved` | Hyperliquid builder fee approval is missing |
111
+ | `lighter_api_key_invalid` | Lighter API key cannot create an auth token |
112
+ | `lighter_integrator_not_approved` | Lighter integrator approval is missing |
113
+
54
114
  ## Tracking
55
115
 
56
116
  Each `track*` method returns an unsubscribe function. Callbacks fire with the current snapshot on subscribe (if available) and on every update.
@@ -143,6 +203,7 @@ ACCOUNT_TYPE_LABELS[balance.accountType]; // "Cross Margin", "Portfolio Margin",
143
203
 
144
204
  ```typescript
145
205
  await tracker.disconnect();
206
+ connection.ws.destroy();
146
207
  ```
147
208
 
148
- Credentials are fetched automatically on `connect`; for Hyperliquid and Lighter the account index is resolved from the backend.
209
+ Credentials and the trade account are fetched automatically on `connect`; Hyperliquid and Lighter account identifiers come from `connection.account.exchangeIdentifier`.
@@ -16,6 +16,23 @@ async function fetchAccount(accountIndex) {
16
16
  if (!res.ok) throw new Error(`Lighter /account returned ${res.status}`);
17
17
  return res.json();
18
18
  }
19
+ async function fetchAccountMetadata(accountIndex, authToken) {
20
+ const res = await fetch(`${LIGHTER_BASE_URL}/api/v1/accountMetadata?by=index&value=${accountIndex}`, {
21
+ headers: {
22
+ accept: "application/json",
23
+ authorization: authToken
24
+ }
25
+ });
26
+ if (!res.ok) throw new Error(`Lighter /accountMetadata returned ${res.status}`);
27
+ const data = await res.json();
28
+ if (data.code !== 200) {
29
+ throw new Error(`Lighter /accountMetadata returned ${data.code}: ${data.message ?? "unknown error"}`);
30
+ }
31
+ const matchesAccount = data.account_metadatas?.some((metadata) => metadata.account_index === accountIndex);
32
+ if (!matchesAccount) {
33
+ throw new Error(`Lighter /accountMetadata did not return account ${accountIndex}`);
34
+ }
35
+ }
19
36
  async function fetchOrderBookDetails() {
20
37
  const res = await fetch(`${LIGHTER_BASE_URL}/api/v1/orderBookDetails?filter=perp`);
21
38
  if (!res.ok) throw new Error(`Lighter /orderBookDetails returned ${res.status}`);
@@ -58,4 +75,4 @@ async function fetchTradesSince(accountIndex, authToken, since) {
58
75
  return Array.from(byId.values());
59
76
  }
60
77
 
61
- export { fetchAccount, fetchAccountsByL1Address, fetchApiKeys, fetchOrderBookDetails, fetchTradesSince };
78
+ export { fetchAccount, fetchAccountMetadata, fetchAccountsByL1Address, fetchApiKeys, fetchOrderBookDetails, fetchTradesSince };
@@ -1,11 +1,12 @@
1
+ import { createHyperliquidHelper } from './hyperliquid';
1
2
  import { createLighterHelper } from './lighter';
2
3
 
3
- function createExchangeHelper(exchange, sdk) {
4
- switch (exchange) {
4
+ function createExchangeHelper(config) {
5
+ switch (config.exchange) {
6
+ case "hyperliquid":
7
+ return createHyperliquidHelper();
5
8
  case "lighter":
6
- return createLighterHelper(sdk);
7
- default:
8
- throw new Error(`Helpers not available for exchange: ${exchange}`);
9
+ return createLighterHelper(config.sdk);
9
10
  }
10
11
  }
11
12
 
@@ -0,0 +1,23 @@
1
+ import { fetchInfo } from '../../client/hyperliquid/rest';
2
+
3
+ function createHyperliquidHelper() {
4
+ return {
5
+ async checkApiWallet(params) {
6
+ const agents = await fetchInfo({
7
+ type: "extraAgents",
8
+ user: params.userAddress
9
+ });
10
+ return agents.some((agent) => agent.address.toLowerCase() === params.agentWalletAddress.toLowerCase());
11
+ },
12
+ async checkBuilderFeeApproval(userAddress, builderAddress) {
13
+ const maxBuilderFee = await fetchInfo({
14
+ type: "maxBuilderFee",
15
+ user: userAddress,
16
+ builder: builderAddress
17
+ });
18
+ return Number(maxBuilderFee) > 0;
19
+ }
20
+ };
21
+ }
22
+
23
+ export { createHyperliquidHelper };
@@ -0,0 +1 @@
1
+ export { createHyperliquidHelper } from './helper';
@@ -0,0 +1 @@
1
+
@@ -1,2 +1,3 @@
1
+ export * from './hyperliquid';
1
2
  export * from './lighter';
2
3
  export * from './types';