|
7 | 7 |
|
8 | 8 |
|
9 | 9 | def main(): |
| 10 | + """ |
| 11 | + Sets up an environment for testing purposes by creating an agent that can place trades on behalf of the account. |
| 12 | + The agent does not have permission to transfer or withdraw funds. You can run this part on a separate machine or |
| 13 | + change the code to connect the agent via a wallet app instead of using your private key directly in Python. |
| 14 | + You can also create a named agent using the frontend, which persists the authorization under an agent name. |
| 15 | + |
| 16 | + The main function then proceeds to place a test order with the agent and simulates the process of managing orders |
| 17 | + and ensuring that orders that are no longer needed are cleaned up. |
| 18 | + Finally, it creates an extra agent that persists beyond the current session and places an order with the extra agent. |
| 19 | + """ |
| 20 | + |
| 21 | + # Set up the environment (exchange, account info, etc.) for testing purposes. |
10 | 22 | address, info, exchange = example_utils.setup(constants.TESTNET_API_URL, skip_ws=True) |
11 | 23 |
|
| 24 | + # Ensure that the wallet address and agent address are not the same |
| 25 | + # This prevents the risk of accidentally creating an agent using the wallet itself. |
12 | 26 | if exchange.account_address != exchange.wallet.address: |
13 | 27 | raise Exception("You should not create an agent using an agent") |
14 | 28 |
|
15 | | - # Create an agent that can place trades on behalf of the account. The agent does not have permission to transfer |
16 | | - # or withdraw funds. |
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. |
19 | | - # You can also create a named agent using the frontend, which persists the authorization under an agent name. |
| 29 | + |
20 | 30 | approve_result, agent_key = exchange.approve_agent() |
| 31 | + |
| 32 | + # Check if the agent approval was successful. If not, log the error and return. |
| 33 | + # This prevents proceeding with an agent that isn't properly authorized. |
21 | 34 | if approve_result["status"] != "ok": |
22 | 35 | print("approving agent failed", approve_result) |
23 | 36 | return |
24 | | - |
| 37 | + |
| 38 | + # Create the agent's local account using the agent's private key. |
| 39 | + # We use `eth_account.Account.from_key()` to securely generate the agent's account from its private key. |
25 | 40 | agent_account: LocalAccount = eth_account.Account.from_key(agent_key) |
26 | 41 | print("Running with agent address:", agent_account.address) |
| 42 | + |
| 43 | + # Create a new exchange instance for the agent, providing it with the agent's account information and exchange URL. |
| 44 | + # This exchange object will be used for placing orders and interacting with the Hyperliquid API. |
27 | 45 | 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 |
| 46 | + |
| 47 | + # Place a test order with the agent (setting a very low price so that it rests in the order book). |
| 48 | + # The order is placed as a "limit" order with the time-in-force set to "Good till Cancelled" (GTC). |
| 49 | + # This allows us to test placing an order without immediately executing it. |
29 | 50 | order_result = agent_exchange.order("ETH", True, 0.2, 1000, {"limit": {"tif": "Gtc"}}) |
30 | 51 | print(order_result) |
31 | 52 |
|
32 | | - # Cancel the order |
| 53 | + # If the order was placed successfully and the status is "resting," we attempt to cancel it. |
| 54 | + # This simulates the process of managing orders and ensuring that orders are no longer needed are cleaned up. |
33 | 55 | if order_result["status"] == "ok": |
34 | 56 | status = order_result["response"]["data"]["statuses"][0] |
35 | 57 | if "resting" in status: |
36 | 58 | cancel_result = agent_exchange.cancel("ETH", status["resting"]["oid"]) |
37 | 59 | print(cancel_result) |
38 | 60 |
|
39 | | - # Create an extra named agent |
| 61 | + # Create an extra agent that persists beyond the current session. |
| 62 | + # The "persist" argument ensures that the agent remains available for future interactions and doesn't require re-approval each time. |
| 63 | + |
40 | 64 | approve_result, extra_agent_key = exchange.approve_agent("persist") |
| 65 | + |
| 66 | + # Check if the extra agent was successfully approved. |
41 | 67 | if approve_result["status"] != "ok": |
42 | 68 | print("approving extra agent failed", approve_result) |
43 | 69 | return |
44 | 70 |
|
| 71 | + # Create the extra agent account using its private key and the same process as above. |
45 | 72 | extra_agent_account: LocalAccount = eth_account.Account.from_key(extra_agent_key) |
46 | 73 | extra_agent_exchange = Exchange(extra_agent_account, constants.TESTNET_API_URL, account_address=address) |
47 | 74 | print("Running with extra agent address:", extra_agent_account.address) |
48 | 75 |
|
| 76 | + # Place an order with the extra agent using the same process as the original agent. |
49 | 77 | print("Placing order with original agent") |
50 | 78 | order_result = agent_exchange.order("ETH", True, 0.2, 1000, {"limit": {"tif": "Gtc"}}) |
51 | 79 | print(order_result) |
52 | 80 |
|
| 81 | + # If the extra agent's order is placed successfully, attempt to cancel it. |
53 | 82 | if order_result["status"] == "ok": |
54 | 83 | status = order_result["response"]["data"]["statuses"][0] |
55 | 84 | if "resting" in status: |
|
0 commit comments