Skip to content

feat: multi-protocol trait + Aave V3 on Ethereum + bps margin profit gate#427

Merged
obchain merged 2 commits intomainfrom
feat/v0.2-multi-protocol-aave-eth-bps-gate
May 6, 2026
Merged

feat: multi-protocol trait + Aave V3 on Ethereum + bps margin profit gate#427
obchain merged 2 commits intomainfrom
feat/v0.2-multi-protocol-aave-eth-bps-gate

Conversation

@obchain
Copy link
Copy Markdown
Owner

@obchain obchain commented May 6, 2026

Summary

Three coupled changes that ship v0.2 of the bot. Tight coupling means they share config.rs and cli/main.rs edits — splitting them now would be artificial. They are reviewed as one unit but the body below treats each theme separately.

1. min_profit_margin_bps replaces the absolute USD floor

The old min_profit_usd_1e6 = $5 floor scaled poorly: loose on small trades, meaningless on large ones. Replaced with a basis-points margin gate:

gross_swap_output_wei >= total_cost_wei * (10_000 + min_profit_margin_bps) / 10_000
  • Default 1000 bps (10% margin), validated <= 10_000.
  • Drop reason BelowMinMargin.
  • Fork profile lowers to 100 bps for demo staging.
  • crates/charon-core/src/profit.rs:315, config.rs:303.

2. LendingProtocol trait + ProtocolKind factory

Pipeline now holds Arc<dyn LendingProtocol> and never names a concrete adapter. ProtocolKind enum is #[non_exhaustive] so adding variants is non-breaking. Factory in charon-protocols/src/lib.rs matches on the kind and returns a trait object.

VenusAdapter migrated to impl LendingProtocol unchanged behaviourally. Scanner, executor, profit calc, queue, simulator, submitter, metrics emitter, and Grafana dashboard are protocol-agnostic.

  • crates/charon-core/src/traits.rs:59 — trait
  • crates/charon-core/src/config.rs:588ProtocolKind enum
  • crates/charon-protocols/src/lib.rs:52 — factory

3. Aave V3 adapter + Ethereum support

