Skip to content

Instantly share code, notes, and snippets.

@bjoveski
Last active November 12, 2022 22:42
Show Gist options
  • Save bjoveski/e46dc9ad2c89b997b65c38cb05221124 to your computer and use it in GitHub Desktop.
Save bjoveski/e46dc9ad2c89b997b65c38cb05221124 to your computer and use it in GitHub Desktop.
contract ForwarderFactory {
function cloneForwarder(address forwarder, uint256 salt)
public returns (Forwarder clonedForwarder) {
address clonedAddress = createClone(forwarder, salt);
Forwarder parentForwarder = Forwarder(forwarder);
clonedForwarder = Forwarder(clonedAddress);
clonedForwarder.init(parentForwarder.destination());
}
function createClone(address target, uint256 salt) private returns (address result) {
bytes20 targetBytes = bytes20(target);
assembly {
let clone := mload(0x40)
mstore(clone, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
mstore(add(clone, 0x14), targetBytes)
mstore(add(clone, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
result := create2(0, clone, 0x37, salt)
}
}
}
@Anietz
Copy link

Anietz commented Aug 19, 2021

Hello,

I am using the javascript code below to generate the off-chain code.

I observed that the off-chain address is not equal to the deployed contract address.
Can you help me understand what steps I am missing ?
Thank you.

function buildCreate2Address(senderAddress, saltHex, bytecode) { return web3.utils.toChecksumAddress(0x${web3.utils.sha3(0x${[ 'ff', senderAddress, saltHex, web3.utils.sha3(bytecode) ].map(x => x.replace(/0x/, '')) .join('')}).slice(-40)}); }

@Anietz
Copy link

Anietz commented Aug 26, 2021

solved this with the code below:

`// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8;

import "./ForwarderV3.sol";
import "@openzeppelin/contracts/proxy/Clones.sol";

contract Factory {

//event Deployed(address from, uint value);

event ForwarderCloned(address clonedAdress);

      function cloneForwarder(address payable forwarder, uint256 salt)
            public returns (Forwarder clonedForwarder) {
            address payable clonedAddress = createClone(forwarder, salt);
            Forwarder parentForwarder = Forwarder(forwarder);
            clonedForwarder = Forwarder(clonedAddress);
            clonedForwarder.init(parentForwarder.destinationAddress());
            emit ForwarderCloned(clonedAddress);
      }
      
      
      function createClone(address target, uint256 salt) private returns (address payable result) {
        bytes20 targetBytes = bytes20(target);
        assembly {
          let clone := mload(0x40)
          mstore(clone, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
          mstore(add(clone, 0x14), targetBytes)
          mstore(add(clone, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
          result := create2(0, clone, 0x37, salt)
        }
      }
      
      function predictCloneAddress(address forwarderAddress_, uint256 salt_)
    public
    view
    returns (address)
        {
            address predictedAddress =
                Clones.predictDeterministicAddress(
                    forwarderAddress_,
                    bytes32(salt_)
                );
    
            return predictedAddress;
        }

}

`

@gitchand
Copy link

gitchand commented Apr 2, 2022

Is init function of Forwarder public used in cloneForwarder? Can you please share code of init function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment