Skip to content

Commit

Permalink
feat: create2
Browse files Browse the repository at this point in the history
  • Loading branch information
kyriediculous committed Apr 12, 2024
1 parent 139f666 commit 4b3b7f9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
8 changes: 5 additions & 3 deletions script/Factory_Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ import { ERC1967Proxy } from "openzeppelin-contracts/proxy/ERC1967/ERC1967Proxy.

address constant FACTORY = address(0);

uint256 constant VERSION = 1;

contract Swap_Deploy is Script {
// Contracts are deployed deterministically.
// e.g. `foo = new Foo{salt: salt}(constructorArgs)`
// The presence of the salt argument tells forge to use https://github.com/Arachnid/deterministic-deployment-proxy
bytes32 private constant salt = 0x0;
bytes32 constant SALT = bytes32(VERSION);

// Start broadcasting with private key from `.env` file
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");

function run() public {
vm.startBroadcast(deployerPrivateKey);
address fac = address(new SwapFactory());
address proxy = address(new ERC1967Proxy(fac, abi.encodeWithSelector(SwapFactory.initialize.selector)));
address fac = address(new SwapFactory{ salt: SALT }());
address proxy = address(new ERC1967Proxy{ salt: SALT }(fac, abi.encodeWithSelector(SwapFactory.initialize.selector)));
console2.log("SwapFactory deployed at: ", proxy);
console2.log("Implementation deployed at: ", fac);
vm.stopBroadcast();
Expand Down
15 changes: 11 additions & 4 deletions src/Factory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ contract SwapFactory is Initializable, UUPSUpgradeable, OwnableUpgradeable {
event SwapDeployed(address underlying, address swap, address implementation);
event SwapUpgraded(address underlying, address swap, address implementation);

mapping(address pool => uint256 v) public version;

function initialize() public initializer {
__Ownable_init();
__UUPSUpgradeable_init();
Expand All @@ -35,13 +37,16 @@ contract SwapFactory is Initializable, UUPSUpgradeable, OwnableUpgradeable {
}

function deploy(ConstructorConfig memory cfg) external onlyOwner returns (address proxy, address implementation) {
uint256 v = 1;
// Deploy the implementation
implementation = address(new TenderSwap(cfg));
implementation = address(new TenderSwap{ salt: bytes32(v) }(cfg));
// deploy the contract
proxy = address(new ERC1967Proxy(implementation, abi.encodeWithSelector(TenderSwap.initialize.selector)));
proxy = address(
new ERC1967Proxy{ salt: bytes32("tenderswap") }(implementation, abi.encodeWithSelector(TenderSwap.initialize.selector))
);

TenderSwap(proxy).transferOwnership(owner());

version[proxy] = v;
emit SwapDeployed(address(cfg.UNDERLYING), proxy, implementation);
}

Expand All @@ -50,7 +55,9 @@ contract SwapFactory is Initializable, UUPSUpgradeable, OwnableUpgradeable {
revert("SwapFactory: UNDERLYING_MISMATCH");
}

implementation = address(new TenderSwap(cfg));
uint256 v = ++version[swapProxy];

implementation = address(new TenderSwap{ salt: bytes32(v) }(cfg));

TenderSwap(swapProxy).upgradeTo(implementation);
}
Expand Down

0 comments on commit 4b3b7f9

Please sign in to comment.