Learn how to test your Clarity smart contracts against real-world Mainnet data using the Clarinet JS SDK's Mainnet Execution Simulation (MXS) feature.
Mainnet Execution Simulation (MXS) allows you to test your Clarity smart contracts using actual data and state from the Stacks mainnet. This powerful feature helps ensure your contracts behave as expected in a live environment by enabling you to:
Test contract interactions against real-world data and dependencies.
Simulate any historical or recent mainnet transaction to observe its outcome or evaluate its execution cost.
This guide focuses on using MXS within the context of unit tests using Vitest, which is the primary use case.
When developing smart contracts, testing against realistic conditions is crucial. Simnet provides an isolated testing environment, but it lacks the complexity and historical state of the live Stacks mainnet.
MXS bridges this gap by allowing your unit tests, executed via the Clarinet JS SDK and Vitest, to interact with a simulated environment that mirrors the Stacks mainnet state at a specific block height. This means you can:
Validate contract logic against real data: Call mainnet contracts or check data derived from mainnet state directly within your tests.
(Re)simulate transactions: Execute specific mainnet transactions to analyze their exact results, trace their execution, or estimate their costs without deploying or spending actual STX.
To use MXS in your unit tests, you need to enable it in your Clarinet.toml file. Add the following section:
[testing]
# Enable mainnet execution simulation
mainnet_simulation=true
# Specify the Stacks block height to simulate (optional)
initial_height=1094766
mainnet_simulation = true: This flag activates the MXS feature for your tests.
initial_height = <block_height>: This optional setting specifies the Stacks block height at which the simulation should start.
If omitted, Clarinet defaults to the latest finalized Stacks block height at the time of execution.
Recommendation: Setting a specific initial_height is highly recommended for consistent and reproducible test results, as the mainnet state constantly changes.
Example Project
You can explore a project demonstrating MXS usage with a Pyth oracle contract in this repository.
Once MXS is enabled, your Vitest tests running via the Clarinet JS SDK (npm test) will automatically operate against the specified mainnet state. You don't need to change your test writing approach significantly.
The simnet object, typically used for interacting with the simulated environment in tests, will now interact with the mainnet state snapshot defined by initial_height.
For example, you could write a test that calls a function on a mainnet contract like pox-4:
1
import{ describe, it, expect }from"vitest";
2
import{ Cl }from"@stacks/transactions";
3
4
constaccounts=simnet.getAccounts();
5
constdeployer=accounts.get("deployer")!;// Or any other account
6
7
describe("pox-4 reward cycle", ()=>{
8
it("returns pox reward cycle id", ()=>{
9
// Call the current-pox-reward-cycle function on the mainnet pox-4 contract
10
constcall=simnet.callReadOnlyFn(
11
"SP000000000000000000002Q6VF78.pox-4",// Mainnet pox-4 contract principal
12
"current-pox-reward-cycle",
13
[],// No arguments needed for this function
14
deployer// Caller address
15
);
16
17
// Assert that the call was successful and returns cycle number u109
18
expect(call.result).toBeUint(109);
19
20
// You can further assert other values by tweaking the initial_height in `Clarinet.toml`
21
// For example, if initial_height = 299000, the value might be:
This test uses simnet.callReadOnlyFn just like in standard unit tests, but because MXS is enabled, the call targets the actual pox-4 contract state at the initial_height specified in Clarinet.toml.
Currently, MXS does not support Clarity functions that rely on querying specific information related to burn blocks or miner tenures. The following functions are not implemented in the simulation environment: