Skip to content

Commit 93b1b1f

Browse files
authored
Update mainnet forking syntax for Cadence Test Framework (#1639)
1 parent afe5839 commit 93b1b1f

1 file changed

Lines changed: 12 additions & 31 deletions

File tree

  • docs/blockchain-development-tutorials/cadence/fork-testing

docs/blockchain-development-tutorials/cadence/fork-testing/index.md

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -112,26 +112,13 @@ Use the [Dependency Manager] to install the `FlowToken` and `FungibleToken` cont
112112
flow dependencies install FlowToken FungibleToken
113113
```
114114

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.
116116

117-
Your `flow.json` now includes an entry like:
117+
## Understanding `mainnet-fork`
118118

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`).
133120

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.
135122

136123
## Test reading live state
137124

@@ -160,7 +147,7 @@ flow generate test FlowToken
160147
Open `cadence/tests/FlowToken_test.cdc` and replace its contents with:
161148

162149
```cadence cadence/tests/FlowToken_test.cdc
163-
#test_fork(network: "mainnet", height: nil)
150+
#test_fork(network: "mainnet-fork", height: nil)
164151
165152
import Test
166153
@@ -179,10 +166,10 @@ access(all) fun testFlowTokenSupplyIsPositive() {
179166

180167
:::info
181168

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
183170
- Use `Test.executeScript()` to read contract state
184171
- 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
186173
- Extract the return value with proper type casting and assert on it
187174
- File paths in `Test.readFile()` are relative to the test file location (use `../scripts/` from `cadence/tests/`)
188175

@@ -198,12 +185,6 @@ flow test cadence/tests/FlowToken_test.cdc
198185

199186
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.
200187

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-
207188
## Deploy and test Your contract
208189

209190
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
330311
Open `cadence/tests/TokenChecker_test.cdc` and replace its contents with:
331312

332313
```cadence cadence/tests/TokenChecker_test.cdc
333-
#test_fork(network: "mainnet", height: nil)
314+
#test_fork(network: "mainnet-fork", height: nil)
334315
335316
import Test
336317
@@ -539,7 +520,7 @@ Test results: "cadence/tests/TokenChecker_test.cdc"
539520
**Recommended:** Configure fork tests in your test file with `#test_fork`
540521

541522
```cadence
542-
#test_fork(network: "mainnet", height: nil)
523+
#test_fork(network: "mainnet-fork", height: nil)
543524
import Test
544525
// Your tests...
545526
```
@@ -569,7 +550,7 @@ Use `Test.deployContract()` to deploy your mock to any mainnet account address.
569550
### Example
570551

571552
```cadence
572-
#test_fork(network: "mainnet", height: nil)
553+
#test_fork(network: "mainnet-fork", height: nil)
573554
574555
import Test
575556
@@ -602,7 +583,7 @@ This validates your contract changes against real production state and integrati
602583
For reproducible test results, pin your tests to a specific block height:
603584

604585
```cadence
605-
#test_fork(network: "mainnet", height: 85229104)
586+
#test_fork(network: "mainnet-fork", height: 85229104)
606587
```
607588

608589
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
614595
To use the latest state instead, use `height: nil`:
615596

616597
```cadence
617-
#test_fork(network: "mainnet", height: nil)
598+
#test_fork(network: "mainnet-fork", height: nil)
618599
```
619600

620601
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

Comments
 (0)