Skip to content

Commit dba8c79

Browse files
authored
Refactored examples (#48)
* push * update
1 parent 3ca3e04 commit dba8c79

36 files changed

+436
-777
lines changed

README.md

+6-12
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ To run your XMTP agent, you must create a `.env` file with the following variabl
2121
```tsx
2222
WALLET_KEY= # the private key of the wallet
2323
ENCRYPTION_KEY= # encryption key for the local database
24+
XMTP_ENV= # local, dev, production
2425
```
2526

26-
You can generate random keys with the following command:
27+
You can generate random xmtp keys with the following command:
2728

2829
```tsx
2930
yarn gen:keys <name>
@@ -125,18 +126,11 @@ Interact with the XMTP network using [xmtp.chat](https://xmtp.chat), the officia
125126

126127
## Examples
127128

128-
- [gm](/examples/gm/): A simple agent that replies to all text messages with "gm".
129+
- [gm](/gm/): A simple agent that replies to all text messages with "gm".
129130
- [gpt](/examples/gpt/): An example using GPT API's to answer messages.
130131
- [gated-group](/examples/gated-group/): Add members to a group that hold a certain NFT.
132+
- [grok](/examples/grok/): Integrate your agent with the Grok API
133+
- [gaia](/examples/gaia/): Integrate with the Gaia API
134+
- [coinbase-langchain](/examples/coinbase-langchain/): Agent that uses a CDP for gassless USDC on base
131135

132136
> See all the available [examples](/examples/).
133-
134-
### Integrations
135-
136-
Examples integrating XMTP with external libraries from the ecosystem
137-
138-
- [grok](/integrations/grok/): Integrate your agent with the Grok API
139-
- [gaia](/integrations/gaia/): Integrate with the Gaia API
140-
- [coinbase-langchain](/integrations/coinbase-langchain/): Agent that uses a CDP for gassless USDC on base
141-
142-
> See all the available [integrations](/integrations/).

integrations/README.md examples/README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# How to contribute an XMTP integration
1+
# How to contribute an XMTP example
22

3-
This guide explains how to help develop or improve an integration using the XMTP protocol.
3+
This guide explains how to help develop or improve an example using the XMTP protocol.
44

55
## Prerequisites
66

@@ -28,15 +28,15 @@ This guide explains how to help develop or improve an integration using the XMTP
2828
Make a new branch for your changes.
2929

3030
```bash
31-
git checkout -b integration/your-integration-name
31+
git checkout -b example/your-example-name
3232
```
3333

3434
4. **Install dependencies**
3535

36-
Go to the integration directory and install packages.
36+
Go to the example directory and install packages.
3737

3838
```bash
39-
cd integrations/your-integration
39+
cd examples/your-example
4040
yarn
4141
```
4242

@@ -46,7 +46,7 @@ This guide explains how to help develop or improve an integration using the XMTP
4646

4747
6. **Test changes**
4848

49-
Run the integration to check your changes.
49+
Run the example to check your changes.
5050

5151
```bash
5252
yarn dev
@@ -58,15 +58,15 @@ This guide explains how to help develop or improve an integration using the XMTP
5858

5959
```bash
6060
git add .
61-
git commit -m "Add integration: description of your integration"
61+
git commit -m "Add example: description of your example"
6262
```
6363

6464
8. **Push changes**
6565

6666
Send your changes to your forked repository.
6767

6868
```bash
69-
git push origin integration/your-integration-name
69+
git push origin example/your-example-name
7070
```
7171

7272
## Submit a Pull Request

integrations/coinbase-langchain/README.md examples/coinbase-langchain/README.md

+17-50
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ A DeFi agent built using Langchain and powered by CDP SDK, operating over the XM
77
- Process blockchain payments using natural language commands
88
- Advanced language processing using LangChain and OpenAI
99
- User-specific wallet management with flexible storage options (Redis or local file)
10-
- XMTP messaging integration for secure, decentralized chat interactions
10+
- XMTP messaging for secure, decentralized chat interactions
1111
- Powered by CDP SDK for reliable blockchain operations and Langchain for AI Agent
1212

1313
## Prerequisites
@@ -18,55 +18,6 @@ A DeFi agent built using Langchain and powered by CDP SDK, operating over the XM
1818
- [Coinbase Developer Platform](https://portal.cdp.coinbase.com) (CDP) API credentials
1919
- Yarn package manager
2020

21-
## Quick Start Guide
22-
23-
Follow these steps to get your x agent up and running:
24-
25-
1. **Clone the repository**:
26-
27-
```bash
28-
git clone https://github.com/ephemeraHQ/xmtp-agent-examples.git
29-
cd integrations/coinbase-langchain
30-
```
31-
32-
2. **Install dependencies**:
33-
34-
```bash
35-
yarn install
36-
```
37-
38-
3. **Set up your environment variables**:
39-
Create a `.env` file like in `.env.example`:
40-
41-
```bash
42-
WALLET_KEY= # the private key for the wallet
43-
ENCRYPTION_KEY= # the encryption key for the wallet
44-
# public key is
45-
46-
NETWORK_ID=base-sepolia # base-mainnet or others
47-
OPENAI_API_KEY= # the OpenAI API key
48-
CDP_API_KEY_NAME= # the name of the CDP API key
49-
CDP_API_KEY_PRIVATE_KEY= # the private key for the CDP API key
50-
XMTP_ENV=local # the environment to use for XMTP
51-
REDIS_URL= # the URL for the Redis database
52-
```
53-
54-
4. **Start the agent**:
55-
56-
```bash
57-
yarn dev
58-
```
59-
60-
5. **Interact with your agent**:
61-
62-
Once running, you'll see a URL in the console like:
63-
64-
```bash
65-
Send a message on http://xmtp.chat/dm/YOUR_AGENT_ADDRESS?env=dev
66-
```
67-
68-
Open this URL in your browser to start chatting with your agent!
69-
7021
## Usage Examples
7122

7223
Once the agent is running, you can interact with it using natural language commands:
@@ -103,3 +54,19 @@ This agent combines key technologies:
10354

10455
- System falls back to local storage if Redis fails
10556
- Check permissions and connection URLs
57+
58+
## Run the agent
59+
60+
```bash
61+
# git clone repo
62+
git clone https://github.com/ephemeraHQ/xmtp-agent-examples.git
63+
# go to the folder
64+
cd xmtp-agent-examples
65+
cd examples/coinbase-langchain
66+
# install packages
67+
yarn
68+
# generate random xmtp keys (optional)
69+
yarn gen:keys
70+
# run the example
71+
yarn dev
72+
```

integrations/coinbase-langchain/package.json examples/coinbase-langchain/package.json

+5-17
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
{
22
"name": "coinbase-langchain",
3-
"version": "1.0.0",
4-
"description": "A DeFi 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": "",
3+
"version": "0.0.1",
4+
"private": true,
135
"type": "module",
146
"scripts": {
157
"build": "tsc",
168
"dev": "tsx src/index.ts",
179
"gen:keys": "tsx ../../scripts/generateKeys.ts",
18-
"lint": "cd ../.. && yarn eslint integrations/coinbase-langchain"
10+
"lint": "cd ../.. && yarn eslint examples/coinbase-langchain"
1911
},
2012
"dependencies": {
2113
"@coinbase/agentkit": "^0.2.0",
@@ -24,14 +16,10 @@
2416
"@langchain/langgraph": "^0.2.24",
2517
"@langchain/openai": "^0.3.14",
2618
"@redis/client": "^1.6.0",
27-
"@xmtp/node-sdk": "1.0.0",
28-
"redis": "^4.7.0",
29-
"uint8arrays": "^5.1.0",
30-
"viem": "^2.7.15"
19+
"@xmtp/node-sdk": "1.0.2",
20+
"redis": "^4.7.0"
3121
},
3222
"devDependencies": {
33-
"@types/node": "^22.13.0",
34-
"dotenv": "^16.4.7",
3523
"tsx": "^4.19.2",
3624
"typescript": "^5.7.3"
3725
}

integrations/coinbase-langchain/src/cdp.ts examples/coinbase-langchain/src/cdp.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from "@coinbase/coinbase-sdk";
88
import { isAddress } from "viem";
99
import { getWalletData, saveWalletData } from "./storage";
10+
import type { XMTPUser } from "./types";
1011

1112
const coinbaseApiKeyName = process.env.CDP_API_KEY_NAME;
1213
let coinbaseApiKeyPrivateKey = process.env.CDP_API_KEY_PRIVATE_KEY;
@@ -92,11 +93,11 @@ export class WalletService {
9293
private inboxId: string;
9394
private sdkInitialized: boolean;
9495

95-
constructor(inboxId: string, address: string) {
96+
constructor(xmtpUser: XMTPUser) {
9697
this.sdkInitialized = initializeCoinbaseSDK();
9798
this.walletStorage = new WalletStorage();
98-
this.humanAddress = address;
99-
this.inboxId = inboxId;
99+
this.humanAddress = xmtpUser.address;
100+
this.inboxId = xmtpUser.inboxId;
100101
console.log(
101102
"WalletService initialized with sender address",
102103
this.humanAddress,

integrations/coinbase-langchain/src/index.ts examples/coinbase-langchain/src/index.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import type { Conversation, DecodedMessage } from "@xmtp/node-sdk";
21
import "dotenv/config";
2+
import type { Conversation, DecodedMessage } from "@xmtp/node-sdk";
33
import { getAddressOfMember } from "@/helpers";
44
import { initializeAgent, processMessage } from "./langchain";
5-
import { initializeStorage as initStorage } from "./storage";
5+
import { initializeStorage } from "./storage";
6+
import type { XMTPUser } from "./types";
67
import { initializeXmtpClient, startMessageListener } from "./xmtp";
78

89
/**
@@ -51,8 +52,12 @@ async function handleMessage(
5152
console.log("Unable to find address, skipping");
5253
return;
5354
}
55+
const xmtpUser: XMTPUser = {
56+
inboxId,
57+
address,
58+
};
5459
// Initialize or get the agent for this user
55-
const { agent, config } = await initializeAgent(inboxId, address);
60+
const { agent, config } = await initializeAgent(xmtpUser);
5661

5762
// Process the message with the agent
5863
const response = await processMessage(
@@ -75,7 +80,7 @@ async function main(): Promise<void> {
7580
validateEnvironment();
7681

7782
// Initialize storage (Redis or local)
78-
await initStorage();
83+
await initializeStorage();
7984

8085
// Initialize XMTP client
8186
const xmtpClient = await initializeXmtpClient();

0 commit comments

Comments
 (0)