code-423n4 / 2021-10-defiprotocol-findings

0 stars 0 forks source link

Cache basketAsERC20.totalSupply() #88

Open code423n4 opened 3 years ago

code423n4 commented 3 years ago

Handle

pauliax

Vulnerability details

Impact

Here basketAsERC20.totalSupply() does not change inside the loop so it can be called outside the loop to avoid multiple duplicate external calls: uint256 tokensNeeded = basketAsERC20.totalSupply() pendingWeights[i] newRatio / BASE / BASE;

Recommended Mitigation Steps

Cache basketAsERC20.totalSupply() in a temporary variable and re-use it.

GalloDaSballo commented 2 years ago

Agree with the finding, this should save at least 2000 gas as you're reading from storage

GalloDaSballo commented 2 years ago

Actually: the first read will always cost 2100 - cannot be reduced But the second one, which would cost 100 (hot storage read ) would cost just 6 gas (3 to store, 3 to read)