code-423n4 / 2021-10-tempus-findings

0 stars 0 forks source link

Adding unchecked directive can save gas #30

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-10-tempus/blob/63f7639aad08f2bba717830ed81e0649f7fc23ee/contracts/TempusPool.sol#L375-L375

uint256 timeToMaturity = (maturityTime > currentTime) ? (maturityTime - currentTime) : 0;

maturityTime - currentTime will never underflow.

RedFox20 commented 2 years ago

Relatively minor because it really affects code readability for a very small gain. Nevertheless, I added the change in the exact suggested location.

In the future, it would help if you shared exact findings on the amount of gas saved, using gasleft()

estimatedYield unchecked gas: 62
estimatedYield checked gas: 174

It's not a big amount of gas saved, but nevertheless, gas saved 😃 👍🏻

Fixed in https://github.com/tempus-finance/tempus-protocol/pull/377