Skip to content

Commit 2e979c3

Browse files
committed
coinbase-langchain agent and cointoss agent working
1 parent 68bc469 commit 2e979c3

22 files changed

+6950
-0
lines changed
+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# Payment Agent
2+
3+
A sophisticated DeFi payment agent built using Coinbase's AgentKit and LangChain, operating over the XMTP messaging protocol.
4+
5+
## Features
6+
7+
- Process blockchain payments using natural language commands
8+
- Advanced language processing using LangChain and OpenAI
9+
- User-specific wallet management with flexible storage options (Redis or local file)
10+
- XMTP messaging integration for secure, decentralized chat interactions
11+
- Powered by CDP SDK for reliable blockchain operations and Langchain for AI Agent
12+
13+
## Prerequisites
14+
15+
- Node.js (v20+)
16+
- [OpenAI](https://platform.openai.com/) API key
17+
- [Coinbase Developer Platform](https://portal.cdp.coinbase.com) (CDP) API credentials
18+
- Yarn package manager
19+
20+
## Quick Start Guide
21+
22+
Follow these steps to get your payment agent up and running:
23+
24+
1. **Clone the repository**:
25+
26+
```bash
27+
git clone https://github.com/ephemeraHQ/xmtp-agent-examples.git
28+
cd integrations/paymentagent
29+
```
30+
31+
2. **Install dependencies**:
32+
33+
```bash
34+
yarn install
35+
```
36+
37+
3. **Generate XMTP keys**:
38+
39+
```bash
40+
yarn gen:keys
41+
```
42+
43+
This will generate random wallet and encryption keys for your agent and output them to the console. Copy these values to your `.env` file.
44+
45+
4. **Set up your environment variables**:
46+
Create a `.env` file with the following variables:
47+
48+
```
49+
# Required: OpenAI API Key
50+
OPENAI_API_KEY=your_openai_api_key_here
51+
52+
# Required: Coinbase Developer Platform credentials
53+
CDP_API_KEY_NAME=your_cdp_api_key_name_here
54+
CDP_API_KEY_PRIVATE_KEY=your_cdp_api_key_private_key_here
55+
56+
# Required: XMTP wallet and encryption keys (from step 3)
57+
WALLET_KEY=your_wallet_private_key_here
58+
ENCRYPTION_KEY=your_encryption_key_here
59+
XMTP_ENV="local" # or "dev" or "production"
60+
61+
# Optional: Network ID (defaults to base-sepolia if not specified)
62+
NETWORK_ID=base-sepolia # or base-mainnet
63+
64+
# Optional: Redis for persistent storage (if not provided, local file storage will be used)
65+
REDIS_URL=your_redis_url_here
66+
```
67+
68+
5. **Start the agent**:
69+
70+
```bash
71+
yarn dev
72+
```
73+
74+
6. **Interact with your agent**:
75+
Once running, you'll see a URL in the console like:
76+
```
77+
Send a message on http://xmtp.chat/dm/YOUR_AGENT_ADDRESS?env=dev
78+
```
79+
Open this URL in your browser to start chatting with your payment agent!
80+
81+
## Usage Examples
82+
83+
Once the agent is running, you can interact with it using natural language commands:
84+
85+
### Basic prompts
86+
87+
- "Send 0.01 ETH to 0x1234..."
88+
- "Check my wallet balance"
89+
- "Transfer 10 USDC to vitalik.eth"
90+
91+
## How It Works
92+
93+
This payment agent combines several technologies:
94+
95+
1. **XMTP Protocol**: For decentralized messaging and chat interface
96+
2. **Langchain**: AI agent framework
97+
3. **CDP SDK**: Blockchain Operations
98+
4. **Storage Options**: Redis or local file storage for wallet data
99+
5. **LLM Integration**: For natural language processing
100+
101+
The payment agent integrates several key technologies:
102+
103+
1. **XMTP Protocol**: Provides decentralized messaging infrastructure
104+
2. **LangChain**: Powers natural language processing and conversation flow
105+
3. **Coinbase AgentKit**: Handles blockchain interactions and wallet management
106+
4. **Storage Layer**: Flexible storage with Redis or local file system
107+
5. **OpenAI Integration**: Advanced language understanding and response generation
108+
109+
### Key Components
110+
111+
- `src/index.ts`: Main application entry point
112+
- `src/cdp.ts`: Coinbase Developer Platform integration
113+
- `src/xmtp.ts`: XMTP messaging protocol implementation
114+
- `src/langchain.ts`: LangChain agent and processing logic
115+
- `src/storage.ts`: Data persistence layer
116+
117+
## Troubleshooting
118+
119+
Common issues and solutions:
120+
121+
### Connection Issues
122+
123+
- Verify XMTP environment settings match your needs (`local`, `dev`, or `production`)
124+
- Check Redis connection string if using Redis storage
125+
- Ensure CDP API credentials are correctly formatted
126+
- Verify OpenAI API key has sufficient quota
127+
128+
### Transaction Issues
129+
130+
- Confirm wallet has sufficient balance for transactions
131+
- Check network settings match your intended network
132+
- Verify gas price settings for your chosen network
133+
- For testnets, ensure you've obtained test tokens from a faucet
134+
135+
### Storage Issues
136+
137+
- If Redis connection fails, the system will automatically fall back to local storage
138+
- Check file permissions if using local storage
139+
- Verify Redis URL format if using Redis
140+
141+
### Transaction Failures
142+
143+
- Check that you're on the correct network (default is base-sepolia)
144+
- Ensure the wallet has sufficient funds for the transaction
145+
- For testnet operations, request funds from a faucet
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "paymentagent",
3+
"version": "1.0.0",
4+
"description": "A DeFi payment agent built using CDP AgentKit that operates over the XMTP messaging protocol",
5+
"keywords": [
6+
"xmtp",
7+
"agent",
8+
"blockchain",
9+
"payment"
10+
],
11+
"license": "ISC",
12+
"author": "",
13+
"type": "module",
14+
"main": "dist/index.js",
15+
"scripts": {
16+
"build": "tsc",
17+
"dev": "node --loader ts-node/esm src/index.ts",
18+
"gen:keys": "node --loader ts-node/esm scripts/generate-keys.ts",
19+
"lint": "eslint . --ext .ts",
20+
"start": "node dist/index.js",
21+
"test": "echo \"Error: no test specified\" && exit 1"
22+
},
23+
"dependencies": {
24+
"@coinbase/agentkit": "^0.2.0",
25+
"@coinbase/agentkit-langchain": "^0.2.0",
26+
"@langchain/core": "^0.3.20",
27+
"@langchain/langgraph": "^0.2.24",
28+
"@langchain/openai": "^0.3.14",
29+
"@redis/client": "^1.6.0",
30+
"@xmtp/node-sdk": "^0.0.47",
31+
"dotenv": "^16.4.7",
32+
"redis": "^4.7.0",
33+
"uint8arrays": "^5.1.0",
34+
"viem": "^2.7.15"
35+
},
36+
"devDependencies": {
37+
"@types/node": "^20.11.0",
38+
"eslint": "^8.56.0",
39+
"ts-node": "^10.9.2",
40+
"typescript": "^5.8.2"
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import * as fs from "fs";
2+
import * as path from "path";
3+
import { fileURLToPath } from "url";
4+
import * as crypto from "crypto";
5+
6+
// Get the directory name in ESM
7+
const __filename = fileURLToPath(import.meta.url);
8+
const __dirname = path.dirname(__filename);
9+
10+
/**
11+
* Generate random keys for the agent
12+
* @returns Object containing random wallet and encryption keys
13+
*/
14+
function generateRandomKeys() {
15+
// This is used for development/testing only
16+
const randomBytes = crypto.randomBytes(32);
17+
const walletKey = `0x${randomBytes.toString('hex')}`;
18+
19+
const randomBytesForEncryption = crypto.randomBytes(32);
20+
const encryptionKey = `0x${randomBytesForEncryption.toString('hex')}`;
21+
22+
return { walletKey, encryptionKey };
23+
}
24+
25+
function main() {
26+
try {
27+
console.log("🔑 Generating random XMTP keys for your payment agent...");
28+
29+
// Generate random keys
30+
const { walletKey, encryptionKey } = generateRandomKeys();
31+
32+
// Create .env if it doesn't exist
33+
const envPath = path.resolve(__dirname, "../.env");
34+
let envContent = "";
35+
let envExists = false;
36+
37+
if (fs.existsSync(envPath)) {
38+
envContent = fs.readFileSync(envPath, "utf8");
39+
envExists = true;
40+
console.log("📝 Found existing .env file, updating with new keys...");
41+
} else {
42+
console.log("📝 Creating new .env file with generated keys...");
43+
}
44+
45+
// Add or update keys in .env
46+
const envLines = envContent.split("\n");
47+
const updatedEnvLines = [];
48+
49+
let walletKeySet = false;
50+
let encryptionKeySet = false;
51+
52+
for (const line of envLines) {
53+
if (line.startsWith("WALLET_KEY=")) {
54+
updatedEnvLines.push(`WALLET_KEY=${walletKey}`);
55+
walletKeySet = true;
56+
} else if (line.startsWith("ENCRYPTION_KEY=")) {
57+
updatedEnvLines.push(`ENCRYPTION_KEY=${encryptionKey}`);
58+
encryptionKeySet = true;
59+
} else if (line.trim()) {
60+
updatedEnvLines.push(line);
61+
}
62+
}
63+
64+
if (!walletKeySet) {
65+
updatedEnvLines.push(`WALLET_KEY=${walletKey}`);
66+
}
67+
68+
if (!encryptionKeySet) {
69+
updatedEnvLines.push(`ENCRYPTION_KEY=${encryptionKey}`);
70+
}
71+
72+
// Write updated .env file
73+
fs.writeFileSync(envPath, updatedEnvLines.join("\n") + "\n");
74+
75+
console.log("\n✅ Successfully generated and saved new keys to .env!");
76+
console.log("\n📋 Your XMTP keys:");
77+
console.log(`WALLET_KEY=${walletKey}`);
78+
console.log(`ENCRYPTION_KEY=${encryptionKey}`);
79+
80+
console.log("\n🚀 Next steps:");
81+
if (!envExists) {
82+
console.log("1. Add your OpenAI API key to the .env file:");
83+
console.log(" OPENAI_API_KEY=your_openai_api_key_here");
84+
console.log("2. Add your Coinbase Developer Platform credentials:");
85+
console.log(" CDP_API_KEY_NAME=your_cdp_api_key_name_here");
86+
console.log(
87+
" CDP_API_KEY_PRIVATE_KEY=your_cdp_api_key_private_key_here",
88+
);
89+
console.log(
90+
"3. (Optional) Add your Redis URL if you want to use Redis for storage:",
91+
);
92+
console.log(" REDIS_URL=your_redis_url_here");
93+
}
94+
console.log(`4. Start your payment agent with: yarn dev`);
95+
console.log("\n📚 For more information, check the README.md file.");
96+
} catch (error) {
97+
console.error("❌ Error generating keys:", error);
98+
process.exit(1);
99+
}
100+
}
101+
102+
// Call main directly since this is the entry point
103+
main();

0 commit comments

Comments
 (0)