A command-line interface (CLI) chatbot powered by Coinbase's AgentKit that demonstrates how to implement custom blockchain actions. This project showcases a custom Uniswap V2 action provider for swapping ETH to USDC on the Base Sepolia testnet.
- CLI Interface: Interactive command-line interface with both chat and autonomous modes
- Custom Uniswap V2 Action: Implementation of a custom action provider for ETH to USDC swaps
- CDP Wallet Integration: Secure wallet management using Coinbase Developer Platform
- Persistent Wallet Storage: Wallet data is saved locally between sessions
- GPT-4o Integration: Leverages OpenAI's GPT-4o-mini for intelligent conversations
- Multiple Operation Modes: Choose between interactive chat or autonomous operation
- Node.js (v16 or later)
- npm or yarn package manager
- Coinbase Developer Platform (CDP) API key
- OpenAI API key
# Clone the repository
git clone https://github.com/HeimLabs/coinbase-cdp-demos
cd "01.AgentKit Demo - Custom Action(Uniswap V2)"
# Install dependencies
npm installCreate a .env file in the root directory:
# Copy the example environment file
cp .env.example .envEdit the .env file and add your API keys:
# OpenAI API Key
OPENAI_API_KEY=your_openai_api_key_here
# CDP API Key Name
CDP_API_KEY_NAME=your_cdp_api_key_name_here
# CDP API Key Private Key
CDP_API_KEY_PRIVATE_KEY=your_cdp_api_key_private_key_here
# Run in development mode (with auto-reload)
npm run dev
# Or run in production mode
npm startAfter starting the application, you'll be prompted to choose between two modes:
In chat mode, you can have an interactive conversation with the agent. The agent can:
- Provide information about your wallet
- Execute ETH to USDC swaps on Uniswap V2
- Respond to general queries about cryptocurrency and DeFi
Example prompts:
- "What's my wallet address?"
- "Swap 0.01 ETH to USDC"
- "Tell me about Uniswap V2"
In autonomous mode, the agent will perform a series of predefined actions at regular intervals. This mode is useful for demonstrating the agent's capabilities without user interaction.
The core feature of this project is the custom action provider for Uniswap V2 operations:
class UniswapV2ActionProvider extends ActionProvider<CdpWalletProvider> {
@CreateAction({
name: "swap-eth-to-usdc",
description: "Swap ETH for USDC using Uniswap V2 on base-sepolia",
schema: UniswapSwapSchema,
})
async swapEthToUsdc(
walletProvider: CdpWalletProvider,
args: z.infer<typeof UniswapSwapSchema>,
): Promise<string> {
// Implementation...
}
}The provider implements a swap-eth-to-usdc action that:
- Uses the Uniswap V2 Router at address
0x1689E7B1F10000AE47eBfE339a4f69dECd19F602 - Supports swapping ETH to USDC on Base Sepolia testnet
- Handles transaction execution and error management
The project consists of a single TypeScript file (chatbot.ts) that includes:
- Custom Action Provider: Implementation of the Uniswap V2 action provider
- Agent Initialization: Setup of the LLM, wallet provider, and agent
- CLI Interface: Interactive command-line interface with mode selection
- Wallet Management: CDP wallet integration with persistence
-
AgentKit Integration: Uses AgentKit to enable blockchain interactions through a conversational interface.
-
LangChain + GPT-4o: Leverages LangChain's ReAct agent pattern with OpenAI's GPT-4o-mini model.
-
Wallet Provider: Uses CDP's MPC wallet for secure transaction signing and execution.
-
Custom Action Provider: Implements the Uniswap V2 integration for token swaps.
chatbot.ts: Main application file containing all logic.env.example: Template for environment variableswallet_data.txt: Persisted wallet data (generated on first run)
To extend the agent with additional capabilities:
- Define a new schema using Zod for the action parameters
- Create a method in the action provider class with the
@CreateActiondecorator - Implement the action logic using the CDP wallet provider
Example:
// Define schema
const MyActionSchema = z.object({
param1: z.string().describe("Description of param1"),
param2: z.number().describe("Description of param2"),
});
// Add action to provider
@CreateAction({
name: "my-custom-action",
description: "Description of my custom action",
schema: MyActionSchema,
})
async myCustomAction(
walletProvider: CdpWalletProvider,
args: z.infer<typeof MyActionSchema>,
): Promise<string> {
// Implementation here
}- Never commit your
.envfile or expose API keys - The project uses testnet by default; be cautious if deploying to mainnet
- Review the CDP API key permissions for production deployments
Contributions are welcome! To contribute:
- Fork the repository
- Create a feature branch
- Implement your changes
- Submit a pull request
This project is licensed under the terms specified in the package.json file.
Disclaimer: This project is for demonstration purposes. Use at your own risk when deploying to production environments or mainnet networks.
Built with ❤️ using AgentKit by Coinbase.