-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinfo_example.ts
More file actions
152 lines (130 loc) · 5.18 KB
/
info_example.ts
File metadata and controls
152 lines (130 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env npx ts-node
// @ts-nocheck
/**
* Info API Example - Query market data and user info.
*
* This example shows how to query exchange metadata, prices, user positions, and more.
*
* Requirements:
* npm install hyperliquid-sdk
*
* Usage:
* export ENDPOINT="https://your-endpoint.example.com/TOKEN"
* npx ts-node info_example.ts
*/
import { HyperliquidSDK } from '@quicknode/hyperliquid-sdk';
// Get endpoint from environment
const ENDPOINT = process.env.ENDPOINT;
if (!ENDPOINT) {
console.error("Error: Set ENDPOINT environment variable");
console.error(" export ENDPOINT='https://your-endpoint.example.com/TOKEN'");
process.exit(1);
}
async function main() {
console.log("Hyperliquid Info API Example");
console.log("=".repeat(50));
console.log(`Endpoint: ${ENDPOINT.slice(0, 50)}...`);
console.log();
// Create SDK client
const sdk = new HyperliquidSDK(ENDPOINT);
// ==========================================================================
// Market Data
// ==========================================================================
console.log("Market Data");
console.log("-".repeat(30));
// Get all mid prices
const mids = await sdk.info.allMids();
const btcMid = parseFloat(mids['BTC'] || '0');
const ethMid = parseFloat(mids['ETH'] || '0');
console.log(`BTC mid: $${btcMid.toLocaleString(undefined, { minimumFractionDigits: 2 })}`);
console.log(`ETH mid: $${ethMid.toLocaleString(undefined, { minimumFractionDigits: 2 })}`);
console.log(`Total assets: ${Object.keys(mids).length}`);
console.log();
// Get L2 order book
const book = await sdk.info.l2Book("BTC");
const levels = book.levels || [[], []];
const bids = levels[0] || [];
const asks = levels[1] || [];
if (bids.length && asks.length) {
const bestBid = bids[0];
const bestAsk = asks[0];
const spread = parseFloat(bestAsk.px) - parseFloat(bestBid.px);
console.log("BTC Book:");
console.log(` Best Bid: ${bestBid.sz} @ $${parseFloat(bestBid.px).toLocaleString()}`);
console.log(` Best Ask: ${bestAsk.sz} @ $${parseFloat(bestAsk.px).toLocaleString()}`);
console.log(` Spread: $${spread.toLocaleString()}`);
}
console.log();
// Get recent trades
const trades = await sdk.info.recentTrades("ETH");
console.log(`Recent ETH trades: ${trades.length}`);
if (trades.length) {
const lastTrade = trades[0] as Record<string, unknown>;
console.log(` Last: ${lastTrade.sz} @ $${parseFloat(String(lastTrade.px || 0)).toLocaleString()}`);
}
console.log();
// ==========================================================================
// Exchange Metadata
// ==========================================================================
console.log("Exchange Metadata");
console.log("-".repeat(30));
const meta = await sdk.info.meta();
const universe = meta.universe || [];
console.log(`Total perp markets: ${universe.length}`);
// Show a few markets
for (const asset of universe.slice(0, 5)) {
const name = asset.name || "?";
const szDecimals = asset.szDecimals ?? "?";
console.log(` ${name}: ${szDecimals} size decimals`);
}
console.log();
// ==========================================================================
// User Account (requires a valid address)
// ==========================================================================
console.log("User Account");
console.log("-".repeat(30));
// Example address - replace with your address
const userAddress = "0x0000000000000000000000000000000000000000";
try {
// Get clearinghouse state (positions, margin)
const state = await sdk.info.clearinghouseState(userAddress);
const equity = parseFloat(state.marginSummary?.accountValue || "0");
console.log(`Account equity: $${equity.toLocaleString()}`);
const positions = state.assetPositions || [];
if (positions.length) {
console.log(`Open positions: ${positions.length}`);
for (const pos of positions.slice(0, 3)) {
const coin = pos.position?.coin || "?";
const size = pos.position?.szi || "0";
const entry = pos.position?.entryPx || "?";
const pnl = parseFloat(pos.position?.unrealizedPnl || "0");
console.log(` ${coin}: ${size} @ ${entry} (PnL: $${pnl.toLocaleString()})`);
}
} else {
console.log(" No open positions");
}
} catch (e) {
console.log(` Could not fetch user data: ${e}`);
}
console.log();
// ==========================================================================
// Funding Rates
// ==========================================================================
console.log("Funding Rates");
console.log("-".repeat(30));
try {
const fundings = await sdk.info.predictedFundings();
console.log(`Predicted funding rates for ${fundings.length} assets:`);
for (const f of fundings.slice(0, 5)) {
const coin = (f as Record<string, unknown>).coin || "?";
const rate = parseFloat(String((f as Record<string, unknown>).fundingRate || 0)) * 100;
console.log(` ${coin}: ${rate.toFixed(4)}%`);
}
} catch (e) {
console.log(` Could not fetch funding: ${e}`);
}
console.log();
console.log("=".repeat(50));
console.log("Done!");
}
main().catch(console.error);