forked from matter-labs/zksync
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use create2 for deploying target smart contracts
- Loading branch information
1 parent
22612e5
commit 1653481
Showing
5 changed files
with
128 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
pragma solidity ^0.7.0; | ||
|
||
contract Create2Factory { | ||
/** | ||
* @dev Deploys a contract using `CREATE2`. The address where the contract | ||
* will be deployed can be known in advance via {computeAddress}. Note that | ||
* a contract cannot be deployed twice using the same salt. | ||
*/ | ||
function deploy(bytes32 salt, bytes memory bytecode) public returns (address) { | ||
address addr; | ||
// solhint-disable-next-line no-inline-assembly | ||
assembly { | ||
addr := create2(0, add(bytecode, 0x20), mload(bytecode), salt) | ||
} | ||
require(addr != address(0), "Create2: Failed on deploy"); | ||
return addr; | ||
} | ||
|
||
/** | ||
* @dev Returns the address where a contract will be stored if deployed via {deploy}. Any change in the `bytecode` | ||
* or `salt` will result in a new destination address. | ||
*/ | ||
function computeAddress(bytes32 salt, bytes memory bytecode) external view returns (address) { | ||
return computeAddress(salt, bytecode, address(this)); | ||
} | ||
|
||
/** | ||
* @dev Returns the address where a contract will be stored if deployed via {deploy} from a contract located at | ||
* `deployer`. If `deployer` is this contract's address, returns the same value as {computeAddress}. | ||
*/ | ||
function computeAddress( | ||
bytes32 salt, | ||
bytes memory bytecodeHash, | ||
address deployer | ||
) public pure returns (address) { | ||
bytes32 bytecodeHashHash = keccak256(bytecodeHash); | ||
bytes32 _data = keccak256(abi.encodePacked(bytes1(0xff), deployer, salt, bytecodeHashHash)); | ||
return address(bytes20(_data << 96)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters