@pear-protocol/exchanges-sdk 0.6.5 → 0.6.7
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/dist/client/base-ws.js +15 -6
- package/dist/client/binance/exchange-ws.js +22 -8
- package/dist/client/binance/rest.js +40 -27
- package/dist/client/binance/ws.js +21 -2
- package/dist/client/bybit/exchange-ws.js +13 -6
- package/dist/client/bybit/rest.js +30 -36
- package/dist/client/bybit/ws.js +16 -2
- package/dist/client/exchange-ws.js +5 -0
- package/dist/client/hmac.js +9 -1
- package/dist/client/hyperliquid/exchange-ws.js +13 -5
- package/dist/client/hyperliquid/rest.js +37 -7
- package/dist/client/hyperliquid/ws.js +10 -4
- package/dist/client/index.js +9 -2
- package/dist/client/lighter/constants.js +4 -0
- package/dist/client/lighter/exchange-ws.js +13 -5
- package/dist/client/lighter/rest.js +153 -18
- package/dist/client/lighter/ws.js +10 -4
- package/dist/client/okx/exchange-ws.js +13 -6
- package/dist/client/okx/rest.js +27 -24
- package/dist/client/okx/ws.js +5 -0
- package/dist/helper/factory.js +3 -0
- package/dist/helper/hyperliquid/helper.js +18 -23
- package/dist/helper/lighter/auth-token.helper.js +68 -0
- package/dist/helper/lighter/cctp-deposit.helper.js +297 -0
- package/dist/helper/lighter/ethereum/abi.js +55 -11
- package/dist/helper/lighter/ethereum/index.js +22 -3
- package/dist/helper/lighter/ethereum/usdc.helper.js +77 -0
- package/dist/helper/lighter/helper.js +116 -37
- package/dist/helper/lighter/index.js +1 -0
- package/dist/helper/lighter/onboarding.helper.js +28 -0
- package/dist/helper/lighter/types.js +6 -1
- package/dist/helper/subaccount.helper.js +20 -0
- package/dist/index.d.ts +202 -1
- package/dist/sdk.js +18 -7
- package/dist/status/base.js +16 -3
- package/dist/status/binance/status.js +3 -0
- package/dist/status/bybit/status.js +3 -0
- package/dist/status/factory.js +3 -0
- package/dist/status/hyperliquid/status.js +8 -6
- package/dist/status/lighter/status.js +11 -3
- package/dist/status/okx/status.js +3 -0
- package/dist/syncer/base.js +63 -24
- package/dist/syncer/binance/syncer.js +14 -13
- package/dist/syncer/bybit/syncer.js +10 -6
- package/dist/syncer/factory.js +3 -0
- package/dist/syncer/hyperliquid/funding.js +3 -0
- package/dist/syncer/hyperliquid/syncer.js +52 -66
- package/dist/syncer/lighter/funding.js +3 -0
- package/dist/syncer/lighter/syncer.js +26 -25
- package/dist/syncer/okx/syncer.js +15 -13
- package/dist/tracker/base.js +15 -5
- package/dist/tracker/binance/mapper.js +5 -1
- package/dist/tracker/binance/tracker.js +63 -23
- package/dist/tracker/binance/types.js +2 -2
- package/dist/tracker/bybit/mapper.js +7 -1
- package/dist/tracker/bybit/tracker.js +68 -24
- package/dist/tracker/bybit/types.js +2 -2
- package/dist/tracker/hyperliquid/const.js +3 -1
- package/dist/tracker/hyperliquid/mapper.js +36 -20
- package/dist/tracker/hyperliquid/tracker.js +147 -54
- package/dist/tracker/hyperliquid/types.js +2 -2
- package/dist/tracker/index.js +3 -0
- package/dist/tracker/lighter/mapper.js +41 -31
- package/dist/tracker/lighter/tracker.js +68 -27
- package/dist/tracker/lighter/types.js +4 -4
- package/dist/tracker/okx/mapper.js +8 -0
- package/dist/tracker/okx/tracker.js +69 -18
- package/dist/tracker/okx/types.js +2 -2
- package/dist/tracker/utils.js +4 -0
- package/package.json +3 -3
- package/dist/client/hyperliquid/client.test.js +0 -41
- package/dist/client/lighter/constants.test.js +0 -22
- package/dist/client/lighter/rest.test.js +0 -158
- package/dist/helper/hyperliquid/helper.test.js +0 -72
- package/dist/helper/lighter/helper.test.js +0 -289
- package/dist/sdk.test.js +0 -117
- package/dist/status/hyperliquid/status.test.js +0 -94
- package/dist/status/lighter/status.test.js +0 -86
- package/dist/syncer/base.test.js +0 -178
- package/dist/syncer/hyperliquid/funding.test.js +0 -63
- package/dist/syncer/hyperliquid/syncer.test.js +0 -271
- package/dist/syncer/lighter/funding.test.js +0 -48
- package/dist/syncer/lighter/syncer.test.js +0 -274
- package/dist/tracker/base.test.js +0 -132
- package/dist/tracker/binance/tracker.test.js +0 -138
- package/dist/tracker/bybit/tracker.test.js +0 -145
- package/dist/tracker/hyperliquid/mapper.test.js +0 -260
- package/dist/tracker/hyperliquid/tracker.test.js +0 -597
- package/dist/tracker/lighter/mapper.test.js +0 -199
- package/dist/tracker/lighter/tracker.test.js +0 -515
- package/dist/tracker/okx/tracker.test.js +0 -126
- package/dist/tracker/utils.test.js +0 -8
|
@@ -1,199 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import { buildAccountBalance, mapBalance, toLighterAccountTier, mapPositions } from './mapper';
|
|
3
|
-
import { LighterAccountTier, LighterAccountType } from './types';
|
|
4
|
-
|
|
5
|
-
const markets = /* @__PURE__ */ new Map([
|
|
6
|
-
[
|
|
7
|
-
1,
|
|
8
|
-
{
|
|
9
|
-
market_id: 1,
|
|
10
|
-
symbol: "BTC",
|
|
11
|
-
status: "active",
|
|
12
|
-
default_initial_margin_fraction: 1e3,
|
|
13
|
-
maintenance_margin_fraction: 1e3
|
|
14
|
-
}
|
|
15
|
-
],
|
|
16
|
-
[
|
|
17
|
-
2,
|
|
18
|
-
{
|
|
19
|
-
market_id: 2,
|
|
20
|
-
symbol: "ETH",
|
|
21
|
-
status: "active",
|
|
22
|
-
default_initial_margin_fraction: 2e3,
|
|
23
|
-
maintenance_margin_fraction: 500
|
|
24
|
-
}
|
|
25
|
-
]
|
|
26
|
-
]);
|
|
27
|
-
describe("Lighter account mapping", () => {
|
|
28
|
-
it("combines cross and isolated collateral, risk requirements, and unrealized PnL", () => {
|
|
29
|
-
const balance = buildAccountBalance(
|
|
30
|
-
{
|
|
31
|
-
account_type: 2,
|
|
32
|
-
account_trading_mode: 1,
|
|
33
|
-
collateral: "1000",
|
|
34
|
-
total_asset_value: "1145",
|
|
35
|
-
available_balance: "800",
|
|
36
|
-
cross_asset_value: "1020",
|
|
37
|
-
cross_initial_margin_requirement: "100",
|
|
38
|
-
cross_maintenance_margin_requirement: "50",
|
|
39
|
-
assets: [
|
|
40
|
-
{
|
|
41
|
-
symbol: "USDC",
|
|
42
|
-
balance: "10",
|
|
43
|
-
locked_balance: "2",
|
|
44
|
-
margin_balance: "1000"
|
|
45
|
-
}
|
|
46
|
-
],
|
|
47
|
-
positions: [
|
|
48
|
-
{
|
|
49
|
-
market_id: 1,
|
|
50
|
-
symbol: "BTC",
|
|
51
|
-
initial_margin_fraction: "10",
|
|
52
|
-
sign: 1,
|
|
53
|
-
position: "1",
|
|
54
|
-
avg_entry_price: "1000",
|
|
55
|
-
position_value: "1000",
|
|
56
|
-
unrealized_pnl: "20",
|
|
57
|
-
liquidation_price: "500",
|
|
58
|
-
margin_mode: 0,
|
|
59
|
-
allocated_margin: "0"
|
|
60
|
-
},
|
|
61
|
-
{
|
|
62
|
-
market_id: 2,
|
|
63
|
-
symbol: "ETH",
|
|
64
|
-
initial_margin_fraction: "20",
|
|
65
|
-
sign: -1,
|
|
66
|
-
position: "2",
|
|
67
|
-
avg_entry_price: "250",
|
|
68
|
-
position_value: "500",
|
|
69
|
-
unrealized_pnl: "-25",
|
|
70
|
-
liquidation_price: "400",
|
|
71
|
-
margin_mode: 1,
|
|
72
|
-
allocated_margin: "150"
|
|
73
|
-
}
|
|
74
|
-
]
|
|
75
|
-
},
|
|
76
|
-
markets,
|
|
77
|
-
LighterAccountTier.PLUS
|
|
78
|
-
);
|
|
79
|
-
expect(balance).toMatchObject({
|
|
80
|
-
accountType: "unified",
|
|
81
|
-
accountTier: "plus",
|
|
82
|
-
totalEquity: "1145",
|
|
83
|
-
walletBalance: "1150.000000",
|
|
84
|
-
unrealizedPnl: "-5.000000",
|
|
85
|
-
initialMarginUsed: "200.000000",
|
|
86
|
-
maintenanceMargin: "75.000000",
|
|
87
|
-
marginRatio: "0.2000",
|
|
88
|
-
free: { USDC: "1008" },
|
|
89
|
-
used: { USDC: "2" },
|
|
90
|
-
total: { USDC: "1010" }
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
it("uses the riskiest independently liquidatable bucket for live margin ratio", () => {
|
|
94
|
-
const balance = mapBalance(
|
|
95
|
-
{
|
|
96
|
-
collateral: "1050",
|
|
97
|
-
portfolioValue: "950",
|
|
98
|
-
crossPortfolioValue: "900",
|
|
99
|
-
availableToTrade: "800",
|
|
100
|
-
marginUsagePct: "0",
|
|
101
|
-
accountType: LighterAccountType.CLASSIC
|
|
102
|
-
},
|
|
103
|
-
/* @__PURE__ */ new Map(),
|
|
104
|
-
[
|
|
105
|
-
rawPosition({ position_value: "100", margin_mode: 0 }),
|
|
106
|
-
rawPosition({
|
|
107
|
-
market_id: 2,
|
|
108
|
-
symbol: "ETH",
|
|
109
|
-
position_value: "500",
|
|
110
|
-
unrealized_pnl: "-100",
|
|
111
|
-
margin_mode: 1,
|
|
112
|
-
allocated_margin: "150"
|
|
113
|
-
})
|
|
114
|
-
],
|
|
115
|
-
markets
|
|
116
|
-
);
|
|
117
|
-
expect(balance.maintenanceMargin).toBe("35.000000");
|
|
118
|
-
expect(balance.marginRatio).toBe("0.5000");
|
|
119
|
-
});
|
|
120
|
-
it.each(["0", "-1"])("fails closed for live maintenance risk with nonpositive equity %s", (portfolioValue) => {
|
|
121
|
-
const balance = mapBalance(
|
|
122
|
-
{
|
|
123
|
-
collateral: "0",
|
|
124
|
-
portfolioValue,
|
|
125
|
-
availableToTrade: "0",
|
|
126
|
-
marginUsagePct: "0",
|
|
127
|
-
accountType: LighterAccountType.CLASSIC
|
|
128
|
-
},
|
|
129
|
-
/* @__PURE__ */ new Map(),
|
|
130
|
-
[rawPosition({ position_value: "1000" })],
|
|
131
|
-
markets
|
|
132
|
-
);
|
|
133
|
-
expect(balance.maintenanceMargin).toBe("100.000000");
|
|
134
|
-
expect(balance.marginRatio).toBe("1.0000");
|
|
135
|
-
});
|
|
136
|
-
it.each(["0", "-1"])("fails closed for REST maintenance risk with nonpositive equity %s", (totalAssetValue) => {
|
|
137
|
-
const balance = buildAccountBalance(
|
|
138
|
-
{
|
|
139
|
-
account_type: 0,
|
|
140
|
-
account_trading_mode: 0,
|
|
141
|
-
collateral: "0",
|
|
142
|
-
total_asset_value: totalAssetValue,
|
|
143
|
-
available_balance: "0",
|
|
144
|
-
cross_asset_value: totalAssetValue,
|
|
145
|
-
cross_initial_margin_requirement: "0",
|
|
146
|
-
cross_maintenance_margin_requirement: "10"
|
|
147
|
-
},
|
|
148
|
-
markets
|
|
149
|
-
);
|
|
150
|
-
expect(balance.maintenanceMargin).toBe("10.000000");
|
|
151
|
-
expect(balance.marginRatio).toBe("1.0000");
|
|
152
|
-
});
|
|
153
|
-
it("maps authenticated account-limit tier names and never infers a tier from account_type", () => {
|
|
154
|
-
expect(toLighterAccountTier("std")).toBe(LighterAccountTier.STANDARD);
|
|
155
|
-
expect(toLighterAccountTier("STANDARD")).toBe(LighterAccountTier.STANDARD);
|
|
156
|
-
expect(toLighterAccountTier("premium")).toBe(LighterAccountTier.PREMIUM);
|
|
157
|
-
expect(toLighterAccountTier("plus")).toBe(LighterAccountTier.PLUS);
|
|
158
|
-
expect(toLighterAccountTier("unknown")).toBeUndefined();
|
|
159
|
-
const balance = buildAccountBalance(
|
|
160
|
-
{
|
|
161
|
-
account_type: 2,
|
|
162
|
-
account_trading_mode: 0,
|
|
163
|
-
collateral: "0",
|
|
164
|
-
total_asset_value: "0",
|
|
165
|
-
available_balance: "0",
|
|
166
|
-
cross_asset_value: "0",
|
|
167
|
-
cross_initial_margin_requirement: "0",
|
|
168
|
-
cross_maintenance_margin_requirement: "0"
|
|
169
|
-
},
|
|
170
|
-
/* @__PURE__ */ new Map()
|
|
171
|
-
);
|
|
172
|
-
expect(balance.accountTier).toBeUndefined();
|
|
173
|
-
});
|
|
174
|
-
it("filters full-snapshot zero rows and preserves signed short exposure", () => {
|
|
175
|
-
const positions = mapPositions({
|
|
176
|
-
1: rawPosition({ market_id: 1, symbol: "BTC", sign: -1, position: "2" }),
|
|
177
|
-
2: rawPosition({ market_id: 2, symbol: "ETH", sign: 1, position: "0.0000" })
|
|
178
|
-
}).positions;
|
|
179
|
-
expect(Array.from(positions.values())).toEqual([
|
|
180
|
-
expect.objectContaining({ symbol: "BTC", side: "short", size: "-2" })
|
|
181
|
-
]);
|
|
182
|
-
});
|
|
183
|
-
});
|
|
184
|
-
function rawPosition(overrides) {
|
|
185
|
-
return {
|
|
186
|
-
market_id: 1,
|
|
187
|
-
symbol: "BTC",
|
|
188
|
-
initial_margin_fraction: "10",
|
|
189
|
-
sign: 1,
|
|
190
|
-
position: "1",
|
|
191
|
-
avg_entry_price: "1000",
|
|
192
|
-
position_value: "1000",
|
|
193
|
-
unrealized_pnl: "0",
|
|
194
|
-
liquidation_price: "500",
|
|
195
|
-
margin_mode: 0,
|
|
196
|
-
allocated_margin: "0",
|
|
197
|
-
...overrides
|
|
198
|
-
};
|
|
199
|
-
}
|