Yield Witch v2 contest - defsec's results

Fixed-rate borrowing and lending on Ethereum

General Information

Platform: Code4rena

Start Date: 14/07/2022

Pot Size: $25,000 USDC

Total HM: 2

Participants: 63

Period: 3 days

Judge: PierrickGT

Total Solo HM: 1

Id: 147

League: ETH

Yield

Findings Distribution

Researcher Performance

Rank: 48/63

Findings: 1

Award: $17.85

🌟 Selected for report: 0

🚀 Solo Findings: 0

[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.

ISSUE LIST

C4-001: Revert String Size Optimization [S]
C4-002 : Check if amount > 0 before token transfer can save gas [S]
C4-003 : Non-strict inequalities are cheaper than strict ones [M]
C4-004 : Use Custom Errors instead of Revert Strings to save Gas [S]
C4-005 : Use calldata instead of memory for function parameters [M]
C4-006 : > 0 can be replaced with != 0 for gas optimization [S]
C4-007 : Free gas savings for using solidity 0.8.15+ - [S]

C4-001: Revert String Size Optimization

Impact

Shortening revert strings to fit in 32 bytes will decrease deploy time gas and will decrease runtime gas when the revert condition has been met.

Revert strings that are longer than 32 bytes require at least one additional mstore, along with additional overhead for computing memory offset, etc.

Proof of Concept

Revert strings > 32 bytes are here:

https://github.com/code-423n4/2022-07-yield/blob/main/contracts/Witch.sol#L358

Tools Used

Manual Review

Shorten the revert strings to fit in 32 bytes. That will affect gas optimization.

C4-002 : Check if liquidatorCut > 0 before token transfer can save gas

Impact

Since liquidatorCut can be 0. Checking if (liquidatorCut != 0) before the transfer can potentially save an external call and the unnecessary gas cost of a 0 token transfer.

Proof of Concept

https://github.com/code-423n4/2022-07-yield/blob/main/contracts/Witch.sol#L324

All Contracts

Tools Used

None

Consider checking amount != 0.

C4-003 : Non-strict inequalities are cheaper than strict ones

Impact

Strict inequalities add a check of non equality which costs around 3 gas.

Proof of Concept

https://github.com/code-423n4/2022-07-yield/blob/main/contracts/Witch.sol#L162

Tools Used

Code Review

Use >= or <= instead of > and < when possible.

C4-004 : Use Custom Errors instead of Revert Strings to save Gas

Custom errors from Solidity 0.8.4 are cheaper than revert strings (cheaper deployment cost and runtime cost when the revert condition is met)

Source Custom Errors in Solidity:

Starting from Solidity v0.8.4, there is a convenient and gas-efficient way to explain to users why an operation failed through the use of custom errors. Until now, you could already use strings to give more information about failures (e.g., revert("Insufficient funds.");), but they are rather expensive, especially when it comes to deploy cost, and it is difficult to use dynamic information in them.

Custom errors are defined using the error statement, which can be used inside and outside of contracts (including interfaces and libraries).

Instances include:

https://github.com/code-423n4/2022-07-yield/blob/main/contracts/Witch.sol#L358

Tools Used

Code Review

Recommended to replace revert strings with custom errors.

C4-005 : Use calldata instead of memory for function parameters

Impact

In some cases, having function arguments in calldata instead of memory is more optimal.

Consider the following generic example:

contract C { function add(uint[] memory arr) external returns (uint sum) { uint length = arr.length; for (uint i = 0; i < arr.length; i++) { sum += arr[i]; } } }

In the above example, the dynamic array arr has the storage location memory. When the function gets called externally, the array values are kept in calldata and copied to memory during ABI decoding (using the opcode calldataload and mstore). And during the for loop, arr[i] accesses the value in memory using a mload. However, for the above example this is inefficient. Consider the following snippet instead:

contract C { function add(uint[] calldata arr) external returns (uint sum) { uint length = arr.length; for (uint i = 0; i < arr.length; i++) { sum += arr[i]; } } }

In the above snippet, instead of going via memory, the value is directly read from calldata using calldataload. That is, there are no intermediate memory operations that carries this value.

Gas savings: In the former example, the ABI decoding begins with copying value from calldata to memory in a for loop. Each iteration would cost at least 60 gas. In the latter example, this can be completely avoided. This will also reduce the number of instructions and therefore reduces the deploy time cost of the contract.

In short, use calldata instead of memory if the function argument is only read.

Note that in older Solidity versions, changing some function arguments from memory to calldata may cause "unimplemented feature error". This can be avoided by using a newer (0.8.*) Solidity compiler.

Proof of Concept

  1. Navigate to the following smart contract line.
https://github.com/code-423n4/2022-07-yield/blob/main/contracts/Witch.sol#L224

Tools Used

None

Some parameters in examples given above are later hashed. It may be beneficial for those parameters to be in memory rather than calldata.

C4-006 : > 0 can be replaced with != 0 for gas optimization

Impact

!= 0 is a cheaper operation compared to > 0, when dealing with uint.

Proof of Concept

  1. Navigate to the following contract sections.
  2022-07-yield/contracts/Witch.sol::255 => require(auction_.start > 0, "Vault not under auction");
  2022-07-yield/contracts/Witch.sol::300 => require(auction_.start > 0, "Vault not under auction");
  2022-07-yield/contracts/Witch.sol::358 => require(auction_.start > 0, "Vault not under auction");
  2022-07-yield/contracts/Witch.sol::393 => if (liquidatorCut > 0) {
  2022-07-yield/contracts/Witch.sol::398 => if (auctioneerCut > 0) {
  2022-07-yield/contracts/Witch.sol::416 => require(auction_.start > 0, "Vault not under auction");

Tools Used

None

Consider to replace > 0 with != 0 for gas optimization.

C4-007 : Free gas savings for using solidity 0.8.15+

Impact

Using newer compiler versions and the optimizer gives gas optimizations and additional safety checks are available for free.

Proof of Concept

All Contracts

Solidity 0.8.15 has a useful change which reduced gas costs of external calls which expect a return value: https://docs.soliditylang.org/en/v0.8.15/

Solidity 0.8.15 has some improvements too but not well tested.

Code Generator: Skip existence check for external contract if return data is expected. In this case, the ABI decoder will revert if the contract does not exist

All Contracts

Tools Used

None

Consider to upgrade pragma to at least 0.8.15.

AuditHub

A portfolio for auditors, a security profile for protocols, a hub for web3 security.

Built bymalatrax © 2024

Auditors

Browse

Contests

Browse

Get in touch

ContactTwitter