Putty contest - 0xNazgul'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: 57/133

Findings: 2

Award: $75.27

🌟 Selected for report: 0

πŸš€ Solo Findings: 0

Event Spamming

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.

Missing Equivalence Checks in Setters

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.

Unnecessary Import

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.

Missing or Incomplete NatSpec

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.

Spelling Errors

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.

Calculating 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.

Use calldata Instead of memory For Function Parameters

Context: 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.

Functions Visibility Can Be Declared External

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.

Same State Variable Read More Than Once

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 SLOADs into much cheaper MLOADs.

Recommendation: It's best to cach the state variables into memory when read more than once.

In require(), Use != 0 Instead of > 0 With Uint Values

Context: 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.

The Increment In 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: (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.

Catching The Array Length Prior To Loop

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.

Use of Custom Errors Instead of String

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.

Setting The Constructor To Payable

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.

Function Ordering via Method ID

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.

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