code-423n4 / 2021-04-vader-findings

1 stars 0 forks source link

Gas Optimization: in Vader.sol, Turn Variables _1m, maxSupply, and decimals, into Constants/Immutable variables #194

Closed code423n4 closed 3 years ago

code423n4 commented 3 years ago

Handle

jvaqa

Vulnerability details

Impact

Gas Optimization: in Vader.sol, Turn Variables _1m, maxSupply, and decimals, into Constants/Immutable variables

Currently, in Vader.sol, the variables "_1m", "maxSupply", and "decimals", are kept mutable, incurring an SLOAD call on every invocation. However, the variable "_1m" is only ever used to refer to the constant value of 1 million weiDecimals, the variable maxSupply is only ever used to refer to 2 million weiDecimals, and decimals is only ever used to refer to 10 ** 18. If you make these variables constant, the compiler will inline those values into every call to _1m, maxSupply, and decimals, avoiding all SLOAD's in any calls that use them.

Proof of Concept

In Vader.sol's constructor, the following lines are used: decimals = 18; _1m = 10*6 10 * decimals; //1m maxSupply = 2 _1m;

Recommended Mitigation Steps

If you would like to set the value in the constructor rather than simply hardcoding it, then use the keyword "immutable" to set it once and never use an SLOAD again. Or, you can simply hardcode the value in the variable's declaration, and then use the "constant" keyword.

0xBrian commented 3 years ago

Most likely addressed in one of https://github.com/vetherasset/vaderprotocol-contracts/commit/66ce8d72bcd91544f5f1b0382d5e809aa6b71a44 https://github.com/vetherasset/vaderprotocol-contracts/commit/2dacdc3716706c8f635e7173b3fc5f1b42869a35

dmvt commented 3 years ago

duplicate of #286