# alexzoid-eth/2023-10-badger-fv architecture

> Architecture diagram of the GitHub repository alexzoid-eth/2023-10-badger-fv, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/alexzoid-eth/2023-10-badger-fv
- Repository: https://github.com/alexzoid-eth/2023-10-badger-fv

## Overview

#### Purpose and entry points
eBTC is an Ethereum smart-contract protocol, derived from Liquity, that accepts only stETH collateral and issues an ERC-20/ERC-2612 synthetic BTC asset. The deployable protocol is in `packages/contracts/contracts`; user entry points are `packages/contracts/contracts/BorrowerOperations.sol` (borrower lifecycle) and `packages/contracts/contracts/CdpManager.sol` (liquidations/redemptions). Deployment scripts and network parameters live in `packages/contracts/mainnetDeployment/eBTCDeployScript.js` and `packages/contracts/mainnetDeployment/eBTCDeploymentParams.mainnet.js`.

#### Borrowing and CDP lifecycle
`BorrowerOperations` opens, adjusts, and closes collateralized debt positions (CDPs), moves stETH into system custody, and mints/burns eBTC through authorized paths. It also supports delegated position managers and EIP-3156 flash loans.
- `packages/contracts/contracts/BorrowerOperations.sol`
- `packages/contracts/contracts/Interfaces/IBorrowerOperations.sol`
- `packages/contracts/contracts/Dependencies/EbtcBase.sol`

#### CDP state, ordering, liquidation, redemption
`CdpManager` owns non-borrower CDP operations and coordinates system-wide debt/collateral accounting. `LiquidationLibrary` is delegatecalled by the manager, sharing state through `CdpManagerStorage`; this delegatecall/storage coupling is a critical code and upgrade boundary. `SortedCdps` maintains the ICR-ordered doubly linked list that determines redemption order, while `HintHelpers` supplies approximate insertion hints.
- `packages/contracts/contracts/CdpManager.sol`
- `packages/contracts/contracts/LiquidationLibrary.sol`
- `packages/contracts/contracts/CdpManagerStorage.sol`
- `packages/contracts/contracts/SortedCdps.sol`
- `packages/contracts/contracts/HintHelpers.sol`

#### Collateral custody and surplus isolation
`ActivePool` holds/accountably tracks system stETH collateral, supports fee-recipient collateral-share claims, and exposes ERC-3156 flash-lending behavior. Liquidation/redemption collateral that belongs to users but is no longer system collateral is isolated in `CollSurplusPool`; this separation is an explicit accounting boundary.
- `packages/contracts/contracts/ActivePool.sol`
- `packages/contracts/contracts/CollSurplusPool.sol`
- `packages/contracts/contracts/Interfaces/IActivePool.sol`

#### eBTC token and authorization
`EBTCToken` is the issued debt asset, implementing ERC-20, permit (ERC-2612), and controlled mint/burn functions. `Governor` plus inherited authorization contracts assign function capabilities; operations affecting supply, fees, pauses, oracle fallback, and redemption parameters are governed, expected behind multisig/timelock except selected emergency actions.
- `packages/contracts/contracts/EBTCToken.sol`
- `packages/contracts/contracts/Governor.sol`
- `packages/contracts/contracts/Dependencies/AuthNoOwner.sol`
- `packages/contracts/contracts/Dependencies/RolesAuthority.sol`

#### Pricing and external dependencies
`PriceFeed` is a state machine over a primary oracle and configurable fallback oracle, handling oracle failures before CDP health, liquidation, and redemption logic consumes prices. Runtime external dependencies are Ethereum mainnet, stETH, Chainlink, and potentially an arbitrary fallback oracle; Tellor code exists but is stated not to be used.
- `packages/contracts/contracts/PriceFeed.sol`
- `packages/contracts/contracts/Interfaces/IPriceFeed.sol`
- `packages/contracts/contracts/Interfaces/IFallbackCaller.sol`

#### Leverage and smart-wallet integration
Leverage macros compose external swaps/callbacks with CDP actions. The reference macro is owner-scoped and factory deployed; the delegate target is intended for compatible smart wallets. `SimplifiedDiamondLike` permits custom callback handling and delegatecall, making caller context and callback validation security-critical.
- `packages/contracts/contracts/LeverageMacroBase.sol`
- `packages/contracts/contracts/LeverageMacroFactory.sol`
- `packages/contracts/contracts/SimplifiedDiamondLike.sol`

