-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMetamorphicContract.t.sol
59 lines (47 loc) · 1.47 KB
/
MetamorphicContract.t.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// SPDX-License-Identifier: WTFPL
pragma solidity 0.8.28;
import {Test} from "forge-std/Test.sol";
contract A {
function kill() public {
selfdestruct(payable(address(0)));
}
}
contract B {
uint256 private x;
constructor(uint256 x_) {
x = x_;
}
}
contract Factory {
function helloA() public returns (address) {
return address(new A());
}
function helloB() public returns (address) {
return address(new B(1337));
}
function kill() public {
selfdestruct(payable(address(0)));
}
}
contract MetamorphicContract is Test {
A private a;
B private b;
Factory private factory;
function setUp() public {
factory = new Factory{salt: keccak256(abi.encode("evil"))}();
a = A(factory.helloA());
/// @dev Call `selfdestruct` during the `setUp` call (see https://github.com/foundry-rs/foundry/issues/1543).
a.kill();
factory.kill();
}
function testMorphingContract() public {
/// @dev Verify that the code was destroyed during the `setUp` call.
assertEq(address(a).code.length, 0);
assertEq(address(factory).code.length, 0);
/// @dev Redeploy the factory contract at the same address.
factory = new Factory{salt: keccak256(abi.encode("evil"))}();
/// @dev Deploy another logic contract at the same address as previously contract `a`.
b = B(factory.helloB());
assertEq(address(a), address(b));
}
}