Skip to content

[BUG]: The Rebalance Pool should not be restricted to only auction owner and investigate and fix the console warnings related to fetchLastRebalanceEvent #61

@VishwajeetTulse

Description

@VishwajeetTulse

Bug Description

The "Rebalance Pool" section in the pool interaction page (/[pool]) has three distinct issues:

1. Incorrect Access Control (Public Function Restricted)
The rebalance() smart contract function is public, but the frontend blocks non-creators from using it.

  • Access Issue: The button is disabled for all wallets except the pool creator showing a "creator only" tooltip.
  • Logic Bug: Due to an operator precedence error (&& before ||), the disabled check fails for disconnected wallets, making the button incorrectly clickable when no wallet is connected.
  • Location: src/app/[pool]/InteractionClient.tsx ~line 1363

2. Stale Data After Rebalance
The pool data (reserves, prices, percentages) does not automatically update after a successful rebalance transaction. The page requires a manual reload to reflect the new state because refetchPool isn't triggered correctly on confirmation.

  • Location: src/app/[pool]/InteractionClient.tsx ~line 1065

3. False Console Warning
A warning (WARN: All approaches failed – No rebalance events found) appears in the console even after a successful rebalance. The event fetching logic fails to find the new event immediately, triggering a misleading warning despite the operation succeeding.

  • Location: src/app/[pool]/InteractionClient.tsx ~line 773

Proposed Fixes

Bug 1 — Remove incorrect creator-only restriction

Changes needed in src/app/[pool]/InteractionClient.tsx ~line 1363:

  1. Fix the disabled logic to allow any connected wallet to call rebalance:
// Current (buggy)
disabled={
  address !== pool?.vault_creator && address !== undefined ||
  isDistributeLoading
}

// Fixed
disabled={
  !isConnected || 
  !address || 
  isDistributeLoading ||
  isTransactionPending
}
  1. Remove the creator-only tooltip (~line 1374-1380):
    • Remove the entire conditional tooltip that says "This action can only be performed by the pool creator"
    • Or replace it with a generic tooltip explaining what rebalance does

Impact: Any connected wallet will be able to trigger rebalance, matching the smart contract's public external nonReentrant function signature.


Bug 2 — Ensure post-rebalance data refresh

Changes needed in src/app/[pool]/InteractionClient.tsx ~line 1065:

  1. Replace handlePoll with direct refetchPool call in the confirmation useEffect:
// Current
handlePoll();

// Fixed
await refetchPool?.();
  1. Optionally add a slight delay to ensure blockchain state propagates:
setTimeout(async () => {
  await refetchPool?.();
  logger.debug('Pool data refreshed after rebalance confirmation');
}, 1000);
  1. Alternative approach: Invalidate and refetch ALL contract reads related to the pool (reserves, prices, balances) to ensure complete UI update.

Impact: Pool data (reserves, percentages, token prices) will automatically update after a successful rebalance without requiring a manual page reload.


Bug 3 — Downgrade false warning to debug level

Changes needed in src/app/[pool]/InteractionClient.tsx ~line 773:

Replace:

logger.warn('All approaches failed - No rebalance events found for pool:', { poolId });

With:

logger.debug('No rebalance events found in blockchain history - this is expected for new pools or if RPC indexing is delayed. Using localStorage fallback.', { poolId });

Rationale:

  • lastRebalanceTime is persisted in localStorage immediately when rebalance is called, making it the source of truth
  • Event fetching is supplementary verification, not critical functionality
  • For newly created pools that have never been rebalanced, zero events is expected behavior
  • RPC nodes may have indexing delays after a fresh rebalance
  • The warn level incorrectly suggests a problem when the system is working as designed

Impact: Removes misleading console warnings that appear even when the rebalance feature is functioning correctly.


Logs and Screenshots

Bug 1 (Creator-only restriction):
Image


Environment Details

  • Files affected:
    • src/app/[pool]/InteractionClient.tsx (lines ~773, ~1065, ~1363)
  • Framework: Next.js (React)
  • Smart Contract: PredictionPool ABI (rebalance() is external nonReentrant)
  • Blockchain interaction: wagmi hooks (useWriteContract, useWaitForTransactionReceipt, useReadContracts)
  • Browser: Chrome 120+ / Firefox / Safari (any modern browser)
  • OS: Windows 11 / macOS / Linux
  • Node.js: v18+ (as per project requirements)

Impact

Medium - Feature works but has issues

Code of Conduct

  • I have joined the Discord server and will post updates there
  • I have searched existing issues to avoid duplicates

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions