code-423n4 / 2022-07-swivel-findings

0 stars 1 forks source link

Gas Optimizations #98

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

1 visibility

https://github.com/code-423n4/2022-07-swivel/blob/fd36ce96b46943026cb2dfcb76dfa3f884f51c18/Swivel/Swivel.sol#L25-L27

change visibility from public to private or internal can save gas. so i recommend to change it.

2 use storage instead of memory

https://github.com/code-423n4/2022-07-swivel/blob/fd36ce96b46943026cb2dfcb76dfa3f884f51c18/Swivel/Swivel.sol#L248

https://github.com/code-423n4/2022-07-swivel/blob/fd36ce96b46943026cb2dfcb76dfa3f884f51c18/Marketplace/MarketPlace.sol#L91

https://github.com/code-423n4/2022-07-swivel/blob/fd36ce96b46943026cb2dfcb76dfa3f884f51c18/Marketplace/MarketPlace.sol#L116

https://github.com/code-423n4/2022-07-swivel/blob/fd36ce96b46943026cb2dfcb76dfa3f884f51c18/Marketplace/MarketPlace.sol#L132

https://github.com/code-423n4/2022-07-swivel/blob/fd36ce96b46943026cb2dfcb76dfa3f884f51c18/Marketplace/MarketPlace.sol#L149

https://github.com/code-423n4/2022-07-swivel/blob/fd36ce96b46943026cb2dfcb76dfa3f884f51c18/Marketplace/MarketPlace.sol#L177

https://github.com/code-423n4/2022-07-swivel/blob/fd36ce96b46943026cb2dfcb76dfa3f884f51c18/Marketplace/MarketPlace.sol#L216

https://github.com/code-423n4/2022-07-swivel/blob/fd36ce96b46943026cb2dfcb76dfa3f884f51c18/Marketplace/MarketPlace.sol#L228

https://github.com/code-423n4/2022-07-swivel/blob/fd36ce96b46943026cb2dfcb76dfa3f884f51c18/Marketplace/MarketPlace.sol#L248

https://github.com/code-423n4/2022-07-swivel/blob/fd36ce96b46943026cb2dfcb76dfa3f884f51c18/Marketplace/MarketPlace.sol#L266

https://github.com/code-423n4/2022-07-swivel/blob/fd36ce96b46943026cb2dfcb76dfa3f884f51c18/Marketplace/MarketPlace.sol#L284

https://github.com/code-423n4/2022-07-swivel/blob/fd36ce96b46943026cb2dfcb76dfa3f884f51c18/VaultTracker/VaultTracker.sol#L52

https://github.com/code-423n4/2022-07-swivel/blob/fd36ce96b46943026cb2dfcb76dfa3f884f51c18/VaultTracker/VaultTracker.sol#L115

https://github.com/code-423n4/2022-07-swivel/blob/fd36ce96b46943026cb2dfcb76dfa3f884f51c18/VaultTracker/VaultTracker.sol#L155-L156

Use storage instead of memory to reduce the gas fee. i suggest to change from e.g

  Hash.Order memory order = o[i];

to

  Hash.Order storage order = o[i];

apply to others.

3 use calldata instead memory

https://github.com/code-423n4/2022-07-swivel/blob/fd36ce96b46943026cb2dfcb76dfa3f884f51c18/Swivel/Swivel.sol#L495

https://github.com/code-423n4/2022-07-swivel/blob/fd36ce96b46943026cb2dfcb76dfa3f884f51c18/Marketplace/MarketPlace.sol#L64-L69

https://github.com/code-423n4/2022-07-swivel/blob/fd36ce96b46943026cb2dfcb76dfa3f884f51c18/Creator/Creator.sol#L36-L7

In the external functions where the function argument is read-only, the function() has an inputed parameter that using memory, if this function didnt change the parameter, its cheaper to use calldata then memory. so we suggest to change it. e.g

  function setFee(uint16[] memory i, uint16[] memory d) external authorized(admin) returns (bool) {

to

  function setFee(uint16[] calldata i, uint16[] calldata d) external authorized(admin) returns (bool) {

apply to others.

4 remove max

https://github.com/code-423n4/2022-07-swivel/blob/fd36ce96b46943026cb2dfcb76dfa3f884f51c18/Swivel/Swivel.sol#L549-L562

And add type(uint256).max change max

uint256 max = 2**256 - 1;  // remove this
uint256 when;

for (uint256 i; i < len;) {
  when = approvals[u[i]];

  if (when == 0) { revert Exception(16, 0, 0, address(0), address(0)); }

  if (block.timestamp < when) { revert Exception(17, block.timestamp, when, address(0), address(0));
  }

  approvals[u[i]] = 0;
  IErc20 uToken = IErc20(u[i]);
  Safe.approve(uToken, c[i], max); //change max

To

uint256 when;

for (uint256 i; i < len;) {
  when = approvals[u[i]];

  if (when == 0) { revert Exception(16, 0, 0, address(0), address(0)); }

  if (block.timestamp < when) { revert Exception(17, block.timestamp, when, address(0), address(0));
  }

  approvals[u[i]] = 0;
  IErc20 uToken = IErc20(u[i]);
  Safe.approve(uToken, c[i], type(uint256).max);