@@ -5,6 +5,14 @@ import * as fs from "fs";
5
5
const WALLET_DATA_FILE = "wallet_data.txt" ;
6
6
7
7
export async function getClient ( ) : Promise < CdpAgentkit > {
8
+ // Validate required environment variables first
9
+ const apiKeyName = process . env . CDP_API_KEY_NAME ;
10
+ const apiKeyPrivateKey = process . env . CDP_API_KEY_PRIVATE_KEY ;
11
+
12
+ if ( ! apiKeyName || ! apiKeyPrivateKey ) {
13
+ throw new Error ( "Missing required CDP API credentials. Please set CDP_API_KEY_NAME and CDP_API_KEY_PRIVATE_KEY environment variables." ) ;
14
+ }
15
+
8
16
let walletDataStr : string | null = null ;
9
17
10
18
// Read existing wallet data if available
@@ -21,14 +29,20 @@ export async function getClient(): Promise<CdpAgentkit> {
21
29
const config = {
22
30
cdpWalletData : walletDataStr || undefined ,
23
31
networkId : process . env . CDP_AGENT_KIT_NETWORK || "base-sepolia" ,
32
+ apiKeyName : apiKeyName ,
33
+ apiKeyPrivateKey : apiKeyPrivateKey
24
34
} ;
25
35
26
- const agentkit = await CdpAgentkit . configureWithWallet ( config ) ;
27
- // Save wallet data
28
- const exportedWallet = await agentkit . exportWallet ( ) ;
29
- fs . writeFileSync ( WALLET_DATA_FILE , exportedWallet ) ;
30
-
31
- return agentkit ;
36
+ try {
37
+ const agentkit = await CdpAgentkit . configureWithWallet ( config ) ;
38
+ // Save wallet data
39
+ const exportedWallet = await agentkit . exportWallet ( ) ;
40
+ fs . writeFileSync ( WALLET_DATA_FILE , exportedWallet ) ;
41
+ return agentkit ;
42
+ } catch ( error ) {
43
+ console . error ( "Failed to initialize CDP AgentKit:" , error ) ;
44
+ throw new Error ( `Failed to initialize CDP AgentKit: ${ error . message || 'Unknown error' } ` ) ;
45
+ }
32
46
}
33
47
34
48
export const walletProvider : Provider = {
@@ -39,7 +53,7 @@ export const walletProvider: Provider = {
39
53
return `AgentKit Wallet Address: ${ address } ` ;
40
54
} catch ( error ) {
41
55
console . error ( "Error in AgentKit provider:" , error ) ;
42
- return null ;
56
+ return `Error initializing AgentKit wallet: ${ error . message } ` ;
43
57
}
44
58
} ,
45
59
} ;
0 commit comments