Elffuss BraidHub

kikocis/Based-Agent public

Transfer error handling
Changeset8a8f406d160acbd021e9c06b150b1c37f1a352f42a966f8a9fb2d2c88498e4fb
AuthorLincoln Murr <lincoln.murr@coinbase.com> · 2024-10-26 11:07 -04:00
CommitterLincoln Murr <lincoln.murr@coinbase.com> · 2024-10-26 11:07 -04:00
Parentsaf54ff081530

Browse the files at this changeset

2 files changed

modified Based-Agent/agents.py +22 −4
@@ -8,8 +8,11 @@
88 from typing import Union
99 from web3 import Web3
1010 from web3.exceptions import ContractLogicError
11+from cdp.errors import ApiError, UnsupportedAssetError
12+
13+
1114 # Configure the CDP SDK
12-Cdp.configure_from_json("./examples/based_agent/cdp_api_key.json")
15+Cdp.configure_from_json("./Based-Agent/cdp_api_key.json")
1316
1417 # If you want to create a wallet on Base Mainnet, use Wallet.create(network_id="base-mainnet") and comment out the faucet line
1518 agent_wallet = Wallet.create()
@@ -33,9 +36,23 @@
3336 asset_id: Asset identifier ("eth", "usdc" are the only supported values) or contract address of another ERC-20 token. Unless the asset is ETH or USDC, you must use the contract address of the token.
3437 destination_address: Recipient's address
3538 """
36- transfer = agent_wallet.transfer(amount, asset_id, destination_address)
37- transfer.wait()
38- return f"Transferred {amount} {asset_id} to {destination_address}"
39+ try:
40+ # Check if the asset exists and is ready for transfer
41+ try:
42+ Asset.fetch(asset_id)
43+ except ApiError as e:
44+ if e.api_code == "400":
45+ return f"The asset {asset_id} is not yet available for transfer. It may have been recently deployed. Please try again in about 30 minutes."
46+ else:
47+ raise e
48+
49+ transfer = agent_wallet.transfer(amount, asset_id, destination_address)
50+ transfer.wait()
51+ return f"Transferred {amount} {asset_id} to {destination_address}"
52+ except UnsupportedAssetError as e:
53+ return f"Error: The asset {asset_id} is not supported on this network. It may have been recently deployed. Please try again in about 30 minutes."
54+ except Exception as e:
55+ return f"Error transferring asset: {str(e)}. If this is a custom token, it may have been recently deployed. Please try again in about 30 minutes, as it needs to be indexed by CDP first."
3956
4057 def get_balance(asset_id):
4158 """Get the balance of a specific asset in the agent's wallet.
@@ -174,6 +191,7 @@
174191 L2_RESOLVER_ADDRESS_MAINNET = "0xC6d566A56A1aFf6508b41f6c90ff131615583BCD"
175192 L2_RESOLVER_ADDRESS_TESTNET = "0x6533C94869D28fAA8dF77cc63f9e2b2D6Cf77eBA"
176193
194+# Create registration arguments for Basenames
177195 def create_register_contract_method_args(base_name: str, address_id: str, is_mainnet: bool) -> dict:
178196 """
179197 Create registration arguments for Basenames.
modified Based-Agent/run.py +5 −5
@@ -137,17 +137,17 @@
137137 print("\nAvailable modes:")
138138 print("1. chat - Interactive chat mode")
139139 print("2. auto - Autonomous action mode")
140- print("3. openai - AI-to-agent conversation mode")
140+ print("3. two-agent - AI-to-agent conversation mode")
141141
142142 choice = input("\nChoose a mode (enter number or name): ").lower().strip()
143143
144144 mode_map = {
145145 '1': 'chat',
146146 '2': 'auto',
147- '3': 'openai',
147+ '3': 'two-agent',
148148 'chat': 'chat',
149149 'auto': 'auto',
150- 'openai': 'openai'
150+ 'two-agent': 'two-agent'
151151 }
152152
153153 if choice in mode_map:
@@ -160,7 +160,7 @@
160160 mode_functions = {
161161 'chat': lambda: run_demo_loop(based_agent),
162162 'auto': lambda: run_autonomous_loop(based_agent),
163- 'openai': lambda: run_openai_conversation_loop(based_agent)
163+ 'two-agent': lambda: run_openai_conversation_loop(based_agent)
164164 }
165165
166166 print(f"\nStarting {mode} mode...")
@@ -168,4 +168,4 @@
168168
169169 if __name__ == "__main__":
170170 print("Starting Based Agent...")
171- main()
No newline at the end of the file
171+ main()