@pear-protocol/exchanges-sdk 0.0.17 → 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.
- package/README.md +73 -137
- package/dist/client/base-ws.d.ts +30 -0
- package/dist/client/base-ws.js +113 -0
- package/dist/client/binance/exchange-ws.d.ts +19 -0
- package/dist/client/binance/exchange-ws.js +26 -0
- package/dist/{account/exchanges → client}/binance/rest.d.ts +2 -6
- package/dist/{account/exchanges → client}/binance/rest.js +4 -11
- package/dist/client/binance/types.d.ts +68 -0
- package/dist/client/binance/types.js +1 -0
- package/dist/client/binance/ws.d.ts +30 -0
- package/dist/client/binance/ws.js +54 -0
- package/dist/client/bybit/exchange-ws.d.ts +20 -0
- package/dist/client/bybit/exchange-ws.js +28 -0
- package/dist/{account/exchanges → client}/bybit/rest.js +3 -2
- package/dist/{account/exchanges → client}/bybit/types.d.ts +1 -9
- package/dist/client/bybit/types.js +1 -0
- package/dist/client/bybit/ws.d.ts +17 -0
- package/dist/client/bybit/ws.js +50 -0
- package/dist/client/exchange-ws.d.ts +12 -0
- package/dist/client/exchange-ws.js +10 -0
- package/dist/client/hyperliquid/exchange-ws.d.ts +15 -0
- package/dist/client/hyperliquid/exchange-ws.js +21 -0
- package/dist/client/hyperliquid/rest.d.ts +3 -0
- package/dist/client/hyperliquid/rest.js +12 -0
- package/dist/{account/exchanges → client}/hyperliquid/types.d.ts +18 -39
- package/dist/client/hyperliquid/types.js +1 -0
- package/dist/client/hyperliquid/ws.d.ts +10 -0
- package/dist/client/hyperliquid/ws.js +16 -0
- package/dist/client/index.d.ts +31 -0
- package/dist/client/index.js +47 -0
- package/dist/client/lighter/constants.d.ts +4 -0
- package/dist/client/lighter/constants.js +4 -0
- package/dist/client/lighter/exchange-ws.d.ts +15 -0
- package/dist/client/lighter/exchange-ws.js +21 -0
- package/dist/client/lighter/rest.d.ts +56 -0
- package/dist/client/lighter/rest.js +27 -0
- package/dist/client/lighter/types.d.ts +32 -0
- package/dist/client/lighter/types.js +1 -0
- package/dist/client/lighter/ws.d.ts +10 -0
- package/dist/client/lighter/ws.js +17 -0
- package/dist/client/okx/exchange-ws.d.ts +20 -0
- package/dist/client/okx/exchange-ws.js +28 -0
- package/dist/{account/exchanges → client}/okx/rest.js +2 -2
- package/dist/{account/exchanges → client}/okx/types.d.ts +1 -10
- package/dist/client/okx/types.js +1 -0
- package/dist/client/okx/ws.d.ts +17 -0
- package/dist/client/okx/ws.js +61 -0
- package/dist/helper/factory.d.ts +10 -0
- package/dist/helper/factory.js +12 -0
- package/dist/helper/index.d.ts +7 -0
- package/dist/helper/index.js +2 -0
- package/dist/helper/lighter/ethereum/abi.d.ts +66 -0
- package/dist/helper/lighter/ethereum/abi.js +40 -0
- package/dist/helper/lighter/ethereum/index.d.ts +8 -0
- package/dist/helper/lighter/ethereum/index.js +34 -0
- package/dist/helper/lighter/ethereum/types.d.ts +23 -0
- package/dist/helper/lighter/ethereum/types.js +1 -0
- package/dist/helper/lighter/helper.d.ts +9 -0
- package/dist/helper/lighter/helper.js +67 -0
- package/dist/helper/lighter/index.d.ts +6 -0
- package/dist/helper/lighter/index.js +2 -0
- package/dist/helper/lighter/types.d.ts +63 -0
- package/dist/helper/lighter/types.js +35 -0
- package/dist/helper/types.d.ts +11 -0
- package/dist/helper/types.js +1 -0
- package/dist/index.d.ts +37 -24
- package/dist/index.js +6 -21
- package/dist/sdk.d.ts +43 -0
- package/dist/sdk.js +52 -0
- package/dist/syncer/base.d.ts +33 -0
- package/dist/syncer/base.js +14 -0
- package/dist/syncer/index.d.ts +18 -0
- package/dist/syncer/index.js +1 -0
- package/dist/tracker/base.d.ts +79 -0
- package/dist/tracker/base.js +120 -0
- package/dist/tracker/binance/const.d.ts +4 -0
- package/dist/tracker/binance/const.js +4 -0
- package/dist/{account/exchanges → tracker}/binance/mapper.d.ts +4 -1
- package/dist/{account/exchanges → tracker}/binance/mapper.js +7 -6
- package/dist/tracker/binance/tracker.d.ts +60 -0
- package/dist/tracker/binance/tracker.js +181 -0
- package/dist/tracker/binance/types.d.ts +6 -0
- package/dist/{account/exchanges → tracker}/bybit/mapper.d.ts +5 -3
- package/dist/{account/exchanges → tracker}/bybit/mapper.js +11 -10
- package/dist/tracker/bybit/tracker.d.ts +51 -0
- package/dist/tracker/bybit/tracker.js +138 -0
- package/dist/tracker/bybit/types.d.ts +7 -0
- package/dist/{account/exchanges → tracker}/hyperliquid/mapper.d.ts +6 -4
- package/dist/{account/exchanges → tracker}/hyperliquid/mapper.js +18 -16
- package/dist/tracker/hyperliquid/tracker.d.ts +62 -0
- package/dist/tracker/hyperliquid/tracker.js +219 -0
- package/dist/tracker/hyperliquid/types.d.ts +23 -0
- package/dist/tracker/index.d.ts +38 -0
- package/dist/tracker/index.js +81 -0
- package/dist/tracker/lighter/const.d.ts +3 -0
- package/dist/tracker/lighter/const.js +3 -0
- package/dist/tracker/lighter/mapper.d.ts +16 -0
- package/dist/tracker/lighter/mapper.js +77 -0
- package/dist/tracker/lighter/tracker.d.ts +53 -0
- package/dist/tracker/lighter/tracker.js +150 -0
- package/dist/tracker/lighter/types.d.ts +16 -0
- package/dist/tracker/lighter/types.js +6 -0
- package/dist/{account/exchanges → tracker}/okx/mapper.d.ts +5 -3
- package/dist/{account/exchanges → tracker}/okx/mapper.js +11 -10
- package/dist/tracker/okx/tracker.d.ts +51 -0
- package/dist/tracker/okx/tracker.js +134 -0
- package/dist/tracker/okx/types.d.ts +8 -0
- package/dist/tracker/types.d.ts +63 -0
- package/dist/{account → tracker}/types.js +6 -4
- package/dist/tracker/utils.d.ts +11 -0
- package/dist/{account → tracker}/utils.js +0 -2
- package/dist/types.d.ts +50 -0
- package/dist/types.js +1 -0
- package/package.json +8 -4
- package/dist/account/account-manager.d.ts +0 -31
- package/dist/account/account-manager.js +0 -119
- package/dist/account/exchange-account.d.ts +0 -35
- package/dist/account/exchange-account.js +0 -158
- package/dist/account/exchanges/base.d.ts +0 -48
- package/dist/account/exchanges/base.js +0 -41
- package/dist/account/exchanges/binance/asset-tracker.d.ts +0 -26
- package/dist/account/exchanges/binance/asset-tracker.js +0 -38
- package/dist/account/exchanges/binance/const.d.ts +0 -10
- package/dist/account/exchanges/binance/const.js +0 -10
- package/dist/account/exchanges/binance/orchestrator.d.ts +0 -31
- package/dist/account/exchanges/binance/orchestrator.js +0 -91
- package/dist/account/exchanges/binance/state-handler.d.ts +0 -33
- package/dist/account/exchanges/binance/state-handler.js +0 -113
- package/dist/account/exchanges/binance/types.d.ts +0 -5
- package/dist/account/exchanges/binance/ws.d.ts +0 -23
- package/dist/account/exchanges/binance/ws.js +0 -69
- package/dist/account/exchanges/bybit/asset-tracker.d.ts +0 -26
- package/dist/account/exchanges/bybit/asset-tracker.js +0 -37
- package/dist/account/exchanges/bybit/const.d.ts +0 -7
- package/dist/account/exchanges/bybit/const.js +0 -7
- package/dist/account/exchanges/bybit/orchestrator.d.ts +0 -22
- package/dist/account/exchanges/bybit/orchestrator.js +0 -66
- package/dist/account/exchanges/bybit/state-handler.d.ts +0 -23
- package/dist/account/exchanges/bybit/state-handler.js +0 -70
- package/dist/account/exchanges/bybit/ws.d.ts +0 -19
- package/dist/account/exchanges/bybit/ws.js +0 -83
- package/dist/account/exchanges/hyperliquid/asset-tracker.d.ts +0 -35
- package/dist/account/exchanges/hyperliquid/asset-tracker.js +0 -83
- package/dist/account/exchanges/hyperliquid/orchestrator.d.ts +0 -26
- package/dist/account/exchanges/hyperliquid/orchestrator.js +0 -102
- package/dist/account/exchanges/hyperliquid/state-handler.d.ts +0 -25
- package/dist/account/exchanges/hyperliquid/state-handler.js +0 -104
- package/dist/account/exchanges/index.d.ts +0 -11
- package/dist/account/exchanges/index.js +0 -32
- package/dist/account/exchanges/okx/asset-tracker.d.ts +0 -26
- package/dist/account/exchanges/okx/asset-tracker.js +0 -34
- package/dist/account/exchanges/okx/const.d.ts +0 -7
- package/dist/account/exchanges/okx/const.js +0 -7
- package/dist/account/exchanges/okx/orchestrator.d.ts +0 -22
- package/dist/account/exchanges/okx/orchestrator.js +0 -57
- package/dist/account/exchanges/okx/state-handler.d.ts +0 -24
- package/dist/account/exchanges/okx/state-handler.js +0 -78
- package/dist/account/exchanges/okx/ws.d.ts +0 -20
- package/dist/account/exchanges/okx/ws.js +0 -103
- package/dist/account/index.d.ts +0 -11
- package/dist/account/index.js +0 -4
- package/dist/account/types.d.ts +0 -5
- package/dist/account/utils.d.ts +0 -10
- package/dist/credentials.d.ts +0 -28
- package/dist/credentials.js +0 -46
- package/dist/types-Caz2X4h0.d.ts +0 -151
- package/dist/utils/ws.d.ts +0 -10
- package/dist/utils/ws.js +0 -42
- /package/dist/{account/exchanges → client}/bybit/rest.d.ts +0 -0
- /package/dist/{utils → client}/hmac.d.ts +0 -0
- /package/dist/{utils → client}/hmac.js +0 -0
- /package/dist/{account/exchanges → client}/okx/rest.d.ts +0 -0
- /package/dist/{account/exchanges → tracker}/binance/types.js +0 -0
- /package/dist/{account/exchanges → tracker}/bybit/types.js +0 -0
- /package/dist/{account/exchanges → tracker}/hyperliquid/const.d.ts +0 -0
- /package/dist/{account/exchanges → tracker}/hyperliquid/const.js +0 -0
- /package/dist/{account/exchanges → tracker}/hyperliquid/types.js +0 -0
- /package/dist/{account/exchanges → tracker}/okx/types.js +0 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { BybitCredentialsDTO } from '@pear-protocol/types';
|
|
2
|
+
import { BaseWs } from '../base-ws.js';
|
|
3
|
+
import { ExchangeWs } from '../exchange-ws.js';
|
|
4
|
+
import { BybitWs } from './ws.js';
|
|
5
|
+
import 'partysocket';
|
|
6
|
+
|
|
7
|
+
declare class BybitExchangeWs extends ExchangeWs {
|
|
8
|
+
main: BybitWs;
|
|
9
|
+
private readonly creds;
|
|
10
|
+
readonly exchange: "bybit";
|
|
11
|
+
constructor(main: BybitWs, creds: BybitCredentialsDTO);
|
|
12
|
+
protected connections(): BaseWs[];
|
|
13
|
+
authenticate(): Promise<void>;
|
|
14
|
+
static create(config: {
|
|
15
|
+
demo: boolean;
|
|
16
|
+
credentials: BybitCredentialsDTO;
|
|
17
|
+
}): Promise<BybitExchangeWs>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { BybitExchangeWs };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { ExchangeWs } from '../exchange-ws';
|
|
2
|
+
import { BybitWs } from './ws';
|
|
3
|
+
|
|
4
|
+
class BybitExchangeWs extends ExchangeWs {
|
|
5
|
+
constructor(main, creds) {
|
|
6
|
+
super();
|
|
7
|
+
this.main = main;
|
|
8
|
+
this.creds = creds;
|
|
9
|
+
}
|
|
10
|
+
main;
|
|
11
|
+
creds;
|
|
12
|
+
exchange = "bybit";
|
|
13
|
+
connections() {
|
|
14
|
+
return [this.main];
|
|
15
|
+
}
|
|
16
|
+
authenticate() {
|
|
17
|
+
return this.main.authenticate(this.creds);
|
|
18
|
+
}
|
|
19
|
+
static async create(config) {
|
|
20
|
+
const main = new BybitWs({ demo: config.demo });
|
|
21
|
+
await main.connect();
|
|
22
|
+
const wrapper = new BybitExchangeWs(main, config.credentials);
|
|
23
|
+
await wrapper.authenticate();
|
|
24
|
+
return wrapper;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export { BybitExchangeWs };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { hmacSha256Hex } from '
|
|
2
|
-
import { BASE_URL_DEMO, BASE_URL } from './const';
|
|
1
|
+
import { hmacSha256Hex } from '../hmac';
|
|
3
2
|
|
|
3
|
+
const BASE_URL = "https://api.bybit.com";
|
|
4
|
+
const BASE_URL_DEMO = "https://api-demo.bybit.com";
|
|
4
5
|
async function signedGet(creds, path, params, demo) {
|
|
5
6
|
const baseUrl = demo ? BASE_URL_DEMO : BASE_URL;
|
|
6
7
|
const timestamp = Date.now().toString();
|
|
@@ -23,11 +23,6 @@ interface BybitRawWallet {
|
|
|
23
23
|
accountMMRate: string;
|
|
24
24
|
coin: BybitRawCoin[];
|
|
25
25
|
}
|
|
26
|
-
declare enum BybitAccountType {
|
|
27
|
-
ISOLATED_MARGIN = "ISOLATED_MARGIN",
|
|
28
|
-
REGULAR_MARGIN = "REGULAR_MARGIN",
|
|
29
|
-
PORTFOLIO_MARGIN = "PORTFOLIO_MARGIN"
|
|
30
|
-
}
|
|
31
26
|
interface BybitRawAccountInfo {
|
|
32
27
|
marginMode: string;
|
|
33
28
|
updatedTime: string;
|
|
@@ -63,8 +58,5 @@ type BybitWsMessage = {
|
|
|
63
58
|
topic: 'position';
|
|
64
59
|
data: BybitRawPosition[];
|
|
65
60
|
};
|
|
66
|
-
interface BybitWsHandlers {
|
|
67
|
-
onMessage: (msg: BybitWsMessage) => void;
|
|
68
|
-
}
|
|
69
61
|
|
|
70
|
-
export {
|
|
62
|
+
export type { BybitRawAccountInfo, BybitRawCoin, BybitRawCollateralRatioEntry, BybitRawCollateralTier, BybitRawPosition, BybitRawWallet, BybitResponse, BybitWsMessage };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { BybitCredentialsDTO } from '@pear-protocol/types';
|
|
2
|
+
import { BaseWs, WsMessage } from '../base-ws.js';
|
|
3
|
+
import 'partysocket';
|
|
4
|
+
|
|
5
|
+
type BybitWsConfig = {
|
|
6
|
+
demo: boolean;
|
|
7
|
+
};
|
|
8
|
+
declare class BybitWs extends BaseWs {
|
|
9
|
+
private readonly demo;
|
|
10
|
+
constructor(config: BybitWsConfig);
|
|
11
|
+
protected getWsUrl(): string;
|
|
12
|
+
protected getPingIntervalMs(): number;
|
|
13
|
+
protected buildPingMessage(): WsMessage;
|
|
14
|
+
authenticate(creds: BybitCredentialsDTO): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { BybitWs, type BybitWsConfig };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { BaseWs } from '../base-ws';
|
|
2
|
+
import { hmacSha256Hex } from '../hmac';
|
|
3
|
+
|
|
4
|
+
const WS_URL = "wss://stream.bybit.com/v5/private";
|
|
5
|
+
const WS_URL_DEMO = "wss://stream-demo.bybit.com/v5/private";
|
|
6
|
+
const PING_INTERVAL_MS = 2e4;
|
|
7
|
+
const AUTH_TIMEOUT_MS = 1e4;
|
|
8
|
+
class BybitWs extends BaseWs {
|
|
9
|
+
demo;
|
|
10
|
+
constructor(config) {
|
|
11
|
+
super();
|
|
12
|
+
this.demo = config.demo;
|
|
13
|
+
}
|
|
14
|
+
getWsUrl() {
|
|
15
|
+
return this.demo ? WS_URL_DEMO : WS_URL;
|
|
16
|
+
}
|
|
17
|
+
getPingIntervalMs() {
|
|
18
|
+
return PING_INTERVAL_MS;
|
|
19
|
+
}
|
|
20
|
+
buildPingMessage() {
|
|
21
|
+
return { op: "ping" };
|
|
22
|
+
}
|
|
23
|
+
async authenticate(creds) {
|
|
24
|
+
const expires = Date.now() + AUTH_TIMEOUT_MS;
|
|
25
|
+
const signature = await hmacSha256Hex(creds.api_secret, `GET/realtime${expires}`);
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
let listenerId = "";
|
|
28
|
+
const timer = setTimeout(() => {
|
|
29
|
+
this.removeMessageListener(listenerId);
|
|
30
|
+
reject(new Error("Bybit auth timeout"));
|
|
31
|
+
}, AUTH_TIMEOUT_MS);
|
|
32
|
+
listenerId = this.addMessageListener((raw) => {
|
|
33
|
+
let msg;
|
|
34
|
+
try {
|
|
35
|
+
msg = JSON.parse(raw);
|
|
36
|
+
} catch {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (msg.op !== "auth") return;
|
|
40
|
+
clearTimeout(timer);
|
|
41
|
+
this.removeMessageListener(listenerId);
|
|
42
|
+
if (msg.success) resolve();
|
|
43
|
+
else reject(new Error(`Bybit auth failed: ${msg.ret_msg}`));
|
|
44
|
+
});
|
|
45
|
+
this.send({ op: "auth", args: [creds.api_key, expires, signature] });
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export { BybitWs };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Connector } from '@pear-protocol/types';
|
|
2
|
+
import { BaseWs } from './base-ws.js';
|
|
3
|
+
import 'partysocket';
|
|
4
|
+
|
|
5
|
+
declare abstract class ExchangeWs {
|
|
6
|
+
abstract readonly exchange: Connector;
|
|
7
|
+
protected abstract connections(): BaseWs[];
|
|
8
|
+
destroy(): void;
|
|
9
|
+
get isConnected(): boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export { ExchangeWs };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { BaseWs } from '../base-ws.js';
|
|
2
|
+
import { ExchangeWs } from '../exchange-ws.js';
|
|
3
|
+
import { HyperliquidWs } from './ws.js';
|
|
4
|
+
import 'partysocket';
|
|
5
|
+
import '@pear-protocol/types';
|
|
6
|
+
|
|
7
|
+
declare class HyperliquidExchangeWs extends ExchangeWs {
|
|
8
|
+
main: HyperliquidWs;
|
|
9
|
+
readonly exchange: "hyperliquid";
|
|
10
|
+
constructor(main: HyperliquidWs);
|
|
11
|
+
protected connections(): BaseWs[];
|
|
12
|
+
static create(): Promise<HyperliquidExchangeWs>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { HyperliquidExchangeWs };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ExchangeWs } from '../exchange-ws';
|
|
2
|
+
import { HyperliquidWs } from './ws';
|
|
3
|
+
|
|
4
|
+
class HyperliquidExchangeWs extends ExchangeWs {
|
|
5
|
+
constructor(main) {
|
|
6
|
+
super();
|
|
7
|
+
this.main = main;
|
|
8
|
+
}
|
|
9
|
+
main;
|
|
10
|
+
exchange = "hyperliquid";
|
|
11
|
+
connections() {
|
|
12
|
+
return [this.main];
|
|
13
|
+
}
|
|
14
|
+
static async create() {
|
|
15
|
+
const main = new HyperliquidWs();
|
|
16
|
+
await main.connect();
|
|
17
|
+
return new HyperliquidExchangeWs(main);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { HyperliquidExchangeWs };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const HL_INFO_URL = "https://api.hyperliquid.xyz/info";
|
|
2
|
+
async function fetchInfo(body) {
|
|
3
|
+
const res = await fetch(HL_INFO_URL, {
|
|
4
|
+
method: "POST",
|
|
5
|
+
headers: { "Content-Type": "application/json" },
|
|
6
|
+
body: JSON.stringify(body)
|
|
7
|
+
});
|
|
8
|
+
if (!res.ok) throw new Error(`Hyperliquid info request failed: ${res.status}`);
|
|
9
|
+
return res.json();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export { fetchInfo };
|
|
@@ -1,24 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
coin: string;
|
|
10
|
-
total: number;
|
|
11
|
-
hold: number;
|
|
12
|
-
}
|
|
13
|
-
interface AggregatedClearinghouseSummary {
|
|
14
|
-
accountValue: number;
|
|
15
|
-
totalMarginUsed: number;
|
|
16
|
-
totalRawUsd: number;
|
|
17
|
-
crossMaintenanceMarginUsed: number;
|
|
18
|
-
withdrawable: number;
|
|
19
|
-
totalUnrealizedPnl: number;
|
|
20
|
-
}
|
|
21
|
-
declare const COLLATERAL_TOKEN_MAP: Record<number, string>;
|
|
1
|
+
type LeverageInfo = {
|
|
2
|
+
type: 'isolated';
|
|
3
|
+
value: number;
|
|
4
|
+
rawUsd: string;
|
|
5
|
+
} | {
|
|
6
|
+
type: 'cross';
|
|
7
|
+
value: number;
|
|
8
|
+
};
|
|
22
9
|
type MetaResponse = {
|
|
23
10
|
universe: {
|
|
24
11
|
szDecimals: number;
|
|
@@ -41,14 +28,6 @@ type MetaResponse = {
|
|
|
41
28
|
collateralToken: number;
|
|
42
29
|
};
|
|
43
30
|
type AllPerpMetasResponse = MetaResponse[];
|
|
44
|
-
type LeverageInfo = {
|
|
45
|
-
type: 'isolated';
|
|
46
|
-
value: number;
|
|
47
|
-
rawUsd: string;
|
|
48
|
-
} | {
|
|
49
|
-
type: 'cross';
|
|
50
|
-
value: number;
|
|
51
|
-
};
|
|
52
31
|
type ClearinghouseStateResponse = {
|
|
53
32
|
marginSummary: {
|
|
54
33
|
accountValue: string;
|
|
@@ -87,6 +66,15 @@ type ClearinghouseStateResponse = {
|
|
|
87
66
|
time: number;
|
|
88
67
|
};
|
|
89
68
|
type AssetPosition = ClearinghouseStateResponse['assetPositions'][number];
|
|
69
|
+
type SpotClearinghouseStateResponse = {
|
|
70
|
+
balances: {
|
|
71
|
+
coin: string;
|
|
72
|
+
token: number;
|
|
73
|
+
total: string;
|
|
74
|
+
hold: string;
|
|
75
|
+
entryNtl: string;
|
|
76
|
+
}[];
|
|
77
|
+
};
|
|
90
78
|
type AllDexsClearinghouseStateEvent = {
|
|
91
79
|
user: `0x${string}`;
|
|
92
80
|
clearinghouseStates: [dex: string, state: ClearinghouseStateResponse][];
|
|
@@ -99,15 +87,6 @@ type ActiveAssetDataEvent = {
|
|
|
99
87
|
availableToTrade: [min: string, max: string];
|
|
100
88
|
markPx: string;
|
|
101
89
|
};
|
|
102
|
-
type SpotClearinghouseStateResponse = {
|
|
103
|
-
balances: {
|
|
104
|
-
coin: string;
|
|
105
|
-
token: number;
|
|
106
|
-
total: string;
|
|
107
|
-
hold: string;
|
|
108
|
-
entryNtl: string;
|
|
109
|
-
}[];
|
|
110
|
-
};
|
|
111
90
|
type SpotStateEvent = {
|
|
112
91
|
user: `0x${string}`;
|
|
113
92
|
spotState: SpotClearinghouseStateResponse;
|
|
@@ -131,4 +110,4 @@ type WebData3Event = {
|
|
|
131
110
|
}[];
|
|
132
111
|
};
|
|
133
112
|
|
|
134
|
-
export {
|
|
113
|
+
export type { ActiveAssetDataEvent, AllDexsClearinghouseStateEvent, AllPerpMetasResponse, AssetPosition, ClearinghouseStateResponse, LeverageInfo, MetaResponse, SpotClearinghouseStateResponse, SpotStateEvent, WebData3Event };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { BaseWs } from '../base-ws';
|
|
2
|
+
|
|
3
|
+
const HL_WS_URL = "wss://api.hyperliquid.xyz/ws";
|
|
4
|
+
class HyperliquidWs extends BaseWs {
|
|
5
|
+
getWsUrl() {
|
|
6
|
+
return HL_WS_URL;
|
|
7
|
+
}
|
|
8
|
+
getPingIntervalMs() {
|
|
9
|
+
return 0;
|
|
10
|
+
}
|
|
11
|
+
buildPingMessage() {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { HyperliquidWs };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Connector } from '@pear-protocol/types';
|
|
2
|
+
import { SupportedCredentials } from '../types.js';
|
|
3
|
+
import { BinanceExchangeWs } from './binance/exchange-ws.js';
|
|
4
|
+
import { BybitExchangeWs } from './bybit/exchange-ws.js';
|
|
5
|
+
import { HyperliquidExchangeWs } from './hyperliquid/exchange-ws.js';
|
|
6
|
+
import { LighterExchangeWs } from './lighter/exchange-ws.js';
|
|
7
|
+
import { OkxExchangeWs } from './okx/exchange-ws.js';
|
|
8
|
+
export { BaseWs, MessageListener, OpenListener, WsMessage } from './base-ws.js';
|
|
9
|
+
export { BinanceUserDataWs, BinanceUserDataWsConfig, BinanceWsApi, BinanceWsApiConfig } from './binance/ws.js';
|
|
10
|
+
export { BybitWs, BybitWsConfig } from './bybit/ws.js';
|
|
11
|
+
export { ExchangeWs } from './exchange-ws.js';
|
|
12
|
+
export { HyperliquidWs } from './hyperliquid/ws.js';
|
|
13
|
+
export { LighterWs } from './lighter/ws.js';
|
|
14
|
+
export { OkxWs, OkxWsConfig } from './okx/ws.js';
|
|
15
|
+
import '@pear-protocol/core-sdk';
|
|
16
|
+
import 'partysocket';
|
|
17
|
+
|
|
18
|
+
type ExchangeWsByConnector = {
|
|
19
|
+
binance: BinanceExchangeWs;
|
|
20
|
+
bybit: BybitExchangeWs;
|
|
21
|
+
okx: OkxExchangeWs;
|
|
22
|
+
hyperliquid: HyperliquidExchangeWs;
|
|
23
|
+
lighter: LighterExchangeWs;
|
|
24
|
+
};
|
|
25
|
+
type CreateExchangeWsConfig = {
|
|
26
|
+
demo: boolean;
|
|
27
|
+
credentials: SupportedCredentials;
|
|
28
|
+
};
|
|
29
|
+
declare function createExchangeWs<E extends Connector>(exchange: E, config: CreateExchangeWsConfig): Promise<ExchangeWsByConnector[E]>;
|
|
30
|
+
|
|
31
|
+
export { BinanceExchangeWs, BybitExchangeWs, type CreateExchangeWsConfig, type ExchangeWsByConnector, HyperliquidExchangeWs, LighterExchangeWs, OkxExchangeWs, createExchangeWs };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { BinanceExchangeWs } from './binance/exchange-ws';
|
|
2
|
+
export { BinanceExchangeWs } from './binance/exchange-ws';
|
|
3
|
+
import { BybitExchangeWs } from './bybit/exchange-ws';
|
|
4
|
+
export { BybitExchangeWs } from './bybit/exchange-ws';
|
|
5
|
+
import { HyperliquidExchangeWs } from './hyperliquid/exchange-ws';
|
|
6
|
+
export { HyperliquidExchangeWs } from './hyperliquid/exchange-ws';
|
|
7
|
+
import { LighterExchangeWs } from './lighter/exchange-ws';
|
|
8
|
+
export { LighterExchangeWs } from './lighter/exchange-ws';
|
|
9
|
+
import { OkxExchangeWs } from './okx/exchange-ws';
|
|
10
|
+
export { OkxExchangeWs } from './okx/exchange-ws';
|
|
11
|
+
export { BaseWs } from './base-ws';
|
|
12
|
+
export { BinanceUserDataWs, BinanceWsApi } from './binance/ws';
|
|
13
|
+
export { BybitWs } from './bybit/ws';
|
|
14
|
+
export { ExchangeWs } from './exchange-ws';
|
|
15
|
+
export { HyperliquidWs } from './hyperliquid/ws';
|
|
16
|
+
export { LighterWs } from './lighter/ws';
|
|
17
|
+
export { OkxWs } from './okx/ws';
|
|
18
|
+
|
|
19
|
+
async function createExchangeWs(exchange, config) {
|
|
20
|
+
switch (exchange) {
|
|
21
|
+
case "binance":
|
|
22
|
+
return BinanceExchangeWs.create({
|
|
23
|
+
demo: config.demo,
|
|
24
|
+
credentials: config.credentials
|
|
25
|
+
});
|
|
26
|
+
case "bybit":
|
|
27
|
+
return BybitExchangeWs.create({
|
|
28
|
+
demo: config.demo,
|
|
29
|
+
credentials: config.credentials
|
|
30
|
+
});
|
|
31
|
+
case "okx":
|
|
32
|
+
return OkxExchangeWs.create({
|
|
33
|
+
demo: config.demo,
|
|
34
|
+
credentials: config.credentials
|
|
35
|
+
});
|
|
36
|
+
case "hyperliquid":
|
|
37
|
+
return HyperliquidExchangeWs.create();
|
|
38
|
+
case "lighter":
|
|
39
|
+
return LighterExchangeWs.create();
|
|
40
|
+
default: {
|
|
41
|
+
const _exhaustive = exchange;
|
|
42
|
+
throw new Error(`Unsupported exchange: ${String(_exhaustive)}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export { createExchangeWs };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { BaseWs } from '../base-ws.js';
|
|
2
|
+
import { ExchangeWs } from '../exchange-ws.js';
|
|
3
|
+
import { LighterWs } from './ws.js';
|
|
4
|
+
import 'partysocket';
|
|
5
|
+
import '@pear-protocol/types';
|
|
6
|
+
|
|
7
|
+
declare class LighterExchangeWs extends ExchangeWs {
|
|
8
|
+
main: LighterWs;
|
|
9
|
+
readonly exchange: "lighter";
|
|
10
|
+
constructor(main: LighterWs);
|
|
11
|
+
protected connections(): BaseWs[];
|
|
12
|
+
static create(): Promise<LighterExchangeWs>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { LighterExchangeWs };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ExchangeWs } from '../exchange-ws';
|
|
2
|
+
import { LighterWs } from './ws';
|
|
3
|
+
|
|
4
|
+
class LighterExchangeWs extends ExchangeWs {
|
|
5
|
+
constructor(main) {
|
|
6
|
+
super();
|
|
7
|
+
this.main = main;
|
|
8
|
+
}
|
|
9
|
+
main;
|
|
10
|
+
exchange = "lighter";
|
|
11
|
+
connections() {
|
|
12
|
+
return [this.main];
|
|
13
|
+
}
|
|
14
|
+
static async create() {
|
|
15
|
+
const main = new LighterWs();
|
|
16
|
+
await main.connect();
|
|
17
|
+
return new LighterExchangeWs(main);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { LighterExchangeWs };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
type ApiKey = {
|
|
2
|
+
account_index: number;
|
|
3
|
+
api_key_index: number;
|
|
4
|
+
nonce: number;
|
|
5
|
+
public_key: string;
|
|
6
|
+
transaction_time: number;
|
|
7
|
+
};
|
|
8
|
+
type LighterSubAccount = {
|
|
9
|
+
account_type: number;
|
|
10
|
+
index: number;
|
|
11
|
+
};
|
|
12
|
+
type LighterAccountResponse = {
|
|
13
|
+
code: number;
|
|
14
|
+
message?: string;
|
|
15
|
+
sub_accounts?: LighterSubAccount[];
|
|
16
|
+
};
|
|
17
|
+
type ApprovedIntegrator = {
|
|
18
|
+
account_index: number;
|
|
19
|
+
name: string;
|
|
20
|
+
max_perps_taker_fee: number;
|
|
21
|
+
max_perps_maker_fee: number;
|
|
22
|
+
max_spot_taker_fee: number;
|
|
23
|
+
max_spot_maker_fee: number;
|
|
24
|
+
approval_expiry: number;
|
|
25
|
+
};
|
|
26
|
+
type RestPosition = {
|
|
27
|
+
market_id: number;
|
|
28
|
+
symbol: string;
|
|
29
|
+
initial_margin_fraction: string;
|
|
30
|
+
sign: 1 | -1;
|
|
31
|
+
position: string;
|
|
32
|
+
avg_entry_price: string;
|
|
33
|
+
unrealized_pnl: string;
|
|
34
|
+
liquidation_price: string;
|
|
35
|
+
margin_mode: 0 | 1;
|
|
36
|
+
};
|
|
37
|
+
type DetailedAccountResponse = {
|
|
38
|
+
code: number;
|
|
39
|
+
message?: string;
|
|
40
|
+
accounts: {
|
|
41
|
+
positions: RestPosition[];
|
|
42
|
+
approved_integrators?: ApprovedIntegrator[];
|
|
43
|
+
}[];
|
|
44
|
+
};
|
|
45
|
+
type OrderBookDetail = {
|
|
46
|
+
symbol: string;
|
|
47
|
+
market_id: number;
|
|
48
|
+
status: 'active' | 'inactive';
|
|
49
|
+
default_initial_margin_fraction: number;
|
|
50
|
+
};
|
|
51
|
+
declare function fetchApiKeys(accountIndex: number): Promise<ApiKey[]>;
|
|
52
|
+
declare function fetchAccountsByL1Address(l1Address: string): Promise<LighterAccountResponse>;
|
|
53
|
+
declare function fetchAccount(accountIndex: number): Promise<DetailedAccountResponse>;
|
|
54
|
+
declare function fetchOrderBookDetails(): Promise<OrderBookDetail[]>;
|
|
55
|
+
|
|
56
|
+
export { type ApiKey, type ApprovedIntegrator, type DetailedAccountResponse, type LighterAccountResponse, type LighterSubAccount, type OrderBookDetail, type RestPosition, fetchAccount, fetchAccountsByL1Address, fetchApiKeys, fetchOrderBookDetails };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { LIGHTER_BASE_URL } from './constants';
|
|
2
|
+
|
|
3
|
+
async function fetchApiKeys(accountIndex) {
|
|
4
|
+
const url = `${LIGHTER_BASE_URL}/api/v1/apikeys?account_index=${accountIndex}`;
|
|
5
|
+
const response = await fetch(url);
|
|
6
|
+
if (!response.ok) throw new Error(`Lighter /apikeys returned ${response.status}`);
|
|
7
|
+
const data = await response.json();
|
|
8
|
+
return data.api_keys;
|
|
9
|
+
}
|
|
10
|
+
async function fetchAccountsByL1Address(l1Address) {
|
|
11
|
+
const res = await fetch(`${LIGHTER_BASE_URL}/api/v1/accountsByL1Address?l1_address=${l1Address}`);
|
|
12
|
+
if (!res.ok) throw new Error(`Lighter /accountsByL1Address returned ${res.status}`);
|
|
13
|
+
return res.json();
|
|
14
|
+
}
|
|
15
|
+
async function fetchAccount(accountIndex) {
|
|
16
|
+
const res = await fetch(`${LIGHTER_BASE_URL}/api/v1/account?by=index&value=${accountIndex}`);
|
|
17
|
+
if (!res.ok) throw new Error(`Lighter /account returned ${res.status}`);
|
|
18
|
+
return res.json();
|
|
19
|
+
}
|
|
20
|
+
async function fetchOrderBookDetails() {
|
|
21
|
+
const res = await fetch(`${LIGHTER_BASE_URL}/api/v1/orderBookDetails?filter=perp`);
|
|
22
|
+
if (!res.ok) throw new Error(`Lighter /orderBookDetails returned ${res.status}`);
|
|
23
|
+
const data = await res.json();
|
|
24
|
+
return data.order_book_details;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { fetchAccount, fetchAccountsByL1Address, fetchApiKeys, fetchOrderBookDetails };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
type RawPosition = {
|
|
2
|
+
market_id: number;
|
|
3
|
+
symbol: string;
|
|
4
|
+
initial_margin_fraction: string;
|
|
5
|
+
sign: 1 | -1;
|
|
6
|
+
position: string;
|
|
7
|
+
avg_entry_price: string;
|
|
8
|
+
unrealized_pnl: string;
|
|
9
|
+
liquidation_price: string;
|
|
10
|
+
margin_mode: 0 | 1;
|
|
11
|
+
};
|
|
12
|
+
type RawAsset = {
|
|
13
|
+
symbol: string;
|
|
14
|
+
balance: string;
|
|
15
|
+
locked_balance: string;
|
|
16
|
+
};
|
|
17
|
+
type AccountAllEvent = {
|
|
18
|
+
type: 'subscribed/account_all' | 'update/account_all';
|
|
19
|
+
assets: Record<string, RawAsset> | null;
|
|
20
|
+
positions: Record<string, RawPosition>;
|
|
21
|
+
};
|
|
22
|
+
type UserStatsEvent = {
|
|
23
|
+
type: 'subscribed/user_stats' | 'update/user_stats';
|
|
24
|
+
stats: {
|
|
25
|
+
collateral: string;
|
|
26
|
+
portfolio_value: string;
|
|
27
|
+
available_balance: string;
|
|
28
|
+
margin_usage: string;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type { AccountAllEvent, RawAsset, RawPosition, UserStatsEvent };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { BaseWs, WsMessage } from '../base-ws.js';
|
|
2
|
+
import 'partysocket';
|
|
3
|
+
|
|
4
|
+
declare class LighterWs extends BaseWs {
|
|
5
|
+
protected getWsUrl(): string;
|
|
6
|
+
protected getPingIntervalMs(): number;
|
|
7
|
+
protected buildPingMessage(): WsMessage;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export { LighterWs };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { BaseWs } from '../base-ws';
|
|
2
|
+
import { LIGHTER_WS_URL } from './constants';
|
|
3
|
+
|
|
4
|
+
const PING_INTERVAL_MS = 9e4;
|
|
5
|
+
class LighterWs extends BaseWs {
|
|
6
|
+
getWsUrl() {
|
|
7
|
+
return LIGHTER_WS_URL;
|
|
8
|
+
}
|
|
9
|
+
getPingIntervalMs() {
|
|
10
|
+
return PING_INTERVAL_MS;
|
|
11
|
+
}
|
|
12
|
+
buildPingMessage() {
|
|
13
|
+
return { type: "ping" };
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { LighterWs };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { OkxCredentialsDTO } from '@pear-protocol/types';
|
|
2
|
+
import { BaseWs } from '../base-ws.js';
|
|
3
|
+
import { ExchangeWs } from '../exchange-ws.js';
|
|
4
|
+
import { OkxWs } from './ws.js';
|
|
5
|
+
import 'partysocket';
|
|
6
|
+
|
|
7
|
+
declare class OkxExchangeWs extends ExchangeWs {
|
|
8
|
+
main: OkxWs;
|
|
9
|
+
private readonly creds;
|
|
10
|
+
readonly exchange: "okx";
|
|
11
|
+
constructor(main: OkxWs, creds: OkxCredentialsDTO);
|
|
12
|
+
protected connections(): BaseWs[];
|
|
13
|
+
login(): Promise<void>;
|
|
14
|
+
static create(config: {
|
|
15
|
+
demo: boolean;
|
|
16
|
+
credentials: OkxCredentialsDTO;
|
|
17
|
+
}): Promise<OkxExchangeWs>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { OkxExchangeWs };
|