Cyfrin / solidity-by-example.github.io

Solidity By Example
https://solidity-by-example.org/
MIT License
605 stars 191 forks source link

example not work expected - Create2 #253

Closed huahuayu closed 1 year ago

huahuayu commented 1 year ago

In the Precompute Contract Address with Create2 example

    function deploy(bytes memory bytecode, uint _salt) public payable {
        ......
        assembly {
            addr := create2(
                callvalue(), // wei sent with current call
                ......
            )
       ......

It is said that you can send msg.value to the newly deployed contract.

So I had a test just now, to deploy this contract by create2, and try to send 1 wei to it.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

contract SimpleContract {
    uint public myNumber;

    function setNumber(uint number) public {
        myNumber = number;
    }

    fallback() external payable{}
}

The bytecode of this contract is 0x608060405234801561001057600080fd5b5061015c806100206000396000f3fe60806040526004361061002d5760003560e01c806323fd0e40146100305780633fb5c1cb1461005b5761002e565b5b005b34801561003c57600080fd5b50610045610084565b60405161005291906100ad565b60405180910390f35b34801561006757600080fd5b50610082600480360381019061007d91906100f9565b61008a565b005b60005481565b8060008190555050565b6000819050919050565b6100a781610094565b82525050565b60006020820190506100c2600083018461009e565b92915050565b600080fd5b6100d681610094565b81146100e157600080fd5b50565b6000813590506100f3816100cd565b92915050565b60006020828403121561010f5761010e6100c8565b5b600061011d848285016100e4565b9150509291505056fea2646970667358221220a02fa857b764cbf10d2cc497fdc2937c27717eb3033d67276baef7b1c60d183864736f6c63430008120033

Use any salt you like.

It will get reverted if you send eth value to deploy(bytes memory bytecode, uint _salt) function.

t4sk commented 1 year ago

Declare constructor() payable {} in your example

huahuayu commented 1 year ago

fallback() external payable{} or receive() doesn't work?

t4sk commented 1 year ago

no, contract will receive ETH on deployment, hence a payable constructor