Skip to content

woof-software/compound-multiplier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

277 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CometFoundation

Coverage Tests License Solidity Hardhat

A unified smart contract system for DeFi operations on Compound V3 (Comet) markets using flash loans and DEX aggregators. CometFoundation provides four core functionalities in a single, gas-efficient contract: leveraged position creation, leverage adjustment, position reduction, and collateral swapping.

Overview

CometFoundation offers four atomic operations:

1. Multiply - Create Leveraged Positions

Open or increase leveraged collateral positions in a single transaction by:

  1. Taking a flash loan from lending protocols (Morpho, Euler, UniswapV3, AAVE, Balancer)
  2. Swapping borrowed base assets to collateral via DEX aggregators (LiFi, 1inch, OKX)
  3. Depositing collateral into Compound V3 and borrowing against it
  4. Repaying the flash loan

2. Adjust - Increase Leverage on Existing Positions

Increase the leverage of an existing position without adding new capital by:

  1. Taking a flash loan of the desired additional debt amount in base asset
  2. Swapping the borrowed base asset to collateral via DEX aggregators (LiFi, 1inch, OKX)
  3. Supplying the swapped collateral into the user's Comet position
  4. Borrowing base asset against the user's increased collateral to repay the flash loan

3. Cover - Reduce Leveraged Positions

Reduce or close leveraged positions atomically by:

  1. Caller derives loanDebt from an off-chain swap quote (amount of base asset the collateral swap will produce, minus flash loan fee)
  2. Taking a flash loan of loanDebt base asset to partially or fully repay user's debt
  3. Withdrawing unlocked collateral
  4. Swapping collateral back to base asset
  5. Repaying the flash loan from swap proceeds, returning any excess to the user

4. Exchange - Swap Collateral Assets

Swap one collateral type for another within existing positions while maintaining debt:

  1. Validating position health factor will remain safe
  2. Taking a flash loan of target collateral
  3. Supplying borrowed collateral to improve position
  4. Withdrawing original collateral
  5. Swapping to repay flash loan

Architecture

The system uses a unified contract with a plugin-based architecture:

CometFoundation (Unified Contract)
├── Flash Loan Plugins
│   ├── MorphoPlugin - Morpho Blue (zero-fee)
│   ├── EulerV2Plugin - Euler V2 vaults
│   ├── AAVEPlugin - AAVE V3 pools
│   ├── BalancerPlugin - Balancer vault
│   └── UniswapV3Plugin - Uniswap V3 pools
│
└── Swap Plugins
    ├── LiFiPlugin - LiFi cross-chain aggregator
    ├── OneInchV6Plugin - 1inch v6 aggregator
    ├── OKXPlugin - OKX DEX aggregator
    └── WstEthPlugin - Lido wstETH wrapper

How It Works

Multiply Flow (Create 2x Leverage)

┌─────────────────────────────────────────────────────────────────────────────┐
│                  LEVERAGE CREATION FLOW (2x WETH Position)                  │
└─────────────────────────────────────────────────────────────────────────────┘

1. USER INITIATES LEVERAGE
   User ──► CometFoundation.multiply(1 WETH, 20000 bps)
   ✅ User deposits: 1 WETH

2. FLASH LOAN REQUEST
   CometFoundation ──► FlashLoanPlugin.takeFlashLoan(2500 USDC)
   FlashLoanPlugin ──► Morpho/Euler/Uniswap.flashLoan(2500 USDC)

3. FLASH LOAN CALLBACK
   Morpho/Euler/Uniswap ──► CometFoundation.fallback()
   ✅ Contract Balance: 1 WETH + 2500 USDC

4. SWAP BORROWED ASSET
   CometFoundation ──► SwapPlugin.swap(2500 USDC → WETH)
   SwapPlugin ──► 1inch/LiFi.swap(2500 USDC → 0.996 WETH)
   ✅ Contract Balance: 1.996 WETH total

5. SUPPLY COLLATERAL TO USER
   CometFoundation ──► Comet.supplyTo(user, 1.996 WETH)
   ✅ User's WETH collateral: 1.996 WETH

6. BORROW AGAINST COLLATERAL
   CometFoundation ──► Comet.withdrawFrom(user, 2507.5 USDC)
   ✅ User's debt: 2507.5 USDC (includes flash loan fee)
   ✅ Contract Balance: 2507.5 USDC

7. REPAY FLASH LOAN
   CometFoundation ──► FlashLoanPlugin.repayFlashLoan(2507.5 USDC)
   FlashLoanPlugin ──► Morpho/Euler/Uniswap.repay(2500 + 7.5 fee)
   ✅ Contract Balance: 0

RESULT: User has 2x leveraged WETH position (1.996 WETH collateral, 2507.5 USDC debt)

Adjust Flow (Increase Leverage on Existing Position)

┌─────────────────────────────────────────────────────────────────────────────┐
│            LEVERAGE ADJUSTMENT FLOW (1.5x → 2x WETH Position)             │
└─────────────────────────────────────────────────────────────────────────────┘

   Current position: 1.5 WETH collateral ($2500 each), 1250 USDC debt (~1.5x)

1. USER INITIATES ADJUSTMENT
   User ──► CometFoundation.adjust(additionalDebt: 1250 USDC)
   ✅ No new capital deposited

2. VALIDATION
   CometFoundation ──► Validates new debt stays within health factor limits
   ✅ borrowBalance + debtDelta <= maxLeverage * collateralValue

3. FLASH LOAN REQUEST
   CometFoundation ──► FlashLoanPlugin.takeFlashLoan(1250 USDC)
   FlashLoanPlugin ──► Morpho/Euler/Uniswap.flashLoan(1250 USDC)

4. FLASH LOAN CALLBACK
   Morpho/Euler/Uniswap ──► CometFoundation.fallback()
   ✅ Contract Balance: 1250 USDC

5. SWAP BORROWED ASSET
   CometFoundation ──► SwapPlugin.swap(1250 USDC → WETH)
   SwapPlugin ──► 1inch/LiFi/OKX.swap(1250 USDC → ~0.498 WETH)
   ✅ Contract Balance: ~0.498 WETH

6. SUPPLY COLLATERAL TO USER
   CometFoundation ──► Comet.supplyTo(user, ~0.498 WETH)
   ✅ User's WETH collateral: ~1.998 WETH

7. BORROW AGAINST COLLATERAL
   CometFoundation ──► Comet.withdrawFrom(user, 1253.75 USDC)
   ✅ User's debt: 1250 + 1253.75 = 2503.75 USDC (includes flash loan fee)
   ✅ Contract Balance: 1253.75 USDC

8. REPAY FLASH LOAN
   CometFoundation ──► FlashLoanPlugin.repayFlashLoan(1253.75 USDC)
   FlashLoanPlugin ──► Morpho/Euler/Uniswap.repay(1250 + 3.75 fee)
   ✅ Contract Balance: 0

RESULT: User increased leverage from ~1.5x to ~2x without depositing new capital
        (~1.998 WETH collateral, ~2503.75 USDC debt)

Cover Flow (Reduce Leverage)

┌─────────────────────────────────────────────────────────────────────────────┐
│              LEVERAGE REDUCTION FLOW (2x → 1x WETH Position)                │
└─────────────────────────────────────────────────────────────────────────────┘

   Current position: 2 WETH collateral ($2500 each), 2500 USDC debt

0. CALLER DERIVES loanDebt OFF-CHAIN
   Get swap quote: 1 WETH → ~2490 USDC
   loanDebt = 2490 / (1 + flashLoanFeeRate) ≈ 2488.76 USDC

1. USER INITIATES DELEVERAGE
   User ──► CometFoundation.cover(loanDebt: 2488.76 USDC, 1 WETH withdraw)

