Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD-01 MitigationConfirmed #16

Open
c4-bot-5 opened this issue Apr 10, 2024 · 2 comments
Open

ADD-01 MitigationConfirmed #16

c4-bot-5 opened this issue Apr 10, 2024 · 2 comments
Labels
confirmed for report This issue is confirmed for report mitigation-confirmed MR-ADD-01 satisfactory satisfies C4 submission criteria; eligible for awards

Comments

@c4-bot-5
Copy link
Contributor

Lines of code

Vulnerability details

Issue Report

ADD-01: Gas Fixes 1

Details

Issue#195

Issue#137

The following gas optimizations were identified for integration to the codebase to save gas:

[G-02] Optimization Proposal for Withdraw Function in MagicSpend Contract

[G-04] Check withdrawAmount - maxCost for greater than 0 if 0 it can save 1 Gsreset (~2900 Gas) and 1 Gcoldsload (Saves ~5000 Gas)

[G-06] Switch the order of if statement to fail early saves 1 function call half of the times where function have SLOAD (Saves ~2100 Gas Half of the times)

Mitigation

PR#18

  • G-02: Validations are now reordered in the withdraw function to check signature fisrt and fail early before the _validateRequest gas intensive call which saves more gas.

Loc:

function withdraw(WithdrawRequest memory withdrawRequest) external {
        if (block.timestamp > withdrawRequest.expiry) {
            revert Expired();
        }

        if (!isValidWithdrawSignature(msg.sender, withdrawRequest)) {
            revert InvalidSignature();
        }
        
        _validateRequest(msg.sender, withdrawRequest);

        // reserve funds for gas, will credit user with difference in post op
        _withdraw(withdrawRequest.asset, msg.sender, withdrawRequest.amount);
    }
  • G-04: Additional check was added to function in light of G-04 that checks if withdrawAmt is greater than maxCost before updating mapping. This indeed saves gas.

Loc:

if (withdrawAmount > maxCost) {
            _withdrawable[userOp.sender] += withdrawAmount - maxCost;
        }
  • G-06: if statements reorganized which saves gas when calling this function.

Loc:

function withdraw(WithdrawRequest memory withdrawRequest) external {
        _validateRequest(msg.sender, withdrawRequest);

        if (block.timestamp > withdrawRequest.expiry) {
            revert Expired();
        }

        if (!isValidWithdrawSignature(msg.sender, withdrawRequest)) {
            revert InvalidSignature();
        }

        // reserve funds for gas, will credit user with difference in post op
        _withdraw(withdrawRequest.asset, msg.sender, withdrawRequest.amount);
    }

Conclusion

G-02, G-04, G-06 optimizations implemented successfully.

@c4-judge
Copy link

3docSec marked the issue as confirmed for report

@c4-judge c4-judge added confirmed for report This issue is confirmed for report satisfactory satisfies C4 submission criteria; eligible for awards labels Apr 11, 2024
@c4-judge
Copy link

3docSec marked the issue as satisfactory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed for report This issue is confirmed for report mitigation-confirmed MR-ADD-01 satisfactory satisfies C4 submission criteria; eligible for awards
Projects
None yet
Development

No branches or pull requests

2 participants