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

Gas Optimizations #191

Open
code423n4 opened this issue Jul 4, 2022 · 0 comments
Open

Gas Optimizations #191

code423n4 opened this issue Jul 4, 2022 · 0 comments

Comments

@code423n4
Copy link
Contributor

Gas Report

  1. Removing the abi.encodePacked() from constant typehash encoding when the hashing is done on a single string saves minor gas and removes redundant code. See here and here.

  2. Removing the redundant owner check in the exercise() function in the PuttyV2 contract (this is already validated in the call to transferFrom()) saves gas.

  3. Like the point above, removing the redundant owner check in the withdraw() function in the PuttyV2 contract saves gas.

  4. In the withdraw() function in the PuttyV2 contract, refactoring the fee stack variable to only be created if needed saves gas. This comes at the cost of a minor readability degradation though.

    Here's the change:

        // Before:
        if ((order.isCall && isExercised) || (!order.isCall && !isExercised)) {
            // send the fee to the admin/DAO if fee is greater than 0%
            uint256 feeAmount = 0;
            if (fee > 0) {
                feeAmount = (order.strike * fee) / 1000;
                ERC20(order.baseAsset).safeTransfer(owner(), feeAmount);
            }
    
            ERC20(order.baseAsset).safeTransfer(msg.sender, order.strike - feeAmount);
    
            return;
        }
    
        // After:
        if ((order.isCall && isExercised) || (!order.isCall && !isExercised)) {
            // send the fee to the admin/DAO if fee is greater than 0%
            if (fee > 0) {
                uint256 feeAmount = (order.strike * fee) / 1000;
                ERC20(order.baseAsset).safeTransfer(owner(), feeAmount);
                ERC20(order.baseAsset).safeTransfer(msg.sender, order.strike - feeAmount);
            } else {
                ERC20(order.baseAsset).safeTransfer(msg.sender, order.strike);
            }
    
            return;
        }
    
code423n4 added a commit that referenced this issue Jul 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant