perp-cli 0.3.3
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/LICENSE +21 -0
- package/README.md +293 -0
- package/dist/__tests__/alert-logic.test.d.ts +1 -0
- package/dist/__tests__/alert-logic.test.js +107 -0
- package/dist/__tests__/arb-auto-3dex.test.d.ts +1 -0
- package/dist/__tests__/arb-auto-3dex.test.js +397 -0
- package/dist/__tests__/arb-history-stats.test.d.ts +1 -0
- package/dist/__tests__/arb-history-stats.test.js +176 -0
- package/dist/__tests__/arb-logic.test.d.ts +1 -0
- package/dist/__tests__/arb-logic.test.js +84 -0
- package/dist/__tests__/arb-manage.test.d.ts +1 -0
- package/dist/__tests__/arb-manage.test.js +253 -0
- package/dist/__tests__/arb-new-features.test.d.ts +1 -0
- package/dist/__tests__/arb-new-features.test.js +457 -0
- package/dist/__tests__/arb-sizing.test.d.ts +1 -0
- package/dist/__tests__/arb-sizing.test.js +48 -0
- package/dist/__tests__/arb-state.test.d.ts +1 -0
- package/dist/__tests__/arb-state.test.js +284 -0
- package/dist/__tests__/arb-userflow.test.d.ts +1 -0
- package/dist/__tests__/arb-userflow.test.js +945 -0
- package/dist/__tests__/arb-utils.test.d.ts +1 -0
- package/dist/__tests__/arb-utils.test.js +264 -0
- package/dist/__tests__/bot-conditions.test.d.ts +1 -0
- package/dist/__tests__/bot-conditions.test.js +341 -0
- package/dist/__tests__/client-id-tracker.test.d.ts +1 -0
- package/dist/__tests__/client-id-tracker.test.js +137 -0
- package/dist/__tests__/commands/new-atomic-commands.test.d.ts +1 -0
- package/dist/__tests__/commands/new-atomic-commands.test.js +502 -0
- package/dist/__tests__/commands/order-intent.test.d.ts +1 -0
- package/dist/__tests__/commands/order-intent.test.js +600 -0
- package/dist/__tests__/commands/trade-commands.test.d.ts +1 -0
- package/dist/__tests__/commands/trade-commands.test.js +821 -0
- package/dist/__tests__/config.test.d.ts +1 -0
- package/dist/__tests__/config.test.js +86 -0
- package/dist/__tests__/cross-chain-margin.test.d.ts +1 -0
- package/dist/__tests__/cross-chain-margin.test.js +287 -0
- package/dist/__tests__/dex-asset-map.test.d.ts +1 -0
- package/dist/__tests__/dex-asset-map.test.js +191 -0
- package/dist/__tests__/errors.test.d.ts +1 -0
- package/dist/__tests__/errors.test.js +110 -0
- package/dist/__tests__/event-stream.test.d.ts +1 -0
- package/dist/__tests__/event-stream.test.js +276 -0
- package/dist/__tests__/exchanges/interface.test.d.ts +1 -0
- package/dist/__tests__/exchanges/interface.test.js +132 -0
- package/dist/__tests__/exchanges/mock-adapter.d.ts +69 -0
- package/dist/__tests__/exchanges/mock-adapter.js +137 -0
- package/dist/__tests__/execution-log.test.d.ts +1 -0
- package/dist/__tests__/execution-log.test.js +106 -0
- package/dist/__tests__/funding-calc.test.d.ts +1 -0
- package/dist/__tests__/funding-calc.test.js +71 -0
- package/dist/__tests__/funding-history.test.d.ts +1 -0
- package/dist/__tests__/funding-history.test.js +343 -0
- package/dist/__tests__/funding-rates.test.d.ts +1 -0
- package/dist/__tests__/funding-rates.test.js +342 -0
- package/dist/__tests__/funding.test.d.ts +1 -0
- package/dist/__tests__/funding.test.js +173 -0
- package/dist/__tests__/gap-logic.test.d.ts +1 -0
- package/dist/__tests__/gap-logic.test.js +43 -0
- package/dist/__tests__/hip3-dex.test.d.ts +1 -0
- package/dist/__tests__/hip3-dex.test.js +234 -0
- package/dist/__tests__/integration/agent-features.integration.test.d.ts +1 -0
- package/dist/__tests__/integration/agent-features.integration.test.js +553 -0
- package/dist/__tests__/integration/atomic-commands.integration.test.d.ts +13 -0
- package/dist/__tests__/integration/atomic-commands.integration.test.js +246 -0
- package/dist/__tests__/integration/bridge-simulation.integration.test.d.ts +1 -0
- package/dist/__tests__/integration/bridge-simulation.integration.test.js +453 -0
- package/dist/__tests__/integration/bridge-strict.integration.test.d.ts +1 -0
- package/dist/__tests__/integration/bridge-strict.integration.test.js +812 -0
- package/dist/__tests__/integration/bridge.integration.test.d.ts +1 -0
- package/dist/__tests__/integration/bridge.integration.test.js +309 -0
- package/dist/__tests__/integration/cli-e2e.integration.test.d.ts +1 -0
- package/dist/__tests__/integration/cli-e2e.integration.test.js +202 -0
- package/dist/__tests__/integration/dex-arb.integration.test.d.ts +1 -0
- package/dist/__tests__/integration/dex-arb.integration.test.js +116 -0
- package/dist/__tests__/integration/envelope-consistency.integration.test.d.ts +13 -0
- package/dist/__tests__/integration/envelope-consistency.integration.test.js +205 -0
- package/dist/__tests__/integration/hip3-dex.integration.test.d.ts +1 -0
- package/dist/__tests__/integration/hip3-dex.integration.test.js +147 -0
- package/dist/__tests__/integration/hyperliquid.integration.test.d.ts +1 -0
- package/dist/__tests__/integration/hyperliquid.integration.test.js +79 -0
- package/dist/__tests__/integration/lighter.integration.test.d.ts +1 -0
- package/dist/__tests__/integration/lighter.integration.test.js +53 -0
- package/dist/__tests__/integration/new-commands-e2e.integration.test.d.ts +9 -0
- package/dist/__tests__/integration/new-commands-e2e.integration.test.js +236 -0
- package/dist/__tests__/integration/order-verification.integration.test.d.ts +1 -0
- package/dist/__tests__/integration/order-verification.integration.test.js +321 -0
- package/dist/__tests__/integration/pacifica.integration.test.d.ts +1 -0
- package/dist/__tests__/integration/pacifica.integration.test.js +75 -0
- package/dist/__tests__/integration/response-shapes.integration.test.d.ts +1 -0
- package/dist/__tests__/integration/response-shapes.integration.test.js +278 -0
- package/dist/__tests__/liquidity.test.d.ts +1 -0
- package/dist/__tests__/liquidity.test.js +225 -0
- package/dist/__tests__/plan-executor.test.d.ts +1 -0
- package/dist/__tests__/plan-executor.test.js +314 -0
- package/dist/__tests__/position-history.test.d.ts +1 -0
- package/dist/__tests__/position-history.test.js +367 -0
- package/dist/__tests__/retry.test.d.ts +1 -0
- package/dist/__tests__/retry.test.js +310 -0
- package/dist/__tests__/risk-assessment.test.d.ts +1 -0
- package/dist/__tests__/risk-assessment.test.js +145 -0
- package/dist/__tests__/security-adversarial.test.d.ts +1 -0
- package/dist/__tests__/security-adversarial.test.js +574 -0
- package/dist/__tests__/strategies.test.d.ts +1 -0
- package/dist/__tests__/strategies.test.js +539 -0
- package/dist/__tests__/trade-execution.test.d.ts +1 -0
- package/dist/__tests__/trade-execution.test.js +129 -0
- package/dist/__tests__/trade-validator.test.d.ts +1 -0
- package/dist/__tests__/trade-validator.test.js +655 -0
- package/dist/__tests__/utils.test.d.ts +1 -0
- package/dist/__tests__/utils.test.js +76 -0
- package/dist/api/public/hyperliquid.d.ts +18 -0
- package/dist/api/public/hyperliquid.js +82 -0
- package/dist/api/public/index.d.ts +8 -0
- package/dist/api/public/index.js +8 -0
- package/dist/api/public/lighter.d.ts +24 -0
- package/dist/api/public/lighter.js +100 -0
- package/dist/api/public/pacifica.d.ts +17 -0
- package/dist/api/public/pacifica.js +54 -0
- package/dist/api/public/urls.d.ts +12 -0
- package/dist/api/public/urls.js +33 -0
- package/dist/arb/history-stats.d.ts +44 -0
- package/dist/arb/history-stats.js +135 -0
- package/dist/arb/index.d.ts +4 -0
- package/dist/arb/index.js +4 -0
- package/dist/arb/sizing.d.ts +23 -0
- package/dist/arb/sizing.js +96 -0
- package/dist/arb/state.d.ts +51 -0
- package/dist/arb/state.js +112 -0
- package/dist/arb/utils.d.ts +81 -0
- package/dist/arb/utils.js +267 -0
- package/dist/arb-history-stats.d.ts +5 -0
- package/dist/arb-history-stats.js +5 -0
- package/dist/arb-sizing.d.ts +5 -0
- package/dist/arb-sizing.js +5 -0
- package/dist/arb-state.d.ts +5 -0
- package/dist/arb-state.js +5 -0
- package/dist/arb-utils.d.ts +5 -0
- package/dist/arb-utils.js +5 -0
- package/dist/bot/conditions.d.ts +32 -0
- package/dist/bot/conditions.js +141 -0
- package/dist/bot/config.d.ts +76 -0
- package/dist/bot/config.js +160 -0
- package/dist/bot/engine.d.ts +8 -0
- package/dist/bot/engine.js +519 -0
- package/dist/bot/presets.d.ts +11 -0
- package/dist/bot/presets.js +296 -0
- package/dist/bridge-engine.d.ts +133 -0
- package/dist/bridge-engine.js +1487 -0
- package/dist/cache.d.ts +25 -0
- package/dist/cache.js +99 -0
- package/dist/cli-spec.d.ts +50 -0
- package/dist/cli-spec.js +75 -0
- package/dist/client-id-tracker.d.ts +25 -0
- package/dist/client-id-tracker.js +76 -0
- package/dist/commands/account.d.ts +3 -0
- package/dist/commands/account.js +425 -0
- package/dist/commands/agent.d.ts +3 -0
- package/dist/commands/agent.js +386 -0
- package/dist/commands/alert.d.ts +2 -0
- package/dist/commands/alert.js +421 -0
- package/dist/commands/analytics.d.ts +3 -0
- package/dist/commands/analytics.js +311 -0
- package/dist/commands/arb/index.d.ts +3 -0
- package/dist/commands/arb/index.js +921 -0
- package/dist/commands/arb-auto.d.ts +54 -0
- package/dist/commands/arb-auto.js +1328 -0
- package/dist/commands/arb-manage.d.ts +5 -0
- package/dist/commands/arb-manage.js +5 -0
- package/dist/commands/arb.d.ts +2 -0
- package/dist/commands/arb.js +347 -0
- package/dist/commands/backtest.d.ts +2 -0
- package/dist/commands/backtest.js +327 -0
- package/dist/commands/bot.d.ts +3 -0
- package/dist/commands/bot.js +412 -0
- package/dist/commands/bridge.d.ts +2 -0
- package/dist/commands/bridge.js +396 -0
- package/dist/commands/dashboard.d.ts +3 -0
- package/dist/commands/dashboard.js +176 -0
- package/dist/commands/deposit.d.ts +4 -0
- package/dist/commands/deposit.js +573 -0
- package/dist/commands/dex.d.ts +3 -0
- package/dist/commands/dex.js +114 -0
- package/dist/commands/env.d.ts +2 -0
- package/dist/commands/env.js +136 -0
- package/dist/commands/funding.d.ts +2 -0
- package/dist/commands/funding.js +347 -0
- package/dist/commands/gap.d.ts +2 -0
- package/dist/commands/gap.js +305 -0
- package/dist/commands/health.d.ts +2 -0
- package/dist/commands/health.js +67 -0
- package/dist/commands/history.d.ts +2 -0
- package/dist/commands/history.js +235 -0
- package/dist/commands/init.d.ts +15 -0
- package/dist/commands/init.js +266 -0
- package/dist/commands/jobs.d.ts +2 -0
- package/dist/commands/jobs.js +133 -0
- package/dist/commands/manage.d.ts +4 -0
- package/dist/commands/manage.js +309 -0
- package/dist/commands/market.d.ts +3 -0
- package/dist/commands/market.js +225 -0
- package/dist/commands/plan.d.ts +3 -0
- package/dist/commands/plan.js +95 -0
- package/dist/commands/portfolio.d.ts +3 -0
- package/dist/commands/portfolio.js +169 -0
- package/dist/commands/rebalance.d.ts +3 -0
- package/dist/commands/rebalance.js +293 -0
- package/dist/commands/risk.d.ts +3 -0
- package/dist/commands/risk.js +169 -0
- package/dist/commands/run.d.ts +3 -0
- package/dist/commands/run.js +202 -0
- package/dist/commands/settings.d.ts +2 -0
- package/dist/commands/settings.js +102 -0
- package/dist/commands/stream.d.ts +5 -0
- package/dist/commands/stream.js +123 -0
- package/dist/commands/trade.d.ts +3 -0
- package/dist/commands/trade.js +1273 -0
- package/dist/commands/wallet.d.ts +14 -0
- package/dist/commands/wallet.js +602 -0
- package/dist/commands/withdraw.d.ts +3 -0
- package/dist/commands/withdraw.js +187 -0
- package/dist/config.d.ts +5 -0
- package/dist/config.js +68 -0
- package/dist/cross-chain-margin.d.ts +46 -0
- package/dist/cross-chain-margin.js +107 -0
- package/dist/dashboard/server.d.ts +80 -0
- package/dist/dashboard/server.js +340 -0
- package/dist/dashboard/ui.d.ts +4 -0
- package/dist/dashboard/ui.js +538 -0
- package/dist/dashboard/ws-feeds.d.ts +29 -0
- package/dist/dashboard/ws-feeds.js +660 -0
- package/dist/dex-asset-map.d.ts +80 -0
- package/dist/dex-asset-map.js +201 -0
- package/dist/errors.d.ts +109 -0
- package/dist/errors.js +84 -0
- package/dist/event-stream.d.ts +25 -0
- package/dist/event-stream.js +168 -0
- package/dist/exchanges/hyperliquid.d.ts +212 -0
- package/dist/exchanges/hyperliquid.js +931 -0
- package/dist/exchanges/interface.d.ts +95 -0
- package/dist/exchanges/interface.js +5 -0
- package/dist/exchanges/lighter.d.ts +159 -0
- package/dist/exchanges/lighter.js +793 -0
- package/dist/exchanges/pacifica.d.ts +51 -0
- package/dist/exchanges/pacifica.js +248 -0
- package/dist/execution-log.d.ts +36 -0
- package/dist/execution-log.js +102 -0
- package/dist/funding/history.d.ts +63 -0
- package/dist/funding/history.js +266 -0
- package/dist/funding/index.d.ts +3 -0
- package/dist/funding/index.js +3 -0
- package/dist/funding/normalize.d.ts +39 -0
- package/dist/funding/normalize.js +66 -0
- package/dist/funding/rates.d.ts +45 -0
- package/dist/funding/rates.js +172 -0
- package/dist/funding-history.d.ts +5 -0
- package/dist/funding-history.js +5 -0
- package/dist/funding-rates.d.ts +5 -0
- package/dist/funding-rates.js +5 -0
- package/dist/funding.d.ts +5 -0
- package/dist/funding.js +5 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +458 -0
- package/dist/jobs.d.ts +37 -0
- package/dist/jobs.js +152 -0
- package/dist/liquidity.d.ts +34 -0
- package/dist/liquidity.js +100 -0
- package/dist/mcp-server.d.ts +9 -0
- package/dist/mcp-server.js +1206 -0
- package/dist/pacifica/client.d.ts +111 -0
- package/dist/pacifica/client.js +310 -0
- package/dist/pacifica/constants.d.ts +27 -0
- package/dist/pacifica/constants.js +47 -0
- package/dist/pacifica/deposit.d.ts +14 -0
- package/dist/pacifica/deposit.js +78 -0
- package/dist/pacifica/index.d.ts +6 -0
- package/dist/pacifica/index.js +11 -0
- package/dist/pacifica/signing.d.ts +49 -0
- package/dist/pacifica/signing.js +97 -0
- package/dist/pacifica/types/account.d.ts +42 -0
- package/dist/pacifica/types/account.js +1 -0
- package/dist/pacifica/types/index.d.ts +6 -0
- package/dist/pacifica/types/index.js +6 -0
- package/dist/pacifica/types/lake.d.ts +18 -0
- package/dist/pacifica/types/lake.js +1 -0
- package/dist/pacifica/types/market.d.ts +64 -0
- package/dist/pacifica/types/market.js +1 -0
- package/dist/pacifica/types/order.d.ts +92 -0
- package/dist/pacifica/types/order.js +1 -0
- package/dist/pacifica/types/position.d.ts +25 -0
- package/dist/pacifica/types/position.js +1 -0
- package/dist/pacifica/types/ws.d.ts +34 -0
- package/dist/pacifica/types/ws.js +41 -0
- package/dist/pacifica/ws-client.d.ts +42 -0
- package/dist/pacifica/ws-client.js +180 -0
- package/dist/plan-executor.d.ts +48 -0
- package/dist/plan-executor.js +280 -0
- package/dist/position-history.d.ts +68 -0
- package/dist/position-history.js +222 -0
- package/dist/rebalance.d.ts +64 -0
- package/dist/rebalance.js +142 -0
- package/dist/retry.d.ts +74 -0
- package/dist/retry.js +129 -0
- package/dist/risk.d.ts +48 -0
- package/dist/risk.js +156 -0
- package/dist/settings.d.ts +19 -0
- package/dist/settings.js +45 -0
- package/dist/shared-api.d.ts +5 -0
- package/dist/shared-api.js +5 -0
- package/dist/strategies/dca.d.ts +25 -0
- package/dist/strategies/dca.js +114 -0
- package/dist/strategies/funding-arb.d.ts +15 -0
- package/dist/strategies/funding-arb.js +281 -0
- package/dist/strategies/grid.d.ts +34 -0
- package/dist/strategies/grid.js +185 -0
- package/dist/strategies/trailing-stop.d.ts +17 -0
- package/dist/strategies/trailing-stop.js +121 -0
- package/dist/strategies/twap.d.ts +20 -0
- package/dist/strategies/twap.js +78 -0
- package/dist/trade-validator.d.ts +39 -0
- package/dist/trade-validator.js +154 -0
- package/dist/utils.d.ts +38 -0
- package/dist/utils.js +110 -0
- package/package.json +63 -0
- package/skills/perp-cli/SKILL.md +149 -0
- package/skills/perp-cli/references/commands.md +143 -0
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
/**
|
|
3
|
+
* Unit tests for HIP-3 deployed perp dex support.
|
|
4
|
+
*
|
|
5
|
+
* Tests the parsing logic for allPerpMetas, dex-specific meta/markets,
|
|
6
|
+
* and the dex getter/setter behavior — all without real API calls.
|
|
7
|
+
*/
|
|
8
|
+
// ── Mock data matching real Hyperliquid API responses ──
|
|
9
|
+
const MOCK_ALL_PERP_METAS = [
|
|
10
|
+
// Entry 0: native/validator perps
|
|
11
|
+
{
|
|
12
|
+
universe: [
|
|
13
|
+
{ name: "BTC", szDecimals: 5, maxLeverage: 40 },
|
|
14
|
+
{ name: "ETH", szDecimals: 4, maxLeverage: 25 },
|
|
15
|
+
{ name: "SOL", szDecimals: 2, maxLeverage: 20 },
|
|
16
|
+
],
|
|
17
|
+
marginTables: [],
|
|
18
|
+
collateralToken: 0,
|
|
19
|
+
},
|
|
20
|
+
// Entry 1: "xyz" dex
|
|
21
|
+
{
|
|
22
|
+
universe: [
|
|
23
|
+
{ name: "xyz:XYZ100", szDecimals: 4, maxLeverage: 30 },
|
|
24
|
+
{ name: "xyz:TSLA", szDecimals: 3, maxLeverage: 10 },
|
|
25
|
+
{ name: "xyz:NVDA", szDecimals: 3, maxLeverage: 20 },
|
|
26
|
+
{ name: "xyz:GOLD", szDecimals: 3, maxLeverage: 10 },
|
|
27
|
+
],
|
|
28
|
+
marginTables: [],
|
|
29
|
+
collateralToken: 0,
|
|
30
|
+
},
|
|
31
|
+
// Entry 2: "vntl" dex
|
|
32
|
+
{
|
|
33
|
+
universe: [
|
|
34
|
+
{ name: "vntl:SPACEX", szDecimals: 2, maxLeverage: 5 },
|
|
35
|
+
{ name: "vntl:OPENAI", szDecimals: 2, maxLeverage: 5 },
|
|
36
|
+
{ name: "vntl:ANTHROPIC", szDecimals: 2, maxLeverage: 5 },
|
|
37
|
+
],
|
|
38
|
+
marginTables: [],
|
|
39
|
+
collateralToken: 360,
|
|
40
|
+
},
|
|
41
|
+
// Entry 3: empty dex (edge case)
|
|
42
|
+
{
|
|
43
|
+
universe: [],
|
|
44
|
+
marginTables: [],
|
|
45
|
+
collateralToken: 0,
|
|
46
|
+
},
|
|
47
|
+
];
|
|
48
|
+
const MOCK_DEX_META = {
|
|
49
|
+
universe: [
|
|
50
|
+
{ name: "xyz:TSLA", szDecimals: 3, maxLeverage: 10 },
|
|
51
|
+
{ name: "xyz:NVDA", szDecimals: 3, maxLeverage: 20 },
|
|
52
|
+
],
|
|
53
|
+
};
|
|
54
|
+
const MOCK_DEX_CTXS = [
|
|
55
|
+
{ markPx: "392.26", funding: "-0.0000239162", openInterest: "39985.292", dayNtlVlm: "3054400.08", oraclePx: "392.00" },
|
|
56
|
+
{ markPx: "175.94", funding: "-0.0000094967", openInterest: "392790.598", dayNtlVlm: "38782380.60", oraclePx: "176.00" },
|
|
57
|
+
];
|
|
58
|
+
const MOCK_DEX_CLEARINGHOUSE = {
|
|
59
|
+
marginSummary: { accountValue: "1000.00", totalMarginUsed: "200.00" },
|
|
60
|
+
crossMarginSummary: { accountValue: "1000.00", totalMarginUsed: "200.00" },
|
|
61
|
+
withdrawable: "800.00",
|
|
62
|
+
assetPositions: [
|
|
63
|
+
{
|
|
64
|
+
position: {
|
|
65
|
+
coin: "xyz:TSLA",
|
|
66
|
+
szi: "5.0",
|
|
67
|
+
entryPx: "380.00",
|
|
68
|
+
positionValue: "1961.30",
|
|
69
|
+
liquidationPx: "350.00",
|
|
70
|
+
unrealizedPnl: "61.30",
|
|
71
|
+
leverage: { value: 2 },
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
position: {
|
|
76
|
+
coin: "xyz:NVDA",
|
|
77
|
+
szi: "-10.0",
|
|
78
|
+
entryPx: "180.00",
|
|
79
|
+
positionValue: "1759.40",
|
|
80
|
+
liquidationPx: "210.00",
|
|
81
|
+
unrealizedPnl: "40.60",
|
|
82
|
+
leverage: { value: 3 },
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
],
|
|
86
|
+
};
|
|
87
|
+
// ── Tests ──
|
|
88
|
+
describe("HIP-3 Deployed Perps — listDeployedDexes parsing", () => {
|
|
89
|
+
it("extracts dex names from asset prefixes, skipping native perps", () => {
|
|
90
|
+
// Simulate the parsing logic from listDeployedDexes
|
|
91
|
+
const allMetas = MOCK_ALL_PERP_METAS;
|
|
92
|
+
const dexes = [];
|
|
93
|
+
for (let i = 1; i < allMetas.length; i++) {
|
|
94
|
+
const meta = allMetas[i];
|
|
95
|
+
const universe = meta.universe ?? [];
|
|
96
|
+
if (universe.length === 0)
|
|
97
|
+
continue;
|
|
98
|
+
const firstAsset = universe[0].name;
|
|
99
|
+
const colonIdx = firstAsset.indexOf(":");
|
|
100
|
+
const dexName = colonIdx > 0 ? firstAsset.slice(0, colonIdx) : `dex-${i}`;
|
|
101
|
+
dexes.push({
|
|
102
|
+
name: dexName,
|
|
103
|
+
assets: universe.map((a) => a.name),
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
expect(dexes).toHaveLength(2); // xyz, vntl (empty one skipped)
|
|
107
|
+
expect(dexes[0].name).toBe("xyz");
|
|
108
|
+
expect(dexes[0].assets).toEqual(["xyz:XYZ100", "xyz:TSLA", "xyz:NVDA", "xyz:GOLD"]);
|
|
109
|
+
expect(dexes[1].name).toBe("vntl");
|
|
110
|
+
expect(dexes[1].assets).toContain("vntl:SPACEX");
|
|
111
|
+
expect(dexes[1].assets).toContain("vntl:ANTHROPIC");
|
|
112
|
+
});
|
|
113
|
+
it("skips entries with empty universe", () => {
|
|
114
|
+
const allMetas = MOCK_ALL_PERP_METAS;
|
|
115
|
+
const nonEmpty = allMetas.slice(1).filter((m) => m.universe.length > 0);
|
|
116
|
+
expect(nonEmpty).toHaveLength(2);
|
|
117
|
+
});
|
|
118
|
+
it("handles response with only native perps", () => {
|
|
119
|
+
const onlyNative = [MOCK_ALL_PERP_METAS[0]];
|
|
120
|
+
const dexes = [];
|
|
121
|
+
for (let i = 1; i < onlyNative.length; i++) {
|
|
122
|
+
dexes.push("should-not-reach");
|
|
123
|
+
}
|
|
124
|
+
expect(dexes).toHaveLength(0);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
describe("HIP-3 Deployed Perps — dex-specific market parsing", () => {
|
|
128
|
+
it("parses metaAndAssetCtxs with dex param correctly", () => {
|
|
129
|
+
const universe = MOCK_DEX_META.universe;
|
|
130
|
+
const ctxs = MOCK_DEX_CTXS;
|
|
131
|
+
const markets = universe.map((asset, i) => {
|
|
132
|
+
const ctx = ctxs[i] ?? {};
|
|
133
|
+
return {
|
|
134
|
+
symbol: asset.name,
|
|
135
|
+
markPrice: String(ctx.markPx ?? "0"),
|
|
136
|
+
indexPrice: String(ctx.oraclePx ?? "0"),
|
|
137
|
+
fundingRate: String(ctx.funding ?? "0"),
|
|
138
|
+
volume24h: String(ctx.dayNtlVlm ?? "0"),
|
|
139
|
+
openInterest: String(ctx.openInterest ?? "0"),
|
|
140
|
+
maxLeverage: Number(asset.maxLeverage ?? 50),
|
|
141
|
+
};
|
|
142
|
+
});
|
|
143
|
+
expect(markets).toHaveLength(2);
|
|
144
|
+
expect(markets[0].symbol).toBe("xyz:TSLA");
|
|
145
|
+
expect(markets[0].markPrice).toBe("392.26");
|
|
146
|
+
expect(markets[0].fundingRate).toBe("-0.0000239162");
|
|
147
|
+
expect(markets[0].maxLeverage).toBe(10);
|
|
148
|
+
expect(markets[1].symbol).toBe("xyz:NVDA");
|
|
149
|
+
expect(Number(markets[1].volume24h)).toBeGreaterThan(1_000_000);
|
|
150
|
+
expect(markets[1].maxLeverage).toBe(20);
|
|
151
|
+
});
|
|
152
|
+
it("asset map is built from dex universe", () => {
|
|
153
|
+
const assetMap = new Map();
|
|
154
|
+
MOCK_DEX_META.universe.forEach((asset, idx) => {
|
|
155
|
+
assetMap.set(asset.name, idx);
|
|
156
|
+
});
|
|
157
|
+
expect(assetMap.get("xyz:TSLA")).toBe(0);
|
|
158
|
+
expect(assetMap.get("xyz:NVDA")).toBe(1);
|
|
159
|
+
expect(assetMap.get("BTC")).toBeUndefined(); // native asset not in dex
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
describe("HIP-3 Deployed Perps — dex-specific balance/positions parsing", () => {
|
|
163
|
+
it("parses clearinghouseState with dex param", () => {
|
|
164
|
+
const s = MOCK_DEX_CLEARINGHOUSE;
|
|
165
|
+
const margin = s.marginSummary ?? {};
|
|
166
|
+
const cross = s.crossMarginSummary ?? {};
|
|
167
|
+
const equity = Number(margin.accountValue ?? cross.accountValue ?? 0);
|
|
168
|
+
const available = Number(s.withdrawable ?? 0);
|
|
169
|
+
const marginUsed = Number(margin.totalMarginUsed ?? cross.totalMarginUsed ?? 0);
|
|
170
|
+
expect(equity).toBe(1000);
|
|
171
|
+
expect(available).toBe(800);
|
|
172
|
+
expect(marginUsed).toBe(200);
|
|
173
|
+
});
|
|
174
|
+
it("parses dex positions with prefixed coin names", () => {
|
|
175
|
+
const positions = MOCK_DEX_CLEARINGHOUSE.assetPositions
|
|
176
|
+
.filter((p) => Number(p.position.szi) !== 0)
|
|
177
|
+
.map((p) => {
|
|
178
|
+
const pos = p.position;
|
|
179
|
+
const szi = Number(pos.szi);
|
|
180
|
+
return {
|
|
181
|
+
symbol: pos.coin,
|
|
182
|
+
side: szi > 0 ? "long" : "short",
|
|
183
|
+
size: String(Math.abs(szi)),
|
|
184
|
+
entryPrice: pos.entryPx,
|
|
185
|
+
unrealizedPnl: pos.unrealizedPnl,
|
|
186
|
+
};
|
|
187
|
+
});
|
|
188
|
+
expect(positions).toHaveLength(2);
|
|
189
|
+
expect(positions[0].symbol).toBe("xyz:TSLA");
|
|
190
|
+
expect(positions[0].side).toBe("long");
|
|
191
|
+
expect(positions[0].size).toBe("5");
|
|
192
|
+
expect(positions[1].symbol).toBe("xyz:NVDA");
|
|
193
|
+
expect(positions[1].side).toBe("short");
|
|
194
|
+
expect(positions[1].size).toBe("10");
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
describe("HIP-3 Deployed Perps — setDex behavior", () => {
|
|
198
|
+
it("setDex clears asset map and sets dex name", () => {
|
|
199
|
+
const assetMap = new Map();
|
|
200
|
+
assetMap.set("BTC", 0);
|
|
201
|
+
assetMap.set("ETH", 1);
|
|
202
|
+
let dex = "";
|
|
203
|
+
// Simulate setDex("xyz")
|
|
204
|
+
dex = "xyz";
|
|
205
|
+
assetMap.clear();
|
|
206
|
+
expect(dex).toBe("xyz");
|
|
207
|
+
expect(assetMap.size).toBe(0);
|
|
208
|
+
});
|
|
209
|
+
it("empty dex string means native perps", () => {
|
|
210
|
+
let dex = "xyz";
|
|
211
|
+
dex = "";
|
|
212
|
+
expect(dex).toBeFalsy();
|
|
213
|
+
expect(!dex).toBe(true); // Used as condition: if (this._dex) { ... }
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
describe("HIP-3 — dex name extraction edge cases", () => {
|
|
217
|
+
it("handles single-char prefix", () => {
|
|
218
|
+
const name = "a:BTC";
|
|
219
|
+
const prefix = name.slice(0, name.indexOf(":"));
|
|
220
|
+
expect(prefix).toBe("a");
|
|
221
|
+
});
|
|
222
|
+
it("handles no colon (fallback to dex-N)", () => {
|
|
223
|
+
const name = "UNKNOWN";
|
|
224
|
+
const colonIdx = name.indexOf(":");
|
|
225
|
+
const dexName = colonIdx > 0 ? name.slice(0, colonIdx) : "dex-1";
|
|
226
|
+
expect(dexName).toBe("dex-1");
|
|
227
|
+
});
|
|
228
|
+
it("handles multiple colons (takes first)", () => {
|
|
229
|
+
const name = "abc:def:GHI";
|
|
230
|
+
const colonIdx = name.indexOf(":");
|
|
231
|
+
const dexName = name.slice(0, colonIdx);
|
|
232
|
+
expect(dexName).toBe("abc");
|
|
233
|
+
});
|
|
234
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|