solidity/test/libsolidity/semanticTests/salted_create/prediction_example.sol

30 lines
693 B
Solidity
Raw Normal View History

contract D {
uint public x;
constructor(uint a) {
x = a;
}
}
contract C {
function createDSalted(bytes32 salt, uint arg) public {
address predictedAddress = address(uint160(uint(keccak256(abi.encodePacked(
bytes1(0xff),
address(this),
salt,
keccak256(abi.encodePacked(
type(D).creationCode,
arg
))
)))));
D d = new D{salt: salt}(arg);
require(address(d) == predictedAddress, "Address mismatch.");
}
}
// ====
// EVMVersion: >=constantinople
// compileViaYul: also
// ----
// createDSalted(bytes32,uint256): 42, 64 ->
// gas legacy: 104365