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

2 stars 0 forks source link

Gas in `LaunchEvent.sol:pairBalance()`: `wavaxAllocated` should get cached #219

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Handle

Dravee

Vulnerability details

Impact

SLOADs are expensive (~100 gas) compared to MLOADs/MSTOREs (~3 gas). Minimizing them can save gas.

Proof of Concept

The code is as such (see @audit comments):

File: LaunchEvent.sol
580:     function pairBalance(address _user) public view returns (uint256) {
581:         UserInfo memory user = getUserInfo[_user];
582:         if (wavaxAllocated == 0 || user.hasWithdrawnPair) { //@audit wavaxAllocated SLOAD 1
583:             return 0;
584:         }
585:         return (user.balance * lpSupply) / wavaxAllocated / 2; //@audit wavaxAllocated SLOAD 2
586:     }

By caching this in memory and using it, it's possible to save 1 SLOAD

Tools Used

VS Code

Recommended Mitigation Steps

Cache the value in a variable