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: 100/133
Findings: 1
Award: $28.80
🌟 Selected for report: 0
🚀 Solo Findings: 0
🌟 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
28.8033 USDC - $28.80
Solidity does not recognize null as a value, so uint variables are initialized to zero. Setting a uint variable to zero is redundant and can waste gas.
Locations where this was found include https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L497 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L556 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L594 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L611 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L627 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L637 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L647 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L658 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L670 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L728 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L742
Remove the redundant zero initialization
uint256 i;
instead of uint256 i = 0;
Using > 0
uses slightly more gas than using != 0
. Use != 0
when comparing uint variables to zero, which cannot hold values below zero
Locations where this was found include https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L293 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L327 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L351 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L427 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L498 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L598 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L599
Replace > 0
with != 0
to save gas
Using a prefix increment (++i) instead of a postfix increment (i++) saves gas for each loop cycle and so can have a big gas impact when the loop executes on a large number of elements.
Locations where this was found include https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L556 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L594 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L611 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L627 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L637 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L647 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L658 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L670 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L728 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L742
Use prefix not postfix to increment in a loop
For loops that use i++ do not need to use safemath for this operation because the loop would run out of gas long before this point. Making this addition operation unsafe using unchecked saves gas.
Sample code to make the for loop increment unsafe
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; } }
Idea borrowed from https://gist.github.com/hrkrshnn/ee8fabd532058307229d65dcd5836ddc#the-increment-in-for-loop-post-condition-can-be-made-unchecked
Locations where this was found include https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L556 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L594 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L611 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L627 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L637 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L647 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L658 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L670 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L728 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L742
Make the increment in for loops unsafe to save gas
Comparing a value to zero can be done using the iszero
EVM opcode. This can save gas
Source from t11s https://twitter.com/transmissions11/status/1474465495243898885
Locations where this was found include https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L284 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L298 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L406
Use the assembly iszero
evm opcode to compare values to zero
Use unchecked math when there is no overflow risk to save gas. Before index is decreased in remove it is checked for zero condition. This means index will not underflow and can be unchecked.
Locations where this was found include https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L503
Add unchecked around math that can't overflow for gas savings. In Solidity before 0.8.0, use the normal math operators instead of safe math functions.
Identifying a function as payable saves gas. Functions that have a modifier like onlyOwner cannot be called by normal users and will not mistakenly receive ETH. These functions can be payable to save gas.
Locations where this was found include https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L228 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L240
Add payable to these functions for gas savings
Identifying a constructor as payable saves gas. Constructors should only be called by the admin or deployer and should not mistakenly receive ETH. Constructors can be payable to save gas.
Locations where this was found include https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L209
Add payable to these functions for gas savings
An internal function can save gas vs. a modifier. A modifier inlines the code of the original function but an internal function does not.
Locations where the onlyOwner modifier was found include https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L228 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L240
Use internal functions in place of modifiers to save gas.
Booleans are more expensive than uint256 or any type that takes up a full word because each write operation emits an extra SLOAD to first read the slot's contents, replace the bits taken up by the boolean, and then write back. This is the compiler's defense against contract upgrades and pointer aliasing, and it cannot be disabled.
Locations where this was found include https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L70 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L71 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L106 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L107 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L478
Replace bool variables with uints
Solidity errors introduced in version 0.8.4 can save gas on revert conditions https://blog.soliditylang.org/2021/04/21/custom-errors/ https://twitter.com/PatrickAlphaC/status/1505197417884528640
Locations where this was found include https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L214 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L241 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L278 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L281 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L284 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L287 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L290 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L293 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L297 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2Nft.sol#L13 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2Nft.sol#L26 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2Nft.sol#L27 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2Nft.sol#L28 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2Nft.sol#L41
Replace require blocks with new solidity errors described in https://blog.soliditylang.org/2021/04/21/custom-errors/
Many constant variables are public, but changing the visibility of these variables to private or internal can save gas.
Locations where this was found include https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L89 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L95 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L101 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L127
Declare some public variables as private or internal to save gas
Using calldata instead of memory for function arguments saves gas sometimes. This can happen when a function is called externally and the memory array values are kept in calldata
and copied to memory
during ABI decoding (using the opcode calldataload
and mstore
). If the array is used in a for loop, arr[i]
accesses the value in memory using a mload
. If calldata is used instead, then 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.
Locations where this was found include https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L271 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L547 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L549 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L593 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L610 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L623 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L624 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L636 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L646 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L657 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L669 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L727 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L741
Change function arguments from memory to calldata
The contracts are all written entirely in solidity. Writing contracts with vyper instead of solidity can save gas.
Source https://twitter.com/eiber_david/status/1515737811881807876 doggo demonstrates https://twitter.com/fubuloubu/status/1528179581974417414?t=-hcq_26JFDaHdAQZ-wYxCA&s=19
Write some or all of the contracts in vyper to save gas
abi.encodePacked()
not abi.encode()
Changing abi.encode
to abi.encodePacked
can save gas. abi.encode
pads extra null bytes at the end of the call data which is normally unnecessary. In general, abi.encodePacked
is more gas-efficient.
Locations where this was found include https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L685 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L701 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L731 https://github.com/code-423n4/2022-06-putty/tree/main/contracts/src/PuttyV2.sol#L745
Change abi.encode
to abi.encodePacked