Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored examples #48

Merged
merged 2 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ To run your XMTP agent, you must create a `.env` file with the following variabl
```tsx
WALLET_KEY= # the private key of the wallet
ENCRYPTION_KEY= # encryption key for the local database
XMTP_ENV= # local, dev, production
```

You can generate random keys with the following command:
You can generate random xmtp keys with the following command:

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

## Examples

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

> See all the available [examples](/examples/).

### Integrations

Examples integrating XMTP with external libraries from the ecosystem

- [grok](/integrations/grok/): Integrate your agent with the Grok API
- [gaia](/integrations/gaia/): Integrate with the Gaia API
- [coinbase-langchain](/integrations/coinbase-langchain/): Agent that uses a CDP for gassless USDC on base

> See all the available [integrations](/integrations/).
16 changes: 8 additions & 8 deletions integrations/README.md → examples/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# How to contribute an XMTP integration
# How to contribute an XMTP example

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

## Prerequisites

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

```bash
git checkout -b integration/your-integration-name
git checkout -b example/your-example-name
```

4. **Install dependencies**

Go to the integration directory and install packages.
Go to the example directory and install packages.

```bash
cd integrations/your-integration
cd examples/your-example
yarn
```

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

6. **Test changes**

Run the integration to check your changes.
Run the example to check your changes.

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

```bash
git add .
git commit -m "Add integration: description of your integration"
git commit -m "Add example: description of your example"
```

8. **Push changes**

Send your changes to your forked repository.

```bash
git push origin integration/your-integration-name
git push origin example/your-example-name
```

## Submit a Pull Request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ A DeFi agent built using Langchain and powered by CDP SDK, operating over the XM
- Process blockchain payments using natural language commands
- Advanced language processing using LangChain and OpenAI
- User-specific wallet management with flexible storage options (Redis or local file)
- XMTP messaging integration for secure, decentralized chat interactions
- XMTP messaging for secure, decentralized chat interactions
- Powered by CDP SDK for reliable blockchain operations and Langchain for AI Agent

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

## Quick Start Guide

Follow these steps to get your x agent up and running:

1. **Clone the repository**:

```bash
git clone https://github.com/ephemeraHQ/xmtp-agent-examples.git
cd integrations/coinbase-langchain
```

2. **Install dependencies**:

```bash
yarn install
```

3. **Set up your environment variables**:
Create a `.env` file like in `.env.example`:

```bash
WALLET_KEY= # the private key for the wallet
ENCRYPTION_KEY= # the encryption key for the wallet
# public key is

NETWORK_ID=base-sepolia # base-mainnet or others
OPENAI_API_KEY= # the OpenAI API key
CDP_API_KEY_NAME= # the name of the CDP API key
CDP_API_KEY_PRIVATE_KEY= # the private key for the CDP API key
XMTP_ENV=local # the environment to use for XMTP
REDIS_URL= # the URL for the Redis database
```

4. **Start the agent**:

```bash
yarn dev
```

5. **Interact with your agent**:

Once running, you'll see a URL in the console like:

```bash
Send a message on http://xmtp.chat/dm/YOUR_AGENT_ADDRESS?env=dev
```

Open this URL in your browser to start chatting with your agent!

## Usage Examples

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

- System falls back to local storage if Redis fails
- Check permissions and connection URLs

## Run the agent

```bash
# git clone repo
git clone https://github.com/ephemeraHQ/xmtp-agent-examples.git
# go to the folder
cd xmtp-agent-examples
cd examples/coinbase-langchain
# install packages
yarn
# generate random xmtp keys (optional)
yarn gen:keys
# run the example
yarn dev
```
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
{
"name": "coinbase-langchain",
"version": "1.0.0",
"description": "A DeFi agent built using CDP AgentKit that operates over the XMTP messaging protocol",
"keywords": [
"xmtp",
"agent",
"blockchain",
"payment"
],
"license": "ISC",
"author": "",
"version": "0.0.1",
"private": true,
"type": "module",
"scripts": {
"build": "tsc",
"dev": "tsx src/index.ts",
"gen:keys": "tsx ../../scripts/generateKeys.ts",
"lint": "cd ../.. && yarn eslint integrations/coinbase-langchain"
"lint": "cd ../.. && yarn eslint examples/coinbase-langchain"
},
"dependencies": {
"@coinbase/agentkit": "^0.2.0",
Expand All @@ -24,14 +16,10 @@
"@langchain/langgraph": "^0.2.24",
"@langchain/openai": "^0.3.14",
"@redis/client": "^1.6.0",
"@xmtp/node-sdk": "1.0.0",
"redis": "^4.7.0",
"uint8arrays": "^5.1.0",
"viem": "^2.7.15"
"@xmtp/node-sdk": "1.0.2",
"redis": "^4.7.0"
},
"devDependencies": {
"@types/node": "^22.13.0",
"dotenv": "^16.4.7",
"tsx": "^4.19.2",
"typescript": "^5.7.3"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "@coinbase/coinbase-sdk";
import { isAddress } from "viem";
import { getWalletData, saveWalletData } from "./storage";
import type { XMTPUser } from "./types";

const coinbaseApiKeyName = process.env.CDP_API_KEY_NAME;
let coinbaseApiKeyPrivateKey = process.env.CDP_API_KEY_PRIVATE_KEY;
Expand Down Expand Up @@ -92,11 +93,11 @@ export class WalletService {
private inboxId: string;
private sdkInitialized: boolean;

constructor(inboxId: string, address: string) {
constructor(xmtpUser: XMTPUser) {
this.sdkInitialized = initializeCoinbaseSDK();
this.walletStorage = new WalletStorage();
this.humanAddress = address;
this.inboxId = inboxId;
this.humanAddress = xmtpUser.address;
this.inboxId = xmtpUser.inboxId;
console.log(
"WalletService initialized with sender address",
this.humanAddress,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { Conversation, DecodedMessage } from "@xmtp/node-sdk";
import "dotenv/config";
import type { Conversation, DecodedMessage } from "@xmtp/node-sdk";
import { getAddressOfMember } from "@/helpers";
import { initializeAgent, processMessage } from "./langchain";
import { initializeStorage as initStorage } from "./storage";
import { initializeStorage } from "./storage";
import type { XMTPUser } from "./types";
import { initializeXmtpClient, startMessageListener } from "./xmtp";

/**
Expand Down Expand Up @@ -51,8 +52,12 @@ async function handleMessage(
console.log("Unable to find address, skipping");
return;
}
const xmtpUser: XMTPUser = {
inboxId,
address,
};
// Initialize or get the agent for this user
const { agent, config } = await initializeAgent(inboxId, address);
const { agent, config } = await initializeAgent(xmtpUser);

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

// Initialize storage (Redis or local)
await initStorage();
await initializeStorage();

// Initialize XMTP client
const xmtpClient = await initializeXmtpClient();
Expand Down
Loading