AaveV3Adapter implements LendingProtocol against the Aave V3 Pool and PoolDataProvider. Reuses the lenient getReserveData decoder (PR #421) so Ethereum's 15-field tuple parses cleanly alongside BSC's truncated 12-field one.

CharonLiquidatorAave.sol is the on-chain twin: flashLoanSimplePool.liquidationCall → Uniswap V3 exactInputSingle → repay Aave + 5 bps premium → sweep residual to immutable cold wallet.

  • config/fork-eth.toml — Ethereum fork profile pinned at block 20459016 (Aug 5 2024 yen-unwind cascade).
  • deploy/compose/local-stack-eth.yml — Prometheus on 127.0.0.1:9093, Grafana on 127.0.0.1:3001 so BSC and ETH stacks run side-by-side without collision.
  • crates/charon-protocols/tests/aave_v3_{connect,fetch}.rs — integration tests against a live anvil ETH fork; both pass.

Demo numbers (reproducible)

FORK_RPC=https://eth.drpc.org FORK_BLOCK=20459016 FORK_CHAIN_ID=1 \
  CHARON_SKIP_DEV0_NONCE_RESET=1 ./scripts/anvil_fork.sh

# wipe slot, deploy CharonLiquidatorAave (see Notion walkthrough)

CHARON_SIGNER_KEY=0xac0974...0ff80 \
CHARON_PRICE_MAX_AGE_SECS=63072000 \
./target/release/charon --config config/fork-eth.toml replay \
  --protocol aave_v3_eth --block 20459016 \
  --borrower-file /tmp/seed-eth.txt --hold-secs 600

emits 2 / 4 seeded borrowers, total predicted net profit $14,538.74:

Borrower Debt → Collateral Predicted profit
0x99E881…5Ddc3 DAI → WETH $7,687.01
0x66466…36DE5 DAI → WETH $6,851.73
0xe6a5e9…9da4 USDT → LINK dropped (debug pending)
0xd8cd65…566b USDC → WETH dropped (debug pending)

The two silent drops between profit gate and queue are tracked as a follow-up.

Test plan

  • cargo build --release passes
  • cargo test --workspace passes
  • cargo test -p charon-protocols --test aave_v3_connect passes against ETH fork at 20459016
  • cargo test -p charon-protocols --test aave_v3_fetch passes against ETH fork at 20459016
  • BSC × Venus replay at block 91323624 still emits 4 / 4 with $39,524.48 (regression check)
  • ETH × Aave V3 replay at block 20459016 emits 2 / 4 with $14,538.74
  • Grafana ETH stack on :3001 renders: positions-by-bucket, queue depth, profit cumulative
  • Adding a third chain × Aave V3 (e.g. BSC × Aave V3) requires only TOML — verified by reading factory code path

obchain added 2 commits May 6, 2026 12:51
Three coupled changes that ship v0.2 of the bot.

1. min_profit_margin_bps replaces the absolute USD floor

The old min_profit_usd_1e6 = $5 floor scaled poorly: loose on small trades,
meaningless on large ones. Replaced with a basis-points margin gate:

    gross_swap_output_wei >= total_cost_wei * (10_000 + min_profit_margin_bps) / 10_000

Default 1000 bps (10% margin), validated <= 10_000. Drop reason
BelowMinMargin. Field added to BotConfig with serde default; fork profile
lowers to 100 bps for demo staging.

2. LendingProtocol trait + ProtocolKind factory

Pipeline now holds Arc<dyn LendingProtocol> and never names a concrete
adapter. ProtocolKind enum is #[non_exhaustive] so adding variants is
non-breaking. Factory in charon-protocols/src/lib.rs matches on the kind
and returns a trait object.

VenusAdapter migrated to impl LendingProtocol unchanged behaviourally.
Scanner, executor, profit calc, queue, simulator, submitter, metrics
emitter, and Grafana dashboard are protocol-agnostic.

3. Aave V3 adapter + Ethereum support

AaveV3Adapter implements LendingProtocol against the Aave V3 Pool and
PoolDataProvider. Tolerates Ethereum's 15-field getReserveData tuple via
the same lenient decoder used for BSC's truncated 12-field tuple (PR #421).

CharonLiquidatorAave.sol is the on-chain twin: Aave flashLoanSimple ->
Pool.liquidationCall -> Uniswap V3 exactInputSingle -> repay Aave + 5 bps
premium -> sweep residual to immutable cold wallet.

config/fork-eth.toml ships a complete Ethereum fork profile pinned at
block 20459016 (Aug 5 2024 yen-unwind cascade). deploy/compose/local-stack-eth.yml
runs Prometheus + Grafana on parallel ports (9093 / 3001) so BSC and ETH
stacks can run side-by-side without collision.

Demo numbers reproducible:

  cargo run -p charon-cli --release -- --config config/fork-eth.toml \
    replay --protocol aave_v3_eth --block 20459016 \
    --borrower-file /tmp/seed-eth.txt --hold-secs 600

emits 2 / 4 seeded borrowers, total predicted net profit $14,538.74. The
two silent drops between profit gate and queue (LINK collateral, USDC->WETH
single-hop) are tracked as a follow-up debugging task.

Integration tests aave_v3_connect and aave_v3_fetch exercise the adapter
against a live anvil ETH fork; both pass on the pinned block.

Closes the v0.2 milestone: multi-chain (BSC + ETH), multi-protocol
(Venus + Aave V3), single shared pipeline.
@obchain obchain merged commit 30f8938 into main May 6, 2026
4 checks passed
@obchain obchain deleted the feat/v0.2-multi-protocol-aave-eth-bps-gate branch May 6, 2026 07:31
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