-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinfo_user_data.py
More file actions
100 lines (82 loc) · 2.95 KB
/
info_user_data.py
File metadata and controls
100 lines (82 loc) · 2.95 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
#!/usr/bin/env python3
"""
User Account Data Example
Shows how to query user positions, orders, and account state.
Requirements:
pip install hyperliquid-sdk
Usage:
export ENDPOINT="https://your-endpoint.hype-mainnet.quiknode.pro/TOKEN"
export USER_ADDRESS="0x..."
python info_user_data.py
"""
import os
import sys
from hyperliquid_sdk import HyperliquidSDK
ENDPOINT = os.environ.get("ENDPOINT") or os.environ.get("QUICKNODE_ENDPOINT")
USER = os.environ.get("USER_ADDRESS") or "0x2ba553d9f990a3b66b03b2dc0d030dfc1c061036"
if not ENDPOINT:
print("Error: Set ENDPOINT environment variable")
print(" export ENDPOINT='https://your-endpoint.hype-mainnet.quiknode.pro/TOKEN'")
sys.exit(1)
def main():
print("=" * 50)
print(f"User Data: {USER[:10]}...")
print("=" * 50)
# Single SDK instance - access everything through sdk.info, sdk.core, sdk.evm
sdk = HyperliquidSDK(ENDPOINT)
info = sdk.info
# Clearinghouse state (positions + margin)
print("\n1. Positions & Margin:")
try:
state = info.clearinghouse_state(USER)
margin = state.get("marginSummary", {})
print(f" Account Value: ${margin.get('accountValue', '0')}")
print(f" Margin Used: ${margin.get('totalMarginUsed', '0')}")
positions = state.get("assetPositions", [])
if positions:
print(f" Positions: {len(positions)}")
for pos in positions[:3]:
p = pos.get("position", {})
print(f" - {p.get('coin')}: {p.get('szi')} @ {p.get('entryPx')}")
else:
print(" No positions")
except Exception as e:
print(f" (clearinghouse_state not available: {e})")
# Open orders
print("\n2. Open Orders:")
try:
orders = info.open_orders(USER)
if orders:
print(f" {len(orders)} orders:")
for o in orders[:3]:
side = "BUY" if o.get("side") == "B" else "SELL"
print(f" - {o.get('coin')}: {side} {o.get('sz')} @ {o.get('limitPx')}")
else:
print(" No open orders")
except Exception as e:
print(f" (open_orders not available: {e})")
# User fees
print("\n3. Fee Structure:")
try:
fees = info.user_fees(USER)
print(f" Maker: {fees.get('makerRate', 'N/A')}")
print(f" Taker: {fees.get('takerRate', 'N/A')}")
except Exception as e:
print(f" (user_fees not available: {e})")
# Spot balances
print("\n4. Spot Balances:")
try:
spot = info.spot_clearinghouse_state(USER)
balances = spot.get("balances", [])
if balances:
for b in balances[:5]:
print(f" - {b.get('coin')}: {b.get('total')}")
else:
print(" No spot balances")
except Exception as e:
print(f" (spot_clearinghouse_state not available: {e})")
print()
print("=" * 50)
print("Done!")
if __name__ == "__main__":
main()