Leveraging

1. Initial Deposit and Leverage

The most common approach is to deposit and leverage in a single transaction. This is ideal for users who want to create a leveraged position from scratch.

Preparing for Leverage

Before leveraging, you need to:

  1. Understand your target position: Determine the collateral asset, borrow asset, and desired leverage ratio.

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

  3. Approve the necessary contracts: Your collateral token must approve the Position Management contract.

Here's how to prepare the deposit and leverage transaction:

// Approve the position management contract to spend your tokens if depositing and leveraging in one transaction
const underlyingContract = new ethers.Contract(
  underlyingAsset,
  UNDERLYING_ABI,
  signer
);
const depositAmount = ethers.utils.parseUnits('1000', 18); // Adjust amount and decimals
await underlyingContract.approve(POSITION_MANAGEMENT, depositAmount);

Constructing the LeverageStruct

The key to a successful leverage operation is properly constructing the LeverageStruct:

// Create the swap data structure
// This example uses 1inch swap data, but any DEX can be used
const swapData = {
  target: '0x1111111254EEB25477B68fb85Ed929f73A960582', // 1inch router
  inputToken: COLLATERAL_ASSET_UNDERLYING,
  outputToken: BORROWED_ASSET_ADDRESS,
  inputAmount: ethers.utils.parseUnits('500', 18), // Amount to borrow and swap
  call: '0x...', // Encoded swap call data from 1inch API
};

// Construct the leverage struct
const leverageData = {
  borrowToken: E_TOKEN,
  borrowAmount: ethers.utils.parseUnits('500', 18),
  positionToken: P_TOKEN,
  swapData: swapData,
  auxData: '0x' // Optional auxiliary data for specialized protocols
};

Executing the Leverage Operation

With the LeverageStruct prepared, execute the leverage operation:

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

// Execute deposit and leverage in one transaction
const tx = await positionManagement.depositAndLeverage(
  depositAmount,
  leverageData,
  slippage,
  2000000
);

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

2. Leveraging an Existing Position

If you already have collateral deposited, you can increase your leverage without an additional deposit:

const tx = await positionManagement.leverage(
  leverageData, // Same structure as above
  slippage,
  2000000
);

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

Important Considerations

  1. Position Health and Risk Management:

    1. Monitor your position's health factor before and after leveraging.

    2. Consider the maximum leverage ratio allowed by the protocol.

    3. Be aware of liquidation risks when increasing leverage.

    4. Calculate the minimum collateral required to maintain a safe position.

  2. Swap Data Configuration:

    1. The swapData must accurately reflect the desired swap route.

    2. Ensure the inputAmount matches the borrowAmount in the leverage struct.

    3. Set appropriate slippage tolerance for the swap (typically 0.3-1% for stable pairs, 1-3% for volatile pairs).

  3. Amount Calculations:

    1. Ensure these amounts are properly calculated to achieve desired leverage ratio.

    2. Consider protocol fees when calculating amounts.

  4. Protocol-Specific Features:

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

    2. Check protocol documentation for specific requirements.

Last updated