Elffuss BraidHub

kikocis/Based-Agent public

Cleaning repo
Changesetaf54ff08153045e937352d62a9c83ebe8f3202e04cf1838f2e610c84a904f12d
AuthorLincoln Murr <lincoln.murr@coinbase.com> · 2024-10-26 10:13 -04:00
CommitterLincoln Murr <lincoln.murr@coinbase.com> · 2024-10-26 10:13 -04:00
Parents653c48ff15c3

Browse the files at this changeset

4 files changed

modified Based-Agent/agents.py +152 −2
@@ -6,7 +6,8 @@
66 from openai import OpenAI
77 from decimal import Decimal
88 from typing import Union
9-
9+from web3 import Web3
10+from web3.exceptions import ContractLogicError
1011 # Configure the CDP SDK
1112 Cdp.configure_from_json("./examples/based_agent/cdp_api_key.json")
1213
@@ -55,6 +56,10 @@
5556 Returns:
5657 Status message about the faucet request
5758 """
59+ # if the agent is on mainnet, this will not work
60+ if agent_wallet.network_id == "base-mainnet":
61+ return "Error: The faucet is only available on Base Sepolia testnet."
62+
5863 faucet_tx = agent_wallet.faucet()
5964 return f"Requested ETH from faucet. Transaction: {faucet_tx}"
6065
@@ -161,10 +166,107 @@
161166 except Exception as e:
162167 return f"Error swapping assets: {str(e)}"
163168
169+
170+
171+# Contract addresses
172+BASENAMES_REGISTRAR_CONTROLLER_ADDRESS_MAINNET = "0x4cCb0BB02FCABA27e82a56646E81d8c5bC4119a5"
173+BASENAMES_REGISTRAR_CONTROLLER_ADDRESS_TESTNET = "0x49aE3cC2e3AA768B1e5654f5D3C6002144A59581"
174+L2_RESOLVER_ADDRESS_MAINNET = "0xC6d566A56A1aFf6508b41f6c90ff131615583BCD"
175+L2_RESOLVER_ADDRESS_TESTNET = "0x6533C94869D28fAA8dF77cc63f9e2b2D6Cf77eBA"
176+
177+def create_register_contract_method_args(base_name: str, address_id: str, is_mainnet: bool) -> dict:
178+ """
179+ Create registration arguments for Basenames.
180+
181+ Args:
182+ base_name (str): The Basename (e.g., "example.base.eth" or "example.basetest.eth")
183+ address_id (str): The Ethereum address
184+ is_mainnet (bool): True if on mainnet, False if on testnet
185+
186+ Returns:
187+ dict: Formatted arguments for the register contract method
188+ """
189+ w3 = Web3()
190+
191+ # Create contract instances
192+ resolver_contract = w3.eth.contract(abi=l2_resolver_abi)
193+
194+ # Get namehash of the domain
195+ name_hash = w3.ens.namehash(base_name)
196+
197+ # Encode function data for setAddr and setName
198+ address_data = resolver_contract.encode_abi(
199+ "setAddr",
200+ args=[name_hash, address_id]
201+ )
202+
203+ name_data = resolver_contract.encode_abi(
204+ "setName",
205+ args=[name_hash, base_name]
206+ )
207+
208+ # Create register arguments
209+ register_args = {
210+ "request": [
211+ base_name.replace(".base.eth" if is_mainnet else ".basetest.eth", ""),
212+ address_id,
213+ "31557600", # 1 year in seconds
214+ L2_RESOLVER_ADDRESS_MAINNET if is_mainnet else L2_RESOLVER_ADDRESS_TESTNET,
215+ [address_data, name_data],
216+ True
217+ ]
218+ }
219+
220+ return register_args
221+
222+def register_basename(basename: str, amount: float = 0.002):
223+ """Register a basename for the agent's wallet.
224+
225+ Args:
226+ basename: The basename to register (e.g. "myname.base.eth" or "myname.basetest.eth")
227+ amount: Amount of ETH to pay for registration (default 0.002)
228+
229+ Returns:
230+ Status message about the basename registration
231+ """
232+ address_id = agent_wallet.default_address.address_id
233+ is_mainnet = agent_wallet.network_id == "base-mainnet"
234+
235+ # Ensure the basename ends with the correct suffix
236+ suffix = ".base.eth" if is_mainnet else ".basetest.eth"
237+ if not basename.endswith(suffix):
238+ basename += suffix
239+
240+ register_args = create_register_contract_method_args(basename, address_id, is_mainnet)
241+
242+ try:
243+ # Determine the correct contract address based on the network
244+ contract_address = (
245+ BASENAMES_REGISTRAR_CONTROLLER_ADDRESS_MAINNET if is_mainnet
246+ else BASENAMES_REGISTRAR_CONTROLLER_ADDRESS_TESTNET
247+ )
248+
249+ # Invoke register function on registrar contract
250+ invocation = agent_wallet.invoke_contract(
251+ contract_address=contract_address,
252+ method="register",
253+ args=register_args,
254+ abi=registrar_abi,
255+ amount=amount,
256+ asset_id="eth",
257+ )
258+ invocation.wait()
259+ return f"Successfully registered basename {basename} for address {address_id}"
260+ except ContractLogicError as e:
261+ return f"Error registering basename: {str(e)}"
262+ except Exception as e:
263+ return f"Unexpected error registering basename: {str(e)}"
264+
265+
164266 # Add the new functions to the based_agent
165267 based_agent = Agent(
166268 name="Based Agent",
167- instructions="You are a helpful agent that can interact onchain on the Base Layer 2 using the Coinbase Developer Platform SDK. You can create tokens, transfer assets, generate art, deploy NFTs, mint NFTs, and swap assets (on mainnet only). If you ever need to know your address, it is {agent_wallet.default_address.address_id}. If you ever need funds, you can request them from the faucet. You can also deploy your own ERC-20 tokens, NFTs, and interact with them. If someone asks you to do something you can't do, you can say so, and encourage them to implement it themselves using the CDP SDK, recommend they go to docs.cdp.coinbase.com for more informaton. You can also offer to help them implement it by writing the function and telling them to add it to the agents.py file and within your list of callable functions.",
269+ instructions="You are a helpful agent that can interact onchain on the Base Layer 2 using the Coinbase Developer Platform SDK. You can create tokens, transfer assets, generate art, deploy NFTs, mint NFTs, register basenames, and swap assets (on mainnet only). If you ever need to know your address, it is {agent_wallet.default_address.address_id}. If you ever need funds, you can request them from the faucet. You can also deploy your own ERC-20 tokens, NFTs, and interact with them. If someone asks you to do something you can't do, you can say so, and encourage them to implement it themselves using the CDP SDK, recommend they go to docs.cdp.coinbase.com for more informaton. You can also offer to help them implement it by writing the function and telling them to add it to the agents.py file and within your list of callable functions.",
168270 functions=[
169271 create_token,
170272 transfer_asset,
@@ -174,5 +276,53 @@
174276 deploy_nft,
175277 mint_nft,
176278 swap_assets, # Add the new function here
279+ register_basename
177280 ],
178281 )
282+
283+l2_resolver_abi = [
284+ {
285+ "inputs": [
286+ {"internalType": "bytes32", "name": "node", "type": "bytes32"},
287+ {"internalType": "address", "name": "a", "type": "address"}
288+ ],
289+ "name": "setAddr",
290+ "outputs": [],
291+ "stateMutability": "nonpayable",
292+ "type": "function"
293+ },
294+ {
295+ "inputs": [
296+ {"internalType": "bytes32", "name": "node", "type": "bytes32"},
297+ {"internalType": "string", "name": "newName", "type": "string"}
298+ ],
299+ "name": "setName",
300+ "outputs": [],
301+ "stateMutability": "nonpayable",
302+ "type": "function"
303+ }
304+]
305+
306+registrar_abi = [
307+ {
308+ "inputs": [
309+ {
310+ "components": [
311+ {"internalType": "string", "name": "name", "type": "string"},
312+ {"internalType": "address", "name": "owner", "type": "address"},
313+ {"internalType": "uint256", "name": "duration", "type": "uint256"},
314+ {"internalType": "address", "name": "resolver", "type": "address"},
315+ {"internalType": "bytes[]", "name": "data", "type": "bytes[]"},
316+ {"internalType": "bool", "name": "reverseRecord", "type": "bool"}
317+ ],
318+ "internalType": "struct RegistrarController.RegisterRequest",
319+ "name": "request",
320+ "type": "tuple"
321+ }
322+ ],
323+ "name": "register",
324+ "outputs": [],
325+ "stateMutability": "payable",
326+ "type": "function"
327+ }
328+]
modified Based-Agent/run.py +1 −1
@@ -69,7 +69,7 @@
6969 # Generate a thought
7070 thought = (
7171 "Be creative and do something interesting on the Base blockchain. "
72- "Don't take any more input from me. Choose an action and execute it now."
72+ "Don't take any more input from me. Choose an action and execute it now. Choose those that highlight your identity and abilities best."
7373 )
7474 messages.append({"role": "user", "content": thought})
7575
deleted examples/based_agent/__pycache__/agents.cpython-313.pyc +0 −0

Binary content is not shown. View the file.

deleted examples/based_agent/cdp_api_key.json +0 −4
@@ -1,4 +0,0 @@
1-{
2- "name": "organizations/6a38dfc6-b491-4421-a500-450eb61d8962/apiKeys/8fc379bc-ec49-4176-8adc-3b1581cae8fe",
3- "privateKey": "-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIDqoQNtKqfWUDjcsMoMNx3mOq5SA1Af7G+qZ7mhGzc5XoAoGCCqGSM49\nAwEHoUQDQgAEsctzgrmFUiBcu5N53GtN1KwwGVs6tNz0ahk65rjJGEPv1I2Z//Nn\ntAe7NohyrUuSc7neLL/yRuytqw+2fZG1MQ==\n-----END EC PRIVATE KEY-----"
4-}
No newline at the end of the file