API 集成 — 实现

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

Goal: Replace mock Oracle prices with real CoinGecko + Binance dual-source API, fix the critical keccak256 bug, and support 100+ trading pairs.

Architecture: Trait-based multi-source design (PriceSource trait) with CoinGeckoSource, BinanceSource, and MockSource implementations. A PriceAggregator fetches all sources in parallel, computes median prices, detects deviations, and calculates confidence scores. The existing OracleService uses the aggregator instead of hardcoded mock data.

Tech Stack: Rust, sha3 (keccak256), reqwest (HTTP), serde_json, tokio (async), async-trait


File Map

All paths relative to /Users/judybaby/CodeBase/github/Layer2/.

Existing files to modify:

  • baby-modules/oracle-wiring/Cargo.toml — add sha3, reqwest, serde_json, futures deps

  • baby-modules/oracle-wiring/src/lib.rs — add pub mod sources;

  • baby-modules/oracle-wiring/src/config.rs — replace symbols: Vec<String> with symbols: Vec<SymbolConfig> + add sources field

  • baby-modules/oracle-wiring/src/service.rs — fix keccak256, adapt to new config

  • baby-modules/oracle-wiring/src/wiring.rs — replace mock fetcher with PriceAggregator

New files to create:

  • baby-modules/oracle-wiring/src/sources/mod.rs — PriceSource trait + SourcePrice

  • baby-modules/oracle-wiring/src/sources/mock.rs — MockSource

  • baby-modules/oracle-wiring/src/sources/coingecko.rs — CoinGeckoSource

  • baby-modules/oracle-wiring/src/sources/binance.rs — BinanceSource

  • baby-modules/oracle-wiring/src/sources/aggregator.rs — PriceAggregator

Config file to modify:

  • config/baby-chain.toml — new [[oracle.symbols]] table array format


Task 1: keccak256 Fix + PriceSource Trait + MockSource

Files:

  • Modify: baby-modules/oracle-wiring/Cargo.toml

  • Modify: baby-modules/oracle-wiring/src/lib.rs

  • Modify: baby-modules/oracle-wiring/src/service.rs:156-171

  • Create: baby-modules/oracle-wiring/src/sources/mod.rs

  • Create: baby-modules/oracle-wiring/src/sources/mock.rs

Step 1: Add dependencies to Cargo.toml

Add these lines to [dependencies] in baby-modules/oracle-wiring/Cargo.toml:

Step 2: Fix keccak256 in service.rs

Replace lines 156-171 in baby-modules/oracle-wiring/src/service.rs:

Old:

New:

Note: Change visibility to pub — it will be used by sources/mod.rs and tests.

Step 3: Add keccak256 test to service.rs

Add this test to the #[cfg(test)] mod tests block in service.rs (after the existing tests):

Also add hex = "0.4" to [dev-dependencies] in Cargo.toml:

Step 4: Create sources/mod.rs with PriceSource trait

Create baby-modules/oracle-wiring/src/sources/mod.rs:

Step 5: Create sources/mock.rs

Create baby-modules/oracle-wiring/src/sources/mock.rs:

Step 6: Add pub mod sources; to lib.rs

In baby-modules/oracle-wiring/src/lib.rs, add after pub mod wiring;:

And add to the re-exports:

Step 7: Run tests

Expected: All tests pass (existing 3 + new keccak256 test + 3 mock tests = 7 total).

Note: The existing test_encode_oracle_calldata_with_prices and test_oracle_wiring_layer_build tests use the old config format (symbols: Vec<String>). They will break in Task 5 when we change the config — that's intentional. For now they should still pass.

Step 8: Commit


Task 2: CoinGeckoSource Implementation + Tests

Files:

  • Create: baby-modules/oracle-wiring/src/sources/coingecko.rs

Step 1: Write CoinGeckoSource

Create baby-modules/oracle-wiring/src/sources/coingecko.rs:

Step 2: Run tests

