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

1 stars 0 forks source link

Adding unchecked directive can save gas #135

Open code423n4 opened 2 years ago

code423n4 commented 2 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-09-defiProtocol/blob/main/contracts/contracts/Auction.sol#L135

uint256 id = _bounties.length - 1;

Can be changed to:

uint256 id;
unchecked { id = _bounties.length - 1; }
GalloDaSballo commented 2 years ago

When using Solidity with version >= 0.8.X you can use unchecked to save about 30 / 40 gas, at the cost of readability