2. FLASH LOAN REQUEST
   CometFoundation ──► FlashLoanPlugin.takeFlashLoan(2488.76 USDC)
   FlashLoanPlugin ──► Morpho/Euler/Uniswap.flashLoan(2488.76 USDC)

3. FLASH LOAN CALLBACK
   Morpho/Euler/Uniswap ──► CometFoundation.fallback()
   ✅ Contract Balance: 2488.76 USDC

4. REPAY USER'S DEBT (partially)
   CometFoundation ──► Comet.supplyTo(user, 2488.76 USDC)
   ✅ User's debt: 2500 - 2488.76 = 11.24 USDC remaining

5. WITHDRAW COLLATERAL
   CometFoundation ──► Comet.withdrawFrom(user, 1 WETH)
   ✅ Contract Balance: 1 WETH
   ✅ User's position: 1 WETH collateral, 11.24 USDC debt (healthy)

6. SWAP COLLATERAL TO BASE
   CometFoundation ──► SwapPlugin.swap(1 WETH → USDC)
   SwapPlugin ──► 1inch/LiFi/OKX.swap(1 WETH → ~2490 USDC)
   ✅ Contract Balance: ~2490 USDC

7. REPAY FLASH LOAN
   CometFoundation ──► FlashLoanPlugin.repayFlashLoan(~2490 USDC)
   FlashLoanPlugin ──► Morpho/Euler/Uniswap.repay(2488.76 + 1.24 fee)
   ✅ Contract Balance: ~0 USDC

8. RETURN REMAINDER TO USER
   CometFoundation ──► Transfer any excess USDC dust to user

RESULT: User deleveraged from 2x to ~1x. Remaining 1 WETH collateral, ~11 USDC debt
        For full close, pass loanDebt = borrowBalance and collateralAmount = MaxUint256

Exchange Flow (Swap Collateral)

┌─────────────────────────────────────────────────────────────────────────────┐
│                    COLLATERAL SWAP FLOW (WETH → USDC)                      │
└─────────────────────────────────────────────────────────────────────────────┘

1. USER INITIATES SWAP
   User ──► CometFoundation.exchange(fromAsset: WETH, toAsset: USDC)

2. HEALTH FACTOR VALIDATION
   CometFoundation ──► Validates position safety after swap

3. FLASH LOAN REQUEST
   CometFoundation ──► FlashLoanPlugin.takeFlashLoan(1000 USDC)
   FlashLoanPlugin ──► AAVE/Balancer/Uniswap.flashLoan(1000 USDC)

4. FLASH LOAN CALLBACK
   AAVE/Balancer/Uniswap ──► CometFoundation.fallback()
   ✅ Contract Balance: +1000 USDC

5. SUPPLY BORROWED ASSET
   CometFoundation ──► Comet.supplyTo(user, 1000 USDC)
   ✅ User's USDC collateral increases, health factor improves

6. COVER EXISTING COLLATERAL
   CometFoundation ──► Comet.withdrawFrom(user, 0.5 WETH)
   ✅ Contract Balance: 1000 USDC + 0.5 WETH

7. SWAP COVERN COLLATERAL
   CometFoundation ──► SwapPlugin.swap(0.5 WETH → USDC)
   SwapPlugin ──► 1inch/LiFi.swap(0.5 WETH → 1005 USDC)
   ✅ Contract Balance: 2005 USDC total

8. SUPPLY DUST BACK TO USER
   CometFoundation ──► Comet.supplyTo(user, dust USDC)
   ✅ Excess USDC supplied back to user's position

9. REPAY FLASH LOAN
   CometFoundation ──► FlashLoanPlugin.repayFlashLoan(1005 USDC)
   FlashLoanPlugin ──► AAVE/Balancer/Uniswap.repay(1000 + 5 fee)
   ✅ Contract Balance: 0

RESULT: User's collateral successfully swapped from WETH to USDC

Token Flow Tables

Multiply Token Flow

