自动结算 — 实现

For Claude: REQUIRED SUB-SKILL: Use superpowers:executing-plans to implement this plan task-by-task.

Goal: Build a Rust background service that automatically settles FastWithdrawalPoolV2 withdrawal requests using era L2→L1 message proofs, and reclaims timed-out requests.

Architecture: Follows the existing baby-modules pattern (oracle-wiring, credit-score). Config loads from TOML, a WiringLayer spawns a tokio background task that polls the FastWithdrawalPoolV2 contract via JSON-RPC, and submits settlement/reclaim transactions when conditions are met. Pure RPC + manual ABI encoding (no ethers-rs).

Tech Stack: Rust (edition 2021), tokio, serde, sha3, reqwest, serde_json, hex, anyhow, tracing

Design Doc: docs/plans/P3-bridge/2026-03-10-bridge-enhancer-design.md


Task 1: Update Cargo.toml Dependencies

Files:

  • Modify: baby-modules/bridge-enhancer/Cargo.toml

Step 1: Update Cargo.toml with all required dependencies

Replace the contents of baby-modules/bridge-enhancer/Cargo.toml:

[package]
name = "baby-bridge-enhancer"
description = "BabyDriver Bridge Enhancer — auto-settlement monitor for FastWithdrawalPoolV2"
version.workspace = true
edition.workspace = true
license.workspace = true

[dependencies]
tokio = { version = "1", features = ["full"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
tracing = "0.1"
anyhow = "1"
sha3 = "0.10"
reqwest = { version = "0.12", features = ["json"] }
hex = "0.4"

Step 2: Verify workspace compiles

Run: cd /Users/judybaby/CodeBase/github/Layer2/baby-modules && cargo check -p baby-bridge-enhancer Expected: Compiles with no errors

Step 3: Commit


Task 2: config.rs — BridgeEnhancerConfig

Files:

  • Create: baby-modules/bridge-enhancer/src/config.rs

  • Modify: baby-modules/bridge-enhancer/src/lib.rs

Step 1: Create config.rs with BridgeEnhancerConfig struct + TOML deserialization + tests

Step 2: Update lib.rs to declare the config module

Step 3: Add toml dependency (needed for config parsing)

Add toml = "0.8" to Cargo.toml dependencies section.

Step 4: Verify compilation + run tests

Run: cd /Users/judybaby/CodeBase/github/Layer2/baby-modules && cargo test -p baby-bridge-enhancer -- --nocapture Expected: 4 tests PASS

Step 5: Commit


Task 3: rpc.rs — JSON-RPC Helpers + ABI Encoding

Files:

  • Create: baby-modules/bridge-enhancer/src/rpc.rs

  • Modify: baby-modules/bridge-enhancer/src/lib.rs

Step 1: Create rpc.rs with RPC call helpers and ABI encode/decode functions

This module provides:

  • Generic JSON-RPC call helper

  • eth_call wrapper

  • Function selector computation (keccak256)

  • ABI encoding for FastWithdrawalPoolV2 view functions

  • ABI decoding for getRequest() return values

Step 2: Update lib.rs

Step 3: Verify compilation + run tests

Run: cd /Users/judybaby/CodeBase/github/Layer2/baby-modules && cargo test -p baby-bridge-enhancer -- --nocapture Expected: All tests PASS (4 config + 11 rpc = 15)

Step 4: Commit


Task 4: monitor.rs — SettlementMonitor Core Logic

Files:

  • Create: baby-modules/bridge-enhancer/src/monitor.rs

  • Modify: baby-modules/bridge-enhancer/src/lib.rs

Step 1: Create monitor.rs with the core polling + settlement logic

The monitor tracks last_scanned_id and scans new requests each cycle. For each unsettled request, it either reclaims (if timed out) or logs it as a settlement candidate. Actual settlement transaction signing is deferred to a future phase — MVP logs the action.

Step 2: Update lib.rs

Step 3: Verify compilation + run tests

Run: cd /Users/judybaby/CodeBase/github/Layer2/baby-modules && cargo test -p baby-bridge-enhancer -- --nocapture Expected: All tests PASS (4 config + 11 rpc + 6 monitor = 21)

Step 4: Commit


Task 5: wiring.rs — SharedBridgeService + WiringLayer + Background Loop

Files:

  • Create: baby-modules/bridge-enhancer/src/wiring.rs

  • Modify: baby-modules/bridge-enhancer/src/lib.rs

Step 1: Create wiring.rs with the WiringLayer pattern and background polling loop

Step 2: Update lib.rs with final module layout and re-exports

Step 3: Verify compilation + run all tests

Run: cd /Users/judybaby/CodeBase/github/Layer2/baby-modules && cargo test -p baby-bridge-enhancer -- --nocapture Expected: All tests PASS (4 config + 11 rpc + 6 monitor + 3 wiring = 24)

Step 4: Commit


Task 6: Full Test Suite + Workspace Compilation Verification

Files: None (verification only)

Step 1: Run bridge-enhancer tests

Run: cd /Users/judybaby/CodeBase/github/Layer2/baby-modules && cargo test -p baby-bridge-enhancer -v Expected: 24 tests PASS

Step 2: Run full baby-modules workspace tests to verify no regressions

Run: cd /Users/judybaby/CodeBase/github/Layer2/baby-modules && cargo test Expected: All crate tests pass (oracle-wiring + credit-score + bridge-enhancer)

Step 3: Run Solidity tests to confirm no regressions

Run: cd /Users/judybaby/CodeBase/github/Layer2/contracts && forge test --match-contract FastWithdrawalPoolV2 -v Expected: 31 tests PASS

Step 4: Commit if any adjustments were made


Task 7: Update dev-log + docs README

Files:

  • Modify: docs/dev-notes/dev-log.md

  • Modify: docs/README.md

Step 1: Add Phase 3 bridge-enhancer entry to dev-log

Append to the dev-log's latest section:

Step 2: Update docs/README.md — add bridge-enhancer to P3-bridge section

In the ### P3-bridge/ — Bridge 增强 table, add a row for the bridge-enhancer design doc:

Step 3: Commit


Summary

Task
Description
Tests

1

Cargo.toml dependencies

2

config.rs — BridgeEnhancerConfig + TOML

4

3

rpc.rs — JSON-RPC + ABI encode/decode

+11 = 15

4

monitor.rs — SettlementMonitor + classification

+6 = 21

5

wiring.rs — WiringLayer + background loop

+3 = 24

6

Full test suite verification

7

dev-log + docs update

Total: 7 tasks, 24 Rust tests, ~7 commits

Files created:

  • baby-modules/bridge-enhancer/src/config.rs

  • baby-modules/bridge-enhancer/src/rpc.rs

  • baby-modules/bridge-enhancer/src/monitor.rs

  • baby-modules/bridge-enhancer/src/wiring.rs

Files modified:

  • baby-modules/bridge-enhancer/Cargo.toml

  • baby-modules/bridge-enhancer/src/lib.rs

  • docs/dev-notes/dev-log.md

  • docs/README.md

Future work (not in this plan):

  • Transaction signing with secp256k1 (settlement + reclaim transactions)

  • Proof fetching from era Mailbox for actual settlement

  • Integration with baby-chain.toml config loading

  • Metrics endpoint for Grafana dashboard

Last updated