Skip to content

feat: Add oracle reroute example - #12

Open
petarvujovic98 wants to merge 1 commit into
mainfrom
08-04-feat_add_oracle_reroute_example
Open

feat: Add oracle reroute example#12
petarvujovic98 wants to merge 1 commit into
mainfrom
08-04-feat_add_oracle_reroute_example

Conversation

@petarvujovic98

@petarvujovic98 petarvujovic98 commented Aug 4, 2026

Copy link
Copy Markdown

TL;DR

Upgrades simulator-api and simulator-client to v0.17.0 from the git source and rewrites the counterfactual_flow example to use account overrides with a time-shifted schedule instead of a discovery-based program upgrade hook.

What changed?

simulator-api and simulator-client are now pulled from the nitro-stream git repository at a pinned revision rather than from crates.io. The new versions add spl-token-2022-interface, spl-token-interface, thiserror, uuid, solana-account-decoder, and solana-sdk-ids as dependencies, and pull in tonic, axum, hyper-timeout, matchit, pin-project, httpdate, and uuid transitively.

The counterfactual_flow example is substantially reworked:

  • The previous approach paused on BPF Loader Upgrade events and injected a replacement transaction. That discovery-based flow, along with discovery.rs, is removed.
  • The new approach uses account overrides: the venue's oracle, curve, or fee account is rewritten per slot using real captured states on a shifted schedule. --lag K posts the state from slot s-K at slot s, pricing the cost of staleness, while --lead K posts a future state — with caveats for venues that stamp their own last-update slot into the account.
  • Three subcommands replace the single command: capture records the account's per-slot states to JSONL via subscribe_account_diffs, run executes one session with optional overrides, and compare runs an unmodified baseline alongside the modified world and produces a per-leg delta report with median, mean, and p90 bps statistics.
  • --filter-mint and --filter-pool flags scope which swaps are requoted. --program-id is now optional and resolves the venue's Metis route label only when provided.
  • ManagedBacktestSession replaces the manual BacktestClient session loop. ConnectionArgs centralises URL handling, resolving bare hostnames to wss:// and inferring http/https for the RPC endpoint based on the websocket scheme.
  • session_id() call sites in quote_compare and regression_test are updated to use map_or_else since the return type changed.
  • Unit tests cover build_shift_actions (positive and negative shifts, empty capture), delta_bps (signed scaling, zero-baseline, sub-bps precision), join_legs (key matching, zero-baseline exclusion), delta_summary (percentile correctness), and ConnectionArgs URL construction.

Documentation in both READMEs is updated to reflect the new methodology, a worked result over a SOL/USDC prop-AMM pool showing a 99.6% fill-rate collapse at four seconds of staleness, and guidance on Metis quoting non-determinism and the minimum replicate count needed to avoid false positives.

How to test?

export SIMULATOR_API_KEY=<key>
POOL=8FnX3xo2yYw3EUE6w3nQA4GfXGS9wpK6oj3veJpbFzLo
PROGRAM=BiSoNHVpsVZW2F7rx2eQ59yQwKxzU5NvBcmKshCSUypi

# Record account states across the slot range.
cargo run --bin counterfactual_flow -- capture \
  --start-slot 433838452 --slot-count 9999 \
  --account $POOL --out capture.jsonl

# Run the unmodified baseline.
cargo run --bin counterfactual_flow -- run \
  --start-slot 433838452 --slot-count 9999 \
  --account $POOL --filter-pool $POOL --program-id $PROGRAM \
  --out baseline.jsonl

# Compare baseline against the venue updating 10 slots slower.
cargo run --bin counterfactual_flow -- compare \
  --start-slot 433838452 --slot-count 9999 \
  --account $POOL --filter-pool $POOL --program-id $PROGRAM \
  --capture capture.jsonl --lag 10 \
  --report compare-report.jsonl

Run the unit tests with cargo test --bin counterfactual_flow.

Why make this change?

The discovery-based approach required a real program upgrade event in the replayed range and only allowed injecting a replacement transaction at that moment, making it impractical for testing arbitrary parameter changes. Account overrides are more general: any account the venue reads — oracle, curve, fee config — can be rewritten per slot using real, well-formed historical states on a shifted schedule. This directly quantifies the value of latency improvements and the cost of staleness without needing to construct synthetic account data or wait for an upgrade event.

petarvujovic98 commented Aug 4, 2026

Copy link
Copy Markdown
Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@petarvujovic98
petarvujovic98 force-pushed the 08-04-feat_add_oracle_reroute_example branch from 57a4be3 to 8a43e48 Compare August 5, 2026 11:25
@petarvujovic98 petarvujovic98 self-assigned this Aug 5, 2026
@petarvujovic98 petarvujovic98 added the enhancement New feature or request label Aug 5, 2026 — with Graphite App
@petarvujovic98
petarvujovic98 force-pushed the 08-04-feat_add_oracle_reroute_example branch from 8a43e48 to 5dee162 Compare August 5, 2026 14:39
@petarvujovic98
petarvujovic98 marked this pull request as ready for review August 5, 2026 14:39
@petarvujovic98
petarvujovic98 force-pushed the 08-04-feat_add_oracle_reroute_example branch from 5dee162 to 62fdc28 Compare August 5, 2026 16:04
Comment thread Cargo.toml Outdated
Comment on lines +17 to +18
simulator-api = { git = "https://github.com/nitro-svm/nitro-stream", rev = "f49c4f11bf18a4648ac39ef61ed220572fb3e53f" }
simulator-client = { git = "https://github.com/nitro-svm/nitro-stream", rev = "f49c4f11bf18a4648ac39ef61ed220572fb3e53f" }

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will have to be updated to the client/api version that gets published after the nitro-stream PRs land

Comment thread src/bin/counterfactual_flow/README.md Outdated
Comment thread src/bin/counterfactual_flow/README.md Outdated
@petarvujovic98
petarvujovic98 force-pushed the 08-04-feat_add_oracle_reroute_example branch 4 times, most recently from 7337e1e to b5956d6 Compare August 6, 2026 14:33
@petarvujovic98
petarvujovic98 force-pushed the 08-04-feat_add_oracle_reroute_example branch from b5956d6 to 1b1fa6c Compare August 7, 2026 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants