code-423n4 / 2022-02-pooltogether-findings

0 stars 0 forks source link

Gas Optimizations #31

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

[S]: Suggested optimation, save a decent amount of gas without compromising readability;

[M]: Minor optimation, the amount of gas saved is minor, change when you see fit;

[N]: Non-preferred, the amount of gas saved is at cost of readability, only apply when gas saving is a top priority.

[S] Move lockUntil from Delegation to TWABDelegator can save gas from external call

The lockUntil of a Delegation is currently stored on the Delegation contract and usually used by the TWABDelegator contract through an external call (_delegation.lockUntil()).

https://github.com/pooltogether/v4-twab-delegator/blob/21bb53b2ea54a248bbd1d3170dbadd3a0c83d874/contracts/TWABDelegator.sol#L624-L626

  function _requireDelegationUnlocked(Delegation _delegation) internal view {
    require(block.timestamp >= _delegation.lockUntil(), "TWABDelegator/delegation-locked");
  }

In comparison to storing the lockUntil on TWABDelegator contract, this creates an overhead of one external call.

[M] Use custom error may save gas

https://github.com/pooltogether/v4-twab-delegator/blob/21bb53b2ea54a248bbd1d3170dbadd3a0c83d874/contracts/Delegation.sol#L30-L30

[M] ++i is more gas efficient than i++

https://github.com/pooltogether/v4-twab-delegator/blob/21bb53b2ea54a248bbd1d3170dbadd3a0c83d874/contracts/Delegation.sol#L42-L42

[N] "> 0" is less efficient than "!= 0" for unsigned integers

https://github.com/pooltogether/v4-twab-delegator/blob/21bb53b2ea54a248bbd1d3170dbadd3a0c83d874/contracts/TWABDelegator.sol#L601-L601

PierrickGT commented 2 years ago

[S] Move lockUntil from Delegation to TWABDelegator can save gas from external call

The lockUntil value is specific to each delegation so it can't live in the TWABDelegator contract.

[M] Use custom error may save gas

Duplicate of https://github.com/code-423n4/2022-02-pooltogether-findings/issues/30

For the other issues, duplicate of https://github.com/code-423n4/2022-02-pooltogether-findings/issues/15