You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/blockchain-development-tutorials/cadence/fork-testing/index.md
+12-31Lines changed: 12 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -112,26 +112,13 @@ Use the [Dependency Manager] to install the `FlowToken` and `FungibleToken` cont
112
112
flow dependencies install FlowToken FungibleToken
113
113
```
114
114
115
-
This downloads the contracts and their dependencies into the `imports/` folder and updates your `flow.json` with the correct addresses and aliases across all networks (mainnet, testnet, emulator).
115
+
This downloads the contracts into the `imports/` folder and configures aliases for different networks.
116
116
117
-
Your `flow.json` now includes an entry like:
117
+
## Understanding `mainnet-fork`
118
118
119
-
```json
120
-
{
121
-
"dependencies": {
122
-
"FlowToken": {
123
-
"source": "mainnet://1654653399040a61.FlowToken",
124
-
"aliases": {
125
-
"emulator": "0ae53cb6e3f42a79",
126
-
"mainnet": "1654653399040a61",
127
-
"testnet": "7e60df042a9c0868"
128
-
}
129
-
}
130
-
}
131
-
}
132
-
```
119
+
When you run `flow init`, the CLI automatically creates a `mainnet-fork` network in your `flow.json`. This network inherits all contract addresses from mainnet, so imports like `"FlowToken"` automatically resolve to their mainnet addresses (`0x1654653399040a61`).
133
120
134
-
Your `flow.json` now has the mainnet and testnet networks configured from `flow init`. In fork mode, contract imports automatically resolve to the correct network addresses.
121
+
Using `#test_fork(network: "mainnet-fork")` in your test files runs your tests against a local snapshot of mainnet state. You can deploy contracts, impersonate accounts, and modify state locally without affecting the real network.
135
122
136
123
## Test reading live state
137
124
@@ -160,7 +147,7 @@ flow generate test FlowToken
160
147
Open `cadence/tests/FlowToken_test.cdc` and replace its contents with:
161
148
162
149
```cadence cadence/tests/FlowToken_test.cdc
163
-
#test_fork(network: "mainnet", height: nil)
150
+
#test_fork(network: "mainnet-fork", height: nil)
164
151
165
152
import Test
166
153
@@ -179,10 +166,10 @@ access(all) fun testFlowTokenSupplyIsPositive() {
179
166
180
167
:::info
181
168
182
-
-**The `#test_fork` pragma**at the top configures this test to run against mainnet
169
+
-**The `#test_fork` pragma** configures this test to run against the `mainnet-fork` network
183
170
- Use `Test.executeScript()` to read contract state
184
171
- The script imports `FlowToken` by name - the dependency manager handles address resolution
185
-
-In fork mode, this automatically uses the mainnet FlowToken contract
172
+
-Because we're using `mainnet-fork`, this automatically uses the mainnet FlowToken contract
186
173
- Extract the return value with proper type casting and assert on it
187
174
- File paths in `Test.readFile()` are relative to the test file location (use `../scripts/` from `cadence/tests/`)
188
175
@@ -198,12 +185,6 @@ flow test cadence/tests/FlowToken_test.cdc
198
185
199
186
The pragma handles the fork configuration automatically! You will see the test PASS. If not, verify your network host in `flow.json` and that dependencies are installed.
200
187
201
-
**To test against testnet instead**, simply change the pragma in the test file:
202
-
203
-
```cadence
204
-
#test_fork(network: "testnet", height: nil)
205
-
```
206
-
207
188
## Deploy and test Your contract
208
189
209
190
Now you'll create a contract that depends on FlowToken and test it against the forked mainnet state. There's no need to bootstrap tokens or set up test accounts.
@@ -330,7 +311,7 @@ flow generate test TokenChecker
330
311
Open `cadence/tests/TokenChecker_test.cdc` and replace its contents with:
331
312
332
313
```cadence cadence/tests/TokenChecker_test.cdc
333
-
#test_fork(network: "mainnet", height: nil)
314
+
#test_fork(network: "mainnet-fork", height: nil)
334
315
335
316
import Test
336
317
@@ -539,7 +520,7 @@ Test results: "cadence/tests/TokenChecker_test.cdc"
539
520
**Recommended:** Configure fork tests in your test file with `#test_fork`
540
521
541
522
```cadence
542
-
#test_fork(network: "mainnet", height: nil)
523
+
#test_fork(network: "mainnet-fork", height: nil)
543
524
import Test
544
525
// Your tests...
545
526
```
@@ -569,7 +550,7 @@ Use `Test.deployContract()` to deploy your mock to any mainnet account address.
569
550
### Example
570
551
571
552
```cadence
572
-
#test_fork(network: "mainnet", height: nil)
553
+
#test_fork(network: "mainnet-fork", height: nil)
573
554
574
555
import Test
575
556
@@ -602,7 +583,7 @@ This validates your contract changes against real production state and integrati
602
583
For reproducible test results, pin your tests to a specific block height:
This ensures your tests run against the same blockchain state every time, useful for:
@@ -614,7 +595,7 @@ This ensures your tests run against the same blockchain state every time, useful
614
595
To use the latest state instead, use `height: nil`:
615
596
616
597
```cadence
617
-
#test_fork(network: "mainnet", height: nil)
598
+
#test_fork(network: "mainnet-fork", height: nil)
618
599
```
619
600
620
601
Note that block heights are only available within the current spork (network upgrade period). See [Testing Smart Contracts] for more on managing pinned heights over time.
0 commit comments