From 70ee3ca6a6c3171f8df89dce80c1b30f7755eeaa Mon Sep 17 00:00:00 2001 From: zramsay Date: Fri, 21 Mar 2025 20:35:52 -0400 Subject: [PATCH] wallet fixes --- src/app/api/analyze/route.ts | 80 +++++++++++-------- src/services/blockchain/tokenRewardService.ts | 31 ++++++- 2 files changed, 74 insertions(+), 37 deletions(-) diff --git a/src/app/api/analyze/route.ts b/src/app/api/analyze/route.ts index dd33253..58450c8 100644 --- a/src/app/api/analyze/route.ts +++ b/src/app/api/analyze/route.ts @@ -148,11 +148,17 @@ export async function POST(req: NextRequest): Promise { // Check for wallet address in form data const walletAddress = formData.get('walletAddress'); + + // Log more detailed data about the wallet address console.log('Form data received:', { hasImage: !!imageFile, imageType: imageFile ? typeof imageFile : 'undefined', + hasWalletAddress: !!walletAddress, + walletAddressType: typeof walletAddress, walletAddress: walletAddress || 'Not provided', - formDataKeys: Array.from(formData.keys()) + formDataKeys: Array.from(formData.keys()), + // Log all form data for debugging + formData: Object.fromEntries(formData.entries()) }); // For server-side, check if we have a valid Blob-like object @@ -375,10 +381,16 @@ export async function POST(req: NextRequest): Promise { // This is separate so issues with token rewards don't affect points try { // Check if wallet address was provided in the form data - const walletAddress = formData.get('walletAddress') as string; + const walletAddressRaw = formData.get('walletAddress'); + // Ensure we get a string value - formData can have different types + const walletAddress = walletAddressRaw ? String(walletAddressRaw) : null; // Log the wallet address for debugging - console.log('Wallet address for token award:', walletAddress || 'Not provided'); + console.log('Wallet address for token award:', { + rawValue: walletAddressRaw, + processedValue: walletAddress || 'Not provided', + valueType: typeof walletAddressRaw + }); // Import dynamically to avoid server-side issues with window object const { awardTokensForSighting } = await import('../../../services/blockchain/tokenRewardService'); @@ -386,37 +398,39 @@ export async function POST(req: NextRequest): Promise { // Get species from vision result for token amount calculation const species = visionResult.mainObject || 'animal'; - // Log wallet information - console.log('Token award attempt:', { - walletAddressProvided: !!walletAddress, - walletAddress: walletAddress || 'Not provided', - species - }); - - // Log wallet address for debugging token award - console.log('Starting token award with address:', { - walletAddressFromForm: walletAddress, - species: species, - hasAddress: !!walletAddress - }); - - // Attempt to award tokens for this wildlife sighting - const tokenResult = await awardTokensForSighting(species, undefined, walletAddress); - - // Add token reward info to response if successful - if (tokenResult.success) { - responseData.tokenReward = { - amount: tokenResult.tokenAmount, - txHash: tokenResult.txHash - }; - - console.log('Token reward successful:', tokenResult); - } else if (tokenResult.walletConnected) { - // Wallet was connected but award failed - console.error('Token reward failed:', tokenResult.error); + // Skip token award if no wallet address is provided + if (!walletAddress) { + console.log('No wallet address provided, skipping token award'); + // Continue with other operations without token reward } else { - // No wallet connected - this is not an error, just informational - console.log('No wallet connected, skipping token reward'); + // Log wallet information + console.log('Token award attempt:', { + walletAddressProvided: true, + walletAddress: walletAddress, + species + }); + + // Log detailed information for debugging + console.log('Starting token award with explicit address:', walletAddress); + + // Attempt to award tokens for this wildlife sighting + const tokenResult = await awardTokensForSighting(species, undefined, walletAddress); + + // Add token reward info to response if successful + if (tokenResult.success) { + responseData.tokenReward = { + amount: tokenResult.tokenAmount, + txHash: tokenResult.txHash + }; + + console.log('Token reward successful:', tokenResult); + } else if (tokenResult.walletConnected) { + // Wallet was connected but award failed + console.error('Token reward failed:', tokenResult.error); + } else { + // No wallet connected - this is not an error, just informational + console.log('No wallet connected, skipping token reward'); + } } } catch (tokenError) { console.error('Error processing token reward:', tokenError); diff --git a/src/services/blockchain/tokenRewardService.ts b/src/services/blockchain/tokenRewardService.ts index e75c860..64e0c62 100644 --- a/src/services/blockchain/tokenRewardService.ts +++ b/src/services/blockchain/tokenRewardService.ts @@ -57,10 +57,34 @@ export const awardTokensForSighting = async ( // Determine wallet address - either from parameter or connected wallet let walletAddress = explicitWalletAddress; - // If no explicit address was provided, check if wallet is connected - if (!walletAddress) { + // If explicit address was provided, use it directly (server-side case) + if (walletAddress) { + console.log('Using explicitly provided wallet address:', walletAddress); + } else { + // Client-side case - check if wallet is connected console.log('No explicit wallet address provided, checking wallet connection state...'); - if (!isWalletConnected()) { + + // Try to get from memory first + if (isWalletConnected()) { + walletAddress = getWalletAddress(); + console.log('Found connected wallet in memory:', walletAddress); + } + + // Try localStorage as a fallback (client-side only) + if (!walletAddress && typeof window !== 'undefined') { + try { + const storedAddress = localStorage.getItem('wildlife_wallet_address'); + if (storedAddress) { + console.log('Using wallet address from localStorage:', storedAddress); + walletAddress = storedAddress; + } + } catch (e) { + console.warn('Error accessing localStorage:', e); + } + } + + // If we still don't have an address, return no wallet connected + if (!walletAddress) { console.log('No wallet connected, skipping token award.'); return { success: false, @@ -68,7 +92,6 @@ export const awardTokensForSighting = async ( walletConnected: false }; } - walletAddress = getWalletAddress(); } // Validate we have a wallet address by this point