40,000 USDC
View results
Submission Details
Severity: gas

Save a 2422 Units of gas by marking constructors payable

Summary

Marking constructors as payable is one of the great ways to optimize contract deployments.

According to a post by pcaversaccio on Openzeppelin Forum

You can cut out 10 opcodes in the creation-time EVM bytecode if you declare a constructor payable. The following opcodes are cut out:
CALLVALUE
DUP1
ISZERO
PUSH2
JUMPI
PUSH1
DUP1
REVERT
JUMPDEST
POP

Thanks to his approach, I have tried it and we saved a bunch of sweet deployment gas.

Impact

Here are the results that I've obtained from the Foundry gas snapshot:

Escrow Factory Contract

Before adding payable constructor

test_EscrowFactoryDeploymentGas() (gas: 1752574)

After Adding Payable Constructor

test_EscrowFactoryDeploymentGas() (gas: 1752552)

1752574 - 1752552 = 22 units of gas

ESCROW contract

Before adding payable to the constructor :

Deployment Cost Deployment Size
1863762 9139
Function Name min avg median max # calls
run 1757938 1757938 1757938 1757938 1

After adding payable // :

Deployment Cost Deployment Size
1861362 9127
Function Name min avg median max # calls
run 1755538 1755538 1755538 1755538 1

We can see a sweet difference of 1757938-1755538 = 2400 gas units.

that is huge when the Ethereum Mainnet is in demand.

Total Impact 2400+22 = 2422 Units of gas 👀🥳

Tools Used

Foundry, Manual review, pcaversaccio's post

Recommendations

Add payable to the constructor of the Escrow contract.

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.