Optimize ERC-20/ERC-721 Approval Flow Allowance Checks and Eliminating Redundant approve Transactions Across Auction Services#69
Conversation
WalkthroughUpdated the auction service API to require caller identity ( Changes
Sequence Diagram(s)sequenceDiagram
participant UI as Client UI
participant S as AuctionService
participant R as readContract
participant W as writeContract
participant C as Blockchain
UI->>S: createAuction(params, userAddress)
S->>R: read getApproved / allowance (token, userAddress)
alt approval needed
S->>W: approve(spender, amount/tokenId)
W->>C: submit approve tx
C-->>W: tx confirmed
end
S->>W: createAuction(...) (mint/register)
W->>C: submit createAuction tx
C-->>W: tx confirmed
W-->>S: result
S-->>UI: success
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@lib/auction-service.ts`:
- Around line 87-96: The service interface changed: createAuction, placeBid, and
revealBid now require a userAddress parameter; update all callers to pass the
connected wallet address (e.g., the address from useAccount/useWallet) when
invoking AuctionService.createAuction, AuctionService.placeBid, and
AuctionService.revealBid so signatures match; specifically modify the create
page component, the BidForm component, the VickreyRevealForm component, and the
DutchAuctionPrice component to obtain the current wallet address and forward it
as the new final argument in each call (preserve existing args order).
In `@lib/services/english-auction-service.ts`:
- Line 2: The import line currently brings Config as a runtime import with
readContract and readContracts; change it so that Config is imported as a
type-only import (using "import type { Config } from '@wagmi/core'") and keep
readContract and readContracts as normal imports (import { readContract,
readContracts } from '@wagmi/core'), and apply the same split to the other
updated service files referencing Config to avoid including the type at runtime.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 18a8074d-89b0-4880-b284-4c03110822b9
📒 Files selected for processing (7)
lib/auction-service.tslib/services/allpay-auction-service.tslib/services/english-auction-service.tslib/services/exponential-dutch-auction-service.tslib/services/linear-dutch-auction-service.tslib/services/logarithmic-dutch-auction-service.tslib/services/vickrey-auction-service.ts
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
components/auction/dutch-auction-price.tsx (1)
1-6:⚠️ Potential issue | 🟡 MinorMissing
"use client"directive.This component uses React hooks (
useState,useEffect,useAccount,useChainId,usePublicClient,useWriteContract) which require client-side rendering. The"use client"directive is required for Next.js App Router.🔧 Proposed fix
+"use client"; + import { useEffect, useState } from "react"; import { useAccount, useChainId, usePublicClient, useWriteContract } from "wagmi";As per coding guidelines, "Ensure that 'use client' is being used" for files matching
**/*.{ts,tsx,js,jsx}.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@components/auction/dutch-auction-price.tsx` around lines 1 - 6, This file is missing the Next.js App Router client directive: add the "use client" directive as the very first line of the module (before any imports) in components/auction/dutch-auction-price.tsx so the React hooks used (useState, useEffect, useAccount, useChainId, usePublicClient, useWriteContract) in the DutchAuctionPrice component are allowed to run on the client; ensure the exact string "use client" (including quotes) is placed at the top of the file.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@components/auction/dutch-auction-price.tsx`:
- Around line 1-6: This file is missing the Next.js App Router client directive:
add the "use client" directive as the very first line of the module (before any
imports) in components/auction/dutch-auction-price.tsx so the React hooks used
(useState, useEffect, useAccount, useChainId, usePublicClient, useWriteContract)
in the DutchAuctionPrice component are allowed to run on the client; ensure the
exact string "use client" (including quotes) is placed at the top of the file.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 24fd83e8-9c47-439a-a910-914ef4d4979f
📒 Files selected for processing (5)
app/create/page.tsxcomponents/auction/bid-form.tsxcomponents/auction/dutch-auction-price.tsxcomponents/auction/vickrey-reveal-form.tsxlib/services/english-auction-service.ts
|
@coderabbitai review and approve |
|
Regarding approval — I'm not able to approve the PR through this comment. To request an approval, please post ✅ Actions performedReview triggered.
|
Addressed Issues:
Closes #31
Issue:
approveTokenwas called unconditionally before everybid/createAuction, forcing users to pay gas for an ERC-20approvetransaction every single time — even when a sufficient allowance already existed.Fix: Read the current on-chain allowance / approval status first. Only send the
approvetransaction if it is actually needed.What Changed
1.
approveTokenLogic (All Services)approveallowance(user, spender)viareadContract; approves only ifallowance < amountapprovegetApproved(tokenId)viareadContract; approves only if not already approved to spenderuserAddress: Addressadded (needed to callallowance(owner, spender))readContractsonlyreadContractadded from@wagmi/coreAI Usage Disclosure:
We encourage contributors to use AI tools responsibly when creating Pull Requests. While AI can be a valuable aid, it is essential to ensure that your contributions meet the task requirements, build successfully, include relevant tests, and pass all linters. Submissions that do not meet these standards may be closed without warning to maintain the quality and integrity of the project. Please take the time to understand the changes you are proposing and their impact. AI slop is strongly discouraged and may lead to banning and blocking. Do not spam our repos with AI slop.
Check one of the checkboxes below:
I have used the following AI models and tools: TODO
Checklist
Summary by CodeRabbit
Bug Fixes
Refactor