20,000 USDC
View results
Submission Details
Severity: gas
Valid

Use smaller data types for `lenderFee` and `borrowerFee` for better storage packing

Summary

lenderFee and borrowerFee can be packed into smaller data types to save gas.

Vulnerability Details

The current storage layout for Lender.sol is as follow:

uint256 public lenderFee = 1000; // slot 1
uint256 public borrowerFee = 50; // slot 2
address public feeReceiver; // slot 3

All of these slots are initialized to non-zero during construction, incurring 21000 gas each for using a Gsset operation (for 63000 gas in total).

Note that lenderFee and borrowerFee cannot exceed and , respectively. Therefore we can use smaller data types to pack all of those values into one slot.

// all these takes a single slot
uint48 public lenderFee = 1000;
uint48 public borrowerFee = 50;
address public feeReceiver;

This converts two Gsset (21000 gas) into two Gsreset (2900 gas), for a total of 34200 gas per lendings created.

Impact

Saves 34200 gas per lendings created.

Tools Used

Manual review

Recommendations

Use the recommended storage packing.

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.