Skip to content

0xPolygon/Cross-Chain-Lending

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 

Repository files navigation

Cross-Chain Lending: Morpho Blue Cross-Chain Collateral Design Pattern

Note: The core scripts for the cross-chain lending workflows have been completed. Integration with AggSandbox is underway, and we are actively developing the frontends and polishing the full end-to-end workflow. Please see the detailed description below for the complete implementation and usage notes.

Overview

Cross-Chain Lending demonstrates fundamental concepts of using assets from one blockchain as collateral to borrow on another blockchain. This educational example shows how to bridge LP tokens from Sepolia and use them as collateral in Morpho Blue's isolated lending markets on Tatara (Katana testnet) to borrow AUSD, showcasing advanced DeFi composability across chains.

Learning Objectives

Concept What You'll Learn Real-World Application
Isolated Lending Markets How Morpho Blue creates risk-isolated lending pools Advanced lending protocol design
Cross-Chain Collateral Using assets from one chain as collateral on another Multi-chain capital efficiency
Oracle Integration Price feeds for non-standard collateral types Custom asset valuation systems
Risk Parameters LTV ratios and liquidation mechanics DeFi risk management

Table of Contents

  1. Understanding Morpho Blue Architecture
  2. Cross-Chain Collateral Logic
  3. Market Creation and Management
  4. Oracle and Price Discovery
  5. Lending Flow Implementation
  6. File Structure and Responsibilities
  7. Key Implementation Patterns
  8. Educational Code Examples

1. Understanding Morpho Blue Architecture

1.1 What Makes Morpho Blue Different

Traditional Lending vs Isolated Markets:

Traditional Lending (Compound/Aave)
├── Shared risk pools
├── System-wide liquidation risk
└── Limited asset support

Morpho Blue Isolated Markets
├── Each market is independent
├── Isolated risk per market
└── Any asset can have its own market

1.2 Market Structure Components

A Morpho Blue market consists of:

Component Description Example
Loan Token What users borrow AUSD
Collateral Token What users deposit LP tokens from Sepolia
Oracle Price feed for collateral Custom LP token oracle
IRM Interest rate model Dynamic rate based on utilization
LLTV Liquidation Loan-to-Value 86% (860000000000000000)

1.3 The Isolated Market Advantage

Why Isolation Matters:

  • Each market has independent risk parameters
  • Bad debt in one market doesn't affect others
  • Custom oracles for exotic collateral types
  • Flexible interest rate models per asset pair

2. Cross-Chain Collateral Logic

2.1 The Collateral Bridge Pattern

File: scripts/morpho-complete-test.js

Core Concept: Instead of just bridging tokens for trading, we bridge them with lending instructions.

Cross-Chain Flow Design:

Sepolia LP Position
    │
    ├── LP tokens represent liquidity provision
    ├── Have inherent value from underlying assets
    └── Exist as ERC-20 tokens
    │
Bridge + Lending Instructions
    │
    ├── Bridge LP tokens to Tatara
    ├── Include market parameters
    ├── Include borrowing preferences
    └── Execute lending operations automatically
    │
Tatara Lending Market
    │
    ├── Receive LP tokens as collateral
    ├── Create/verify market exists
    ├── Supply collateral to Morpho
    └── Borrow AUSD against collateral

2.2 Collateral Valuation Challenge

Educational Insight: LP tokens are complex assets that require custom price oracles.

LP Token Value Components:

  • Underlying Token A reserves
  • Underlying Token B reserves
  • Current exchange rates
  • Pool fees accumulated
  • Impermanent loss considerations

3. Market Creation and Management

3.1 Market Parameters Definition

File: scripts/final-morpho-blue-test.js

Purpose: Understanding how Morpho markets are configured and created.

Market Parameter Structure:

MarketParams Configuration
    │
    ├── loanToken: AUSD_ADDRESS
    │   ├── The asset users will borrow
    │   └── Must be an ERC-20 token
    │
    ├── collateralToken: LP_TOKEN_ADDRESS  
    │   ├── The asset users deposit as collateral
    │   └── Bridged LP tokens from Sepolia
    │
    ├── oracle: LP_ORACLE_ADDRESS
    │   ├── Price feed for LP token valuation
    │   └── Returns price in 1e36 scaled format
    │
    ├── irm: IRM_ADDRESS
    │   ├── Interest rate model contract
    │   └── Calculates borrow/supply rates
    │
    └── lltv: 860000000000000000
        ├── 86% liquidation threshold
        └── Maximum borrowable amount ratio

