8
8
} from "@coinbase/coinbase-sdk" ;
9
9
import { isAddress } from "viem" ;
10
10
import { storage } from "./storage" ;
11
- import type { AgentWalletData , UserWallet } from "./types" ;
11
+ import type { AgentWalletData } from "./types" ;
12
12
13
13
const coinbaseApiKeyName = process . env . CDP_API_KEY_NAME ;
14
14
let coinbaseApiKeyPrivateKey = process . env . CDP_API_KEY_PRIVATE_KEY ;
@@ -55,78 +55,94 @@ export class WalletService {
55
55
this . senderAddress = sender . toLowerCase ( ) ;
56
56
}
57
57
58
- async createWallet ( key : string ) : Promise < AgentWalletData > {
58
+ async createWallet ( inboxId : string ) : Promise < AgentWalletData > {
59
59
try {
60
- key = key . toLowerCase ( ) ;
60
+ console . log ( `Creating new wallet for key ${ inboxId } ...` ) ;
61
61
62
+ // Initialize SDK if not already done
62
63
if ( ! sdkInitialized ) {
63
64
sdkInitialized = initializeCoinbaseSDK ( ) ;
64
65
}
65
66
67
+ // Log the network we're using
68
+ console . log ( `Creating wallet on network: ${ networkId } ` ) ;
69
+
70
+ // Create wallet
66
71
const wallet = await Wallet . create ( {
67
- networkId : Coinbase . networks . BaseSepolia ,
72
+ networkId : networkId ,
73
+ } ) . catch ( ( err : unknown ) => {
74
+ const errorDetails =
75
+ typeof err === "object" ? JSON . stringify ( err , null , 2 ) : err ;
76
+ console . error ( "Detailed wallet creation error:" , errorDetails ) ;
77
+ throw err ;
68
78
} ) ;
69
79
80
+ console . log ( "Wallet created successfully, exporting data..." ) ;
70
81
const data = wallet . export ( ) ;
82
+
83
+ console . log ( "Getting default address..." ) ;
71
84
const address = await wallet . getDefaultAddress ( ) ;
72
85
const walletAddress = address . getId ( ) ;
73
86
74
- await storage . saveUserWallet ( {
75
- userId : `wallet:${ key } ` ,
76
- walletData : data ,
77
- } ) ;
87
+ // Make the wallet address visible in the logs for funding
88
+ console . log ( "-----------------------------------------------------" ) ;
89
+ console . log ( `NEW WALLET CREATED FOR USER: ${ inboxId } ` ) ;
90
+ console . log ( `WALLET ADDRESS: ${ walletAddress } ` ) ;
91
+ console . log ( `NETWORK: ${ networkId } ` ) ;
92
+ console . log ( `SEND FUNDS TO THIS ADDRESS TO TEST: ${ walletAddress } ` ) ;
93
+ console . log ( "-----------------------------------------------------" ) ;
78
94
79
- return {
95
+ const walletInfo : AgentWalletData = {
80
96
id : walletAddress ,
81
97
wallet : wallet ,
82
- data : data ,
83
- human_address : this . senderAddress ,
98
+ walletData : data ,
99
+ human_address : inboxId ,
84
100
agent_address : walletAddress ,
85
- inboxId : key ,
101
+ inboxId : inboxId ,
86
102
} ;
87
- } catch ( error ) {
103
+
104
+ const walletInfoToStore : AgentWalletData = {
105
+ id : walletAddress ,
106
+ //no wallet
107
+ walletData : data ,
108
+ human_address : inboxId ,
109
+ agent_address : walletAddress ,
110
+ inboxId : inboxId ,
111
+ } ;
112
+
113
+ await storage . saveUserWallet ( inboxId , JSON . stringify ( walletInfoToStore ) ) ;
114
+ console . log ( "Wallet created and saved successfully" ) ;
115
+ return walletInfo ;
116
+ } catch ( error : unknown ) {
117
+ console . error ( "Failed to create wallet:" , error ) ;
118
+
119
+ // Provide more detailed error information
88
120
if ( error instanceof Error ) {
89
121
throw new Error ( `Wallet creation failed: ${ error . message } ` ) ;
90
122
}
123
+
91
124
throw new Error ( `Failed to create wallet: ${ String ( error ) } ` ) ;
92
125
}
93
126
}
94
127
95
- async getWallet (
96
- inboxId : string ,
97
- createIfNotFound : boolean = true ,
98
- ) : Promise < AgentWalletData | undefined > {
128
+ async getWallet ( inboxId : string ) : Promise < AgentWalletData | undefined > {
99
129
// Try to retrieve existing wallet data
100
130
const walletData = await storage . getUserWallet ( inboxId ) ;
101
- if ( ! walletData ) {
102
- return undefined ;
131
+ if ( walletData === null ) {
132
+ console . log ( `No wallet found for ${ inboxId } , creating new one` ) ;
133
+ return this . createWallet ( inboxId ) ;
103
134
}
104
135
105
- try {
106
- const importedWallet = await Wallet . import ( walletData . walletData ) ;
107
-
108
- return {
109
- id : importedWallet . getId ( ) ?? "" ,
110
- wallet : importedWallet ,
111
- data : walletData . walletData ,
112
- human_address : walletData . userId ,
113
- agent_address : walletData . userId ,
114
- inboxId : walletData . userId ,
115
- } ;
116
- } catch ( error ) {
117
- // If wallet data exists but is corrupted/invalid and we're allowed to create a new one
118
- if ( createIfNotFound ) {
119
- console . warn (
120
- `Failed to import existing wallet for ${ inboxId } , creating new one` ,
121
- ) ;
122
- return this . createWallet ( inboxId ) ;
123
- }
136
+ const importedWallet = await Wallet . import ( walletData . walletData ) ;
124
137
125
- // Otherwise, fail explicitly
126
- throw new Error (
127
- `Invalid wallet data: ${ error instanceof Error ? error . message : String ( error ) } ` ,
128
- ) ;
129
- }
138
+ return {
139
+ id : importedWallet . getId ( ) ?? "" ,
140
+ wallet : importedWallet ,
141
+ walletData : walletData . walletData ,
142
+ human_address : walletData . human_address ,
143
+ agent_address : walletData . agent_address ,
144
+ inboxId : walletData . inboxId ,
145
+ } ;
130
146
}
131
147
132
148
async transfer (
@@ -160,7 +176,7 @@ export class WalletService {
160
176
console . log (
161
177
`💰 Checking balance for source wallet: ${ from . agent_address } ...` ,
162
178
) ;
163
- const balance = await from . wallet . getBalance ( Coinbase . assets . Usdc ) ;
179
+ const balance = await from . wallet ? .getBalance ( Coinbase . assets . Usdc ) ;
164
180
console . log ( `💵 Available balance: ${ Number ( balance ) } USDC` ) ;
165
181
166
182
if ( Number ( balance ) < amount ) {
@@ -179,7 +195,7 @@ export class WalletService {
179
195
// Get or validate destination wallet
180
196
let destinationAddress = toAddress ;
181
197
console . log ( `🔑 Validating destination: ${ toAddress } ...` ) ;
182
- const to = await this . getWallet ( toAddress , false ) ;
198
+ const to = await this . getWallet ( toAddress ) ;
183
199
if ( to ) {
184
200
destinationAddress = to . agent_address ;
185
201
console . log ( `✅ Destination wallet found: ${ destinationAddress } ` ) ;
@@ -198,7 +214,7 @@ export class WalletService {
198
214
console . log (
199
215
`🚀 Executing transfer of ${ amount } USDC from ${ from . agent_address } to ${ destinationAddress } ...` ,
200
216
) ;
201
- const transfer = await from . wallet . createTransfer ( {
217
+ const transfer = await from . wallet ? .createTransfer ( {
202
218
amount,
203
219
assetId : Coinbase . assets . Usdc ,
204
220
destination : destinationAddress ,
@@ -207,7 +223,7 @@ export class WalletService {
207
223
208
224
console . log ( `⏳ Waiting for transfer to complete...` ) ;
209
225
try {
210
- await transfer . wait ( ) ;
226
+ await transfer ? .wait ( ) ;
211
227
console . log ( `✅ Transfer completed successfully!` ) ;
212
228
console . log (
213
229
`📝 Transaction details: ${ JSON . stringify ( transfer , null , 2 ) } ` ,
@@ -245,7 +261,7 @@ export class WalletService {
245
261
return { address : undefined , balance : 0 } ;
246
262
}
247
263
248
- const balance = await walletData . wallet . getBalance ( Coinbase . assets . Usdc ) ;
264
+ const balance = await walletData . wallet ? .getBalance ( Coinbase . assets . Usdc ) ;
249
265
return {
250
266
address : walletData . agent_address ,
251
267
balance : Number ( balance ) ,
@@ -262,12 +278,14 @@ export class WalletService {
262
278
const walletData = await this . getWallet ( address ) ;
263
279
if ( ! walletData ) return undefined ;
264
280
265
- const trade = await walletData . wallet . createTrade ( {
281
+ const trade = await walletData . wallet ? .createTrade ( {
266
282
amount,
267
283
fromAssetId,
268
284
toAssetId,
269
285
} ) ;
270
286
287
+ if ( ! trade ) return undefined ;
288
+
271
289
try {
272
290
await trade . wait ( ) ;
273
291
} catch ( err ) {
@@ -283,12 +301,16 @@ export class WalletService {
283
301
key = key . toLowerCase ( ) ;
284
302
const encryptedKey = `wallet:${ key } ` ;
285
303
286
- const emptyWallet : UserWallet = {
287
- userId : encryptedKey ,
304
+ const emptyWallet : AgentWalletData = {
305
+ id : "" ,
306
+ wallet : { } as Wallet ,
288
307
walletData : { } as WalletData ,
308
+ human_address : encryptedKey ,
309
+ agent_address : encryptedKey ,
310
+ inboxId : encryptedKey ,
289
311
} ;
290
312
291
- await storage . saveUserWallet ( emptyWallet ) ;
313
+ await storage . saveUserWallet ( encryptedKey , JSON . stringify ( emptyWallet ) ) ;
292
314
return true ;
293
315
}
294
316
}
0 commit comments