Skip to content

Event-source Balancer V2 swaps - #33

Merged
KaiCode2 merged 2 commits into
mainfrom
feat/balancer-event-sourcing
Jul 7, 2026
Merged

Event-source Balancer V2 swaps#33
KaiCode2 merged 2 commits into
mainfrom
feat/balancer-event-sourcing

Conversation

@KaiCode2

@KaiCode2 KaiCode2 commented Jul 7, 2026

Copy link
Copy Markdown
Owner

Summary

  • event-source Balancer V2 Swap logs into warmed vault cash slots when cold-start has probed token cash locations
  • fall back to scoped balance-slot resync for liquidity changes, unknown token cash locations, stale slots, or invalid metadata shapes
  • add access-list/read-set cold-start fast paths, release-surface docs, protocol support docs, benchmarks, and live RPC parity coverage

Final review changes

  • guard the exact Balancer cash write path by pool specialization so non-TWO_TOKEN pools never treat the high managed-balance field as cash
  • reject duplicate token-cash field mappings instead of claiming exact event-sourced writes
  • apply rustfmt to stale files from the previous branch state

Validation

  • git diff --check
  • cargo fmt --all --check
  • cargo clippy --all-targets --all-features --no-deps -- -D warnings
  • cargo test --all-features
  • cargo test --no-default-features
  • cargo package --locked --allow-dirty

Live RPC parity tests remain #[ignore] / env-gated and were not run without E2E_RPC_URL.

KaiCode2 and others added 2 commits July 7, 2026 17:49
Extend the exact-write posture (V2/Solidly `Sync`, V3 `Mint`/`Burn`) to
Balancer V2 `Swap`s: apply each swapped token's exact vault `cash` delta
directly from the event's `amountIn`/`amountOut` with no RPC, falling back to a
balance-slots resync only where a token's cash location is unknown.

Mechanism
- Vault balances pack as [lastChangeBlock:32][managed:112][cash:112]; a swap
  changes only the 112-bit `cash` field, by exactly +/-amount (protocol fees
  settle lazily on join/exit, so there is no per-swap skim -- verified live).
- `BalancerTokenBalance` records each token's (slot, high_field). The discover
  cold-start probes it by matching getPoolTokens balances to warmed slot cash
  fields -- specialization-agnostic (TWO_TOKEN shared slot + GENERAL/MINIMAL
  per-token slots); ambiguous/zero/managed matches are skipped -> resync.
- TWO_TOKEN's two fields share one word, so both deltas accumulate into a single
  write (two writes from one pre-image would clobber).
- `PoolBalanceChanged` (join/exit) is now subscribed: it routes + resyncs the
  balances (event-sourcing its deltas is a follow-up).

`token_cash` is built in discover mode and carried through verify-only cold
starts, so event-sourcing works after either path; a slots-only pre-population
safely falls back to resync.

Tests
- 7 inline unit tests: cash-field round-trips, RMW add/sub/overflow/underflow,
  probe (TWO_TOKEN + GENERAL + ambiguous-skip), event_source_swap (shared-slot
  accumulation, separate slots, unknown/cold -> None).
- 4 reactive integration tests: warm TWO_TOKEN + GENERAL event-sourcing (Exact,
  no resync), unmapped-swap resync fallback, PoolBalanceChanged route+resync.
- Live RPC parity (env-gated, #[ignore]): for a real TWO_TOKEN and GENERAL swap,
  trace_replayTransaction(stateDiff) confirms the event-sourced cash fields
  reproduce the on-chain per-tx values exactly.

Offline gate green: tests (all-features/default/no-default), clippy -D warnings
(all-features + no-default), doc -D warnings (missing_docs), fmt.

Also fmt-normalizes 2 pre-existing nits in the Curve files inherited from the
base commit (same rustfmt the repo uses).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@KaiCode2
KaiCode2 force-pushed the feat/balancer-event-sourcing branch from ad6889c to 50cb65b Compare July 7, 2026 16:53
@KaiCode2
KaiCode2 merged commit 6298cbc into main Jul 7, 2026
9 checks passed
@KaiCode2
KaiCode2 deleted the feat/balancer-event-sourcing branch July 8, 2026 09:37
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