Borrow and Repay

Borrow & Repay tokens on InitCore's position.


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);


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);

Last updated