Stage Contract Balance User Position Action External Call
Initial 1 WETH 0 collateral
0 debt
User calls multiply() User transfers 1 WETH
Flash Loan 1 WETH
+2500 USDC
0 collateral
0 debt
Flash loan received FlashProvider.flashLoan()
Swap ~2 WETH 0 collateral
0 debt
Swap borrowed USDC to WETH DEX.swap(USDC → WETH)
Supply Collateral 0 2 WETH collateral
0 debt
Supply total collateral Comet.supplyTo(user, 2 WETH)
Borrow +2500 USDC 2 WETH collateral
2500 USDC debt
Borrow against collateral Comet.withdrawFrom(user, USDC)
Repay 0 2 WETH collateral
2500 USDC debt
Repay flash loan + fee FlashProvider.repay(2505 USDC)

Adjust Token Flow

Stage Contract Balance User Position Action External Call
Initial 0 1.5 WETH collateral
1250 USDC debt
User calls adjust() -
Flash Loan +1250 USDC 1.5 WETH collateral
1250 USDC debt
Flash loan received FlashProvider.flashLoan()
Swap ~0.498 WETH 1.5 WETH collateral
1250 USDC debt
Swap borrowed USDC to WETH DEX.swap(USDC → WETH)
Supply Collateral 0 ~1.998 WETH collateral
1250 USDC debt
Supply swapped collateral Comet.supplyTo(user, WETH)
Borrow +1253.75 USDC ~1.998 WETH collateral
2503.75 USDC debt
Borrow against collateral Comet.withdrawFrom(user, USDC)
Repay 0 ~1.998 WETH collateral
2503.75 USDC debt
Repay flash loan + fee FlashProvider.repay(1253.75)

Cover Token Flow

The caller derives loanDebt off-chain from the swap quote: loanDebt = swapAmountOut / (1 + flashFeeRate), capped at the user's borrow balance.

Stage Contract Balance User Position Action External Call
Initial 0 2 WETH collateral
2500 USDC debt
User calls cover(loanDebt) -
Flash Loan +loanDebt USDC 2 WETH collateral
2500 USDC debt
Flash loan received FlashProvider.flashLoan()
Repay Debt 0 2 WETH collateral
reduced debt
Repay loanDebt of user's debt Comet.supplyTo(user, USDC)
Withdraw +1 WETH 1 WETH collateral
reduced debt
Withdraw collateral Comet.withdrawFrom(user, WETH)
Swap ~amountOut USDC 1 WETH collateral
reduced debt
Swap WETH to USDC DEX.swap(WETH → USDC)
Repay Flash ~dust USDC 1 WETH collateral
reduced debt
Repay flash loan + fee FlashProvider.repay()
Return Dust 0 1 WETH collateral
reduced debt
Return remainder to user Transfer remaining to user

Exchange Token Flow

Stage Contract Balance Action External Call
Initial 0 User calls exchange() -
Flash Loan +1000 USDC Flash loan received FlashProvider.flashLoan()
Supply 1000 USDC Supply borrowed asset to user Comet.supplyTo(user, USDC)
Withdraw 1000 USDC
+0.5 WETH
Withdraw user's collateral Comet.withdrawFrom(user, WETH)
Swap ~2005 USDC
-0.5 WETH
Swap withdrawn collateral DEX.swap(WETH → USDC)
Supply Dust ~1005 USDC Supply excess back to user Comet.supplyTo(user, dust)
Repay 0 Repay flash loan + fee FlashProvider.repay(1005 USDC)

Key Features

  • Unified Contract: All four operations (multiply, adjust, cover, exchange) in one gas-efficient contract
  • Atomic Execution: Each operation completes in a single transaction
  • Health Factor Protection: Validates position safety for exchange operations
  • Multi-Protocol Support: AAVE, Balancer, Uniswap V3, Morpho, Euler flash loans
  • Optimal Routing: 1inch, LiFi, and OKX aggregators for best swap execution
  • Signature-Based Authorization: EIP-712 gasless approvals for all operations
  • Plugin Architecture: Modular design for extensibility
  • Native ETH Support: Direct ETH deposits with automatic WETH wrapping
  • Gas Efficiency: Transient storage (EIP-1153) and delegatecall patterns
  • Dust Management: Automatic handling of leftover tokens

