LLM Notice: This documentation site supports content negotiation for AI agents. Request any page with Accept: text/markdown or Accept: text/plain header to receive Markdown instead of HTML. Alternatively, append ?format=md to any URL. All markdown files are available at /md/ prefix paths. For all content in one file, visit /llms-full.txt
Skip to main content

Architecture Overview

ALP is built on a modular architecture with several core components that work together to provide a secure and efficient lending protocol.

Core Components


_19
graph TB
_19
subgraph "ALP Core"
_19
Pool[Pool<br/>Central Logic Hub]
_19
Position[Position<br/>User Accounts]
_19
TokenState[TokenState<br/>Per-Token Metrics]
_19
Oracle[Price Oracle<br/>Price Feeds]
_19
end
_19
_19
User[User Wallet] -->|Creates/Manages| Position
_19
Position -->|Operations| Pool
_19
Pool -->|Tracks| TokenState
_19
Pool -->|Queries Prices| Oracle
_19
Pool -->|Stores| Reserves[Token Reserves]
_19
_19
Position -->|Auto-Push| Sink[DrawDownSink<br/>Automated Flows]
_19
Position -->|Auto-Pull| Source[TopUpSource<br/>Automated Flows]
_19
_19
style Pool fill:#f9f,stroke:#333,stroke-width:4px
_19
style Position fill:#bbf,stroke:#333,stroke-width:2px

Pool

The Pool is the central smart contract that manages all protocol operations. It serves as the primary logic hub for:


_16
graph LR
_16
subgraph "Pool Responsibilities"
_16
R1[Track All<br/>Positions]
_16
R2[Manage Token<br/>Balances]
_16
R3[Store<br/>Reserves]
_16
R4[Calculate<br/>Interest]
_16
R5[Execute<br/>Liquidations]
_16
end
_16
_16
R1 --> Pool[Pool Contract]
_16
R2 --> Pool
_16
R3 --> Pool
_16
R4 --> Pool
_16
R5 --> Pool
_16
_16
style Pool fill:#f9f,stroke:#333,stroke-width:3px

The Pool tracks global state for all positions, manages credit and debit balances for each supported token, stores reserves as they are deposited, coordinates interest rate calculations, and executes liquidations and rebalancing operations. It maintains a global ledger that tracks the state of each token type, including interest indices, total deposits, total borrows, and reserve factors.

Position

A Position represents a user's credit account within the protocol. Each position tracks:


_16
graph TD
_16
subgraph "Position Components"
_16
C[Collateral<br/>Deposits]
_16
D[Debt<br/>Obligations]
_16
H[Health<br/>Factor]
_16
A[DeFi Actions<br/>Connectors]
_16
end
_16
_16
C --> Position[Your Position]
_16
D --> Position
_16
H --> Position
_16
A --> Position
_16
_16
Position --> Operations[Operations:<br/>Deposit, Borrow<br/>Repay, Withdraw]
_16
_16
style Position fill:#bbf,stroke:#333,stroke-width:3px

  • Collateral deposits: Assets deposited to back borrowing
  • Debt obligations: Amount borrowed against collateral
  • Health factor: Ratio of collateral value to debt (must stay above 1.0)
  • DeFi Actions connectors: Optional Sink and Source for automated flows

Positions are external objects representing ownership of deposited value, with each position capable of holding multiple token balances (both deposits and borrows). They can be configured with different min/max health targets and support composability through DeFi Actions interfaces.

TokenState

Each supported token in the protocol has an associated TokenState that tracks per-token metrics:


_10
graph LR
_10
Token[Token<br/>e.g., FLOW] --> State[TokenState]
_10
_10
State --> I[Interest<br/>Indices]
_10
State --> TD[Total<br/>Deposits]
_10
State --> TB[Total<br/>Borrows]
_10
State --> CF[Collateral<br/>Factor 0.8]
_10
State --> IR[Interest Rate<br/>Parameters]
_10
_10
style State fill:#bfb,stroke:#333,stroke-width:2px

TokenState maintains interest indices for scaled balance calculations, tracks total deposits and borrows for each token, stores the collateral factor (percentage of token value usable as collateral, e.g., 0.8 = 80%), applies borrow factors as multipliers to borrowed amounts, and configures interest rate parameters for rate curves.

Scaled Balance System

ALP uses a scaled balance system to track user balances efficiently:


_20
sequenceDiagram
_20
participant User
_20
participant Position
_20
participant Pool
_20
participant Index
_20
_20
User->>Position: Borrow 1000 MOET
_20
Position->>Pool: Record balance
_20
Note over Pool: Index = 1.0<br/>Scaled = 1000 / 1.0 = 1000
_20
_20
Note over Index: Time passes...<br/>Interest accrues
_20
_20
Index->>Index: Index grows to 1.05
_20
_20
User->>Position: Check balance
_20
Position->>Pool: Query
_20
Pool->>Pool: Calculate: 1000 × 1.05
_20
Pool-->>User: Balance = 1050 MOET
_20
_20
Note over User,Index: No transaction needed<br/>for interest to accrue!

Instead of updating every user's balance when interest accrues, the protocol:

  1. Tracks each user's "scaled balance" (actual balance / interest index)
  2. Updates the global interest index as time passes
  3. Calculates true balance on-demand as: scaled balance × current interest index

This means balances grow automatically without requiring transactions, as the interest index increases over time.

This system is highly gas efficient since it eliminates per-user balance updates, enables automatic compounding for all users simultaneously, provides precise calculations using UFix128 precision, and scales to unlimited users without additional overhead. See FCM Mathematical Foundations for detailed formulas.

Price Oracle

The Price Oracle provides token prices in terms of the default token (MOET):


_19
graph TB
_19
subgraph "Oracle Safety Features"
_19
S1[Staleness Checks<br/>Price not too old]
_19
S2[Deviation Guards<br/>Prevent extreme moves]
_19
S3[TWAP Support<br/>Time-weighted prices]
_19
S4[Fallback Sources<br/>Redundancy]
_19
end
_19
_19
Oracle[Price Oracle] --> S1
_19
Oracle --> S2
_19
Oracle --> S3
_19
Oracle --> S4
_19
_19
Oracle --> Prices[Token Prices<br/>in MOET terms]
_19
_19
Prices --> HF[Health Factor<br/>Calculations]
_19
Prices --> Liq[Liquidation<br/>Triggers]
_19
_19
style Oracle fill:#bfb,stroke:#333,stroke-width:3px

The oracle implements the DeFi Actions PriceOracle interface, enabling standardized price queries across the protocol.

The oracle includes multiple safety features: configurable staleness thresholds per token (typically 5 minutes), maximum deviation checks against the last price snapshot, additional DEX price deviation checks during liquidations, and TWAP (Time-Weighted Average Price) support for manipulation resistance.

Key Interfaces

FungibleToken.Vault


_12
graph LR
_12
Vault[FungibleToken.Vault<br/>Standard Interface] --> Op1[Deposit<br/>Tokens]
_12
Vault --> Op2[Withdraw<br/>Tokens]
_12
Vault --> Op3[Balance<br/>Queries]
_12
Vault --> Op4[Transfer<br/>Tokens]
_12
_12
Op1 --> Compat[Flow Ecosystem<br/>Compatibility]
_12
Op2 --> Compat
_12
Op3 --> Compat
_12
Op4 --> Compat
_12
_12
style Compat fill:#bfb,stroke:#333,stroke-width:2px

ALP integrates with Flow's standard FungibleToken.Vault interface for token operations, ensuring compatibility with all Flow fungible tokens and wallets.

DeFi Actions Framework

ALP implements the DeFi Actions framework for protocol composability:


_15
graph TB
_15
subgraph "Sink Pattern (Push)"
_15
S1[Position<br/>Overcollateralized] --> S2[Auto-Borrow<br/>MOET]
_15
S2 --> S3[Push to<br/>DrawDownSink]
_15
S3 --> S4[User Wallet<br/>or DeFi Protocol]
_15
end
_15
_15
subgraph "Source Pattern (Pull)"
_15
P1[Position<br/>Undercollateralized] --> P2[Need to<br/>Repay Debt]
_15
P2 --> P3[Pull from<br/>TopUpSource]
_15
P3 --> P4[User Wallet<br/>or DeFi Protocol]
_15
end
_15
_15
style S1 fill:#bbf
_15
style P1 fill:#fbb

The Sink Interface receives tokens when positions are overcollateralized, automatically pushing borrowed funds to user wallets or other protocols through the drawDownSink configuration on positions, enabling automated value flows out of positions. The Source Interface provides tokens when positions need rebalancing, automatically pulling funds to repay debt when undercollateralized through the topUpSource configuration, enabling automated value flows into positions.

Learn more: DeFi Actions Integration

ViewResolver

The ViewResolver interface provides metadata for wallet integration, including position details and balance sheets, supported token types, protocol parameters and configuration, and user-friendly data formatting. This enables wallets and dApps to display ALP positions with rich, contextual information.

System Architecture Diagram


_13
graph TB
_13
User[User Wallet] -->|Deposit/Withdraw| Position[Position]
_13
Position -->|Manages| Pool[Pool Contract]
_13
Pool -->|Stores| Reserves[Token Reserves]
_13
Pool -->|Queries| Oracle[Price Oracle]
_13
Pool -->|Updates| TokenState[TokenState per Token]
_13
Position -->|Auto-Push| Sink[DrawDown Sink]
_13
Position -->|Auto-Pull| Source[TopUp Source]
_13
Pool -->|Liquidates| Liquidator[Liquidators/Keepers]
_13
_13
style Pool fill:#f9f,stroke:#333,stroke-width:4px
_13
style Position fill:#bbf,stroke:#333,stroke-width:2px
_13
style Oracle fill:#bfb,stroke:#333,stroke-width:2px

Data Flow

Deposit Flow


_16
sequenceDiagram
_16
participant User
_16
participant Position
_16
participant Pool
_16
participant TokenState
_16
participant Sink
_16
_16
User->>Position: deposit()
_16
Position->>Pool: Transfer tokens to reserves
_16
Pool->>Pool: Update scaled balance
_16
Pool->>TokenState: Update totals
_16
Pool->>Pool: Check if overcollateralized
_16
alt Overcollateralized & DrawDownSink enabled
_16
Pool->>Pool: Auto-borrow
_16
Pool->>Sink: Push borrowed tokens
_16
end

Steps:

  1. User calls deposit() on their Position
  2. Position transfers tokens to Pool reserves
  3. Pool updates user's scaled balance
  4. Pool updates global TokenState
  5. If drawDownSink enabled and overcollateralized → auto-borrow

Borrow Flow


_16
sequenceDiagram
_16
participant User
_16
participant Position
_16
participant Pool
_16
participant Reserves
_16
_16
User->>Position: withdraw(debt token)
_16
Position->>Pool: Check health factor
_16
alt Health would remain safe
_16
Pool->>Pool: Update debt scaled balance
_16
Pool->>Reserves: Transfer tokens
_16
Reserves-->>User: Receive tokens
_16
Pool->>Position: Update health
_16
else Health would drop too low
_16
Pool-->>User: Revert: Insufficient health
_16
end

Steps:

  1. User calls withdraw() for debt token
  2. Pool checks health factor would remain above minimum
  3. Pool updates user's debt scaled balance
  4. Pool transfers tokens from reserves to user
  5. Position health is recalculated

Interest Accrual


