📚
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
  • Borrow
  • Repay
  1. Guides
  2. Basic Interaction

Borrow and Repay

Borrow & Repay tokens on InitCore's position.

Borrow

Users can borrow tokens into their positions. Only the position owner or approved party to modify can call this function. The function returns a user's debt shares from the total borrows of the lending pool. Users can specify the receiver address to receive the tokens.

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

// Example borrow function
function borrow(uint256 posId, address lendingPool, uint256 amount, address receiver) external returns (uint256 debtShares) {
    // 1. borrow tokens to a position
    debtShares = IInitCore(INIT_CORE).borrow(lendingPool, amount, posId, receiver);
}

Repay

Users can repay their positions to increase the position's health factor.

Users must have underlying tokens in their wallet and pre-approve the token to InitCore.

For best practice, it is recommended that the caller approves the contract with an amount slightly higher than the pre-calculated debt amount, since the interest may accrue over time between the transaction submission to the network and the actual execution and inclusion onto the blockchain.

// Example repay function
function repay(uint256 posId, address lendingPool, uint256 repayShares) external returns (uint256 repaidAmount) {
    // 0. .. pull in repay tokens from the caller ..
    
    // 1. calculate the token amount to repay
    uint256 repayAmount = ILendingPool(lendingPool).debtShareToAmtCurrent(repayShares);
    
    // 2. user approves underlying token with a bit extra amount
    IERC20(underlyingToken).safeApprove(INIT_CORE, repayAmount);
    
    // 3. repay
    repaidAmount = IInitCore(INIT_CORE).repay(lendingPool, repayShares, posId);
}
PreviousAdd and Remove CollateralNextChanging Position Mode

Last updated 1 year ago