Skip to content

feat: implement pull-based refund model for gas safety - #123

Open
ChukwuemekaP1 wants to merge 2 commits into
PhasoraLabs:mainfrom
ChukwuemekaP1:feat/pull-based-refund-model
Open

feat: implement pull-based refund model for gas safety#123
ChukwuemekaP1 wants to merge 2 commits into
PhasoraLabs:mainfrom
ChukwuemekaP1:feat/pull-based-refund-model

Conversation

@ChukwuemekaP1

Copy link
Copy Markdown

Closes #66
🎯 Overview
This PR implements a pull-based refund model to replace the gas-intensive push-based approach in grant cancellation. The previous implementation looped through all funders during cancellation, causing potential gas/memory issues with large numbers of funders.
✨ Key Changes

  1. Core Implementation
    lib.rs: Refactored cancel_grant() to remove O(n) funder loop; added new refund_claim() function
    storage.rs: Added RefundClaimed storage key for tracking individual funder claims
    types.rs: Added GrantNotCancelled and RefundAlreadyClaimed error types
  2. How It Works
    Before: cancel_grant() automatically distributed refunds to all funders (push model) ❌
    After: cancel_grant() marks grant as cancelled; funders manually claim via refund_claim() ✅
  3. Security Features
    ✅ Double-claim prevention via persistent storage flag
    ✅ Authorization checks (only actual funders can claim)
    ✅ Checked arithmetic for overflow protection
    ✅ Reentrancy guard protection
    ✅ Follows Checks → Effects → Interactions pattern
  4. Benefits
    🚀 Gas Efficiency: Cancellation is now O(1) instead of O(n)
    💾 Memory Safety: No loading entire funder list during cancellation
    ♾️ Scalability: Supports unlimited funders without gas concerns
    🔐 Unclaimed Funds: Remain locked in contract indefinitely (intentional design)
    🧪 Testing
    68 tests passing
    Comprehensive test coverage for:
    Single/multiple funder claims
    Failure cases (before cancellation, non-funder, double attempts)
    Edge cases (zero balance, partial claims, unclaimed funds)
    Regression tests for existing functionality

- Add GrantNotCancelled and RefundAlreadyClaimed error types
- Add RefundClaimed storage key for tracking funder claims
- Refactor cancel_grant to remove O(n) funder loop
- Add refund_claim function for manual funder claims
- Implement double-claim prevention via persistent storage
- Add comprehensive inline documentation
- Follow Checks → Effects → Interactions pattern
- Ensure reentrancy protection and checked arithmetic

Fixes gas/memory issues from looping over funders during cancellation.
@drips-wave

drips-wave Bot commented Mar 28, 2026

Copy link
Copy Markdown

@ChukwuemekaP1 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Samuel1505

Samuel1505 commented Mar 28, 2026

Copy link
Copy Markdown
Contributor

@ChukwuemekaP1 kindly fix workflow

@ChukwuemekaP1

Copy link
Copy Markdown
Author

Alright
On it @Samuel1505

@Samuel1505

Copy link
Copy Markdown
Contributor

@ChukwuemekaP1 resolve conflicts

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.

Fix Gas Limit Issue in grant_cancel with Many Funders

2 participants