raw

M1 local end-to-end review: DEX (RFP-004), TWAP oracle (RFP-019), stablecoin (RFP-013)

Date: 2026-10-15. Reviewer: laptop/SPEL (Václav's agent).

Target:

Aim: find where these programs break, not confirm they run. Every action was logged with a state snapshot. Raw data:

All findings below were observed on chain unless marked (code), which means read from source.


0. Setup and what was exercised

Piece How Result
LEZ v0.2.4 sequencer + wallet built from tag, standalone ok
6 guest programs make build-programs (Docker, release profile) ok. token / amm / twap_oracle ImageIDs are identical to DEPLOYMENTS.md, so the build is reproducible. token_mint_authority differs (1ea6d932… vs b0163189…), meaning its source changed since the Sep-10 deploy.
Unit + integration tests cargo test --workspace (dev mode) ~950 tests, 0 failures. They miss every critical finding below (§4).
Program CLI SPEL v0.7.0, then SPEL 415b7bc (see F-ST3) ok
AMM core module nix build .#amm-module + lez_core @ acf0cd5 + logoscore daemon works: config read, pool resolve, quote, swap submit
Stablecoin module not driven: today it only exposes discovery, param reads and init (see the module issues on GitHub) n/a
TWAP module does not exist. The AMM module only has the two setup calls. gap
apps/amm UI in real Basecamp Basecamp-Desktop v0.3.2 AppImage, own --user-dir, on an isolated Xvfb display, driven with xdotool, full screen recording works: swap, add liquidity, remove liquidity, create pool, all verified on chain. Serious UX bugs, see §2b

Note on the restarted sequencer: it hit a 2-hour tool limit once and was restarted on the same state directory. All state survived.


1. TWAP oracle (RFP-019): the core design does not produce a manipulation-resistant TWAP

Pool TKA/TKB started at 1e18 A / 2e18 B (spot 2.0, tick 6932). The window was 600,000 ms (10 min), observation capacity 2048, so min_interval = 292 ms.

Observation buffer, reconstructed from the account itself (exact, not from snapshots)

obs tick credited price credited for what really happened in that interval
1 6932 2.000 214.6 s true price ≈ 2.0. Correct.
2 -976 0.907 654.5 s Price was ≈ 2.0 for almost all of it. 0.907 existed only between the attacker's 5e17 A swap (A1a) and the attacker's own record_tick (A1b), about one block.
3 8140 2.257 32.1 s True tick after unwind was 9013. The clamp anchored on the manipulated -976, so honest recovery was capped at -976 + 9116 = 8140.
4 9857 2.680 66.0 s A second manipulation (A2a, tick 408) was never recorded because nobody recorded before the unwind. Whoever times record_tick decides which prices exist.
5 741 (clamped) 1.077 4803 s (80 min) First record after the pool was drained (§2). A value that never existed, credited for 80 minutes.
6-8 -131,245 0.000002 15-17 s each Pool drained to 750/1641 units, then a 1e6-unit swap. The clamp was already gone by the second record.

The published price fell from 2.0 to 1.131 (-43%) right after A1 (one manipulated block plus an attacker-timed record), and stayed at 1.128 after the price recovered.

Findings

Against RFP-019 hard requirements (code + observed)

Requirement Status
Recording must not depend on a keeper or crank; captured as a side effect of pool activity Not met. Pool ops only update the current tick; observations require separate record_tick calls (verified: swaps never added entries).
Cardinality default 1, expandable to 65,535 Not met. Fixed 2048, no expansion instruction.
Read-only query interface (pool + window → TWAP + timestamps used), maxAge Not met. Only the state-writing publish_price; the window is ignored (T4).
Owner registers feed sources; per-pool params (e.g. MAX_TICK_DELTA) governed via RFP-001 Not met. No owner or registration; anyone with a signer can create feeds (T7); MAX_TICK_DELTA is a compile-time const.
Canonical price account struct with base/quote/price/timestamp/source/confidence; reject zero/invalid Struct present; zero rejected (S1d). No scale/decimals field (see F-ST1). Not published as a standalone SPEL IDL.
SDK/module, dashboard mini-app, CLI, reference consumer + multi-source example, clear errors Mostly missing: module has setup only, no dashboard, generic spel CLI only, errors invisible (F-DX1).

2. DEX / AMM (RFP-004): the core mechanics hold

Verified working (observed):

Findings:

2b. DEX UI in real Basecamp (RFP-004 M1 "UI working")

Setup:

What works (observed on chain)

Flow UI preview On-chain result
Load app Wallet auto-adopted, lez_core synced to the chain head ok (01)
Swap 100 TKA → TKB buy 98, min 97 reserves 10000/10000 → 10100/9902, i.e. +100 A, -98 B, exactly as quoted (05, 06, video)
Add liquidity 1000 TKA auto-filled 683 TKB, deposit 999 + 683, LP 825 +999 A, +683 B, +825 LP, exact (21, 22b, video)
Remove liquidity 50% 5943 A + 4060 B −4912 LP, −5943 A, −4060 B, exact (25, video)
Create pool TKA/TKC 1001 + 1001, 1 LP pool ikunktkb… created, reserves 1001/1001, LP supply 1001 (32, 34, video)
Guards "Amount too small" for dust; Add-liquidity CTA disabled with empty fields ok (10, 20)

UI findings


3. Stablecoin (RFP-013): two critical bugs, a bootstrap trap, and a fund lock

Setup: collateral = TKA, redemption price 0.5, CR 1.5x, Kp = 1.0 (RAY), Ki = 0, fee 1 + 1.5e-12 /ms (≈ 4.8%/yr).

step result detail
SC0 oracle (fresh person-fed source) ok Price account base = future stablecoin PDA, quote = TKA, price 0.5 in Q64.64
SC1 initialize_program ok only with SPEL 415b7bc SPEL v0.7.0 CLI cannot encode i128 (F-ST3)
SC3 open_position (fresh owner) ok 1e18 A locked
SC4 generate_debt (same owner) rejected DeclaredAccountMissingFromOutput for the owner (F-ST2)
SC5 accrue_stability_fee ok rate 1.000000841 after 561 s, matches (1 + 1.5e-12)^561000
SC6 update_redemption_rate ok but wrong rate became 1.00001 /ms instead of 1.0 (F-ST1)
SC7 refresh_globals ~20 min later ok redemption price 0.5 → 91,499.8 (×183,000)
SC8 open_position (auth-transfer-initialized owner) ok
SC9 generate_debt 1e15 vs 1e18 collateral rejected "Position is undercollateralized": minting bricked by F-ST1
SC10 deposit_collateral (same owner, 2nd op) ok confirms the auth-transfer workaround for owners
SC11 admin set_controller_gains (admin's 2nd action) rejected "invalid length: expected 32 bytes, got 0". Unexplained; admin can never act again (see also F-ST2)
auth-transfer init on the stuck owner rejected "Account must be uninitialized": the SC3 position's 1e18 collateral is permanently locked

4. Cross-cutting


5. What M1 needs (proposal)

  1. Blockers before any testnet deploy:

    • F-ST1 (scale);
    • F-ST2 (signer accounts; SPEL issue + program-side declaration or onboarding with auth-transfer);
    • T1 + T2 (accumulate the pre-change tick using last_updated; clamp against the clamped anchor);
    • F-ST3 (cut SPEL v0.7.1 on the v0.2.4 line);
    • UI: U1 (track inclusion, never show a rejected tx as success), U2 (pool-creation lock warning/defaults), U10 (fix the lez_core revision in the app README).
  2. Agree with fryorcraken on M1 scope:

    • RFP-019 hard requirements unmet (keeper-free recording, cardinality, query interface, registration/governance, dashboard);
    • "privacy-preserving" DEX not possible on 0.2.4.

    Either fix them or explicitly descope them for M1.

  3. Docs: update the runbook and module README (D4, D5); add a local e2e script (this harness is a start: harness/).

  4. Then deploy to testnet-legacy.lez.logos.co (nothing is deployed there today).

6. Reproduce

The local environment is in /home/vpavlin/devel/m1-local:

Harness scripts:

Background processes still running: the sequencer (port 3040) and the logoscore daemon. Stop them with pkill -f sequencer_service and result-logoscore/bin/logoscore stop.