Tutorial: Build an AI Agent to Send Tokens on Celo
This tutorial will guide you through creating an AI-powered agent capable of sending tokens on the Celo blockchain. We will adapt the provided code, which demonstrates sending tokens on the Sepolia test network, to function seamlessly on Celo. This will allow you to build an interactive agent that can send both native CELO and ERC20 tokens based on natural language prompts. We will utilize@ai-sdk/openai for AI capabilities, viem for blockchain interaction, and @goat-sdk to simplify the development of our on-chain agent. We will be using @goat-sdk/wallet-evm for sending native tokens and @goat-sdk/plugin-erc20 for handling ERC20 tokens.
Understanding the Code: AI Agent for Sending Tokens
Let’s dissect the provided code, which forms the foundation for our token sending agent. 1. Importing Libraries:- Libraries: The imports are similar to previous examples, including
readline,@ai-sdk/openai,ai,viem, and@goat-sdk. - Chain Import:
baseSepoliais imported fromviem/chains, which we will replace withceloSepoliato target the Celo network. - Token Imports:
PEPEandUSDCare imported from@goat-sdk/plugin-erc20. We will need to ensure these or similar tokens are relevant and available on Celo, or configure for other ERC20 tokens on Celo. sendETHImport:sendETHfrom@goat-sdk/wallet-evmis imported to enable sending native tokens. We will adapt this to send CELO.
- Wallet Client Setup: This section sets up a
viemwallet client, retrieving the private key from theWALLET_PRIVATE_KEYenvironment variable and connecting to the blockchain using theRPC_PROVIDER_URL. We will modifychaintoceloSepoliafor Celo.
- On-Chain Tools for Token Sending: This section configures the tools for sending tokens:
sendETH(): Enables sending native ETH on Base Sepolia. We will adapt this to send native CELO on Celo Sepolia.erc20({ tokens: [USDC, PEPE] }): Enables ERC20 token operations for USDC and PEPE. We will need to review and potentially replace these tokens with relevant ERC20 tokens on Celo Sepolia.
- Interactive Loop: This section sets up the command-line interface and the AI interaction loop, identical to previous examples, using
generateTextto process prompts and interact with the configured tools.
Setup Guide for Celo Sepolia Token Sending Agent
Follow these steps to set up the AI-powered token sending agent on Celo Sepolia: 1. Clone Repository and Navigate to Example Directory: Follow steps 1-5 from the NFT minting tutorial (or previous tutorials) to clone the GOAT repository, navigate to thetypescript directory, install dependencies, build the project, and go to the example directory: examples/by-use-case/evm-mint-nft. You can reuse this directory or create a new one if you prefer.
2. Configure Environment Variables:
.env.template to .env and populate the following environment variables in the .env file:
OPENAI_API_KEY: Your OpenAI API key from OpenAI.WALLET_PRIVATE_KEY: Your private key for the wallet that will send tokens on Celo. Security Best Practices: Use a test wallet and handle private keys with extreme caution.RPC_PROVIDER_URL: The RPC URL for the Celo Sepolia network. Use a Celo Sepolia RPC URL for testing (e.g.,https://forno.celo-sepolia.celo-testnet.org/). Refer to previous articles for more Celo Sepolia RPC options.
.env file (for Celo Sepolia Testnet):
OPENAI_API_KEY, WALLET_PRIVATE_KEY, and RPC_PROVIDER_URL for this token sending agent.
3. Adapt Code for Celo Token Sending:
-
Chain Configuration: In
index.ts, replacebaseSepoliawithcelofromviem/chains: -
Adapt
sendETH()tosendCELO(): The@goat-sdk/wallet-evmmight have a function specifically for sending CELO. Check the@goat-sdkdocumentation forsendCELO(). If it exists, replacesendETH()withsendCELO()in thepluginsarray:IfsendCELO()is not directly available in@goat-sdk/wallet-evm, it’s likely thatsendETH()is designed to be chain-aware and will send the native token of the configured chain (celoin this case). In that case, you might not need to changesendETH(). Test sending CELO after setup to confirm ifsendETH()works for CELO or if you need to find an alternative. IfsendETH()does not work for CELO, you might need to create a custom tool usingviem’ssendTransactionfunction to send CELO directly. -
Review ERC20 Tokens for Celo:
USDCandPEPEmight not be ideal for Celo. Research popular ERC20 tokens on Celo Sepolia Testnet or Celo Mainnet (e.g.,cUSD,cEUR,USDT,DAIon Celo). Update theerc20plugin configuration with relevant tokens:You might need to define or import configurations forcUSD,cEUR,USDTsimilar toUSDCandPEPE, potentially using their token contract addresses on Celo. For a more generic approach, you can remove thetokensarray to allow the agent to handle any ERC20 token specified by symbol or address in the prompt.
-
Run the Interactive CLI:
From the
examples/by-use-case/evm-mint-nftdirectory (or your chosen directory), run: -
Chat with the Agent for Token Sending:
Interact with the agent using natural language prompts to send tokens. Here are example prompts:
- Send CELO:
"Send 1 CELO to 0xRecipientAddressHere"(Replace0xRecipientAddressHerewith a Celo address)"Transfer 0.5 CELO to my friend at 0xFriendAddress"
- Send ERC20 Tokens (e.g., cUSD, cEUR, USDT - adjust based on your configuration):
"Send 10 cUSD to 0xRecipientAddress"(AssumingcUSDis configured)"Transfer 5 EURC to address 0xAnotherAddress"(AssumingcEURis configured)"Send 2 USDT to 0xYetAnotherAddress"(AssumingUSDTis configured)
- Send CELO:
Conclusion
This tutorial has guided you through building an AI-powered agent capable of sending tokens on the Celo Sepolia blockchain. By adapting the provided code, configuring for Celo Sepolia, and utilizing the@goat-sdk/wallet-evm and @goat-sdk/plugin-erc20 tools, you can create an interactive agent that can understand natural language prompts to send both native CELO and ERC20 tokens. Remember to test thoroughly on the Celo Sepolia Testnet before using on Mainnet and always handle private keys securely. Explore the @goat-sdk documentation to understand more advanced configurations and error handling for your token sending agent on Celo!