3.2 Market Creation Process

Educational Flow: Markets must be created before they can be used for lending.

Creation Sequence:

  1. Parameter Validation: Ensure all addresses are valid contracts
  2. Market Creation: Call createMarket() with parameters
  3. Liquidity Seeding: Supply initial AUSD liquidity for borrowers
  4. Market Activation: Market becomes available for collateral deposits

4. Oracle and Price Discovery

4.1 Custom Oracle Design

File: scripts/complete-morpho-workflow.js

Educational Challenge: How do you price LP tokens that don't have standard market prices?

LP Token Pricing Strategy:

LP Token Price Calculation
    │
    ├── 1. Get Underlying Reserves
    │   ├── Read TokenA balance in pool
    │   ├── Read TokenB balance in pool
    │   └── Get current exchange rates
    │
    ├── 2. Calculate Total Value
    │   ├── TokenA_value = reserves_A × price_A
    │   ├── TokenB_value = reserves_B × price_B  
    │   └── Total_pool_value = TokenA_value + TokenB_value
    │
    └── 3. Compute LP Token Price
        ├── LP_supply = total LP tokens outstanding
        ├── Price_per_LP = Total_pool_value / LP_supply
        └── Return price in 1e36 scaled format

4.2 Oracle Interface Compliance

Morpho Blue Oracle Requirements:

  • Must implement price() function
  • Returns price scaled by 1e36 (not 1e8 like Chainlink)
  • Must handle decimal conversions properly
  • Should include staleness checks for security

5. Lending Flow Implementation

5.1 Complete Lending Workflow

File: scripts/morpho-complete-test.js

Purpose: Orchestrate the full lending cycle from collateral deposit to AUSD borrowing.

Workflow Stages:

Lending Cycle Execution
    │
    ├── 1. Market Preparation
    │   ├── Verify market exists or create it
    │   ├── Ensure sufficient AUSD liquidity
    │   └── Validate oracle functionality
    │
    ├── 2. Collateral Operations
    │   ├── Approve LP tokens for Morpho
    │   ├── Call supplyCollateral() with LP amount
    │   └── Verify collateral is recorded
    │
    ├── 3. Borrowing Capacity Calculation
    │   ├── Get LP token price from oracle
    │   ├── Calculate collateral value in USD
    │   ├── Apply LLTV ratio (86%)
    │   └── Determine safe borrow amount
    │
    └── 4. AUSD Borrowing
        ├── Calculate desired borrow amount
        ├── Ensure it's below max LTV
        ├── Execute borrow() transaction
        └── Verify borrowed AUSD received

5.2 Risk Management Integration

Educational Focus: Understanding how lending protocols manage risk.

Risk Parameters:

  • LTV Monitoring: Current borrowed amount vs collateral value
  • Liquidation Threshold: Point where position can be liquidated
  • Safety Buffer: Recommended borrowing below maximum
  • Interest Accrual: Borrowed amount grows over time

6. File Structure and Responsibilities

6.1 Contract Integration Points

Component Contract Address Purpose
MorphoBlue 0xC263...b9021 Main protocol entry point
AUSD 0xa9012a055bd4... Loan token for borrowing
LP Tokens Bridged from Sepolia Collateral tokens
Oracle Custom deployment LP token price feed

7. Key Implementation Patterns

7.1 The Bridge-to-Borrow Pattern

Universal Principle: Any cross-chain asset can be used as collateral through proper bridging and market setup.

1. BRIDGE COLLATERAL
   ├── Transfer LP tokens cross-chain
   ├── Include lending parameters
   └── Preserve asset properties

2. CREATE MARKET
   ├── Define loan/collateral pair
   ├── Set risk parameters
   └── Deploy custom oracle

3. SUPPLY & BORROW
   ├── Deposit bridged assets as collateral
   ├── Calculate borrowing capacity
   └── Execute AUSD borrowing

7.2 The Isolated Risk Pattern

Key Insight: Each market operates independently, allowing for innovative collateral types.

Isolation Benefits:

  • Custom risk parameters per asset
  • Experimental collateral types don't affect other markets
  • Flexible liquidation mechanisms
  • Independent interest rate models

This educational walkthrough demonstrates how cross-chain assets can be effectively used as collateral in isolated lending markets, showcasing the composability of DeFi protocols across different blockchains while maintaining proper risk management and price discovery mechanisms. The key insight is that any asset with deterministic value can become collateral through proper oracle integration and market design.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published