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

Cache basketAsERC20.totalSupply() #88

Open
code423n4 opened this issue Oct 10, 2021 · 2 comments
Open

Cache basketAsERC20.totalSupply() #88

code423n4 opened this issue Oct 10, 2021 · 2 comments
Labels
bug Warden finding G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@code423n4
Copy link
Contributor

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.

code423n4 added a commit that referenced this issue Oct 10, 2021
@frank-beard frank-beard added the sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") label Nov 6, 2021
@GalloDaSballo
Copy link
Collaborator

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

@GalloDaSballo
Copy link
Collaborator

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Warden finding G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

3 participants