Curvance
  • Protocol Overview
    • Click Less, Earn More
    • Protocol Architecture
    • Asset Types
    • Liquidity Markets
      • Borrowing
      • Liquidations
      • Interest Rates
      • Oracles
      • Collateral Caps
      • Bad Debt Socialization
    • Application Specific Sequencing
    • New Age Liquidity Mining
      • Protocols
    • How Are New Assets Integrated
    • Plugin System
    • Universal Account Balance
    • Token Approval Management
    • Lending Risks
  • Security
    • Security and Audits
  • Miscellaneous
    • RPCs and Testnet Stability
    • Glossary
    • TL;DR
      • Customer Types and Benefits
    • Brand Assets
    • Weblinks
    • Disclaimer
    • Frequently Asked Questions
  • Developer Docs
    • Overview
    • Quick Start Guides
      • Atlas Fastlane Auctions (coming soon)
      • Plugin Integration
        • List of Delegable Actions
      • Loans & Collateral
        • Lend Assets
        • Deposit into pTokens
        • Withdraw Loans
        • Withdraw pTokens
      • Borrowing & Repayment
        • Borrow
        • Repaying Debt
      • Leverage
        • Leveraging
        • Deleveraging
    • Lending Protocol
      • Market Manager
      • Position Tokens (pToken)
      • Earn Tokens (eTokens)
      • Dynamic Interest Rate Model
      • Universal Balance
    • Position Management
      • Leverage
      • Deleverage / Fold
    • Dynamic Liquidation Engine (DLE)
      • Orderflow Auction System
      • Bad Debt Socialization
    • Plugin & Delegation System
      • Transfer Lock Mechanism
      • Delegable Actions
    • Cross-Chain Functionality
      • Messaging Hub
      • Fee Manager
      • Reward Manager
    • Auxiliary Functionality
Powered by GitBook
On this page
Export as PDF
  1. Developer Docs
  2. Quick Start Guides
  3. Leverage

Deleveraging

Understanding Deleveraging

Deleveraging is the process of reducing your position's leverage by withdrawing collateral and repaying debt. This can be done in two ways:

  1. Partial deleveraging: Reduce leverage while maintaining an active position.

  2. Full deleveraging: Completely unwind the position by repaying all debt.

Preparing for Deleveraging

Before deleveraging, you need to:

  1. Understand your current position: Review your collateral amount, debt amount, and health factor.

  2. Set an appropriate slippage tolerance: Typically 0.5-2% depending on asset volatility.

  3. Calculate the optimal deleveraging amounts: Determine how much collateral to withdraw and debt to repay.

Constructing the DeleverageStruct

Deleveraging requires constructing a DeleverageStruct that specifies how to unwind your position:

// Define how much collateral to withdraw
const collateralAmount = ethers.utils.parseUnits('300', 18);
const repayAmount = ethers.utils.parseUnits('200', 18); // Amount of debt to repay

// Create swap data to convert collateral to borrow asset
// Note: This is an array of swaps, allowing for multi-hop routes
const swapData = [{
  target: '0x1111111254EEB25477B68fb85Ed929f73A960582', // 1inch router
  inputToken: underlyingAsset,
  outputToken: borrowUnderlying,
  inputAmount: ethers.utils.parseUnits('250', 18), // Amount needed to convert to repay debt
  call: '0x...', // Encoded swap call data
}];

// Construct deleverage data
const deleverageData = {
  positionToken: P_TOKEN,
  collateralAmount: collateralAmount,
  borrowToken: E_TOKEN,
  swapData: swapData,
  repayAmount: repayAmount,
  auxData: '0x' // Optional for specialized protocols
};

Executing the Deleverage Operation

With the DeleverageStruct prepared, execute the deleverage operation:

// Set slippage tolerance (1%)
const slippage = ethers.utils.parseUnits('0.01', 18);

// Execute the deleverage transaction
const tx = await positionManagement.deleverage(
  deleverageData, 
  slippage,
  2000000
);

const receipt = await tx.wait();
console.log(`Position deleveraged! Tx hash: ${receipt.transactionHash}`);

Important Considerations

  1. Swap Data Configuration:

    1. The swapData array can contain multiple swaps for complex routes.

    2. Each swap must specify the correct input/output tokens and amounts.

    3. Slippage tolerance should be set appropriately for each swap.

  2. Amount Calculations:

    1. collateralAmount: The amount of position tokens to withdraw.

    2. repayAmount: The amount of debt to repay in the borrowed asset.

    3. Ensure these amounts are properly calculated to maintain desired position size.

  3. Protocol-Specific Features:

    1. Some protocols may require additional data in the auxData field.

    2. Check protocol documentation for specific requirements.

    3. Consider using protocol-specific optimizations when available.

PreviousLeveragingNextLending Protocol

Last updated 9 days ago