Core Operations

CometFoundation Contract

Multiply Operations

  • multiply(opts, collateral, collateralAmount, baseAmount, maxHealthFactorDrop, swapData) - Create/increase leveraged position
  • multiply(..., allowParams) - With signature-based authorization

Adjust Operations

  • adjust(opts, collateral, additionalDebt, maxHealthFactorDrop, swapData) - Increase leverage on existing position without adding capital
  • adjust(..., allowParams) - With signature-based authorization

Cover Operations

  • cover(opts, loanDebt, collateral, collateralAmount, swapData) - Reduce/close leveraged position. loanDebt is the amount of base asset to flash loan, derived from the off-chain swap quote.
  • cover(..., allowParams) - With signature-based authorization

Exchange Operations

  • exchange() - Swap collateral assets
  • exchange(..., allowParams) - With signature-based authorization

Flash Loan Plugins

MorphoPlugin - Morpho Blue integration

  • ✅ Zero-fee flash loans
  • ✅ High liquidity for major assets
  • ✅ ERC-3156 compliant

EulerV2Plugin - Euler V2 vaults

  • ✅ Vault-specific liquidity
  • ✅ Competitive fees
  • ✅ Per-vault flash loan support

AAVEPlugin - AAVE V3 pools

  • ✅ Wide asset support
  • ✅ High liquidity
  • ✅ Established protocol

BalancerPlugin - Balancer vault

  • ✅ No fees on flash loans
  • ✅ High liquidity for major tokens
  • ✅ Single transaction execution

UniswapV3Plugin - Uniswap V3 pools

  • ✅ Decentralized liquidity
  • ✅ No permission required
  • ✅ Multiple fee tiers (0.05%, 0.3%, 1%)

Swap Plugins

LiFiPlugin - LiFi Diamond aggregator

  • ✅ Cross-chain swap support
  • ✅ Multi-DEX routing
  • ✅ Optimal execution prices

OneInchV6Plugin - 1inch v6 aggregator

  • ✅ Best price discovery
  • ✅ Gas optimization
  • ✅ Wide token support

OKXPlugin - OKX DEX aggregator

  • ✅ Multi-protocol routing (DAG, Uniswap V3, Unxswap)
  • ✅ Optimal price execution
  • ✅ Support for complex swap paths

WstEthPlugin - Lido wstETH wrapper

  • ✅ Direct Lido integration
  • ✅ No slippage wrapping
  • ✅ Automatic fallback to LiFi for unwrapping

Supported Networks

  • Mainnet - Full plugin support
  • Testnet - Coming soon

Installation

Prerequisites: Node.js 22.10+ with pnpm

pnpm install

Configuration

Edit scripts/deploy/deploy.config.ts for network-specific settings:

export const deployConfig: Record<string, DeployConfig> = {
  mainnet: {
    weth: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
    plugins: {
      loanPlugins: {
        morpho: "0xBBBBBbbBBb9cC5e90e3b3Af64bdAF62C37EEFFCb",
        euler: "0xD8b27CF359b7D15710a5BE299AF6e7Bf904984C2",
        uniswapV3: [
          {
            token: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
            pool: "0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640", // USDC/WETH 0.05%
          },
        ],
        aave: "0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2",
        balancer: "0xBA12222222228d8Ba445958a75a0704d566BF2C8",
      },
      swapPlugins: {
        lifi: "0x1231DEB6f5749EF6cE6943a275A1D3E7486F4EaE",
        oneInch: "0x111111125421cA6dc452d289314280a0f8842A65",
        okx: {
          router: "0x3608c8186fF3dCa322DeEFb8c27162162d581081",
          approveProxy: "0x70cBb871E8f30Fc8Ce23609E9E0Ea87B6b222F58",
        },
        wsteth: "0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0",
        wstethSteth: "0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84",
      },
    },
  },
};

