|
1 |
| -import { type Args, parseArgs } from "jsr:@std/cli@1/parse-args"; |
2 |
| -import { BigNumber } from "npm:bignumber.js@9"; |
3 |
| -import { generatePrivateKey } from "npm:viem@2/accounts"; |
4 |
| -import { |
5 |
| - ExchangeClient, |
6 |
| - HttpTransport, |
7 |
| - InfoClient, |
8 |
| - type PerpsAssetCtx, |
9 |
| - type PerpsUniverse, |
10 |
| - SubscriptionClient, |
11 |
| - WebSocketTransport, |
12 |
| -} from "../../../mod.ts"; |
13 |
| -import { getWalletAddress } from "../../../src/signing/mod.ts"; |
14 |
| - |
15 |
| -// —————————— Arguments —————————— |
16 |
| - |
17 |
| -const cliArgs = parseArgs(Deno.args, { default: { wait: 1000 }, string: ["_"] }) as Args<{ |
18 |
| - /** Delay to avoid rate limits */ |
19 |
| - wait: number; |
20 |
| -}>; |
21 |
| - |
22 |
| -const PRIVATE_KEY = cliArgs._[0] as `0x${string}`; // must be sole signer for a multi-sign account |
23 |
| - |
24 |
| -// —————————— Clients —————————— |
25 |
| - |
26 |
| -async function createExchangeClient(mainExchClient: ExchangeClient): Promise<ExchangeClient> { |
27 |
| - const tempExchClient = new ExchangeClient({ |
28 |
| - wallet: generatePrivateKey(), |
29 |
| - transport: new HttpTransport({ isTestnet: true }), |
30 |
| - isTestnet: true, |
31 |
| - }); |
32 |
| - await mainExchClient.usdSend({ |
33 |
| - destination: await getWalletAddress(tempExchClient.wallet), |
34 |
| - amount: "2", |
35 |
| - }); |
36 |
| - return tempExchClient; |
37 |
| -} |
38 |
| - |
39 |
| -// —————————— Functions —————————— |
40 |
| - |
41 |
| -export function runTest( |
42 |
| - name: string, |
43 |
| - testFn: (t: Deno.TestContext, client: SubscriptionClient) => Promise<void>, |
44 |
| - mode: "api" | "rpc", |
45 |
| -): void { |
46 |
| - Deno.test(name, async (t) => { |
47 |
| - await new Promise((r) => setTimeout(r, cliArgs.wait)); // delay to avoid rate limits |
48 |
| - |
49 |
| - await using transport = new WebSocketTransport({ url: `wss://${mode}.hyperliquid-testnet.xyz/ws` }); |
50 |
| - await transport.ready(); |
51 |
| - const subsClient = new SubscriptionClient({ transport }); |
52 |
| - |
53 |
| - await testFn(t, subsClient); |
54 |
| - }); |
55 |
| -} |
56 |
| - |
57 |
| -export function runTestWithExch( |
58 |
| - name: string, |
59 |
| - testFn: ( |
60 |
| - t: Deno.TestContext, |
61 |
| - client: { |
62 |
| - subs: SubscriptionClient; |
63 |
| - exch: ExchangeClient; |
64 |
| - info: InfoClient; |
65 |
| - }, |
66 |
| - ) => Promise<void>, |
67 |
| - topup?: { perp?: string }, |
68 |
| -): void { |
69 |
| - if (!PRIVATE_KEY || !/^0x[a-fA-F0-9]{64}$/.test(PRIVATE_KEY)) { |
70 |
| - throw new Error("Please provide a valid private key (0x-prefixed 64 hex characters) as an argument"); |
71 |
| - } |
72 |
| - |
73 |
| - Deno.test(name, async (t) => { |
74 |
| - await new Promise((r) => setTimeout(r, cliArgs.wait)); // delay to avoid rate limits |
75 |
| - |
76 |
| - await using transport = new WebSocketTransport({ url: `wss://api.hyperliquid-testnet.xyz/ws` }); |
77 |
| - await transport.ready(); |
78 |
| - |
79 |
| - const subsClient = new SubscriptionClient({ transport }); |
80 |
| - |
81 |
| - const mainExchClient = new ExchangeClient({ wallet: PRIVATE_KEY, transport, isTestnet: true }); |
82 |
| - const exchClient = await createExchangeClient(mainExchClient); |
83 |
| - |
84 |
| - const infoClient = new InfoClient({ transport }); |
85 |
| - |
86 |
| - if (topup?.perp) { |
87 |
| - await mainExchClient.usdSend({ |
88 |
| - destination: await getWalletAddress(exchClient.wallet), |
89 |
| - amount: topup.perp, |
90 |
| - }); |
91 |
| - } |
92 |
| - |
93 |
| - try { |
94 |
| - await testFn(t, { subs: subsClient, exch: exchClient, info: infoClient }); |
95 |
| - } finally { |
96 |
| - const state = await infoClient.clearinghouseState({ user: await getWalletAddress(exchClient.wallet) }); |
97 |
| - await exchClient.usdSend({ |
98 |
| - destination: await getWalletAddress(mainExchClient.wallet), |
99 |
| - amount: state.withdrawable, |
100 |
| - }).catch(() => undefined); |
101 |
| - } |
102 |
| - }); |
103 |
| -} |
104 |
| - |
105 |
| -// —————————— Utils —————————— |
106 |
| - |
107 |
| -export function randomCloid(): `0x${string}` { |
108 |
| - return `0x${Array.from({ length: 32 }, () => Math.floor(Math.random() * 16).toString(16)).join("")}`; |
109 |
| -} |
110 |
| - |
111 |
| -export async function getAssetData(assetName: string): Promise<{ |
112 |
| - id: number; |
113 |
| - universe: PerpsUniverse; |
114 |
| - ctx: PerpsAssetCtx; |
115 |
| -}> { |
116 |
| - const infoClient = new InfoClient({ transport: new HttpTransport({ isTestnet: true }) }); |
117 |
| - const data = await infoClient.metaAndAssetCtxs(); |
118 |
| - const id = data[0].universe.findIndex((u) => u.name === assetName); |
119 |
| - if (id === -1) throw new Error(`Asset "${assetName}" not found`); |
120 |
| - const universe = data[0].universe[id]; |
121 |
| - const ctx = data[1][id]; |
122 |
| - return { id, universe, ctx }; |
123 |
| -} |
124 |
| - |
125 |
| -export function formatPrice( |
126 |
| - price: BigNumber.Value, |
127 |
| - szDecimals: number, |
128 |
| - isPerp: boolean = true, |
129 |
| - roundingMode: BigNumber.RoundingMode = BigNumber.ROUND_HALF_UP, |
130 |
| -): string { |
131 |
| - const priceBN = new BigNumber(price); |
132 |
| - if (priceBN.isInteger()) return priceBN.toString(); |
133 |
| - |
134 |
| - const maxDecimals = isPerp ? 6 : 8; |
135 |
| - const maxAllowedDecimals = Math.max(maxDecimals - szDecimals, 0); |
136 |
| - |
137 |
| - return priceBN |
138 |
| - .precision(5, roundingMode) |
139 |
| - .toFixed(maxAllowedDecimals, roundingMode); |
140 |
| -} |
141 |
| - |
142 |
| -export function formatSize( |
143 |
| - size: BigNumber.Value, |
144 |
| - szDecimals: number, |
145 |
| - roundingMode: BigNumber.RoundingMode = BigNumber.ROUND_HALF_UP, |
146 |
| -): string { |
147 |
| - return new BigNumber(size) |
148 |
| - .toFixed(szDecimals, roundingMode) |
149 |
| - .replace(/\.?0+$/, ""); // Remove trailing zeros |
150 |
| -} |
| 1 | +import { type Args, parseArgs } from "jsr:@std/cli@1/parse-args"; |
| 2 | +import { BigNumber } from "npm:bignumber.js@9"; |
| 3 | +import { generatePrivateKey } from "npm:viem@2/accounts"; |
| 4 | +import { |
| 5 | + ExchangeClient, |
| 6 | + HttpTransport, |
| 7 | + InfoClient, |
| 8 | + type PerpsAssetCtx, |
| 9 | + type PerpsUniverse, |
| 10 | + SubscriptionClient, |
| 11 | + WebSocketTransport, |
| 12 | +} from "../../../mod.ts"; |
| 13 | +import { getWalletAddress } from "../../../src/signing/mod.ts"; |
| 14 | + |
| 15 | +// —————————— Arguments —————————— |
| 16 | + |
| 17 | +const cliArgs = parseArgs(Deno.args, { default: { wait: 1000 }, string: ["_"] }) as Args<{ |
| 18 | + /** Delay to avoid rate limits */ |
| 19 | + wait: number; |
| 20 | +}>; |
| 21 | + |
| 22 | +const PRIVATE_KEY = cliArgs._[0] as `0x${string}`; // must be sole signer for a multi-sign account |
| 23 | + |
| 24 | +// —————————— Clients —————————— |
| 25 | + |
| 26 | +async function createExchangeClient(mainExchClient: ExchangeClient): Promise<ExchangeClient> { |
| 27 | + const tempExchClient = new ExchangeClient({ |
| 28 | + wallet: generatePrivateKey(), |
| 29 | + transport: new HttpTransport({ isTestnet: true }), |
| 30 | + isTestnet: true, |
| 31 | + }); |
| 32 | + await mainExchClient.usdSend({ |
| 33 | + destination: await getWalletAddress(tempExchClient.wallet), |
| 34 | + amount: "2", |
| 35 | + }); |
| 36 | + return tempExchClient; |
| 37 | +} |
| 38 | + |
| 39 | +// —————————— Functions —————————— |
| 40 | + |
| 41 | +export function runTest( |
| 42 | + name: string, |
| 43 | + testFn: (t: Deno.TestContext, client: SubscriptionClient) => Promise<void>, |
| 44 | + mode: "api" | "rpc", |
| 45 | +): void { |
| 46 | + Deno.test(name, async (t) => { |
| 47 | + await new Promise((r) => setTimeout(r, cliArgs.wait)); // delay to avoid rate limits |
| 48 | + |
| 49 | + await using transport = new WebSocketTransport({ url: `wss://${mode}.hyperliquid-testnet.xyz/ws` }); |
| 50 | + await transport.ready(); |
| 51 | + const subsClient = new SubscriptionClient({ transport }); |
| 52 | + |
| 53 | + await testFn(t, subsClient); |
| 54 | + }); |
| 55 | +} |
| 56 | + |
| 57 | +export function runTestWithExch( |
| 58 | + name: string, |
| 59 | + testFn: ( |
| 60 | + t: Deno.TestContext, |
| 61 | + client: { |
| 62 | + subs: SubscriptionClient; |
| 63 | + exch: ExchangeClient; |
| 64 | + info: InfoClient; |
| 65 | + }, |
| 66 | + ) => Promise<void>, |
| 67 | + topup?: { perp?: string }, |
| 68 | +): void { |
| 69 | + if (!PRIVATE_KEY || !/^0x[a-fA-F0-9]{64}$/.test(PRIVATE_KEY)) { |
| 70 | + throw new Error("Please provide a valid private key (0x-prefixed 64 hex characters) as an argument"); |
| 71 | + } |
| 72 | + |
| 73 | + Deno.test(name, async (t) => { |
| 74 | + await new Promise((r) => setTimeout(r, cliArgs.wait)); // delay to avoid rate limits |
| 75 | + |
| 76 | + await using transport = new WebSocketTransport({ url: `wss://api.hyperliquid-testnet.xyz/ws` }); |
| 77 | + await transport.ready(); |
| 78 | + |
| 79 | + const subsClient = new SubscriptionClient({ transport }); |
| 80 | + |
| 81 | + const mainExchClient = new ExchangeClient({ wallet: PRIVATE_KEY, transport, isTestnet: true }); |
| 82 | + const exchClient = await createExchangeClient(mainExchClient); |
| 83 | + |
| 84 | + const infoClient = new InfoClient({ transport }); |
| 85 | + |
| 86 | + if (topup?.perp) { |
| 87 | + await mainExchClient.usdSend({ |
| 88 | + destination: await getWalletAddress(exchClient.wallet), |
| 89 | + amount: topup.perp, |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + try { |
| 94 | + await testFn(t, { subs: subsClient, exch: exchClient, info: infoClient }); |
| 95 | + } finally { |
| 96 | + const state = await infoClient.clearinghouseState({ user: await getWalletAddress(exchClient.wallet) }); |
| 97 | + await exchClient.usdSend({ |
| 98 | + destination: await getWalletAddress(mainExchClient.wallet), |
| 99 | + amount: state.withdrawable, |
| 100 | + }).catch(() => undefined); |
| 101 | + } |
| 102 | + }); |
| 103 | +} |
| 104 | + |
| 105 | +// —————————— Utils —————————— |
| 106 | + |
| 107 | +export function randomCloid(): `0x${string}` { |
| 108 | + return `0x${Array.from({ length: 32 }, () => Math.floor(Math.random() * 16).toString(16)).join("")}`; |
| 109 | +} |
| 110 | + |
| 111 | +export async function getAssetData(assetName: string): Promise<{ |
| 112 | + id: number; |
| 113 | + universe: PerpsUniverse; |
| 114 | + ctx: PerpsAssetCtx; |
| 115 | +}> { |
| 116 | + const infoClient = new InfoClient({ transport: new HttpTransport({ isTestnet: true }) }); |
| 117 | + const data = await infoClient.metaAndAssetCtxs(); |
| 118 | + const id = data[0].universe.findIndex((u) => u.name === assetName); |
| 119 | + if (id === -1) throw new Error(`Asset "${assetName}" not found`); |
| 120 | + const universe = data[0].universe[id]; |
| 121 | + const ctx = data[1][id]; |
| 122 | + return { id, universe, ctx }; |
| 123 | +} |
| 124 | + |
| 125 | +export function formatPrice( |
| 126 | + price: BigNumber.Value, |
| 127 | + szDecimals: number, |
| 128 | + isPerp: boolean = true, |
| 129 | + roundingMode: BigNumber.RoundingMode = BigNumber.ROUND_HALF_UP, |
| 130 | +): string { |
| 131 | + const priceBN = new BigNumber(price); |
| 132 | + if (priceBN.isInteger()) return priceBN.toString(); |
| 133 | + |
| 134 | + const maxDecimals = isPerp ? 6 : 8; |
| 135 | + const maxAllowedDecimals = Math.max(maxDecimals - szDecimals, 0); |
| 136 | + |
| 137 | + return priceBN |
| 138 | + .precision(5, roundingMode) |
| 139 | + .toFixed(maxAllowedDecimals, roundingMode); |
| 140 | +} |
| 141 | + |
| 142 | +export function formatSize( |
| 143 | + size: BigNumber.Value, |
| 144 | + szDecimals: number, |
| 145 | + roundingMode: BigNumber.RoundingMode = BigNumber.ROUND_HALF_UP, |
| 146 | +): string { |
| 147 | + return new BigNumber(size) |
| 148 | + .toFixed(szDecimals, roundingMode) |
| 149 | + .replace(/\.?0+$/, ""); // Remove trailing zeros |
| 150 | +} |
0 commit comments