kikocis/Based-Agent public
Changesets · Branches · Tags · Compare
| Changeset | af54ff08153045e937352d62a9c83ebe8f3202e04cf1838f2e610c84a904f12d |
|---|---|
| Author | Lincoln Murr <lincoln.murr@coinbase.com> · 2024-10-26 10:13 -04:00 |
| Committer | Lincoln Murr <lincoln.murr@coinbase.com> · 2024-10-26 10:13 -04:00 |
| Parents | 653c48ff15c3 |
4 files changed
modified Based-Agent/agents.py +152 −2
| @@ -6,7 +6,8 @@ | ||
| 6 | 6 | from openai import OpenAI |
| 7 | 7 | from decimal import Decimal |
| 8 | 8 | from typing import Union |
| 9 | - | |
| 9 | +from web3 import Web3 | |
| 10 | +from web3.exceptions import ContractLogicError | |
| 10 | 11 | # Configure the CDP SDK |
| 11 | 12 | Cdp.configure_from_json("./examples/based_agent/cdp_api_key.json") |
| 12 | 13 | |
| @@ -55,6 +56,10 @@ | ||
| 55 | 56 | Returns: |
| 56 | 57 | Status message about the faucet request |
| 57 | 58 | """ |
| 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 | + | |
| 58 | 63 | faucet_tx = agent_wallet.faucet() |
| 59 | 64 | return f"Requested ETH from faucet. Transaction: {faucet_tx}" |
| 60 | 65 | |
| @@ -161,10 +166,107 @@ | ||
| 161 | 166 | except Exception as e: |
| 162 | 167 | return f"Error swapping assets: {str(e)}" |
| 163 | 168 | |
| 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 | + | |
| 164 | 266 | # Add the new functions to the based_agent |
| 165 | 267 | based_agent = Agent( |
| 166 | 268 | 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.", | |
| 168 | 270 | functions=[ |
| 169 | 271 | create_token, |
| 170 | 272 | transfer_asset, |
| @@ -174,5 +276,53 @@ | ||
| 174 | 276 | deploy_nft, |
| 175 | 277 | mint_nft, |
| 176 | 278 | swap_assets, # Add the new function here |
| 279 | + register_basename | |
| 177 | 280 | ], |
| 178 | 281 | ) |
| 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 @@ | ||
| 69 | 69 | # Generate a thought |
| 70 | 70 | thought = ( |
| 71 | 71 | "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." | |
| 73 | 73 | ) |
| 74 | 74 | messages.append({"role": "user", "content": thought}) |
| 75 | 75 |
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 |