code-423n4 / 2022-01-trader-joe-findings

2 stars 0 forks source link

Reuse existing storage variables' cache can save gas #237

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

Handle

WatchPug

Vulnerability details

https://github.com/code-423n4/2022-01-trader-joe/blob/a1579f6453bc4bf9fb0db9c627beaa41135438ed/contracts/LaunchEvent.sol#L377-L435

function createPair() external isStopped(false) atPhase(Phase.PhaseThree) {
    (address wavaxAddress, address tokenAddress) = (
        address(WAVAX),
        address(token)
    );
    // ...
    if (
        floorPrice > (wavaxReserve * 10**token.decimals()) / tokenAllocated
    ) {
        tokenAllocated = (wavaxReserve * 10**token.decimals()) / floorPrice;
        // ...
    }

    WAVAX.approve(address(router), wavaxReserve);
    token.approve(address(router), tokenAllocated);
    // ...
}

Reusing the cached local variable instead of reading from storage again can save gas.

Read from the stack can save ~100 gas from each extra read (SLOAD after Berlin).

cryptofish7 commented 2 years ago

Duplicate of #236