Skip to content

refactor(twap-monitor): port to Host trait + MockHost tests (BLEU-854)#24

Open
brunota20 wants to merge 1 commit into
feat/example-stop-loss-bleu-852from
feat/twap-monitor-host-trait-bleu-854
Open

refactor(twap-monitor): port to Host trait + MockHost tests (BLEU-854)#24
brunota20 wants to merge 1 commit into
feat/example-stop-loss-bleu-852from
feat/twap-monitor-host-trait-bleu-854

Conversation

@brunota20

Copy link
Copy Markdown
Collaborator

Summary

Closes the M2-side gap that surfaced in the post-M3 QA: twap-monitor still called `chain::request`, `local_store::*`, `cow_api::submit_order` directly inside dispatch logic, and the 13 existing tests covered only parsers and encoders. The state machine (indexer / poll / submit / retry / lifecycle) was unverified in unit.

Same recipe as #22 (price-alert / BLEU-851) and #23 (stop-loss / BLEU-852):

  • `strategy.rs` (new) - pure logic against `shepherd_sdk::host::Host`. `LogView<'a>` and `BlockInfo` keep the strategy wit-independent; `on_logs` / `on_block` are the entry points.
  • `lib.rs` (rewritten, 665 -> 165 lines) - wit-bindgen `generate!`, `WitBindgenHost` adapter implementing all four SDK host traits, `Guest` impl that destructures `types::Event` and delegates to `strategy`.

Tests

Status Test
Preserved 13 pure tests (decoders, encoders, watch-key round-trips, BLEU-830 outcome mapping)
New (MockHost) `index_records_new_watch_on_conditional_order_created`
New (MockHost) `index_overwrites_in_place_on_redelivered_log` (re-org replay guard)
New (MockHost) `poll_skips_when_next_block_gate_is_in_future`
New (MockHost) `poll_ready_submits_order_and_persists_submitted_uid`
New (MockHost) `submit_transient_error_leaves_state_unchanged_for_next_block`
New (MockHost) `submit_permanent_error_drops_watch`
New (MockHost) `poll_dont_try_again_drops_watch_and_gates` (uses a real `OrderNotValid` selector via the SDK-exported `IConditionalOrder` sol! interface)

Total: 20 tests (was 13).

Numbers

`.wasm` (release wasm32-wasip2) 313,926 bytes
Em-dashes added 0

Linear: BLEU-854.

Parallel to BLEU-855 (ethflow-watcher), which lands next stacked on this branch.

Test plan

  • `cargo test -p twap-monitor` -> 20 passing.
  • `cargo clippy --all-targets --workspace -- -D warnings` clean.
  • `cargo build --target wasm32-wasip2 --release -p twap-monitor` clean.
  • `grep -rn '—' modules/twap-monitor/` -> 0 matches.

Mirrors what BLEU-851 (price-alert) and BLEU-852 (stop-loss) did for
the M3 example modules. Closes the parallel M2 gap.

Before: the entire dispatch pipeline (indexer / poll / submit /
retry / lifecycle) lived in `lib.rs` alongside the wit-bindgen
glue, calling `chain::request`, `local_store::*`, `cow_api::submit_order`,
and `logging::log` directly. The 13 existing tests covered only
parsers and encoders - the state machine itself was unverified in
unit.

After:

1. `strategy.rs` (new) - pure logic against `shepherd_sdk::host::Host`.
   Defines `LogView<'a>` and `BlockInfo` so the strategy stays
   wit-independent; exposes `on_logs` / `on_block` entry points.

2. `lib.rs` (rewritten, 665 -> 165 lines) - wit-bindgen `generate!`,
   `WitBindgenHost` adapter implementing all four SDK host traits,
   `Guest` impl that destructures `types::Event` and delegates to
   `strategy`.

3. Tests against `shepherd_sdk_test::MockHost` (7 new) cover the
   dispatch matrix that was previously hand-verified only:

     - `index_records_new_watch_on_conditional_order_created`
     - `index_overwrites_in_place_on_redelivered_log` (re-org
       replay guard, BLEU-826 invariant)
     - `poll_skips_when_next_block_gate_is_in_future`
     - `poll_ready_submits_order_and_persists_submitted_uid`
     - `submit_transient_error_leaves_state_unchanged_for_next_block`
     - `submit_permanent_error_drops_watch`
     - `poll_dont_try_again_drops_watch_and_gates` (uses a real
       `OrderNotValid` selector via the SDK-exported sol! interface)

4. All 13 original pure tests preserved unchanged. Total: 20 tests
   (was 13).

Numbers:

- `.wasm` 313,926 bytes (release wasm32-wasip2).
- 20 tests passing; clippy clean on host + wasm32-wasip2.
- 0 em-dashes in the module tree.

Stacks on PR #23 (BLEU-852) so reviewers can compare strategy /
lib.rs split side-by-side with the price-alert and stop-loss
references.
@linear-code

linear-code Bot commented Jun 17, 2026

Copy link
Copy Markdown

BLEU-854

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant