|
1 | 1 | import eth_account |
2 | | -import utils |
3 | 2 | from eth_account.signers.local import LocalAccount |
4 | 3 |
|
5 | 4 | from hyperliquid.exchange import Exchange |
6 | 5 | from hyperliquid.utils import constants |
| 6 | +import example_utils |
7 | 7 |
|
8 | 8 |
|
9 | 9 | def main(): |
10 | | - config = utils.get_config() |
11 | | - account: LocalAccount = eth_account.Account.from_key(config["secret_key"]) |
12 | | - print("Running with account address:", account.address) |
13 | | - exchange = Exchange(account, constants.TESTNET_API_URL) |
| 10 | + address, info, exchange = example_utils.setup(constants.TESTNET_API_URL, skip_ws=True) |
| 11 | + |
| 12 | + if exchange.account_address != exchange.wallet.address: |
| 13 | + raise Exception("You should not create an agent using an agent") |
14 | 14 |
|
15 | 15 | # Create an agent that can place trades on behalf of the account. The agent does not have permission to transfer |
16 | 16 | # or withdraw funds. |
17 | 17 | # You can run this part on a separate machine or change the code to connect the agent via a wallet app instead of |
18 | | - # using your private key directly in python |
| 18 | + # using your private key directly in python. |
| 19 | + # You can also create a named agent using the frontend, which persists the authorization under an agent name. |
19 | 20 | approve_result, agent_key = exchange.approve_agent() |
20 | 21 | if approve_result["status"] != "ok": |
21 | 22 | print("approving agent failed", approve_result) |
22 | 23 | return |
23 | 24 |
|
24 | | - # Place an order that should rest by setting the price very low |
25 | 25 | agent_account: LocalAccount = eth_account.Account.from_key(agent_key) |
26 | 26 | print("Running with agent address:", agent_account.address) |
27 | | - exchange = Exchange(agent_account, constants.TESTNET_API_URL) |
28 | | - order_result = exchange.order("ETH", True, 0.2, 1000, {"limit": {"tif": "Gtc"}}) |
| 27 | + agent_exchange = Exchange(agent_account, constants.TESTNET_API_URL, account_address=address) |
| 28 | + # Place an order that should rest by setting the price very low |
| 29 | + order_result = agent_exchange.order("ETH", True, 0.2, 1000, {"limit": {"tif": "Gtc"}}) |
29 | 30 | print(order_result) |
30 | 31 |
|
31 | 32 | # Cancel the order |
32 | 33 | if order_result["status"] == "ok": |
33 | 34 | status = order_result["response"]["data"]["statuses"][0] |
34 | 35 | if "resting" in status: |
35 | | - cancel_result = exchange.cancel("ETH", status["resting"]["oid"]) |
| 36 | + cancel_result = agent_exchange.cancel("ETH", status["resting"]["oid"]) |
36 | 37 | print(cancel_result) |
37 | 38 |
|
38 | 39 | # Create an extra named agent |
39 | | - exchange = Exchange(account, constants.TESTNET_API_URL) |
40 | 40 | approve_result, extra_agent_key = exchange.approve_agent("persist") |
41 | 41 | if approve_result["status"] != "ok": |
42 | 42 | print("approving extra agent failed", approve_result) |
43 | 43 | return |
44 | 44 |
|
45 | 45 | extra_agent_account: LocalAccount = eth_account.Account.from_key(extra_agent_key) |
| 46 | + extra_agent_exchange = Exchange(extra_agent_account, constants.TESTNET_API_URL, account_address=address) |
46 | 47 | print("Running with extra agent address:", extra_agent_account.address) |
47 | 48 |
|
48 | 49 | print("Placing order with original agent") |
49 | | - order_result = exchange.order("ETH", True, 0.2, 1000, {"limit": {"tif": "Gtc"}}) |
| 50 | + order_result = extra_agent_exchange.order("ETH", True, 0.2, 1000, {"limit": {"tif": "Gtc"}}) |
50 | 51 | print(order_result) |
51 | 52 |
|
52 | 53 | if order_result["status"] == "ok": |
53 | 54 | status = order_result["response"]["data"]["statuses"][0] |
54 | 55 | if "resting" in status: |
55 | 56 | print("Canceling order with extra agent") |
56 | | - exchange = Exchange(extra_agent_account, constants.TESTNET_API_URL) |
57 | | - cancel_result = exchange.cancel("ETH", status["resting"]["oid"]) |
| 57 | + cancel_result = extra_agent_exchange.cancel("ETH", status["resting"]["oid"]) |
58 | 58 | print(cancel_result) |
59 | 59 |
|
60 | 60 |
|
|
0 commit comments