Merge branch 'master' into fix/enumerable-#1240
This commit is contained in:
49
contracts/utils/Create2.sol
Normal file
49
contracts/utils/Create2.sol
Normal file
@ -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.
|
||||
*
|
||||
* _Available since v2.5.0._
|
||||
*/
|
||||
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)
|
||||
}
|
||||
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) 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));
|
||||
}
|
||||
}
|
||||
@ -12,4 +12,6 @@ Miscellaneous contracts containing utility functions, often related to working w
|
||||
|
||||
{{EnumerableSet}}
|
||||
|
||||
{{Create2}}
|
||||
|
||||
{{ReentrancyGuard}}
|
||||
|
||||
@ -5,9 +5,9 @@ pragma solidity ^0.5.0;
|
||||
* @dev Wrappers over Solidity's uintXX casting operators with added overflow
|
||||
* checks.
|
||||
*
|
||||
* Downcasting from uint256 in Solidity does not revert on overflow. This can
|
||||
* Downcasting from uint256 in Solidity does not revert on overflow. This can
|
||||
* easily result in undesired exploitation or bugs, since developers usually
|
||||
* assume that overflows raise errors. `SafeCast` restores this intuition by
|
||||
* assume that overflows raise errors. `SafeCast` restores this intuition by
|
||||
* reverting the transaction when such an operation overflows.
|
||||
*
|
||||
* Using this library instead of the unchecked operations eliminates an entire
|
||||
@ -15,6 +15,8 @@ pragma solidity ^0.5.0;
|
||||
*
|
||||
* Can be combined with {SafeMath} to extend it to smaller types, by performing
|
||||
* all math on `uint256` and then downcasting.
|
||||
*
|
||||
* _Available since v2.5.0._
|
||||
*/
|
||||
library SafeCast {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user