Expected: 4 unit tests pass (the #[ignore] test is skipped).

Step 3: Commit


Task 3: BinanceSource Implementation + Tests

Files:

  • Create: baby-modules/oracle-wiring/src/sources/binance.rs

Step 1: Write BinanceSource

Create baby-modules/oracle-wiring/src/sources/binance.rs:

Step 2: Run tests

Expected: 5 unit tests pass (the #[ignore] test is skipped).

Step 3: Commit


Task 4: PriceAggregator Implementation + Tests

Files:

  • Create: baby-modules/oracle-wiring/src/sources/aggregator.rs

Step 1: Write PriceAggregator

Create baby-modules/oracle-wiring/src/sources/aggregator.rs:

Step 2: Run tests

Expected: All 15 tests pass (6 unit + 7 async + 2 helper tests).

Step 3: Run all tests together

Expected: All tests pass (Task 1: 7 + Task 2: 4 + Task 3: 5 + Task 4: 15 = ~31 tests).

Step 4: Commit


Task 5: OracleConfig Expansion + Wiring Integration

This task modifies the config and wiring to use SymbolConfig and PriceAggregator instead of hardcoded mock prices.

Files:

  • Modify: baby-modules/oracle-wiring/src/config.rs

  • Modify: baby-modules/oracle-wiring/src/service.rs

  • Modify: baby-modules/oracle-wiring/src/wiring.rs

  • Modify: baby-modules/oracle-wiring/src/lib.rs

Step 1: Rewrite config.rs with SymbolConfig

Replace the entire contents of baby-modules/oracle-wiring/src/config.rs:

Add toml to [dev-dependencies] in Cargo.toml (for config deserialization tests):

Step 2: Update service.rs to use new config

The all_prices_fresh method currently iterates config.symbols as Vec<String>. Update it to use SymbolConfig:

Replace the all_prices_fresh method in baby-modules/oracle-wiring/src/service.rs:

Old (lines 41-47):

New:

Also update encode_oracle_calldata — replace the symbols iteration (lines 56-61):

Old:

New:

Step 3: Update service.rs tests

The existing tests create OracleConfig with symbols: vec!["ETH/USD".to_string(), ...]. Update them to use SymbolConfig:

Replace the test helper in service.rs tests. Where tests construct OracleConfig, use the new format. For example:

Old:

New:

Apply the same pattern to test_encode_oracle_calldata_with_prices (which has 2 symbols).

Add use crate::sources::SymbolConfig; at the top of the test module.

Step 4: Rewrite wiring.rs to use PriceAggregator

Replace the entire contents of baby-modules/oracle-wiring/src/wiring.rs:

Step 5: Update lib.rs re-exports

Replace the entire contents of baby-modules/oracle-wiring/src/lib.rs:

Step 6: Run all tests

Expected: All tests pass (~35+ tests).

Step 7: Commit


Task 6: Config + Compile Verification + Dev-Log

Files:

  • Modify: config/baby-chain.toml

  • Modify: docs/dev-log.md

Step 1: Update baby-chain.toml

Replace the [oracle] section in config/baby-chain.toml:

Old:

New:

Step 2: Run full oracle-wiring test suite

Expected: All tests pass. Count total tests.

Step 3: Verify era-core still compiles

Expected: Compiles successfully (era-core doesn't depend on oracle-wiring yet, so no breakage).

Step 4: Verify Foundry tests still pass

Expected: All existing tests pass (this change doesn't touch Solidity contracts).

Step 5: Update dev-log

Append a new section to docs/dev-log.md documenting this work:

Step 6: Commit


Summary

Task
Tests
Files Created
Files Modified

1: keccak256 + PriceSource + Mock

7

sources/mod.rs, sources/mock.rs

Cargo.toml, lib.rs, service.rs

2: CoinGeckoSource

5

sources/coingecko.rs

3: BinanceSource

6

sources/binance.rs

4: PriceAggregator

15

sources/aggregator.rs

5: Config + Wiring

6

config.rs, service.rs, wiring.rs, lib.rs

6: Config + Dev-Log

0

baby-chain.toml, dev-log.md

Total

~39

5 new

7 modified

Last updated