📚
INIT Capital Developer Docs
  • Overview
  • Core Contracts
  • Guides
    • Basic Interaction
      • Deposit and Withdraw
      • Create Position
      • Add and Remove Collateral
      • Borrow and Repay
      • Changing Position Mode
    • Advanced Interaction
      • Liquidate Position
      • Flashloan
      • Multicall
      • Callback
    • Liquidity Hook
      • Multicall with Callback
      • Money Market Hook
      • Looping Hook
      • Margin Trading Hook
  • Contract References
    • InitCore
    • PosManager
    • LendingPool
    • Config
    • RiskManager
    • InitOracle
    • LiqIncentiveCalculator
    • DoubleSlopeIRM
    • InitErrors
    • MoneyMarketHook
    • LoopingHook
    • MarginTradingHook
  • Contract Addresses
    • Blast
    • Mantle
Powered by GitBook
On this page
  • Add Collateral
  • Remove Collateral
  1. Guides
  2. Basic Interaction

Add and Remove Collateral

Adding & Removing collaterals to/from an existing position.

Add Collateral

To add collateral to a position, users must already create the position and specify the position ID. The collateral token must be an inToken from lending pools.

The position must be in a mode that allows the collateral to be used.

The above 2 steps must be performed in a single transaction (atomically) to avoid potential front-run attack.

// Example add collateral function
function addCollateral(uint256 posId, address lendingPool, uint256 shares) external {
    // 0. .. pull in lending pool tokens from the caller ..

    // 1. transfer inToken to PosManager
    IERC20(lendingPool).safeTransfer(POS_MANAGER, shares);

    // 2. add collateral to position
    IInitCore(INIT_CORE).collateralize(posId, lendingPool);
}

Remove Collateral

To remove collateral, specify the position ID to remove from, shares of collateral to remove, and also the receiver address. The caller must be the position owner or approved party to modify the position.

Always check that the position's health is over 1 after removing collateral to avoid transaction revert.

// Example remove collateral function
function removeCollateral(uint256 posId, address lendingPool, uint256 shares, address receiver) external {
    // 1. remove collateral from position
    IInitCore(INIT_CORE).decollateralize(posId, lendingPool, shares, receiver);
}
PreviousCreate PositionNextBorrow and Repay

Last updated 1 year ago