@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,28 @@
|
|
|
1
|
+
import { ExchangeWs } from '../exchange-ws';
|
|
2
|
+
import { OkxWs } from './ws';
|
|
3
|
+
|
|
4
|
+
class OkxExchangeWs extends ExchangeWs {
|
|
5
|
+
constructor(main, creds) {
|
|
6
|
+
super();
|
|
7
|
+
this.main = main;
|
|
8
|
+
this.creds = creds;
|
|
9
|
+
}
|
|
10
|
+
main;
|
|
11
|
+
creds;
|
|
12
|
+
exchange = "okx";
|
|
13
|
+
connections() {
|
|
14
|
+
return [this.main];
|
|
15
|
+
}
|
|
16
|
+
login() {
|
|
17
|
+
return this.main.login(this.creds);
|
|
18
|
+
}
|
|
19
|
+
static async create(config) {
|
|
20
|
+
const main = new OkxWs({ demo: config.demo });
|
|
21
|
+
await main.connect();
|
|
22
|
+
const wrapper = new OkxExchangeWs(main, config.credentials);
|
|
23
|
+
await wrapper.login();
|
|
24
|
+
return wrapper;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export { OkxExchangeWs };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { hmacSha256Base64 } from '
|
|
2
|
-
import { REST_BASE_URL } from './const';
|
|
1
|
+
import { hmacSha256Base64 } from '../hmac';
|
|
3
2
|
|
|
3
|
+
const REST_BASE_URL = "https://www.okx.com";
|
|
4
4
|
async function signedGet(creds, path, params, demo) {
|
|
5
5
|
const queryString = new URLSearchParams(params).toString();
|
|
6
6
|
const fullPath = queryString ? `${path}?${queryString}` : path;
|
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
declare enum OkxAccountType {
|
|
2
|
-
SPOT = "OKX_SPOT",
|
|
3
|
-
SINGLE_CURRENCY_MARGIN = "OKX_SINGLE",
|
|
4
|
-
MULTI_CURRENCY_MARGIN = "OKX_MULTI",
|
|
5
|
-
PORTFOLIO_MARGIN = "OKX_PORTFOLIO"
|
|
6
|
-
}
|
|
7
1
|
interface OkxRawAccountConfig {
|
|
8
2
|
acctLv: string;
|
|
9
3
|
uid: string;
|
|
@@ -62,8 +56,5 @@ interface OkxWsPositionsMessage {
|
|
|
62
56
|
data: OkxRawPosition[];
|
|
63
57
|
}
|
|
64
58
|
type OkxWsMessage = OkxWsAccountMessage | OkxWsPositionsMessage;
|
|
65
|
-
interface OkxWsHandlers {
|
|
66
|
-
onMessage: (msg: OkxWsMessage) => void;
|
|
67
|
-
}
|
|
68
59
|
|
|
69
|
-
export {
|
|
60
|
+
export type { OkxRawAccountConfig, OkxRawBalance, OkxRawBalanceDetail, OkxRawLeverageInfo, OkxRawPosition, OkxWsAccountMessage, OkxWsMessage, OkxWsPositionsMessage };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { OkxCredentialsDTO } from '@pear-protocol/types';
|
|
2
|
+
import { BaseWs } from '../base-ws.js';
|
|
3
|
+
import 'partysocket';
|
|
4
|
+
|
|
5
|
+
type OkxWsConfig = {
|
|
6
|
+
demo: boolean;
|
|
7
|
+
};
|
|
8
|
+
declare class OkxWs extends BaseWs {
|
|
9
|
+
private readonly demo;
|
|
10
|
+
constructor(config: OkxWsConfig);
|
|
11
|
+
protected getWsUrl(): string;
|
|
12
|
+
protected getPingIntervalMs(): number;
|
|
13
|
+
protected buildPingMessage(): string;
|
|
14
|
+
login(creds: OkxCredentialsDTO): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { OkxWs, type OkxWsConfig };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { BaseWs } from '../base-ws';
|
|
2
|
+
import { hmacSha256Base64 } from '../hmac';
|
|
3
|
+
|
|
4
|
+
const WS_PRIVATE_URL = "wss://ws.okx.com:8443/ws/v5/private";
|
|
5
|
+
const WS_PRIVATE_URL_DEMO = "wss://wspap.okx.com:8443/ws/v5/private?brokerId=9999";
|
|
6
|
+
const PING_INTERVAL_MS = 25e3;
|
|
7
|
+
const LOGIN_TIMEOUT_MS = 1e4;
|
|
8
|
+
class OkxWs extends BaseWs {
|
|
9
|
+
demo;
|
|
10
|
+
constructor(config) {
|
|
11
|
+
super();
|
|
12
|
+
this.demo = config.demo;
|
|
13
|
+
}
|
|
14
|
+
getWsUrl() {
|
|
15
|
+
return this.demo ? WS_PRIVATE_URL_DEMO : WS_PRIVATE_URL;
|
|
16
|
+
}
|
|
17
|
+
getPingIntervalMs() {
|
|
18
|
+
return PING_INTERVAL_MS;
|
|
19
|
+
}
|
|
20
|
+
buildPingMessage() {
|
|
21
|
+
return "ping";
|
|
22
|
+
}
|
|
23
|
+
async login(creds) {
|
|
24
|
+
const timestamp = (Date.now() / 1e3).toString();
|
|
25
|
+
const sign = await hmacSha256Base64(creds.api_secret, `${timestamp}GET/users/self/verify`);
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
let listenerId = "";
|
|
28
|
+
const timer = setTimeout(() => {
|
|
29
|
+
this.removeMessageListener(listenerId);
|
|
30
|
+
reject(new Error("OKX login timeout"));
|
|
31
|
+
}, LOGIN_TIMEOUT_MS);
|
|
32
|
+
listenerId = this.addMessageListener((raw) => {
|
|
33
|
+
if (raw === "pong") return;
|
|
34
|
+
let msg;
|
|
35
|
+
try {
|
|
36
|
+
msg = JSON.parse(raw);
|
|
37
|
+
} catch {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (msg.event !== "login") return;
|
|
41
|
+
clearTimeout(timer);
|
|
42
|
+
this.removeMessageListener(listenerId);
|
|
43
|
+
if (msg.code === "0") resolve();
|
|
44
|
+
else reject(new Error(`OKX login failed: ${msg.msg ?? "unknown"}`));
|
|
45
|
+
});
|
|
46
|
+
this.send({
|
|
47
|
+
op: "login",
|
|
48
|
+
args: [
|
|
49
|
+
{
|
|
50
|
+
apiKey: creds.api_key,
|
|
51
|
+
passphrase: creds.api_pass,
|
|
52
|
+
timestamp,
|
|
53
|
+
sign
|
|
54
|
+
}
|
|
55
|
+
]
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export { OkxWs };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import PearSDK from '@pear-protocol/core-sdk';
|
|
2
|
+
import { ExchangeHelperExchange, ExchangeHelperMap } from './types.js';
|
|
3
|
+
import './lighter/types.js';
|
|
4
|
+
import './lighter/ethereum/types.js';
|
|
5
|
+
import 'viem';
|
|
6
|
+
import '../client/lighter/rest.js';
|
|
7
|
+
|
|
8
|
+
declare function createExchangeHelper<E extends ExchangeHelperExchange>(exchange: E, sdk: PearSDK): ExchangeHelperMap[E];
|
|
9
|
+
|
|
10
|
+
export { createExchangeHelper };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createLighterHelper } from './lighter';
|
|
2
|
+
|
|
3
|
+
function createExchangeHelper(exchange, sdk) {
|
|
4
|
+
switch (exchange) {
|
|
5
|
+
case "lighter":
|
|
6
|
+
return createLighterHelper(sdk);
|
|
7
|
+
default:
|
|
8
|
+
throw new Error(`Helpers not available for exchange: ${exchange}`);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export { createExchangeHelper };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { ASSET_DECIMALS, ApproveIntegratorParams, ApproveIntegratorResult, AssetId, ConnectWalletParams, ConnectWalletResult, DepositParams, LighterHelper, LighterLookupResult, RouteType, WithdrawParams, getAssetDecimals } from './lighter/types.js';
|
|
2
|
+
export { createLighterHelper } from './lighter/helper.js';
|
|
3
|
+
export { ExchangeHelperExchange, ExchangeHelperMap } from './types.js';
|
|
4
|
+
export { ApiKey } from '../client/lighter/rest.js';
|
|
5
|
+
export { WalletClientWithAccount } from './lighter/ethereum/types.js';
|
|
6
|
+
import '@pear-protocol/core-sdk';
|
|
7
|
+
import 'viem';
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
declare const LIGHTER_GATEWAY_ADDRESS: "0x3B4D794a66304F130a4Db8F2551B0070dfCf5ca7";
|
|
2
|
+
declare const lighterGatewayAbi: readonly [{
|
|
3
|
+
readonly inputs: readonly [{
|
|
4
|
+
readonly internalType: "uint48";
|
|
5
|
+
readonly name: "_accountIndex";
|
|
6
|
+
readonly type: "uint48";
|
|
7
|
+
}, {
|
|
8
|
+
readonly internalType: "uint8";
|
|
9
|
+
readonly name: "_apiKeyIndex";
|
|
10
|
+
readonly type: "uint8";
|
|
11
|
+
}, {
|
|
12
|
+
readonly internalType: "bytes";
|
|
13
|
+
readonly name: "_pubKey";
|
|
14
|
+
readonly type: "bytes";
|
|
15
|
+
}];
|
|
16
|
+
readonly name: "changePubKey";
|
|
17
|
+
readonly outputs: readonly [];
|
|
18
|
+
readonly stateMutability: "nonpayable";
|
|
19
|
+
readonly type: "function";
|
|
20
|
+
}, {
|
|
21
|
+
readonly inputs: readonly [{
|
|
22
|
+
readonly internalType: "address";
|
|
23
|
+
readonly name: "_to";
|
|
24
|
+
readonly type: "address";
|
|
25
|
+
}, {
|
|
26
|
+
readonly internalType: "uint16";
|
|
27
|
+
readonly name: "_assetIndex";
|
|
28
|
+
readonly type: "uint16";
|
|
29
|
+
}, {
|
|
30
|
+
readonly internalType: "uint8";
|
|
31
|
+
readonly name: "_routeType";
|
|
32
|
+
readonly type: "uint8";
|
|
33
|
+
}, {
|
|
34
|
+
readonly internalType: "uint256";
|
|
35
|
+
readonly name: "_amount";
|
|
36
|
+
readonly type: "uint256";
|
|
37
|
+
}];
|
|
38
|
+
readonly name: "deposit";
|
|
39
|
+
readonly outputs: readonly [];
|
|
40
|
+
readonly stateMutability: "payable";
|
|
41
|
+
readonly type: "function";
|
|
42
|
+
}, {
|
|
43
|
+
readonly inputs: readonly [{
|
|
44
|
+
readonly internalType: "uint48";
|
|
45
|
+
readonly name: "_accountIndex";
|
|
46
|
+
readonly type: "uint48";
|
|
47
|
+
}, {
|
|
48
|
+
readonly internalType: "uint16";
|
|
49
|
+
readonly name: "_assetIndex";
|
|
50
|
+
readonly type: "uint16";
|
|
51
|
+
}, {
|
|
52
|
+
readonly internalType: "uint8";
|
|
53
|
+
readonly name: "_routeType";
|
|
54
|
+
readonly type: "uint8";
|
|
55
|
+
}, {
|
|
56
|
+
readonly internalType: "uint64";
|
|
57
|
+
readonly name: "_baseAmount";
|
|
58
|
+
readonly type: "uint64";
|
|
59
|
+
}];
|
|
60
|
+
readonly name: "withdraw";
|
|
61
|
+
readonly outputs: readonly [];
|
|
62
|
+
readonly stateMutability: "nonpayable";
|
|
63
|
+
readonly type: "function";
|
|
64
|
+
}];
|
|
65
|
+
|
|
66
|
+
export { LIGHTER_GATEWAY_ADDRESS, lighterGatewayAbi };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const LIGHTER_GATEWAY_ADDRESS = "0x3B4D794a66304F130a4Db8F2551B0070dfCf5ca7";
|
|
2
|
+
const lighterGatewayAbi = [
|
|
3
|
+
{
|
|
4
|
+
inputs: [
|
|
5
|
+
{ internalType: "uint48", name: "_accountIndex", type: "uint48" },
|
|
6
|
+
{ internalType: "uint8", name: "_apiKeyIndex", type: "uint8" },
|
|
7
|
+
{ internalType: "bytes", name: "_pubKey", type: "bytes" }
|
|
8
|
+
],
|
|
9
|
+
name: "changePubKey",
|
|
10
|
+
outputs: [],
|
|
11
|
+
stateMutability: "nonpayable",
|
|
12
|
+
type: "function"
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
inputs: [
|
|
16
|
+
{ internalType: "address", name: "_to", type: "address" },
|
|
17
|
+
{ internalType: "uint16", name: "_assetIndex", type: "uint16" },
|
|
18
|
+
{ internalType: "uint8", name: "_routeType", type: "uint8" },
|
|
19
|
+
{ internalType: "uint256", name: "_amount", type: "uint256" }
|
|
20
|
+
],
|
|
21
|
+
name: "deposit",
|
|
22
|
+
outputs: [],
|
|
23
|
+
stateMutability: "payable",
|
|
24
|
+
type: "function"
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
inputs: [
|
|
28
|
+
{ internalType: "uint48", name: "_accountIndex", type: "uint48" },
|
|
29
|
+
{ internalType: "uint16", name: "_assetIndex", type: "uint16" },
|
|
30
|
+
{ internalType: "uint8", name: "_routeType", type: "uint8" },
|
|
31
|
+
{ internalType: "uint64", name: "_baseAmount", type: "uint64" }
|
|
32
|
+
],
|
|
33
|
+
name: "withdraw",
|
|
34
|
+
outputs: [],
|
|
35
|
+
stateMutability: "nonpayable",
|
|
36
|
+
type: "function"
|
|
37
|
+
}
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
export { LIGHTER_GATEWAY_ADDRESS, lighterGatewayAbi };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { WalletClientWithAccount, ChangePubKeyParams, DepositParams, WithdrawParams } from './types.js';
|
|
2
|
+
import 'viem';
|
|
3
|
+
|
|
4
|
+
declare function writeChangePubKey(walletClient: WalletClientWithAccount, params: ChangePubKeyParams): Promise<`0x${string}`>;
|
|
5
|
+
declare function writeDeposit(walletClient: WalletClientWithAccount, params: DepositParams): Promise<`0x${string}`>;
|
|
6
|
+
declare function writeWithdraw(walletClient: WalletClientWithAccount, params: WithdrawParams): Promise<`0x${string}`>;
|
|
7
|
+
|
|
8
|
+
export { ChangePubKeyParams, DepositParams, WalletClientWithAccount, WithdrawParams, writeChangePubKey, writeDeposit, writeWithdraw };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { mainnet } from 'viem/chains';
|
|
2
|
+
import { lighterGatewayAbi, LIGHTER_GATEWAY_ADDRESS } from './abi';
|
|
3
|
+
|
|
4
|
+
const chain = mainnet;
|
|
5
|
+
async function writeChangePubKey(walletClient, params) {
|
|
6
|
+
return walletClient.writeContract({
|
|
7
|
+
chain,
|
|
8
|
+
address: LIGHTER_GATEWAY_ADDRESS,
|
|
9
|
+
abi: lighterGatewayAbi,
|
|
10
|
+
functionName: "changePubKey",
|
|
11
|
+
args: [params.accountIndex, params.apiKeyIndex, params.pubKey]
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
async function writeDeposit(walletClient, params) {
|
|
15
|
+
return walletClient.writeContract({
|
|
16
|
+
chain,
|
|
17
|
+
address: LIGHTER_GATEWAY_ADDRESS,
|
|
18
|
+
abi: lighterGatewayAbi,
|
|
19
|
+
functionName: "deposit",
|
|
20
|
+
args: [params.to, params.assetIndex, params.routeType, params.amount],
|
|
21
|
+
value: params.value ?? 0n
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
async function writeWithdraw(walletClient, params) {
|
|
25
|
+
return walletClient.writeContract({
|
|
26
|
+
chain,
|
|
27
|
+
address: LIGHTER_GATEWAY_ADDRESS,
|
|
28
|
+
abi: lighterGatewayAbi,
|
|
29
|
+
functionName: "withdraw",
|
|
30
|
+
args: [params.accountIndex, params.assetIndex, params.routeType, params.baseAmount]
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export { writeChangePubKey, writeDeposit, writeWithdraw };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { WalletClient, Transport, Chain, Account } from 'viem';
|
|
2
|
+
|
|
3
|
+
type WalletClientWithAccount = WalletClient<Transport, Chain | undefined, Account>;
|
|
4
|
+
type ChangePubKeyParams = {
|
|
5
|
+
accountIndex: number;
|
|
6
|
+
apiKeyIndex: number;
|
|
7
|
+
pubKey: `0x${string}`;
|
|
8
|
+
};
|
|
9
|
+
type DepositParams = {
|
|
10
|
+
to: `0x${string}`;
|
|
11
|
+
assetIndex: number;
|
|
12
|
+
routeType: number;
|
|
13
|
+
amount: bigint;
|
|
14
|
+
value?: bigint;
|
|
15
|
+
};
|
|
16
|
+
type WithdrawParams = {
|
|
17
|
+
accountIndex: number;
|
|
18
|
+
assetIndex: number;
|
|
19
|
+
routeType: number;
|
|
20
|
+
baseAmount: bigint;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type { ChangePubKeyParams, DepositParams, WalletClientWithAccount, WithdrawParams };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { LighterHelper } from './types.js';
|
|
2
|
+
import PearSDK from '@pear-protocol/core-sdk';
|
|
3
|
+
import '../../client/lighter/rest.js';
|
|
4
|
+
import './ethereum/types.js';
|
|
5
|
+
import 'viem';
|
|
6
|
+
|
|
7
|
+
declare function createLighterHelper(sdk: PearSDK): LighterHelper;
|
|
8
|
+
|
|
9
|
+
export { createLighterHelper };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { fetchApiKeys, fetchAccount, fetchAccountsByL1Address } from '../../client/lighter/rest';
|
|
2
|
+
|
|
3
|
+
function createLighterHelper(sdk) {
|
|
4
|
+
return {
|
|
5
|
+
async lookupAccount(l1Address) {
|
|
6
|
+
const data = await fetchAccountsByL1Address(l1Address);
|
|
7
|
+
if (data.code === 21100) {
|
|
8
|
+
throw new Error("Account not registered on Lighter");
|
|
9
|
+
}
|
|
10
|
+
const accounts = data.sub_accounts ?? [];
|
|
11
|
+
const main = accounts.find((s) => s.account_type === 0);
|
|
12
|
+
if (!main) {
|
|
13
|
+
throw new Error("Main account not found for this address");
|
|
14
|
+
}
|
|
15
|
+
return {
|
|
16
|
+
mainAccountIndex: main.index,
|
|
17
|
+
subAccountIndexes: accounts.filter((s) => s.account_type !== 0).map((s) => s.index)
|
|
18
|
+
};
|
|
19
|
+
},
|
|
20
|
+
async checkIntegratorApproval(accountIndex, integratorAccountIndex) {
|
|
21
|
+
const data = await fetchAccount(accountIndex);
|
|
22
|
+
const account = data.accounts[0];
|
|
23
|
+
if (!account) return false;
|
|
24
|
+
const integrators = account.approved_integrators ?? [];
|
|
25
|
+
return integrators.some((i) => i.account_index === integratorAccountIndex);
|
|
26
|
+
},
|
|
27
|
+
async connectWallet(walletClient, params) {
|
|
28
|
+
const apiKeys = await fetchApiKeys(params.accountIndex);
|
|
29
|
+
const apiKeyIndex = apiKeys.length === 0 ? 3 : apiKeys.reduce((max, k) => k.api_key_index > max ? k.api_key_index : max, 0) + 1;
|
|
30
|
+
const { privateKey, messageToSign, txInfo } = await sdk.core.misc.lighter.createApiKey({
|
|
31
|
+
accountIndex: params.accountIndex,
|
|
32
|
+
apiKeyIndex
|
|
33
|
+
});
|
|
34
|
+
const l1Signature = await walletClient.signMessage({
|
|
35
|
+
account: walletClient.account,
|
|
36
|
+
message: messageToSign
|
|
37
|
+
});
|
|
38
|
+
const { txHash } = await sdk.core.misc.lighter.changePubKey({
|
|
39
|
+
l1Signature,
|
|
40
|
+
txInfo,
|
|
41
|
+
accountIndex: params.accountIndex,
|
|
42
|
+
apiKeyIndex
|
|
43
|
+
});
|
|
44
|
+
return { txHash, apiKeyIndex, apiPrivateKey: privateKey };
|
|
45
|
+
},
|
|
46
|
+
async approveIntegrator(walletClient, params) {
|
|
47
|
+
const { messageToSign, txInfo } = await sdk.core.misc.lighter.prepareApproveIntegrator({
|
|
48
|
+
accountIndex: params.accountIndex,
|
|
49
|
+
apiKeyIndex: params.apiKeyIndex,
|
|
50
|
+
apiPrivateKey: params.apiPrivateKey
|
|
51
|
+
});
|
|
52
|
+
const l1Signature = await walletClient.signMessage({
|
|
53
|
+
account: walletClient.account,
|
|
54
|
+
message: messageToSign
|
|
55
|
+
});
|
|
56
|
+
const { txHash } = await sdk.core.misc.lighter.submitApproveIntegrator({
|
|
57
|
+
l1Signature,
|
|
58
|
+
txInfo,
|
|
59
|
+
accountIndex: params.accountIndex,
|
|
60
|
+
apiKeyIndex: params.apiKeyIndex
|
|
61
|
+
});
|
|
62
|
+
return { txHash };
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export { createLighterHelper };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { ASSET_DECIMALS, ApproveIntegratorParams, ApproveIntegratorResult, AssetId, ConnectWalletParams, ConnectWalletResult, DepositParams, LighterHelper, LighterLookupResult, RouteType, WithdrawParams, getAssetDecimals } from './types.js';
|
|
2
|
+
export { createLighterHelper } from './helper.js';
|
|
3
|
+
export { ApiKey } from '../../client/lighter/rest.js';
|
|
4
|
+
export { WalletClientWithAccount } from './ethereum/types.js';
|
|
5
|
+
import 'viem';
|
|
6
|
+
import '@pear-protocol/core-sdk';
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { WalletClientWithAccount } from './ethereum/types.js';
|
|
2
|
+
export { ApiKey } from '../../client/lighter/rest.js';
|
|
3
|
+
import 'viem';
|
|
4
|
+
|
|
5
|
+
declare const AssetId: {
|
|
6
|
+
readonly ETH: 1;
|
|
7
|
+
readonly LIT: 2;
|
|
8
|
+
readonly USDC: 3;
|
|
9
|
+
readonly LINK: 5;
|
|
10
|
+
readonly UNI: 6;
|
|
11
|
+
readonly AAVE: 7;
|
|
12
|
+
readonly SKY: 8;
|
|
13
|
+
readonly LDO: 9;
|
|
14
|
+
};
|
|
15
|
+
type AssetId = (typeof AssetId)[keyof typeof AssetId];
|
|
16
|
+
declare const RouteType: {
|
|
17
|
+
readonly PERP: 0;
|
|
18
|
+
readonly SPOT: 1;
|
|
19
|
+
};
|
|
20
|
+
type RouteType = (typeof RouteType)[keyof typeof RouteType];
|
|
21
|
+
declare const ASSET_DECIMALS: Record<number, number>;
|
|
22
|
+
declare function getAssetDecimals(assetId: number): number;
|
|
23
|
+
type ConnectWalletParams = {
|
|
24
|
+
accountIndex: number;
|
|
25
|
+
};
|
|
26
|
+
type DepositParams = {
|
|
27
|
+
to: `0x${string}`;
|
|
28
|
+
assetId: number;
|
|
29
|
+
routeType: number;
|
|
30
|
+
amount: string;
|
|
31
|
+
nativeValue?: string;
|
|
32
|
+
};
|
|
33
|
+
type WithdrawParams = {
|
|
34
|
+
accountIndex: number;
|
|
35
|
+
assetId: number;
|
|
36
|
+
routeType: number;
|
|
37
|
+
amount: string;
|
|
38
|
+
};
|
|
39
|
+
type ConnectWalletResult = {
|
|
40
|
+
txHash: string;
|
|
41
|
+
apiKeyIndex: number;
|
|
42
|
+
apiPrivateKey: string;
|
|
43
|
+
};
|
|
44
|
+
type ApproveIntegratorParams = {
|
|
45
|
+
accountIndex: number;
|
|
46
|
+
apiKeyIndex: number;
|
|
47
|
+
apiPrivateKey: string;
|
|
48
|
+
};
|
|
49
|
+
type ApproveIntegratorResult = {
|
|
50
|
+
txHash: string;
|
|
51
|
+
};
|
|
52
|
+
interface LighterHelper {
|
|
53
|
+
lookupAccount(l1Address: string): Promise<LighterLookupResult>;
|
|
54
|
+
checkIntegratorApproval(accountIndex: number, integratorAccountIndex: number): Promise<boolean>;
|
|
55
|
+
connectWallet(walletClient: WalletClientWithAccount, params: ConnectWalletParams): Promise<ConnectWalletResult>;
|
|
56
|
+
approveIntegrator(walletClient: WalletClientWithAccount, params: ApproveIntegratorParams): Promise<ApproveIntegratorResult>;
|
|
57
|
+
}
|
|
58
|
+
type LighterLookupResult = {
|
|
59
|
+
mainAccountIndex: number;
|
|
60
|
+
subAccountIndexes: number[];
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export { ASSET_DECIMALS, type ApproveIntegratorParams, type ApproveIntegratorResult, AssetId, type ConnectWalletParams, type ConnectWalletResult, type DepositParams, type LighterHelper, type LighterLookupResult, RouteType, WalletClientWithAccount, type WithdrawParams, getAssetDecimals };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const AssetId = {
|
|
2
|
+
ETH: 1,
|
|
3
|
+
LIT: 2,
|
|
4
|
+
USDC: 3,
|
|
5
|
+
LINK: 5,
|
|
6
|
+
UNI: 6,
|
|
7
|
+
AAVE: 7,
|
|
8
|
+
SKY: 8,
|
|
9
|
+
LDO: 9
|
|
10
|
+
};
|
|
11
|
+
const RouteType = {
|
|
12
|
+
PERP: 0,
|
|
13
|
+
SPOT: 1
|
|
14
|
+
};
|
|
15
|
+
const ASSET_DECIMALS = {
|
|
16
|
+
[AssetId.ETH]: 8,
|
|
17
|
+
[AssetId.LIT]: 8,
|
|
18
|
+
[AssetId.USDC]: 6,
|
|
19
|
+
[AssetId.LINK]: 8,
|
|
20
|
+
[AssetId.UNI]: 8,
|
|
21
|
+
[AssetId.AAVE]: 8,
|
|
22
|
+
[AssetId.SKY]: 8,
|
|
23
|
+
[AssetId.LDO]: 8
|
|
24
|
+
};
|
|
25
|
+
function getAssetDecimals(assetId) {
|
|
26
|
+
const decimals = ASSET_DECIMALS[assetId];
|
|
27
|
+
if (decimals === void 0) {
|
|
28
|
+
throw new Error(
|
|
29
|
+
`Unknown lighter asset ID: ${assetId}. Provide decimals via ASSET_DECIMALS or use a known AssetId.`
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
return decimals;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { ASSET_DECIMALS, AssetId, RouteType, getAssetDecimals };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { LighterHelper } from './lighter/types.js';
|
|
2
|
+
import './lighter/ethereum/types.js';
|
|
3
|
+
import 'viem';
|
|
4
|
+
import '../client/lighter/rest.js';
|
|
5
|
+
|
|
6
|
+
type ExchangeHelperMap = {
|
|
7
|
+
lighter: LighterHelper;
|
|
8
|
+
};
|
|
9
|
+
type ExchangeHelperExchange = keyof ExchangeHelperMap;
|
|
10
|
+
|
|
11
|
+
export type { ExchangeHelperExchange, ExchangeHelperMap };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
package/dist/index.d.ts
CHANGED
|
@@ -1,25 +1,38 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
5
|
-
export {
|
|
6
|
-
|
|
7
|
-
export {
|
|
8
|
-
export {
|
|
9
|
-
export {
|
|
10
|
-
export {
|
|
1
|
+
export { CreateExchangeWsConfig, ExchangeWsByConnector, createExchangeWs } from './client/index.js';
|
|
2
|
+
export { ASSET_DECIMALS, ApproveIntegratorParams, ApproveIntegratorResult, AssetId, ConnectWalletParams, ConnectWalletResult, DepositParams, LighterHelper, LighterLookupResult, RouteType, WithdrawParams, getAssetDecimals } from './helper/lighter/types.js';
|
|
3
|
+
export { createLighterHelper } from './helper/lighter/helper.js';
|
|
4
|
+
export { ExchangeHelperExchange, ExchangeHelperMap } from './helper/types.js';
|
|
5
|
+
export { ExchangesSDK } from './sdk.js';
|
|
6
|
+
export { AccountPositionSyncer, AccountPositionSyncerConfig } from './syncer/base.js';
|
|
7
|
+
export { CreateAccountTrackerConfig, createAccountTracker } from './tracker/index.js';
|
|
8
|
+
export { ExchangeConnection, ExchangesSDKConfig, HyperliquidAccountCredentials, LighterAccountCredentials, SupportedCredentials } from './types.js';
|
|
9
|
+
export { ACCOUNT_TYPE_LABELS, AccountBalance, AccountPosition, AccountType, AssetBalance, BalanceCallback, ExchangeState, MarginMode, PositionsCallback, StateUpdate, TrackedAssetCallback, TrackedAssetInfo } from './tracker/types.js';
|
|
10
|
+
export { AccountTracker, AccountTrackerConfig, BaseAccountTracker } from './tracker/base.js';
|
|
11
|
+
export { ApiKey } from './client/lighter/rest.js';
|
|
12
|
+
export { BaseWs, MessageListener, OpenListener, WsMessage } from './client/base-ws.js';
|
|
13
|
+
export { BinanceAccountTracker, BinanceAccountTrackerConfig } from './tracker/binance/tracker.js';
|
|
14
|
+
export { BinanceAccountType } from './tracker/binance/types.js';
|
|
15
|
+
export { BinanceExchangeWs } from './client/binance/exchange-ws.js';
|
|
16
|
+
export { BinanceUserDataWs, BinanceUserDataWsConfig, BinanceWsApi, BinanceWsApiConfig } from './client/binance/ws.js';
|
|
17
|
+
export { BybitAccountTracker, BybitAccountTrackerConfig } from './tracker/bybit/tracker.js';
|
|
18
|
+
export { BybitAccountType } from './tracker/bybit/types.js';
|
|
19
|
+
export { BybitExchangeWs } from './client/bybit/exchange-ws.js';
|
|
20
|
+
export { BybitWs, BybitWsConfig } from './client/bybit/ws.js';
|
|
21
|
+
export { ExchangeWs } from './client/exchange-ws.js';
|
|
22
|
+
export { HyperliquidAccountTracker, HyperliquidAccountTrackerConfig } from './tracker/hyperliquid/tracker.js';
|
|
23
|
+
export { HyperliquidAccountType } from './tracker/hyperliquid/types.js';
|
|
24
|
+
export { HyperliquidExchangeWs } from './client/hyperliquid/exchange-ws.js';
|
|
25
|
+
export { HyperliquidWs } from './client/hyperliquid/ws.js';
|
|
26
|
+
export { LighterAccountTracker, LighterAccountTrackerConfig } from './tracker/lighter/tracker.js';
|
|
27
|
+
export { LighterAccountType } from './tracker/lighter/types.js';
|
|
28
|
+
export { LighterExchangeWs } from './client/lighter/exchange-ws.js';
|
|
29
|
+
export { LighterWs } from './client/lighter/ws.js';
|
|
30
|
+
export { OkxAccountTracker, OkxAccountTrackerConfig } from './tracker/okx/tracker.js';
|
|
31
|
+
export { OkxAccountType } from './tracker/okx/types.js';
|
|
32
|
+
export { OkxExchangeWs } from './client/okx/exchange-ws.js';
|
|
33
|
+
export { OkxWs, OkxWsConfig } from './client/okx/ws.js';
|
|
34
|
+
export { WalletClientWithAccount } from './helper/lighter/ethereum/types.js';
|
|
11
35
|
import '@pear-protocol/types';
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
demo?: boolean;
|
|
16
|
-
};
|
|
17
|
-
declare class ExchangesSDK {
|
|
18
|
-
credentials: Credentials;
|
|
19
|
-
account: AccountManager;
|
|
20
|
-
sdk: PearSDK;
|
|
21
|
-
constructor(config: ExchangesSDKConfig);
|
|
22
|
-
destroy(): void;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export { AccountManager, Credentials, ExchangesSDK, type ExchangesSDKConfig };
|
|
36
|
+
import '@pear-protocol/core-sdk';
|
|
37
|
+
import 'partysocket';
|
|
38
|
+
import 'viem';
|
package/dist/index.js
CHANGED
|
@@ -1,21 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
export * from './
|
|
3
|
-
|
|
4
|
-
export * from './
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
credentials;
|
|
8
|
-
account;
|
|
9
|
-
sdk;
|
|
10
|
-
constructor(config) {
|
|
11
|
-
this.sdk = config.sdk;
|
|
12
|
-
this.credentials = new Credentials(config.sdk);
|
|
13
|
-
this.account = new AccountManager(this.credentials, config.sdk, config.demo);
|
|
14
|
-
}
|
|
15
|
-
destroy() {
|
|
16
|
-
this.account.destroy();
|
|
17
|
-
this.credentials.clear();
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export { ExchangesSDK };
|
|
1
|
+
export * from './client';
|
|
2
|
+
export * from './helper';
|
|
3
|
+
export * from './sdk';
|
|
4
|
+
export * from './syncer';
|
|
5
|
+
export * from './tracker';
|
|
6
|
+
export * from './types';
|