Putty contest - 0xkatana's results

An order-book based american options market for NFTs and ERC20s.

General Information

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

Putty

Findings Distribution

Researcher Performance

Rank: 100/133

Findings: 1

Award: $28.80

🌟 Selected for report: 0

🚀 Solo Findings: 0

[G-01] Redundant zero initialization

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;

[G-02] Use != 0 instead of > 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

[G-03] Use prefix not postfix in loops

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

[G-04] For loop incrementing can be unsafe

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

[G-05] Use iszero assembly for zero checks

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

[G-06] Save gas with unchecked

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.

[G-07] Add payable to functions that won't receive ETH

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

[G-08] Add payable to constructors that won't receive ETH

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

[G-09] Use internal function in place of modifier

An internal function can save gas vs. a modifier. A modifier inlines the code of the original function but an internal function does not.

Source https://blog.polymath.network/solidity-tips-and-tricks-to-save-gas-and-reduce-bytecode-size-c44580b218e6#dde7

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.

[G-10] Use uint not bool

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

[G-11] Use Solidity errors instead of require

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/

[G-12] Non-public variables save gas

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

[G-13] Use calldata instead of memory for function arguments

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

Source https://gist.github.com/hrkrshnn/ee8fabd532058307229d65dcd5836ddc#use-calldata-instead-of-memory-for-function-parameters

Change function arguments from memory to calldata

[G-14] Write contracts in vyper

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

[G-15] Use 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

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