Introduction
This tutorial guides you through creating a Node.js application using TypeScript that leverages the GOAT AI agent framework to interact with the Celo blockchain. GOAT allows you to use natural language prompts to perform on-chain actions, such as transferring tokens and checking allowances. Weâll cover setup, configuration, code explanation, and common usage scenarios.Prerequisites
Before you begin, ensure you have the following:- Node.js (v16 or higher) and npm (or yarn) installed. You can download Node.js from https://nodejs.org/.
- A Celo wallet with a private key. Youâll need a wallet with some CELO and cUSD for testing. Never commit your private key to version control.
- An RPC provider URL for the Celo network. Weâll use Forno in this example, which is a public provider. For production, consider using a dedicated RPC provider like Ankr, QuickNode, or others.
- An OpenAI API key. GOAT utilizes OpenAIâs language models. Obtain an API key from https://platform.openai.com/.
- A code editor or IDE. VS Code, Sublime Text, or any other code editor will work.
Project Setup
-
Create a new project directory:
-
Initialize a Node.js project:
-
Install dependencies:
typescript
: For using TypeScript.@ai-sdk/openai
: The OpenAI adapter for AI-SDK.@goat-sdk/adapter-vercel-ai
: GOATâs adapter for using AI-SDK.@goat-sdk/plugin-erc20
: GOAT plugin for interacting with ERC-20 tokens.@goat-sdk/wallet-viem
: GOATâs wallet integration using Viem.ai
: The core AI-SDK library.dotenv
: For loading environment variables from a.env
file.viem
: A lightweight Ethereum library for interacting with the blockchain.@types/node
: TypeScript definitions for Node.js.
-
Initialize TypeScript configuration:
-
Configure
tsconfig.json
: Opentsconfig.json
and update it with the following settings (adjusting paths as needed):outDir
: Specifies where the compiled JavaScript files will be placed.rootDir
: Specifies the root directory for your TypeScript source files.sourceMap
: Enables source map generation, helpful for debugging.resolveJsonModule
: allows importing JSON filesmoduleResolution
: Specifies how modules are resolved. ânodeâ is standard for Node.js projects.
Project Structure
Organize your project files as follows:Code Implementation
1. src/tokens.ts
:
This file defines the Celo tokens weâll be interacting with (CELO, cUSD, and USDC).
2. src/index.ts
:
This is the main application file where we set up GOAT, the wallet, and the interactive prompt.
3. .env
Create a .env
file in the root of your project and add the following, replacing the placeholders with your actual values:
.env
file or your private key to version control (e.g., Git). Add .env
to your .gitignore
file.
Running the Application
Compile the TypeScript code:Enter your prompt (or "exit" to quit):
. You can now enter natural language commands.
Example Prompts and Explanations
Here are some example prompts you can use and explanations of whatâs happening:-
âTransfer 1 cUSD to 0x13F6b54c5491cd4745fF4cFfaA9EfEe59E628657â
- GOAT uses the
get_address
tool (implicitly, you donât need to specify it). - It then uses
get_token_info_by_symbol
to find the cUSD token details. convert_to_base_unit
converts 1 cUSD to its base unit (1 * 10^18).- Finally, the
transfer
tool is called with the token address, recipient, and amount. - The transaction hash is returned.
- GOAT uses the
-
âWhat is my allowance on USDC?â
get_address
is called to get the userâs address.get_token_info_by_symbol
finds the USDC token.get_chain
is used to get the current chainget_token_allowance
is called to check the allowance of your address to spend USDC. The spender, in this case, is also your address, so you get 0. This is by design.- The allowance amount is returned.
-
âApprove 0x13F6b54c5491cd4745fF4cFfaA9EfEe59E628657 to spend 10 USDC on my behalfâ
- This prompt would use the
approve
tool from the ERC-20 plugin. Youâd see similar steps to the transfer, but instead of transferring, it would set an allowance for the specified address to spend your USDC.
- This prompt would use the
-
âWhat is my CELO balance?â
get_address
will be used to get the current user address.get_token_info_by_symbol
finds the CELO token.get_balance
is used to get the current address balance.- The balance amount is returned.
Troubleshooting
-
TypeError: Cannot read properties of undefined (reading 'call')
or similar errors: Double-check yourtsconfig.json
settings, particularlymodule
,target
, andlib
. Make sure your Node.js version is compatible. Reinstall your dependencies (rm -rf node_modules && npm install
). -
Error: Invalid ABI
: Ensure your contract addresses intokens.ts
are correct for the Celo network (chain ID 42220). -
OpenAI API errors (e.g., 401 Unauthorized): Verify your
OPENAI_API_KEY
is correct and that your OpenAI account has sufficient credits. -
Transaction failures:
- Check that your wallet has enough CELO to pay for gas.
- Verify your
RPC_PROVIDER_URL
is correct and functioning. - If youâre using a custom RPC provider, ensure it supports the necessary methods.
-
Type errors after installation: If you encounter persistent type errors, you can try adding
// @ts-ignore
comments above the lines causing issues as a temporary workaround. However, itâs best to resolve the underlying type issues if possible. The provided code avoids this, but itâs a useful debugging technique.
Conclusion
This tutorial demonstrated how to build a Celo blockchain AI agent with GOAT, Node.js, and TypeScript. Youâve learned to set up a project, integrate a wallet, define tokens, and use natural language to execute on-chain actions. This provides a foundation for creating powerful, user-friendly Web3 applications that simplify complex blockchain interactions. Remember to prioritize security and continue exploring the possibilities!Next Steps
- Explore more examples and documentation for GOAT and the ERC-20 plugin and ERC-721 plugin.
- Build a more complex application using GOAT and other Celo tools.
- Contribute to the GOAT project and provide feedback.