Scenario examples
Scenario examples are reusable, named timelines that show how to combine account overrides into a concrete runtime exercise. They give an engineer a starting point for testing a protocol condition in Surfpool, while the broader scenario execution page explains how the runtime applies those changes and scenario registry and protocols explains the templates that the examples use.
Scenario example model
A scenario example is a JSON description of one product or protocol situation. It gives the situation a name and purpose, then binds each account change to an override template, an account, a set of values, and a point in the scenario timeline.
Scenario timeline
The runtime treats each override as an event at a slot relative to the scenario base slot. This lets one example describe both a single starting state and a sequence of later state changes.
The relationship between an example and the runtime is:
flowchart LR
E[Scenario example] --> O[Override instances]
O --> T[Registered templates]
O --> S[Relative slots]
T --> A[Target account state]
S --> R[Runtime slot schedule]
A --> R
R --> X[Exercised protocol behavior]
The example does not define a new protocol rule. It selects existing templates and values so the runtime can present a repeatable account state to the protocol under test.
References: crates/…/scenarios.rs• L464-508 crates/…/scenarios.rs• L535-588 crates/…/README.md• L1-5
Reusable example scenario
The repository currently provides a Kamino liquidation arbitrage example. It is a cross-protocol exercise that creates an unhealthy Kamino obligation and changes the related Whirlpool and Raydium pool states so a liquidation and arbitrage path can be tested together.
Kamino liquidation arbitrage
The crates/…/kamino-liquidation-arbitrage.json file names the scenario Kamino Liquidation Arbitrage - POPCAT/SOL and tags it for liquidation, arbitrage, Kamino, Whirlpool, and Raydium use. All three changes occur at relative slot 0, so they form one initial state when the example starts.
The example connects its parts through these roles:
| Role | Selected template | Scenario effect |
|---|---|---|
| Lending health | kamino-obligation-health | Makes the obligation unhealthy by setting its value limits and debt state. |
| Concentrated-liquidity market | whirlpool-popcat-sol | Sets the POPCAT/SOL Whirlpool liquidity and price state. |
| Constant-product market | raydium-amm-popcat-sol | Sets the matching Raydium AMM state and swap fee values. |
This combination is useful because the example tests a relationship across protocol accounts rather than one isolated account field. The account values are concrete fixture data, so callers can load the example as a known starting point and then exercise the runtime or a liquidation strategy against it.
References: crates/…/kamino-liquidation-arbitrage.json• L1-59 crates/…/overrides.yaml• L40-60 crates/…/overrides.yaml• L48-63
Runtime use
An example becomes active when a caller submits its scenario object through the scenario registration path. Registration places each override at the chosen base slot plus its relative slot, then the runtime materializes the overrides for the base slot immediately so slot-zero setup is available at once.
Registration and scheduling
The surfnet_registerScenario RPC method accepts the scenario and an optional base slot. If the caller omits the slot, the runtime uses the current slot. Each enabled or disabled choice remains part of the scenario until materialization decides whether to apply that override.
The registration flow is:
- Choose the example and its base slot.
- Register each override at
base slot + scenario-relative slot. - Materialize the overrides scheduled for the base slot.
- Let later slot progress materialize any later events.
This makes an example suitable for both immediate setup and time-based tests. A scenario with only slot-zero overrides behaves as a prepared initial state; a scenario with later relative slots can model change over time.
References: crates/…/surfnet_cheatcodes.rs• L1267-1287 crates/…/surfnet_cheatcodes.rs• L1369-1375 crates/…/svm.rs• L4016-4058 crates/…/surfnet_cheatcodes.rs• L2330-2352
Account state materialization
At its scheduled slot, the runtime resolves the target account, optionally refreshes its source data, and applies the values described by the selected template. This lets an example use a fixed public key or an address derived from values while keeping account data changes separate from address selection.
The fetchBeforeUse setting is important for examples that should start from fresh remote account data before the listed values are applied. The Kamino example enables this setting for all three overrides, which keeps the fixture tied to current account contents while still forcing the scenario-specific state.
References: crates/…/svm.rs• L2541-2587 crates/…/scenarios.rs• L464-507 crates/…/kamino-liquidation-arbitrage.json• L6-57
Example authoring boundaries
Scenario examples describe a concrete test situation, while override templates describe the reusable account shapes and fields that make that situation possible. Keeping these roles separate lets one template support many examples with different accounts, values, and slot arrangements.
Template and fixture roles
An example must refer to template IDs that the runtime knows. The template supplies the protocol account model and editable properties; the example supplies the target account and the values for this particular situation. The protocol template catalogue is loaded when the default registry is created, so a missing or renamed template ID breaks the example at the contract between the fixture and the registry.
References: crates/…/scenarios.rs• L373-404 crates/…/registry.rs• L35-55 crates/…/registry.rs• L114-139
Account and value contracts
Each override needs an address that can resolve and values that match the fields exposed by its template. A public key points to a known account; a program-derived address uses seed values from the same override. Seed values identify the account and are not themselves account fields, so they must not be treated as state changes.
This boundary matters most when an example uses a custom or derived account. A missing seed value prevents address resolution, while a value outside the template’s account shape prevents the intended state from being represented correctly.
References: crates/…/scenarios.rs• L63-106 crates/…/scenarios.rs• L184-248 crates/…/scenarios.rs• L893-965
Change checklist
When these files or sections are changed, remember to consider these:
- When an example changes a
templateId, confirm that the ID remains registered and that its protocol account type still exposes every value used by the example. - When an override address changes, check the full account identity and all PDA seed references. A valid account value with a different derived address can silently target a different fixture.
- When a relative slot changes, check whether the intended state is still present at scenario registration. Slot-zero setup is materialized immediately; later setup waits for the matching runtime slot.
- When
fetchBeforeUsechanges, check whether the example expects a fresh remote account as its base or a stable local fixture. - When a protocol template changes its editable field paths, review every example that writes those paths. The example payload does not define or validate a replacement field model.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
A scenario uses an unknown templateId. | The runtime cannot connect the fixture to an account model. | Keep example IDs aligned with the registered template IDs. |
| A PDA seed value is missing or cannot be converted. | The target account address cannot be resolved, so the override is not applied to the intended account. | Provide every seed reference in values and keep its format valid. |
| All changes are scheduled after the base slot when the test expects setup at registration. | The initial protocol state is not ready when the first action runs. | Use relative slot 0 for setup that must exist at the base slot. |
fetchBeforeUse is enabled while the remote account is unavailable or stale for the test. | The example may fail to materialize or may start from a different base state than expected. | Use it only when fresh source data is part of the scenario contract. |
| An override is disabled. | Its account change is skipped even though it remains in the scenario definition. | Check enabled when diagnosing a missing state change. |
Key files
| File | Why to read it |
|---|---|
| crates/…/kamino-liquidation-arbitrage.json | The concrete cross-protocol example and its fixture values. |
| crates/…/scenarios.rs | The shared meaning of scenarios, override instances, templates, addresses, and relative slots. |
| crates/…/surfnet_cheatcodes.rs | The public registration contract and the optional base-slot behavior. |
| crates/…/svm.rs | The high-level scheduling and materialization path used after registration. |
| crates/…/registry.rs | The source of the template IDs that examples must reference. |
| crates/…/README.md | The scenario concepts and the link between templates, IDLs, and scenario registration. |