Skip to content

Commit 232adeb

Browse files
committed
Revert "review"
This reverts commit 8605c36.
1 parent 8605c36 commit 232adeb

File tree

6 files changed

+460
-97
lines changed

6 files changed

+460
-97
lines changed

examples/coinbase-langchain/src/cdp.ts

+16-59
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ import {
22
Coinbase,
33
TimeoutError,
44
Wallet,
5-
type Trade,
65
type Transfer,
76
type WalletData,
87
} from "@coinbase/coinbase-sdk";
98
import { isAddress } from "viem";
109
import { getWalletData, saveWalletData } from "./storage";
11-
import type { AgentWalletData, XMTPUser } from "./types";
10+
import type { XMTPUser } from "./types";
1211

1312
const coinbaseApiKeyName = process.env.CDP_API_KEY_NAME;
1413
let coinbaseApiKeyPrivateKey = process.env.CDP_API_KEY_PRIVATE_KEY;
@@ -75,6 +74,18 @@ function initializeCoinbaseSDK(): boolean {
7574
}
7675
}
7776

77+
// Agent wallet data
78+
export type AgentWalletData = {
79+
id: string;
80+
wallet: Wallet;
81+
data: WalletData;
82+
human_address: string;
83+
agent_address: string;
84+
blockchain?: string;
85+
state?: string;
86+
inboxId: string;
87+
};
88+
7889
// Wallet service class based on cointoss implementation
7990
export class WalletService {
8091
private walletStorage: WalletStorage;
@@ -274,12 +285,12 @@ export class WalletService {
274285
}
275286

276287
async transfer(
277-
xmtpUser: XMTPUser,
288+
inboxId: string,
289+
humanAddress: string,
278290
toAddress: string,
279291
amount: number,
280292
): Promise<Transfer | undefined> {
281-
const humanAddress = xmtpUser.address.toLowerCase();
282-
const inboxId = xmtpUser.inboxId;
293+
humanAddress = humanAddress.toLowerCase();
283294
toAddress = toAddress.toLowerCase();
284295

285296
console.log("📤 TRANSFER INITIATED");
@@ -379,58 +390,4 @@ export class WalletService {
379390
throw error;
380391
}
381392
}
382-
383-
async swap(
384-
xmtpUser: XMTPUser,
385-
fromAssetId: string,
386-
toAssetId: string,
387-
amount: number,
388-
): Promise<Trade | undefined> {
389-
const inboxId = xmtpUser.inboxId;
390-
const walletData = await this.getWallet(inboxId);
391-
if (!walletData) return undefined;
392-
console.log(`Retrieved wallet data for ${inboxId}`);
393-
394-
console.log(
395-
`Initiating swap from ${fromAssetId} to ${toAssetId} for amount: ${amount}`,
396-
);
397-
const trade = await walletData.wallet.createTrade({
398-
amount,
399-
fromAssetId,
400-
toAssetId,
401-
});
402-
403-
try {
404-
await trade.wait();
405-
} catch (err) {
406-
if (err instanceof TimeoutError) {
407-
console.log("Waiting for trade timed out");
408-
} else {
409-
console.error("Error while waiting for trade to complete: ", err);
410-
}
411-
}
412-
413-
return trade;
414-
}
415-
416-
async deleteWallet(xmtpUser: XMTPUser): Promise<boolean> {
417-
const inboxId = xmtpUser.inboxId;
418-
console.log(`Deleting wallet for key ${inboxId}`);
419-
const encryptedKey = `wallet:${inboxId}`;
420-
421-
// Create an empty wallet object to effectively delete the wallet
422-
const emptyWallet: AgentWalletData = {
423-
id: encryptedKey,
424-
wallet: {} as Wallet,
425-
data: {} as WalletData,
426-
human_address: this.humanAddress,
427-
agent_address: this.inboxId,
428-
inboxId: this.inboxId,
429-
};
430-
431-
await this.walletStorage.set(this.inboxId, JSON.stringify(emptyWallet));
432-
433-
console.log(`Wallet deleted for key ${inboxId}`);
434-
return true;
435-
}
436393
}

examples/coinbase-langchain/src/langchain.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ function createWalletTools(xmtpUser: XMTPUser) {
5959

6060
const result = await walletService.transfer(
6161
xmtpUser.inboxId,
62+
xmtpUser.address,
6263
recipientAddress,
6364
numericAmount,
6465
);
@@ -100,7 +101,7 @@ export async function initializeAgent(
100101
// Check if we already have an agent for this user
101102
if (xmtpUser.inboxId in agentStore) {
102103
console.log(`Using existing agent for user: ${xmtpUser.inboxId}`);
103-
const agentConfig: AgentConfig = {
104+
const agentConfig = {
104105
configurable: { thread_id: xmtpUser.inboxId },
105106
};
106107
return { agent: agentStore[xmtpUser.inboxId], config: agentConfig };

examples/coinbase-langchain/src/storage.ts

+27-13
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
11
import * as fs from "fs";
22
import { createClient, type RedisClientType } from "redis";
33

4-
// Storage constantsf
4+
// Storage constants
55
export const WALLET_KEY_PREFIX = "wallet_data:";
6-
export const WALLET_STORAGE_DIR = ".data/wallet_data";
7-
export const XMTP_STORAGE_DIR = ".data/xmtp";
6+
export const LOCAL_STORAGE_DIR = ".data/wallet_data";
87
export let redisClient: RedisClientType | null = null;
98

9+
if (!process.env.REDIS_URL) {
10+
console.warn(
11+
"Warning: REDIS_URL not set, using local file storage for wallet data",
12+
);
13+
}
1014
/**
1115
* Initialize Redis client and handle fallback to local storage
1216
*/
1317
export async function initializeStorage() {
1418
if (process.env.REDIS_URL) {
15-
redisClient = createClient({
16-
url: process.env.REDIS_URL,
17-
});
19+
try {
20+
redisClient = createClient({
21+
url: process.env.REDIS_URL,
22+
});
1823

19-
await redisClient.connect();
20-
console.log("Connected to Redis");
24+
await redisClient.connect();
25+
console.log("Connected to Redis");
26+
} catch (error: unknown) {
27+
console.error("Failed to connect to Redis:", error);
28+
console.log("Falling back to local file storage");
29+
redisClient = null;
30+
ensureLocalStorage();
31+
}
2132
} else {
2233
console.log("Using local file storage for wallet data");
2334
ensureLocalStorage();
@@ -28,8 +39,8 @@ export async function initializeStorage() {
2839
* Ensure local storage directory exists
2940
*/
3041
export function ensureLocalStorage() {
31-
if (!fs.existsSync(WALLET_STORAGE_DIR)) {
32-
fs.mkdirSync(WALLET_STORAGE_DIR, { recursive: true });
42+
if (!fs.existsSync(LOCAL_STORAGE_DIR)) {
43+
fs.mkdirSync(LOCAL_STORAGE_DIR, { recursive: true });
3344
}
3445
}
3546

@@ -48,7 +59,7 @@ export async function saveWalletData(
4859
} else {
4960
// Save to local file
5061
try {
51-
fs.writeFileSync(WALLET_STORAGE_DIR + "/" + key + ".json", walletData);
62+
fs.writeFileSync(LOCAL_STORAGE_DIR + "/" + key + ".json", walletData);
5263
} catch (error: unknown) {
5364
const errorMessage =
5465
error instanceof Error ? error.message : String(error);
@@ -70,8 +81,11 @@ export async function getWalletData(
7081
return await redisClient.get(userKey);
7182
} else {
7283
try {
73-
if (fs.existsSync(WALLET_STORAGE_DIR + "/" + userKey)) {
74-
return fs.readFileSync(WALLET_STORAGE_DIR + "/" + userKey, "utf8");
84+
if (fs.existsSync(LOCAL_STORAGE_DIR + "/" + userKey + ".json")) {
85+
return fs.readFileSync(
86+
LOCAL_STORAGE_DIR + "/" + userKey + ".json",
87+
"utf8",
88+
);
7589
}
7690
} catch (error: unknown) {
7791
const errorMessage =

examples/coinbase-langchain/src/types.ts

-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { Wallet, WalletData } from "@coinbase/coinbase-sdk";
21
import type { MemorySaver } from "@langchain/langgraph";
32
import type { createReactAgent } from "@langchain/langgraph/prebuilt";
43

@@ -14,17 +13,6 @@ export interface AgentConfig {
1413
thread_id: string;
1514
};
1615
}
17-
// Agent wallet data
18-
export type AgentWalletData = {
19-
id: string;
20-
wallet: Wallet;
21-
data: WalletData;
22-
human_address: string;
23-
agent_address: string;
24-
blockchain?: string;
25-
state?: string;
26-
inboxId: string;
27-
};
2816

2917
export type Agent = ReturnType<typeof createReactAgent>;
3018

examples/coinbase-langchain/src/xmtp.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
type XmtpEnv,
66
} from "@xmtp/node-sdk";
77
import { createSigner, getEncryptionKeyFromHex } from "@/helpers";
8-
import { XMTP_STORAGE_DIR } from "./storage";
98

109
/**
1110
* Initialize the XMTP client
@@ -22,20 +21,18 @@ export async function initializeXmtpClient() {
2221
const signer = createSigner(WALLET_KEY as `0x${string}`);
2322
const encryptionKey = getEncryptionKeyFromHex(ENCRYPTION_KEY);
2423

25-
const identifier = await signer.getIdentifier();
26-
const address = identifier.identifier;
2724
// Set the environment to dev or production
2825
const env: XmtpEnv = XMTP_ENV as XmtpEnv;
2926

3027
console.log(`Creating XMTP client on the '${env}' network...`);
31-
const client = await Client.create(signer, encryptionKey, {
32-
env,
33-
dbPath: XMTP_STORAGE_DIR + `/${env}-${address}`,
34-
});
28+
const client = await Client.create(signer, encryptionKey, { env });
3529

3630
console.log("Syncing conversations...");
3731
await client.conversations.sync();
3832

33+
const identifier = await signer.getIdentifier();
34+
const address = identifier.identifier;
35+
3936
console.log(
4037
`Agent initialized on ${address}\nSend a message on http://xmtp.chat/dm/${address}?env=${env}`,
4138
);

0 commit comments

Comments
 (0)