-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(contracts): Add Create2 library to use create2 evm opcode * Upgrade sol-coverage * Add changelog entry * Update comments and code style * Remove create2 helper * Fix linter error * Fix truffle dependency * Fix linter error * refactor(Create2): Remove _deploy internal function * test(Create2): test Create2 with inline assembly code * fix(Create2): Check address returned form create2 instead of codesize of created contract
- Loading branch information
Showing
9 changed files
with
4,115 additions
and
4,333 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
pragma solidity ^0.5.0; | ||
|
||
import "../utils/Create2.sol"; | ||
import "../token/ERC20/ERC20.sol"; | ||
|
||
contract Create2Impl { | ||
function deploy(bytes32 salt, bytes memory code) public { | ||
Create2.deploy(salt, code); | ||
} | ||
|
||
function deployERC20(bytes32 salt) public { | ||
// solhint-disable-next-line indent | ||
Create2.deploy(salt, type(ERC20).creationCode); | ||
} | ||
|
||
function computeAddress(bytes32 salt, bytes memory code) public view returns (address) { | ||
return Create2.computeAddress(salt, code); | ||
} | ||
|
||
function computeAddress(bytes32 salt, bytes memory code, address deployer) public pure returns (address) { | ||
return Create2.computeAddress(salt, code, deployer); | ||
} | ||
} |
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,49 @@ | ||
pragma solidity ^0.5.0; | ||
|
||
/** | ||
* @dev Helper to make usage of the `CREATE2` EVM opcode easier and safer. | ||
* `CREATE2` can be used to compute in advance the address where a smart | ||
* contract will be deployed, which allows for interesting new mechanisms known | ||
* as 'counterfactual interactions'. | ||
* | ||
* See the https://eips.ethereum.org/EIPS/eip-1014#motivation[EIP] for more | ||
* information. | ||
*/ | ||
library Create2 { | ||
/** | ||
* @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) internal returns (address) { | ||
address addr; | ||
// solhint-disable-next-line no-inline-assembly | ||
assembly { | ||
addr := create2(0, add(bytecode, 0x20), mload(bytecode), salt) | ||
if iszero(addr) { | ||
revert(0, 0) | ||
} | ||
} | ||
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) internal 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) internal 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
Oops, something went wrong.