-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathexample_utils.py
48 lines (41 loc) · 2.13 KB
/
example_utils.py
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
import json
import os
import eth_account
from eth_account.signers.local import LocalAccount
from hyperliquid.exchange import Exchange
from hyperliquid.info import Info
def setup(base_url=None, skip_ws=False):
config_path = os.path.join(os.path.dirname(__file__), "config.json")
with open(config_path) as f:
config = json.load(f)
account: LocalAccount = eth_account.Account.from_key(config["secret_key"])
address = config["account_address"]
if address == "":
address = account.address
print("Running with account address:", address)
if address != account.address:
print("Running with agent address:", account.address)
info = Info(base_url, skip_ws)
user_state = info.user_state(address)
spot_user_state = info.spot_user_state(address)
margin_summary = user_state["marginSummary"]
if float(margin_summary["accountValue"]) == 0 and len(spot_user_state["balances"]) == 0:
print("Not running the example because the provided account has no equity.")
url = info.base_url.split(".", 1)[1]
error_string = f"No accountValue:\nIf you think this is a mistake, make sure that {address} has a balance on {url}.\nIf address shown is your API wallet address, update the config to specify the address of your account, not the address of the API wallet."
raise Exception(error_string)
exchange = Exchange(account, base_url, account_address=address)
return address, info, exchange
def setup_multi_sig_wallets():
config_path = os.path.join(os.path.dirname(__file__), "config.json")
with open(config_path) as f:
config = json.load(f)
authorized_user_wallets = []
for wallet_config in config["multi_sig"]["authorized_users"]:
account: LocalAccount = eth_account.Account.from_key(wallet_config["secret_key"])
address = wallet_config["account_address"]
if account.address != address:
raise Exception(f"provided authorized user address {address} does not match private key")
print("loaded authorized user for multi-sig", address)
authorized_user_wallets.append(account)
return authorized_user_wallets