Platform: Code4rena
Start Date: 27/01/2022
Pot Size: $75,000 USDT
Total HM: 6
Participants: 29
Period: 7 days
Judge: leastwood
Total Solo HM: 6
Id: 72
League: ETH
Rank: 17/29
Findings: 2
Award: $168.32
🌟 Selected for report: 2
🚀 Solo Findings: 0
0x1f8b
Gas saving.
Without changing too much it's possible to save gas, in the for loops the i
variable is increased using i++, it will use less opcodes if use ++i.
Affected places:
Manual review.
Change i++ to ++i;
#0 - ColaM12
2022-02-01T13:53:35Z
Duplicate to #13
47.0985 USDT - $47.10
0x1f8b
Gas saving.
Moving the uint32 dexDetail
close to address depositErc20
it's possible to save one slot:
struct TradeVars { uint depositValue; IERC20 depositErc20; uint32 dexDetail; // moved uint fees; uint depositAfterFees; uint tradeSize; uint newHeld; uint borrowValue; uint token0Price; uint totalHeld; }
Moving the uint32 dexDetail;
close to bool isPartialClose
it's possible to save one slot:
struct CloseTradeVars { uint16 marketId; bool longToken; bool depositToken; uint closeRatio; bool isPartialClose; uint32 dexDetail; // moved uint closeAmountAfterFees; uint borrowed; uint repayAmount; uint depositDecrease; uint depositReturn; uint sellAmount; uint receiveAmount; uint token0Price; uint fees; }
Moving marginLimit
close to LiqStatus
it's possible to save one storage slot.
struct LiqVars { LiqStatus status; uint32 marginLimit; // moved uint lastUpdateTime; uint currentMarginRatio; uint cAvgMarginRatio; uint hAvgMarginRatio; }
Moving isSellAllHeld
and dexDetail
it's possible to save 2 slots
struct LiquidateVars {// A variable holder for liquidation process uint16 marketId; bool longToken; bool isSellAllHeld; // moved uint32 dexDetail; // moved uint borrowed; uint fees; uint penalty; uint remainAmountAfterFees; uint depositDecrease; uint depositReturn; uint sellAmount; uint receiveAmount; uint token0Price; uint outstandingAmount; uint finalRepayAmount; }
Moving multiplier
and multiplier
it's possible to save 2 slots
struct MarginRatioVars { address heldToken; address sellToken; address owner; uint16 multiplier; // moved uint8 decimals; // moved uint held; bytes dexData; uint price; uint cAvgPrice; uint hAvgPrice; uint lastUpdateTime; }
Manual review.
Apply the mentioned changes
🌟 Selected for report: 0x1f8b
104.6634 USDT - $104.66
0x1f8b
Gas saving.
The logic around setImplementation
store the previous implementation in order to emit the event, but it's possible to save the variable oldImplementation and the storage read for it if the contract emit the event at the begining of the method
The code looks like:
function setImplementation(address implementation_) public override onlyAdmin { address oldImplementation = implementation; implementation = implementation_; emit NewImplementation(oldImplementation, implementation); }
And it could be like:
function setImplementation(address implementation_) public override onlyAdmin { emit NewImplementation(implementation, implementation_); implementation = implementation_; }
Affected contracts:
Manual review.
Apply the mentioned changes.
10.3004 USDT - $10.30
0x1f8b
Gas saving.
It's possible to avoid storage access a save gas using immutable
keyword for the following variables:
Affected variables:
Manual review.
Use immutable keyword
#0 - ColaM12
2022-02-01T13:55:07Z
Duplicate to #11