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

0 stars 0 forks source link

Adding unchecked directive can save gas #208

Closed code423n4 closed 3 years ago

code423n4 commented 3 years ago

Handle

WatchPug

Vulnerability details

For the arithmetic operations that will never over/underflow, using the unchecked directive (Solidity v0.8 has default overflow/underflow checks) can save some gas from the unnecessary internal over/underflow checks.

For example:

https://github.com/code-423n4/2021-11-vader/blob/429970427b4dc65e37808d7116b9de27e395ce0c/contracts/dex/math/VaderMath.sol#L92-L92

if (originalValue > releasedValue) loss = originalValue - releasedValue;

loss = originalValue - releasedValue will never underflow.

https://github.com/code-423n4/2021-11-vader/blob/429970427b4dc65e37808d7116b9de27e395ce0c/contracts/tokens/Vader.sol#L258-L262

uint256 fee = calculateFee();

uint256 tax = (amount * fee) / _MAX_BASIS_POINTS;

amount -= tax;

amount -= tax will never underflow.

SamSteinGG commented 3 years ago

Duplicate of #43