forked from foundry-rs/foundry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFork.t.sol
More file actions
125 lines (101 loc) · 3.73 KB
/
Copy pathFork.t.sol
File metadata and controls
125 lines (101 loc) · 3.73 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.18;
import "ds-test/test.sol";
import "cheats/Vm.sol";
interface IWETH {
function deposit() external payable;
function balanceOf(address) external view returns (uint256);
}
contract ForkTest is DSTest {
address constant WETH_TOKEN_ADDR = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
uint256 constant mainblock = 14_608_400;
Vm constant vm = Vm(HEVM_ADDRESS);
IWETH WETH = IWETH(WETH_TOKEN_ADDR);
uint256 forkA;
uint256 forkB;
uint256 testValue;
// this will create two _different_ forks during setup
function setUp() public {
forkA = vm.createFork("mainnet", mainblock);
forkB = vm.createFork("mainnet2", mainblock - 1);
testValue = 999;
}
// ensures forks use different ids
function testForkIdDiffer() public {
assert(forkA != forkB);
}
// ensures we can create and select in one step
function testCreateSelect() public {
uint256 fork = vm.createSelectFork("mainnet");
assertEq(fork, vm.activeFork());
}
// ensures forks use different ids
function testCanSwitchForks() public {
vm.selectFork(forkA);
vm.selectFork(forkB);
vm.selectFork(forkB);
vm.selectFork(forkA);
}
function testForksHaveSeparatedStorage() public {
vm.selectFork(forkA);
// read state from forkA
assert(WETH.balanceOf(0x0000000000000000000000000000000000000000) != 1);
vm.selectFork(forkB);
// read state from forkB
uint256 forkBbalance = WETH.balanceOf(0x0000000000000000000000000000000000000000);
assert(forkBbalance != 1);
vm.selectFork(forkA);
// modify state
bytes32 value = bytes32(uint256(1));
// "0x3617319a054d772f909f7c479a2cebe5066e836a939412e32403c99029b92eff" is the slot storing the balance of zero address for the weth contract
// `cast index address uint 0x0000000000000000000000000000000000000000 3`
bytes32 zero_address_balance_slot = 0x3617319a054d772f909f7c479a2cebe5066e836a939412e32403c99029b92eff;
vm.store(WETH_TOKEN_ADDR, zero_address_balance_slot, value);
assertEq(
WETH.balanceOf(0x0000000000000000000000000000000000000000),
1,
"Cheatcode did not change value at the storage slot."
);
// switch forks and ensure the balance on forkB remains untouched
vm.selectFork(forkB);
assert(forkBbalance != 1);
// balance of forkB is untouched
assertEq(
WETH.balanceOf(0x0000000000000000000000000000000000000000),
forkBbalance,
"Cheatcode did not change value at the storage slot."
);
}
function testCanShareDataAcrossSwaps() public {
assertEq(testValue, 999);
uint256 val = 300;
vm.selectFork(forkA);
assertEq(val, 300);
testValue = 100;
vm.selectFork(forkB);
assertEq(val, 300);
assertEq(testValue, 100);
val = 99;
testValue = 300;
vm.selectFork(forkA);
assertEq(val, 99);
assertEq(testValue, 300);
}
// ensures forks use different ids
function testCanChangeChainId() public {
vm.selectFork(forkA);
uint256 newChainId = 1337;
vm.chainId(newChainId);
uint256 expected = block.chainid;
assertEq(newChainId, expected);
}
// ensures forks change chain ids automatically
function testCanAutoUpdateChainId() public {
vm.createSelectFork("sepolia");
assertEq(block.chainid, 11155111);
}
// ensures forks storage is cached at block
function testStorageCaching() public {
vm.createSelectFork("mainnet", 19800000);
}
}