Skip to content

Optimize ERC-20/ERC-721 Approval Flow Allowance Checks and Eliminating Redundant approve Transactions Across Auction Services#69

Open
aniket866 wants to merge 3 commits into
StabilityNexus:mainfrom
aniket866:fixing-gas-usage
Open

Optimize ERC-20/ERC-721 Approval Flow Allowance Checks and Eliminating Redundant approve Transactions Across Auction Services#69
aniket866 wants to merge 3 commits into
StabilityNexus:mainfrom
aniket866:fixing-gas-usage

Conversation

@aniket866

@aniket866 aniket866 commented Mar 24, 2026

Copy link
Copy Markdown
Contributor

Addressed Issues:

Closes #31

Issue: approveToken was called unconditionally before every bid / createAuction, forcing users to pay gas for an ERC-20 approve transaction every single time — even when a sufficient allowance already existed.
Fix: Read the current on-chain allowance / approval status first. Only send the approve transaction if it is actually needed.


What Changed

1. approveToken Logic (All Services)

Aspect Before After
ERC-20 allow check None — always called approve Reads allowance(user, spender) via readContract; approves only if allowance < amount
ERC-721 approval check None — always called approve Reads getApproved(tokenId) via readContract; approves only if not already approved to spender
New parameter userAddress: Address added (needed to call allowance(owner, spender))
Import added readContracts only readContract added from @wagmi/core

AI 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:

  • This PR does not contain AI-generated code at all.
  • This PR contains AI-generated code. I have read the AI Usage Policy and this PR complies with this policy. I have tested the code locally and I am responsible for it.

I have used the following AI models and tools: TODO

Checklist

  • My PR addresses a single issue, fixes a single bug or makes a single improvement.
  • My code follows the project's code style and conventions
  • If applicable, I have made corresponding changes or additions to the documentation
  • If applicable, I have made corresponding changes or additions to tests
  • My changes generate no new warnings or errors
  • I have joined the Discord server and I will share a link to this PR with the project maintainers there
  • I have read the Contribution Guidelines
  • Once I submit my PR, CodeRabbit AI will automatically review it and I will address CodeRabbit's comments.
  • I have filled this PR template completely and carefully, and I understand that my PR may be closed without review otherwise.

Summary by CodeRabbit

  • Bug Fixes

    • Optimized token approval flows to check on-chain state before sending approvals, reducing unnecessary transactions.
    • UI flows now require a connected wallet address before initiating auction actions (create, bid, reveal, buyout).
  • Refactor

    • Auction service APIs updated to consistently accept caller identity for permission and allowance validation.

@coderabbitai

coderabbitai Bot commented Mar 24, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

Updated the auction service API to require caller identity (userAddress) and refactored token approval flows: services now read on-chain approval/allowance via readContract and only call approve when necessary, reducing redundant approval transactions.

Changes

Cohort / File(s) Summary
Interface Definition
lib/auction-service.ts
IAuctionService method signatures updated to include userAddress: Address on createAuction, placeBid, and revealBid; placeBid now accepts tokenAddress as string.
AllPay Auction Service
lib/services/allpay-auction-service.ts
Added userAddress param to createAuction, placeBid, and private approveToken; approval now reads on-chain state (getApproved / allowance) via readContract and only writes when required.
English Auction Service
lib/services/english-auction-service.ts
Added userAddress param on public methods and private approveToken; switched to readContract for conditional NFT/ERC20 approval checks; adjusted imports to type-only Config.
Exponential Dutch Auction Service
lib/services/exponential-dutch-auction-service.ts
Threaded userAddress through public methods and approval helper; replaced unconditional approve with read-before-write checks using readContract.
Linear Dutch Auction Service
lib/services/linear-dutch-auction-service.ts
Added userAddress to public methods and approveToken; approval flow now queries getApproved / allowance before calling approve.
Logarithmic Dutch Auction Service
lib/services/logarithmic-dutch-auction-service.ts
Public methods now accept userAddress; approveToken updated to use readContract for conditional approvals for NFTs/ERC20s.
Vickrey Auction Service
lib/services/vickrey-auction-service.ts
Added userAddress to createAuction and revealBid, and to private approveToken; uses readContract to avoid redundant approvals.
Frontend: Create Page
app/create/page.tsx
Passes connected wallet address into auctionService.createAuction as the new userAddress argument.
Frontend: Bid Form
components/auction/bid-form.tsx
Calls to auctionService.placeBid updated to include connected wallet address cast to Address.
Frontend: Dutch Auction Price
components/auction/dutch-auction-price.tsx
Retrieves address via useAccount() and requires it before buyout; placeBid call now includes address.
Frontend: Vickrey Reveal Form
components/auction/vickrey-reveal-form.tsx
revealBid invocation updated to pass connected wallet address as the final argument.

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
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested labels

Typescript Lang

Suggested reviewers

  • ceilican
  • Zahnentferner

Poem

🐰 Hopped in the code with a twitch and a grin,
Read the chain first, then approve — no needless spin,
User address threaded through every call,
Less gas for the people, fewer failed falls,
I nibble the bugs and dance when tests win! 🥕

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change: optimizing approval flows by checking allowances before calling approve, directly addressing the core objective of eliminating redundant approval transactions.
Linked Issues check ✅ Passed The PR fully implements the requirements from issue #31: adds userAddress parameter, uses readContract to check allowance/approval state before transactions, and only calls approve when necessary, eliminating redundant on-chain operations.
Out of Scope Changes check ✅ Passed All changes are directly scoped to issue #31: interface updates to add userAddress, approval logic optimization across all auction services, and UI component updates to pass the address parameter. No unrelated modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@aniket866 aniket866 requested a review from DengreSarthak March 24, 2026 17:44

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between a954222 and 9eb3579.

📒 Files selected for processing (7)
  • lib/auction-service.ts
  • lib/services/allpay-auction-service.ts
  • lib/services/english-auction-service.ts
  • lib/services/exponential-dutch-auction-service.ts
  • lib/services/linear-dutch-auction-service.ts
  • lib/services/logarithmic-dutch-auction-service.ts
  • lib/services/vickrey-auction-service.ts

Comment thread lib/auction-service.ts
Comment thread lib/services/english-auction-service.ts Outdated
aniket866 and others added 2 commits March 24, 2026 23:30
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 | 🟡 Minor

Missing "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

📥 Commits

Reviewing files that changed from the base of the PR and between 9eb3579 and 708992c.

📒 Files selected for processing (5)
  • app/create/page.tsx
  • components/auction/bid-form.tsx
  • components/auction/dutch-auction-price.tsx
  • components/auction/vickrey-reveal-form.tsx
  • lib/services/english-auction-service.ts

@aniket866

Copy link
Copy Markdown
Contributor Author

@coderabbitai review and approve

@coderabbitai

coderabbitai Bot commented Mar 24, 2026

Copy link
Copy Markdown
Contributor

@aniket866 I'll kick off a full review of the PR right away! 🐇

Regarding approval — I'm not able to approve the PR through this comment. To request an approval, please post @coderabbitai approve as a top-level PR comment (not a reply), and I'll process it from there.

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize ERC-20/ERC-721 Approval Flow Allowance Checks and Eliminating Redundant approve

1 participant