_10
graph LR
_10
Time[Time Passes] --> Touch[Any Position<br/>Touched]
_10
Touch --> Update[Pool Updates<br/>Interest Index]
_10
Update --> Index[Index Increases<br/>Based on Utilization]
_10
Index --> All[All Positions'<br/>Balances Grow<br/>Automatically]
_10
_10
style All fill:#bfb,stroke:#333,stroke-width:2px

Process:

  1. Time passes, interest accumulates
  2. When any position is touched, Pool updates interest indices
  3. Interest index increases based on utilization and rates
  4. All positions' true balances grow automatically via scaled balance math

Security Architecture

ALP includes multiple layers of security:


_17
graph TB
_17
subgraph "Security Layers"
_17
L1[Health Factor<br/>Monitoring]
_17
L2[Oracle<br/>Safety]
_17
L3[Liquidation<br/>Mechanisms]
_17
L4[Circuit<br/>Breakers]
_17
L5[Access<br/>Controls]
_17
end
_17
_17
Protocol[Protocol<br/>Operations] --> L1
_17
L1 -->|Pass| L2
_17
L2 -->|Pass| L3
_17
L3 -->|Pass| L4
_17
L4 -->|Pass| L5
_17
L5 -->|Pass| Execute[Execute<br/>Operation]
_17
_17
style Execute fill:#bfb

  1. Health factor monitoring: Continuous tracking of position solvency
  2. Oracle safety: Staleness and deviation checks
  3. Liquidation mechanisms: Multiple paths to resolve undercollateralized positions
  4. Circuit breakers: Ability to pause operations in emergencies
  5. Access controls: Permissioned functions for admin operations

Gas Optimization

The architecture is optimized for gas efficiency:


_14
graph LR
_14
subgraph "Gas Optimizations"
_14
O1[Scaled Balances<br/>No per-user updates]
_14
O2[Batch Operations<br/>Multiple in one tx]
_14
O3[Efficient Storage<br/>Compact structures]
_14
O4[Lazy Updates<br/>Calculate on-demand]
_14
end
_14
_14
O1 --> Result[Minimal<br/>Gas Costs]
_14
O2 --> Result
_14
O3 --> Result
_14
O4 --> Result
_14
_14
style Result fill:#bfb,stroke:#333,stroke-width:3px

The architecture is optimized for gas efficiency through scaled balances that eliminate per-user interest updates, batch operations that allow single transactions to update multiple positions, efficient storage using compact data structures for on-chain state, and lazy updates that only calculate interest when needed.

Upgradability

The protocol includes mechanisms for upgrades and parameter adjustments:


_16
graph TD
_16
Admin[Protocol Admin] --> Functions[Admin Functions]
_16
_16
Functions --> Rate[Adjust Interest<br/>Rates]
_16
Functions --> Factor[Update Collateral<br/>Factors]
_16
Functions --> Token[Add New<br/>Tokens]
_16
Functions --> Oracle[Switch Price<br/>Feeds]
_16
Functions --> Feature[Enable/Disable<br/>Features]
_16
_16
Rate --> Protocol[Protocol<br/>Configuration]
_16
Factor --> Protocol
_16
Token --> Protocol
_16
Oracle --> Protocol
_16
Feature --> Protocol
_16
_16
style Protocol fill:#f9f,stroke:#333,stroke-width:2px

The protocol supports admin functions to adjust interest rates and collateral factors, dynamic token addition to support new tokens, oracle updates to switch price feed sources, and feature flags to enable or disable features like liquidations.

Summary

Core Architecture:

  • 🏗️ Modular design with Pool, Position, TokenState, and Oracle
  • 🔗 DeFi Actions framework for composability
  • 📊 Scaled balance system for efficiency
  • 🛡️ Multiple security layers

Key Benefits:

  • ✅ Gas efficient scaled balance system
  • ✅ Automated flows via Sink/Source interfaces
  • ✅ Robust oracle safety features
  • ✅ Multi-layer security architecture
  • ✅ Flexible and upgradable design

Integration Points:

  • Flow FungibleToken standard
  • DeFi Actions Sink/Source
  • ViewResolver for wallets
  • Price Oracle interface

Mathematical Foundation

The architecture implements these mathematical principles:

See FCM Mathematical Foundations for complete formulas and proofs.

Next Steps


Key Takeaway

ALP's modular architecture combines efficiency with security. The scaled balance system eliminates gas overhead, DeFi Actions enable composability, and multiple security layers protect users. This design makes ALP both powerful for developers and accessible for users.