Deployment

Deploy All Plugins

# Deploy flash loan plugins
npx hardhat run scripts/deploy/plugins/flashloan/morpho.ts --network mainnet
npx hardhat run scripts/deploy/plugins/flashloan/euler.ts --network mainnet
npx hardhat run scripts/deploy/plugins/flashloan/uniswapv3.ts --network mainnet
npx hardhat run scripts/deploy/plugins/flashloan/aave.ts --network mainnet
npx hardhat run scripts/deploy/plugins/flashloan/balancer.ts --network mainnet

# Deploy swap plugins
npx hardhat run scripts/deploy/plugins/swap/lifi.ts --network mainnet
npx hardhat run scripts/deploy/plugins/swap/oneinch.ts --network mainnet
npx hardhat run scripts/deploy/plugins/swap/okx.ts --network mainnet
npx hardhat run scripts/deploy/plugins/swap/wsteth.ts --network mainnet

Deploy CometFoundation

npx hardhat run scripts/deploy/deploy.main.ts --network mainnet

Deployment addresses are saved to deployments/{network}.json

Deployment Structure

After successful deployment, deployments/{network}.json contains:

{
  "loanPlugins": {
    "morpho": {
      "endpoint": "0x...",
      "config": { "morphoBlue": "0xBBBBBbbBBb9cC5e90e3b3Af64bdAF62C37EEFFCb" }
    },
    "euler": {
      "endpoint": "0x...",
      "config": { "evault": "0xD8b27CF359b7D15710a5BE299AF6e7Bf904984C2" }
    },
    "uniswapV3": {
      "endpoint": "0x...",
      "config": {
        "pools": [{ "token": "0xA0b...", "pool": "0x88e..." }]
      }
    }
  },
  "swapPlugins": {
    "lifi": {
      "endpoint": "0x...",
      "config": { "router": "0x1231DEB6f5749EF6cE6943a275A1D3E7486F4EaE" }
    },
    "oneInch": {
      "endpoint": "0x...",
      "config": { "aggregator": "0x111111125421cA6dc452d289314280a0f8842A65" }
    },
    "okx": {
      "endpoint": "0x...",
      "config": {
        "router": "0x3608c8186fF3dCa322DeEFb8c27162162d581081",
        "approveProxy": "0x70cBb871E8f30Fc8Ce23609E9E0Ea87B6b222F58"
      }
    },
    "wsteth": {
      "endpoint": "0x...",
      "config": {
        "wsteth": "0x7f39...",
        "steth": "0xae7a...",
        "fallbackPlugin": "0x...",
        "fallbackConfig": "0x..."
      }
    }
  },
  "CometFoundation": "0x..."
}

Documentation

Comprehensive documentation for core components:

  • CometFoundation - Complete guide covering all three operations (multiply, cover, exchange), architecture, and integration examples

  • Flash Loan Plugins - Detailed guide to the flash loan plugin system, covering all supported protocols and their configurations

  • Swap Plugins - Complete documentation for swap plugins, including DEX aggregators and specialized converters

Usage Examples

Create 2x Leveraged Position

import { ethers } from "hardhat";

// Setup
const foundation = await ethers.getContractAt(
  "CometFoundation",
  FOUNDATION_ADDRESS,
);
const weth = await ethers.getContractAt("IERC20", WETH_ADDRESS);
const comet = await ethers.getContractAt("IComet", COMET_USDC_MARKET);

// Approve collateral
await weth.approve(FOUNDATION_ADDRESS, ethers.parseEther("1"));

// Authorize foundation on Comet
await comet.allow(FOUNDATION_ADDRESS, true);

// Calculate the base asset amount to borrow (e.g., for 2x leverage)
const collateralAmount = ethers.parseEther("1");
const baseAmount = await calculateLeveragedAmount(
  comet,
  collateralAmount,
  20000, // 2x leverage in basis points
);

