Platform: Code4rena
Start Date: 29/06/2022
Pot Size: $50,000 USDC
Total HM: 20
Participants: 133
Period: 5 days
Judge: hickuphh3
Total Solo HM: 1
Id: 142
League: ETH
Rank: 57/133
Findings: 2
Award: $75.27
π Selected for report: 0
π Solo Findings: 0
π Selected for report: xiaoming90
Also found by: 0x1f8b, 0x29A, 0x52, 0xDjango, 0xNazgul, 0xNineDec, 0xSolus, 0xf15ers, 0xsanson, AmitN, Bnke0x0, BowTiedWardens, Chom, David_, ElKu, Funen, GalloDaSballo, GimelSec, Hawkeye, IllIllI, JC, JohnSmith, Kaiziron, Kenshin, Lambda, Limbooo, MadWookie, Metatron, MiloTruck, Nethermind, Picodes, ReyAdmirado, Sneakyninja0129, StErMi, TomJ, Treasure-Seeker, TrungOre, Waze, Yiko, _Adam, __141345__, antonttc, async, aysha, catchup, cccz, cryptphi, csanuragjain, danb, datapunk, defsec, delfin454000, dirk_y, doddle0x, durianSausage, exd0tpy, fatherOfBlocks, gogo, hake, hansfriese, horsefacts, hubble, itsmeSTYJ, joestakey, oyc_109, pedroais, peritoflores, rajatbeladiya, reassor, robee, rokinot, samruna, saneryee, sashik_eth, shenwilly, shung, simon135, sseefried, unforgiven, zer0dot, zzzitron
52.2103 USDC - $52.21
Severity: Low
Context: PuttyV2Nft.sol#L21-L37
Description:
One can transferFrom()
to themselves to emit the Transfer
event and will confuse off-chain monitoring griefing the system via event spamming.
Recommendation: Consider adding a check to prevent users from transferring to themselves.
Severity: Low
Context: PuttyV2.sol#L228-L246
Description: Setter functions are missing checks to validate if the new value being set is the same as the current value already set in the contract. Such checks will showcase mismatches between on-chain and off-chain states.
Recommendation: This may hinder detecting discrepancies between on-chain and off-chain states leading to flawed assumptions of on-chain state and protocol behavior.
Severity: Informational
Context: PuttyV2.sol
Description:
PuttyV2Nft.sol
imports solmate/tokens/ERC721.sol && openzeppelin/utils/Strings.sol
and is then imported by PuttyV2.sol
which also imports solmate/tokens/ERC721.sol && openzeppelin/utils/Strings.sol
.
Recommendation: This is unnecessary and the import can be removed for better readability.
Severity: Informational
Context: PuttyV2Nft.sol
Description: Some functions are missing @notice/@dev NatSpec comments for the function, @param for all/some of their parameters and @return for return values. Given that NatSpec is an important part of code documentation, this affects code comprehension, auditability and usability.
Recommendation: Add in full NatSpec comments for all functions to have complete code documentation for future use.
Severity: Informational
Context: PuttyV2.sol#L260 (offchain => off-chain)
, PuttyV2.sol#L751 (seperator => separator)
Description: Spelling errors in comments can cause confusion to both users and developers.
Recommendation: Check all misspellings to ensure they are corrected.
π Selected for report: GalloDaSballo
Also found by: 0v3rf10w, 0x1f8b, 0xA5DF, 0xDjango, 0xHarry, 0xKitsune, 0xNazgul, 0xNineDec, 0xc0ffEE, 0xf15ers, 0xkatana, 0xsanson, ACai, Aymen0909, Bnke0x0, BowTiedWardens, Chom, ElKu, Fitraldys, Funen, Haruxe, Hawkeye, IllIllI, JC, JohnSmith, Kaiziron, Kenshin, Lambda, Limbooo, MadWookie, Metatron, MiloTruck, Picodes, PwnedNoMore, Randyyy, RedOneN, ReyAdmirado, Ruhum, Sm4rty, StErMi, StyxRave, TerrierLover, TomJ, Tomio, UnusualTurtle, Waze, Yiko, _Adam, __141345__, ajtra, ak1, apostle0x01, asutorufos, c3phas, cRat1st0s, catchup, codetilda, cryptphi, datapunk, defsec, delfin454000, durianSausage, exd0tpy, fatherOfBlocks, gogo, grrwahrr, hake, hansfriese, horsefacts, ignacio, jayfromthe13th, joestakey, ladboy233, m_Rassska, mektigboy, minhquanym, mrpathfindr, natzuu, oyc_109, rajatbeladiya, reassor, rfa, robee, rokinot, sach1r0, saian, sashik_eth, simon135, slywaters, swit, z3s, zeesaw, zer0dot
23.0597 USDC - $23.06
feeAmount
in safeTransfer()
Context: PuttyV2.sol#L497-L501
Description:
Currently the contracts does the calculation of in a local variable like so feeAmount = (order.strike * fee) / 1000;
and then uses feeAmount
in safeTransfer()
by subtracting it from order.strike
.
Recommendation:
Instead of setting it to a local variable, simply doing this calculation in safeTransfer()
will save on gas.
calldata
Instead of memory
For Function ParametersContext: PuttyV2.sol#L271
, PuttyV2.sol#L669
Description: 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.
Recommendation:
Use calldata
instead of memory
for function parameters to avoid using memory with array values whena function is getting called externally.
Context: PuttyV2.sol#L389
, PuttyV2.sol#L466
, PuttyV2.sol#L550
, PuttyV2.sol#L577
, PuttyV2.sol#L753
, PuttyV2.sol#L764
, PuttyV2Nft.sol#L40
Description: Several functions across multiple contracts have a public visibility and can be marked with external visibility to save gas.
Recommendation: Change the functions visibility to external to save gas.
Context: PuttyV2.sol#L466-L520 (fee)
Description:
Functions that read state variables more than once can cach it into a local variable for repeated reads saving gas by converting expensive SLOAD
s into much cheaper MLOAD
s.
Recommendation: It's best to cach the state variables into memory when read more than once.
require()
, Use != 0
Instead of > 0
With Uint ValuesContext: PuttyV2.sol#L293
, PuttyV2.sol#L598-L599
Description:
In a require, when checking a uint, using != 0
instead of > 0
saves 6 gas. This will jump over or avoid an extra ISZERO
opcode.
Recommendation:
Use != 0
instead of > 0
with uint values but only in require()
statements.
Context: PuttyV2.sol#L546-L559
, PuttyV2.sol#L593-L603
, PuttyV2.sol#L610-L614
, PuttyV2.sol#L622-L630
, PuttyV2.sol#L636-L640
, PuttyV2.sol#L646-L650
, PuttyV2.sol#L657-L661
, PuttyV2.sol#L669-L675
, PuttyV2.sol#L727-L734
, PuttyV2.sol#L741-L748
Description:
(This is only relevant if you are using the default solidity checked arithmetic). i++
involves checked arithmetic, which is not required. This is because the value of i
is always strictly less than length <= 2**256 - 1. Therefore, the theoretical maximum value of i
to enter the for-loop body is 2**256 - 2
. This means that the i++
in the for loop can never overflow. Regardless, the overflow checks are performed by the compiler.
Unfortunately, the Solidity optimizer is not smart enough to detect this and remove the checks. One can manually do this by:
for (uint i = 0; i < length; i = unchecked_inc(i)) { // do something that doesn't change the value of i } function unchecked_inc(uint i) returns (uint) { unchecked { return i + 1; } }
Note that itβs important that the call to unchecked_inc
is inlined. This is only possible for solidity versions starting from 0.8.2
.
Recommendation: The increment in the for loop post condition can be made unchecked.
Context: PuttyV2.sol#L546-L559
, PuttyV2.sol#L593-L603
, PuttyV2.sol#L610-L614
, PuttyV2.sol#L622-L630
, PuttyV2.sol#L636-L640
, PuttyV2.sol#L646-L650
, PuttyV2.sol#L657-L661
, PuttyV2.sol#L669-L675
, PuttyV2.sol#L727-L734
, PuttyV2.sol#L741-L748
Description: One can save gas by caching the array length (in stack) and using that set variable in the loop. Replace state variable reads and writes within loops with local variable reads and writes. This is done by assigning state variable values to new local variables, reading and/or writing the local variables in a loop, then after the loop assigning any changed local variables to their equivalent state variables.
Recommendation:
Simply do something like so before the for loop: uint length = variable.length
. Then add length
in place of variable.length
in the for loop.
Context: All Contracts
Description: To save some gas the use of custom errors leads to cheaper deploy time cost and run time cost. The run time cost is only relevant when the revert condition is met.
Recommendation: Use Custom Errors instead of strings.
Context: All Contracts
Description:
You can cut out 10 opcodes in the creation-time EVM bytecode if you declare a constructor payable. Making the constructor payable eliminates the need for an initial check of msg.value == 0
and saves 21 gas on deployment with no security risks.
Recommendation: Set the constructor to payable.
Context: All Contracts
Description:
Contracts most called functions could simply save gas by function ordering via Method ID. Calling a function at runtime will be cheaper if the function is positioned earlier in the order (has a relatively lower Method ID) because 22 gas are added to the cost of a function for every position that came before it. The caller can save on gas if you prioritize most called functions. One could use This tool
to help find alternative function names with lower Method IDs while keeping the original name intact.
Recommendation:
Find a lower method ID name for the most called functions for example mostCalled()
vs. mostCalled_41q()
is cheaper by 44 gas.