#### Verification and execution environment
Contracts target Solidity 0.8.17 and are developed/tested with Foundry plus Hardhat/JavaScript tooling. CI runs Foundry and contract tests. Security validation includes Echidna/Medusa fuzzing and Certora specs/harnesses, with formal coverage focused on pool, token, and sorted-CDP invariants.
- `packages/contracts/foundry.toml`
- `packages/contracts/hardhat.config.js`
- `certora/confs/ActivePool_verified.conf`
- `certora/specs/ActivePool.spec`

Main entry points: [`packages/contracts/mainnetDeployment/eBTCDeployScript.js`](https://github.com/alexzoid-eth/2023-10-badger-fv/blob/main/packages/contracts/mainnetDeployment/eBTCDeployScript.js) (Mainnet Deployment), [`certora/specs/ActivePool.spec`](https://github.com/alexzoid-eth/2023-10-badger-fv/blob/main/certora/specs/ActivePool.spec) (Formal Verification)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core protocol"]
  node_borrower_ops["Borrower Operations<br/>CDP entry point"]
  node_cdp_manager["CDP Manager<br/>CDP coordinator"]
  node_manager_storage[("Manager Storage<br/>shared storage")]
  node_liquidation_lib{{"Liquidation Library<br/>delegatecall library"}}
  node_sorted_cdps[("Sorted CDPs<br/>ICR index")]
  node_hint_helpers["Hint Helpers<br/>insertion hints"]
  node_active_pool[("Active Pool<br/>stETH custody")]
  node_surplus_pool[("Collateral Surplus Pool<br/>user collateral custody")]
  node_ebtc_token["eBTC Token<br/>ERC-20 / ERC-2612"]
end

subgraph group_governance["Governance"]
  node_governor["Governor<br/>capability governance"]
  node_roles_authority["Roles Authority<br/>authorization base"]
end

subgraph group_integration["Integrations"]
  node_price_feed["Price Feed<br/>oracle state machine"]
  node_mainnet_oracles(("Chainlink &amp; fallback oracle<br/>external oracle"))
  node_steth(("stETH<br/>external collateral"))
  node_leverage_factory["Leverage Macro Factory<br/>macro deployment"]
  node_smart_wallet_delegate{{"Smart-wallet Delegate Target<br/>callback / delegatecall"}}
end

subgraph group_operations["Deployment &amp; assurance"]
  node_deployment["Mainnet Deployment<br/>deployment script"]
  node_formal_verification["Formal Verification<br/>Certora specs<br/>[ActivePool.spec]"]
end

node_borrower_ops -->|"updates CDPs"| node_cdp_manager
node_borrower_ops -->|"moves stETH"| node_active_pool
node_borrower_ops -->|"mints / burns"| node_ebtc_token
node_cdp_manager -->|"delegatecalls"| node_liquidation_lib
node_liquidation_lib -->|"uses"| node_manager_storage
node_cdp_manager -->|"maintains ICR order"| node_sorted_cdps
node_hint_helpers -.->|"supplies hints"| node_sorted_cdps
node_cdp_manager -->|"accounts collateral"| node_active_pool
node_cdp_manager -->|"routes user surplus"| node_surplus_pool
node_cdp_manager -->|"settles debt"| node_ebtc_token
node_borrower_ops -->|"checks CDP health"| node_price_feed
node_cdp_manager -->|"prices liquidation / redemption"| node_price_feed
node_price_feed -->|"primary / fallback reads"| node_mainnet_oracles
node_steth -->|"collateral held"| node_active_pool
node_leverage_factory -->|"deploys compatible macro"| node_smart_wallet_delegate
node_smart_wallet_delegate -->|"composes CDP actions"| node_borrower_ops
node_governor -->|"uses roles"| node_roles_authority
node_governor -.->|"authorizes privileged actions"| node_borrower_ops
node_governor -.->|"controls parameters"| node_cdp_manager
node_governor -.->|"controls supply authority"| node_ebtc_token
node_governor -.->|"configures fallback"| node_price_feed
node_deployment -.->|"deploys"| node_borrower_ops
node_deployment -.->|"deploys"| node_cdp_manager
node_deployment -.->|"configures governance"| node_governor
node_formal_verification -.->|"verifies invariants"| node_active_pool

click node_deployment "https://github.com/alexzoid-eth/2023-10-badger-fv/blob/main/packages/contracts/mainnetDeployment/eBTCDeployScript.js"
click node_formal_verification "https://github.com/alexzoid-eth/2023-10-badger-fv/blob/main/certora/specs/ActivePool.spec"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_borrower_ops,node_cdp_manager,node_manager_storage,node_liquidation_lib,node_sorted_cdps,node_hint_helpers,node_active_pool,node_surplus_pool,node_ebtc_token toneBlue
class node_governor,node_roles_authority toneAmber
class node_price_feed,node_mainnet_oracles,node_steth,node_leverage_factory,node_smart_wallet_delegate toneMint
class node_deployment,node_formal_verification toneRose
```

## Components

### Core protocol

CDP debt, collateral, and settlement.

- **Borrower Operations**: CDP entry point · Opens, adjusts, and closes borrower CDPs.
- **CDP Manager**: CDP coordinator · Coordinates liquidations, redemptions, and global accounting.
- **Manager Storage**: shared storage · Storage boundary shared with liquidation delegatecall code.
- **Liquidation Library**: delegatecall library · Runs in CdpManager storage context.
- **Sorted CDPs**: ICR index · Doubly linked list defining redemption order.
- **Hint Helpers**: insertion hints
- **Active Pool**: stETH custody · Tracks system collateral, fee claims, and flash loans.
- **Collateral Surplus Pool**: user collateral custody · Isolates collateral no longer owned by the system.
- **eBTC Token**: ERC-20 / ERC-2612 · Controlled synthetic-BTC debt token minting and burning.

### Governance

Capability-based administrative control.

- **Governor**: capability governance · Assigns privileged protocol capabilities.
- **Roles Authority**: authorization base

### Integrations

Oracle and smart-wallet execution boundaries.

- **Price Feed**: oracle state machine · Selects primary or fallback pricing during oracle failures.
- **Chainlink & fallback oracle**: external oracle
- **stETH**: external collateral
- **Leverage Macro Factory**: macro deployment · Deploys owner-scoped leverage macros.
- **Smart-wallet Delegate Target**: callback / delegatecall · Custom callbacks and delegatecall require validated caller context.

### Deployment & assurance

- [Mainnet Deployment](https://github.com/alexzoid-eth/2023-10-badger-fv/blob/main/packages/contracts/mainnetDeployment/eBTCDeployScript.js): deployment script · Deploys contracts with mainnet parameters. · `packages/contracts/mainnetDeployment/eBTCDeployScript.js`
- [Formal Verification](https://github.com/alexzoid-eth/2023-10-badger-fv/blob/main/certora/specs/ActivePool.spec): Certora specs · Covers pool, token, and sorted-CDP invariants. · `certora/specs/ActivePool.spec`

## Connections

- Borrower Operations → CDP Manager: updates CDPs
- Borrower Operations → Active Pool: moves stETH
- Borrower Operations → eBTC Token: mints / burns
- CDP Manager → Liquidation Library: delegatecalls · Critical shared-storage and upgrade boundary.
- Liquidation Library → Manager Storage: uses
- CDP Manager → Sorted CDPs: maintains ICR order
- Hint Helpers → Sorted CDPs: supplies hints
- CDP Manager → Active Pool: accounts collateral
- CDP Manager → Collateral Surplus Pool: routes user surplus
- CDP Manager → eBTC Token: settles debt
- Borrower Operations → Price Feed: checks CDP health
- CDP Manager → Price Feed: prices liquidation / redemption
- Price Feed → Chainlink & fallback oracle: primary / fallback reads
- stETH → Active Pool: collateral held
- Leverage Macro Factory → Smart-wallet Delegate Target: deploys compatible macro
- Smart-wallet Delegate Target → Borrower Operations: composes CDP actions
- Governor → Roles Authority: uses roles
- Governor → Borrower Operations: authorizes privileged actions
- Governor → CDP Manager: controls parameters
- Governor → eBTC Token: controls supply authority
- Governor → Price Feed: configures fallback
- Mainnet Deployment → Borrower Operations: deploys
- Mainnet Deployment → CDP Manager: deploys
- Mainnet Deployment → Governor: configures governance
- Formal Verification → Active Pool: verifies invariants