// Get swap data from aggregator API (LiFi, 1inch, or OKX)
const swapData = await getSwapQuote(
  USDC_ADDRESS, // swap from base asset
  WETH_ADDRESS, // to collateral
  baseAmount,
  FOUNDATION_ADDRESS,
);

// Execute multiply
await foundation.multiply(
  {
    comet: COMET_USDC_MARKET,
    loanPlugin: MORPHO_PLUGIN_ADDRESS,
    swapPlugin: LIFI_PLUGIN_ADDRESS, // or OKX_PLUGIN_ADDRESS, ONEINCH_PLUGIN_ADDRESS
  },
  WETH_ADDRESS, // collateral
  collateralAmount, // 1 WETH initial deposit
  baseAmount, // amount of base asset to borrow
  100, // maxHealthFactorDrop (1% = 100 bps)
  swapData,
);

Close Leveraged Position

const comet = await ethers.getContractAt("IComet", COMET_USDC_MARKET);
const borrowBalance = await comet.borrowBalanceOf(userAddress);
const collateralBalance = await comet.collateralBalanceOf(
  userAddress,
  WETH_ADDRESS,
);

// Get swap quote for collateral → base asset
const { swapCalldata, amountOut } = await getSwapQuote(
  WETH_ADDRESS,
  USDC_ADDRESS,
  collateralBalance, // swap all collateral
  FOUNDATION_ADDRESS,
);

// Derive loanDebt: the flash loan amount the swap output can repay
// For full close, cap at borrowBalance
const flashFeeRate = 500n; // 0.05% for Uniswap V3
const loanDebt = (BigInt(amountOut) * 1_000_000n) / (1_000_000n + flashFeeRate);
const cappedLoanDebt = loanDebt > borrowBalance ? borrowBalance : loanDebt;

// Close entire position
await foundation.cover(
  {
    comet: COMET_USDC_MARKET,
    loanPlugin: UNISWAP_V3_PLUGIN_ADDRESS,
    swapPlugin: LIFI_PLUGIN_ADDRESS,
  },
  cappedLoanDebt, // Amount of base asset to flash loan
  WETH_ADDRESS, // Collateral to withdraw
  ethers.MaxUint256, // Withdraw all collateral
  swapCalldata,
);

Swap Collateral

// Swap WETH to wstETH
await foundation.exchange(
  {
    comet: COMET_WETH_MARKET,
    loanPlugin: AAVE_PLUGIN_ADDRESS,
    swapPlugin: WSTETH_PLUGIN_ADDRESS,
  },
  WETH_ADDRESS, // From
  WSTETH_ADDRESS, // To
  ethers.parseEther("1"), // Amount
  ethers.parseEther("0.95"), // Min output
  500, // Max 5% health drop
  "0x", // No swap data for wstETH
);

Testing

Run all tests:

pnpm test

Run specific test suite:

pnpm test test/multiplier/lifi/euler-adapter.test.ts

Run with gas reporting:

REPORT_GAS=true pnpm test

Coverage:

pnpm coverage

Security

  • ✅ Reentrancy protection via OpenZeppelin ReentrancyGuard
  • ✅ Health factor validation for exchange operations
  • ✅ Plugin authorization system with magic byte validation
  • ✅ Balance verification throughout execution
  • ✅ Transient storage prevents state manipulation
  • ✅ Comprehensive test coverage

Gas Optimization

  • Transient Storage (EIP-1153): ~20k gas saved per operation
  • Delegatecall Pattern: Minimized contract size
  • Single Transaction: All operations atomic
  • Plugin Registry: O(1) lookups via mapping

License

MIT

Contributing

Contributions welcome! Please open an issue or submit a pull request.

Support

For issues and questions:

  • GitHub Issues: [Report bugs or request features]
  • Documentation: Review /documentation directory
  • Tests: Check /test directory for integration examples

Built by WOOF! Software

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors