NomicFoundation / hardhat

Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software.
https://hardhat.org
Other
7.34k stars 1.42k forks source link

create2 code works on remix but don't work on hardhat #5791

Open ddomeke opened 2 months ago

ddomeke commented 2 months ago

Version of Hardhat

2.22.12

What happened?

    assembly {
        // Prepare the contract creation code for the clone
        mstore(0x00, or(shr(0xe8, shl(0x60, implementation)), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000))
        mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3))
        instance := create2(0, 0x09, 0x37, salt)
    }
    // Revert if the clone operation failed
    if (instance == address(0))
        revert CloneOpCancelled("Creation Failed");

    // Call the initialization function of the newly cloned contract
    (bool success, ) = instance.call(initData);

Minimal reproduction steps

this code works on remix ide on polygon mainnet. but when I use it on polygon mainnet fork for testing it gives initialization fail error. I use another version of assembly code, it passes initialization but could not generate abi correctly.

        let pointer := mload(0x40)

        // mstore 32 bytes at the start of free memory 
        mstore(pointer, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)

        // overwrite the trailing 0s above with implementation contract's address in bytes
        mstore(add(pointer, 0x14), implementation)

        // store 32 bytes to memory starting at "clone" + 40 bytes
        mstore(add(pointer, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)

        instance := create2(0, pointer, 0x37, salt) 

Search terms

create2 on hardhat

ddomeke commented 1 month ago

Does anyone have any information on this subject?

kanej commented 1 month ago

To clarify, your issue here is that you can run the code on Polygon mainnet (via remix), but when you run the Solidity code against a forked version of Polygon mainnet you are getting a Hardhat error?

Could you provide the error you are seeing? This could potentially be an issue in our underlying runtime (EDR) during forking.

ddomeke commented 1 month ago

Hi John,

I use create2 opcode for my minimal proxy contract to make some copy of it.

The error is below

Error: VM Exception while processing transaction: reverted with custom error 'CloneOpCancelled("Initialization failed")' at AssetModule.authorized (contracts/apps/common/access/ACL.sol:46) at AssetModule.cloneAndInitBaseHub (contracts/apps/main/modules/AssetModule.sol:185) at AssetModule._defineAssetWithSalt (contracts/apps/main/modules/AssetModule.sol:161) at AssetModule.defineAsset (contracts/apps/main/modules/AssetModule.sol:85) at App. (contracts/apps/main/abstracts/Modular.sol:86) at EdrProviderWrapper.request (node_modules\hardhat\src\internal\hardhat-network\provider\provider.ts:428:41) at async HardhatEthersSigner.sendTransaction @.***\hardhat-ethers\src\signers.ts:125:18) at async send (node_modules\ethers\src.ts\contract\contract.ts:313:20)

at async Proxy.defineAsset

(node_modules\ethers\src.ts\contract\contract.ts:352:16) at async Context. (test\CollateralModule.test.ts:369:20)

when I convert the implementation address to bytes20 then there is no error in Hardhat framework. But, at this time, my contract that works in remix does not work properly. I can not reach the function signature.

Maybe this information gives you an idea about it.

bytes20 impl = bytes20(implementation);

After this change the error like this,

Error: Transaction reverted: function returned an unexpected amount of data at CollateralModule.depositCollateral (contracts/apps/main/modules/CollateralModule.sol:156) at App. (contracts/apps/main/abstracts/Modular.sol:86) at EdrProviderWrapper.request (node_modules\hardhat\src\internal\hardhat-network\provider\provider.ts:428:41) at async HardhatEthersSigner.sendTransaction @.***\hardhat-ethers\src\signers.ts:125:18) at async send (node_modules\ethers\src.ts\contract\contract.ts:313:20)

at async Proxy.depositCollateral

(node_modules\ethers\src.ts\contract\contract.ts:352:16) at async Context. (test\CollateralModule.test.ts:659:21)

If it is possible, I want to learn and help you with the EDR and forking issues with your guidance.

Sincerely,

Duran Dömeke

On Mon, Oct 21, 2024 at 2:42 PM John Kane @.***> wrote:

To clarify, your issue here is that you can run the code on Polygon mainnet (via remix), but when you run the Solidity code against a forked version of Polygon mainnet you are getting a Hardhat error?

Could you provide the error you are seeing? This could potentially be an issue in our underlying runtime (EDR) during forking.

— Reply to this email directly, view it on GitHub https://github.com/NomicFoundation/hardhat/issues/5791#issuecomment-2426435459, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGHHPUUA3AUICPKFY4QDUSDZ4TSDTAVCNFSM6AAAAABPA3AXJKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDIMRWGQZTKNBVHE . You are receiving this because you authored the thread.Message ID: @.***>

ddomeke commented 4 weeks ago

Hi John, This issue can be related with lsb and msb. create2 can not set implementation address correctly into the bytecode.