yarn contracts test# Start anvil fork at a specific block
anvil --fork-url https://rpc.katana.network --fork-block-number 12345678
anvil --fork-url https://rpc.hyperliquid.xyz/evm --fork-block-number 12345678
# Run fork tests
yarn contracts test_forked --match-contract XXXX-
Start Anvil Fork at the target block:
anvil --fork-url https://base-mainnet.g.alchemy.com/v2/YOUR_API_KEY --fork-block-number 41323464
-
Run Tests:
# Run all fork tests yarn test_forked # Run specific test contract yarn test_forked --match-contract YearnRedeemTest # Run with verbose output (recommended for debugging) yarn test_forked --match-contract YearnRedeemTest -vvvv
Create a new file in tests_fork/:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import { TxSimulator } from "./TxSimulator.sol";
import "forge-std/console.sol";
contract MyTransactionTest is TxSimulator {
address constant TARGET = 0x...;
address constant USER = 0x...;
function setUp() public {
setupChainConfig(); // Required for ABI fetching
}
function test_my_transaction() public {
SimTx memory txn = SimTx({
to: TARGET,
from: USER,
data: abi.encodeWithSelector(...),
value: 0
});
logPreState(txn);
(SimResult memory result, address[] memory involvedContracts) =
simulateTxWithContracts(txn);
logPostStateWithContracts(txn, result, involvedContracts);
assertTrue(result.success, "Transaction should succeed");
}
}If you have raw transaction data from a failed transaction:
function test_with_raw_calldata() public {
bytes memory callData = hex"ba087652000000000000000000000000...";
SimTx memory txn = SimTx({
to: TARGET,
from: USER,
data: callData,
value: 0
});
(SimResult memory result, address[] memory involvedContracts) =
simulateTxWithContracts(txn);
logPostStateWithContracts(txn, result, involvedContracts);
}-
Get transaction details from block explorer (target contract, from address, block number, input data)
-
Start anvil at that block:
anvil --fork-url https://base-mainnet.g.alchemy.com/v2/KEY --fork-block-number 41323464
-
Create test file with the transaction details
-
Run test:
yarn test_forked --match-test test_my_transaction -vvvv
-
Analyze output: Check decoded error message, review involved contracts, examine state differences
Create a .env file with your API keys:
ETHERSCANV2_VERIFY_API_KEY=your_api_key_hereFetched ABIs are cached in ./cache/abi/. To clear cache: rm -rf ./cache/abi/
For more details, see FORK_TESTING.md.