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 savings by removing state variable baseline in Vader.sol #153

Open
code423n4 opened this issue Apr 27, 2021 · 0 comments
Open

Gas savings by removing state variable baseline in Vader.sol #153

code423n4 opened this issue Apr 27, 2021 · 0 comments
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Collaborator

Handle

0xRajeev

Vulnerability details

Impact

From Solidity’s documentation (https://docs.soliditylang.org/en/v0.8.4/contracts.html#constant-and-immutable-state-variables), “State variables can be declared as constant or immutable. In both cases, the variables cannot be modified after the contract has been constructed. For constant variables, the value has to be fixed at compile-time, while for immutable, it can still be assigned at construction time. The compiler does not reserve a storage slot for these variables, and every occurrence is replaced by the respective value. Compared to regular state variables, the gas costs of constant and immutable variables are much lower.”

State variable baseline is initialized the value of _1m in constructor and then is never modified. Replacing its use directly by a constant_1m avoids a storage slot and associated SLOADs (2600 gas) leading to gas savings.

Proof of Concept

https://github.com/code-423n4/2021-04-vader/blob/3041f20c920821b89d01f652867d5207d18c8703/vader-protocol/contracts/Vader.sol#L25

https://github.com/code-423n4/2021-04-vader/blob/3041f20c920821b89d01f652867d5207d18c8703/vader-protocol/contracts/Vader.sol#L63

https://github.com/code-423n4/2021-04-vader/blob/3041f20c920821b89d01f652867d5207d18c8703/vader-protocol/contracts/Vader.sol#L218-L219

Tools Used

Manual Analysis

Recommended Mitigation Steps

Remove baseline state variable and replace its use directly by a constant_1m

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Apr 27, 2021
code423n4 added a commit that referenced this issue Apr 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization)
Projects
None yet
Development

No branches or pull requests

1 participant