Sweetmantech/myc 3556 apiimagegenerate lookup address by account#8
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
WalkthroughReplaces address-based account lookup with an accountId/name flow (with creation fallback), adds smart-account and paymaster-backed user-op helpers, introduces USDC transfer utilities and constants, and updates payment and middleware flows to resolve and load accounts before sending user operations. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant fetchWithPayment
participant getAccount
participant loadAccount
participant getTransferCalls
participant sendUserOpAndWait
participant getSmartAccount
participant CDP as "Coinbase CDP (EVM)"
Client->>fetchWithPayment: request (includes accountId)
fetchWithPayment->>getAccount: getAccount(accountId)
getAccount-->>fetchWithPayment: account (or created account)
fetchWithPayment->>loadAccount: loadAccount(account.address)
loadAccount->>getTransferCalls: getTransferCalls(recipient, price)
getTransferCalls-->>loadAccount: Call[] (USDC transfer calldata)
loadAccount->>sendUserOpAndWait: sendUserOpAndWait(calls)
sendUserOpAndWait->>getSmartAccount: getSmartAccount()
getSmartAccount->>CDP: getAccount(ACCOUNT_ADDRESS) / getSmartAccount(...)
CDP-->>getSmartAccount: owner account / smart account
getSmartAccount-->>sendUserOpAndWait: EvmSmartAccount
sendUserOpAndWait->>CDP: sendUserOperation(smartAccount, network, paymasterUrl, calls)
CDP-->>sendUserOpAndWait: userOpHash
sendUserOpAndWait->>CDP: waitForUserOperation(userOpHash)
CDP-->>sendUserOpAndWait: confirmed
sendUserOpAndWait->>CDP: getUserOperation(userOpHash)
CDP-->>sendUserOpAndWait: userOp (includes transactionHash)
sendUserOpAndWait-->>loadAccount: transactionHash
loadAccount-->>fetchWithPayment: transactionHash
fetchWithPayment-->>Client: response with tx hash
Estimated code review effort🎯 4 (Complex) | ⏱️ ~50 minutes
Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 inconclusive)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 4
🧹 Nitpick comments (3)
lib/x402/loadAccount.ts (1)
14-18: Orphaned step comments and misleading function name.The comments reference steps "3." and "4." but there are no steps 1 or 2 in this function. Also,
loadAccountis a misleading name since the function sends a USDC payment rather than loading an account.Consider renaming to
sendUsdcPaymentorfundRecipientand removing the orphaned step numbers:- // 3. Get transfer calls + // Get transfer calls const calls = getTransferCalls(recipientAddress, IMAGE_GENERATE_PRICE); - // 4. Send user operation and wait for it to be mined + // Send user operation and wait for it to be mined const transactionHash = await sendUserOpAndWait(calls);lib/coinbase/sendUserOpAndWait.ts (2)
28-31: Consider adding a timeout forwaitForUserOperation.
waitForUserOperationcould potentially hang indefinitely if the user operation never gets mined (e.g., due to gas price issues or network congestion). This could leave request threads blocked.Check if the CDP SDK's
waitForUserOperationaccepts a timeout option, or wrap with a timeout utility:const timeoutMs = 60_000; // 60 seconds await Promise.race([ cdp.evm.waitForUserOperation({ smartAccountAddress: smartAccount.address, userOpHash: sendResult.userOpHash, }), new Promise((_, reject) => setTimeout(() => reject(new Error('User operation timeout')), timeoutMs) ), ]);
6-9: Consider movingCallinterface to a shared types file.The
Callinterface is imported bylib/x402/getTransferCalls.ts. If this interface is reused across multiple modules, consider moving it to a dedicated types file (e.g.,lib/types.ts) for better discoverability.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (8)
lib/coinbase/getAccount.ts(1 hunks)lib/coinbase/getSmartAccount.ts(1 hunks)lib/coinbase/sendUserOpAndWait.ts(1 hunks)lib/const.ts(1 hunks)lib/x402/fetchWithPayment.ts(2 hunks)lib/x402/getTransferCalls.ts(1 hunks)lib/x402/loadAccount.ts(1 hunks)middleware.ts(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (7)
lib/coinbase/getSmartAccount.ts (2)
lib/coinbase/client.ts (1)
cdp(6-6)lib/const.ts (2)
ACCOUNT_ADDRESS(3-3)SMART_ACCOUNT_ADDRESS(4-4)
lib/x402/loadAccount.ts (3)
lib/x402/getTransferCalls.ts (1)
getTransferCalls(14-27)lib/const.ts (1)
IMAGE_GENERATE_PRICE(7-7)lib/coinbase/sendUserOpAndWait.ts (1)
sendUserOpAndWait(17-45)
lib/x402/fetchWithPayment.ts (3)
lib/coinbase/getAccount.ts (1)
getAccount(10-25)lib/credits/deductCredits.ts (1)
deductCredits(23-58)lib/x402/loadAccount.ts (1)
loadAccount(12-27)
lib/coinbase/sendUserOpAndWait.ts (3)
lib/coinbase/getSmartAccount.ts (1)
getSmartAccount(10-24)lib/coinbase/client.ts (1)
cdp(6-6)lib/const.ts (1)
PAYMASTER_URL(6-6)
lib/x402/getTransferCalls.ts (2)
lib/coinbase/sendUserOpAndWait.ts (1)
Call(6-9)lib/const.ts (1)
USDC_ADDRESS(5-5)
middleware.ts (1)
lib/const.ts (2)
SMART_ACCOUNT_ADDRESS(4-4)IMAGE_GENERATE_PRICE(7-7)
lib/coinbase/getAccount.ts (1)
lib/coinbase/client.ts (1)
cdp(6-6)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Vercel Agent Review
🔇 Additional comments (4)
middleware.ts (1)
3-3: LGTM!Good refactoring to use centralized constants for the smart account address and pricing. This improves maintainability and ensures consistency across the codebase.
Also applies to: 43-47
lib/coinbase/getSmartAccount.ts (1)
10-24: LGTM!The function is well-structured with proper error handling. The separation of owner account retrieval and smart account lookup is clean.
Consider whether this function would benefit from caching the smart account instance if called frequently, since the addresses are constant.
lib/x402/getTransferCalls.ts (1)
14-27: LGTM!Clean utility function using viem's encoding utilities correctly. USDC decimals are correctly set to 6.
Minor: The
as Call[]assertion on line 26 may be unnecessary if the object literal already satisfies theCallinterface, but it's harmless.lib/const.ts (1)
3-5: Confirm these are Base mainnet addresses and document the mainnet-only approach.These addresses are verified as Base mainnet (USDC is officially 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913). The application is explicitly configured as "Mainnet x402 Demo" with no dev/staging environment support currently implemented. Consider whether future multi-environment support is needed; if so, these addresses should be moved to environment variables like
RESOURCE_WALLET_ADDRESSto maintain consistency.
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
lib/coinbase/sendUserOpAndWait.ts (2)
24-24: Consider making the network configurable.The network is hardcoded to "base". If there's a possibility of supporting multiple networks in the future, consider making this a parameter or configuration value.
34-39: Validate the transaction hash before returning.The code assumes
userOp.transactionHashis always present. Consider adding validation to handle cases where it might be undefined or null.const userOp = await cdp.evm.getUserOperation({ smartAccount, userOpHash: sendResult.userOpHash, }); - return userOp.transactionHash; + if (!userOp.transactionHash) { + throw new Error('Transaction hash not found in user operation'); + } + return userOp.transactionHash;
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
lib/coinbase/sendUserOpAndWait.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
lib/coinbase/sendUserOpAndWait.ts (3)
lib/coinbase/getSmartAccount.ts (1)
getSmartAccount(10-24)lib/coinbase/client.ts (1)
cdp(6-6)lib/const.ts (1)
PAYMASTER_URL(6-6)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Vercel Agent Review
🔇 Additional comments (5)
lib/coinbase/sendUserOpAndWait.ts (5)
6-9: LGTM! Clean interface definition.The
Callinterface is well-structured and uses appropriate viem types for EVM calls.
11-16: LGTM! Clear documentation.The JSDoc comment effectively describes the function's behavior and parameters.
19-19: LGTM! Proper use of helper function.Correctly retrieves the smart account using the helper function.
40-45: LGTM! Good error handling pattern.The error handling appropriately logs the error and rethrows with descriptive context, which will help with debugging and monitoring.
21-27: The@ts-expect-errorsuppresses a real type compatibility issue, but the code is safe at runtime. Consider using an explicit type assertion instead.The local
Callinterface ({ to: Address; data: Hex }) is a subset of the direct-call variant expected by CDP SDK'sCalls<T>discriminated union type. However, TypeScript struggles to matchreadonly Call[]to the complexCalls<T>recursive/mapped type (which handles both direct and contract call shapes), causing the "excessively deep type instantiation" error.Since the code only uses direct calls, replace the
@ts-expect-errorwith an explicit type assertion:const sendResult = await cdp.evm.sendUserOperation({ smartAccount, network: "base", paymasterUrl: PAYMASTER_URL, calls: calls as any, // or: calls as Calls<unknown[]> });This preserves runtime safety while making the intent explicit. Alternatively, extend the
Callinterface to include the optionalvaluefield to better align with the SDK's direct-call shape and potentially eliminate the type error entirely.
Summary by CodeRabbit
New Features
Breaking Changes
✏️ Tip: You can customize this high-level summary in your review settings.