Skip to content

Commit 5a96552

Browse files
Improve code documentation and add comments for clarity (#83)
* Improve code documentation and add comments for clarity * Update examples/basic_agent.py Co-authored-by: Ben <[email protected]> * Update basic_agent.py --------- Co-authored-by: Ben <[email protected]>
1 parent 9154651 commit 5a96552

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

examples/basic_agent.py

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,78 @@
77

88

99
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.
1022
address, info, exchange = example_utils.setup(constants.TESTNET_API_URL, skip_ws=True)
1123

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.
1226
if exchange.account_address != exchange.wallet.address:
1327
raise Exception("You should not create an agent using an agent")
1428

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+
2030
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.
2134
if approve_result["status"] != "ok":
2235
print("approving agent failed", approve_result)
2336
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.
2540
agent_account: LocalAccount = eth_account.Account.from_key(agent_key)
2641
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.
2745
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.
2950
order_result = agent_exchange.order("ETH", True, 0.2, 1000, {"limit": {"tif": "Gtc"}})
3051
print(order_result)
3152

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.
3355
if order_result["status"] == "ok":
3456
status = order_result["response"]["data"]["statuses"][0]
3557
if "resting" in status:
3658
cancel_result = agent_exchange.cancel("ETH", status["resting"]["oid"])
3759
print(cancel_result)
3860

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+
4064
approve_result, extra_agent_key = exchange.approve_agent("persist")
65+
66+
# Check if the extra agent was successfully approved.
4167
if approve_result["status"] != "ok":
4268
print("approving extra agent failed", approve_result)
4369
return
4470

71+
# Create the extra agent account using its private key and the same process as above.
4572
extra_agent_account: LocalAccount = eth_account.Account.from_key(extra_agent_key)
4673
extra_agent_exchange = Exchange(extra_agent_account, constants.TESTNET_API_URL, account_address=address)
4774
print("Running with extra agent address:", extra_agent_account.address)
4875

76+
# Place an order with the extra agent using the same process as the original agent.
4977
print("Placing order with original agent")
5078
order_result = agent_exchange.order("ETH", True, 0.2, 1000, {"limit": {"tif": "Gtc"}})
5179
print(order_result)
5280

81+
# If the extra agent's order is placed successfully, attempt to cancel it.
5382
if order_result["status"] == "ok":
5483
status = order_result["response"]["data"]["statuses"][0]
5584
if "resting" in status:

0 commit comments

Comments
 (0)