Borrowing and repaying

Borrow against tokenized stock collateral, and repay.

Borrowing uses a Morpho market. You choose the collateral, the asset to borrow, and the market parameters.

Borrow

const quote = await account.prepareBorrowQuote({
  collateralAsset: { name: 'TSLA', amount: { unit: 'human', value: '5' } },
  borrowAsset: { name: 'credits' },
  marketParams: {
    loanToken, collateralToken, oracle, irm,
    lltv: 860000000000000000n, // 86%
  },
  slippageTolerance: 0.005, // optional, default 0.5%, applies to the swap leg
})

quote.collateralAsset
quote.borrowAsset
quote.ltvRatio            // the ratio you would open at
quote.liquidationLtvRatio // the ratio at which the position is liquidated
quote.maxBorrowAmount
quote.rate                // borrow rate

const result = await quote.execute()

execute() batches approve, supply collateral and borrow into one user operation and has the signer sign it.

Show the liquidation ratio. A borrower who sees only the amount does not know how close the position sits to liquidation. Always display liquidationLtvRatio and maxBorrowAmount next to the amount.

Repay

RepayQuoteClient exposes borrowShares, collateral, marketParams and execute(). It repays by shares when the account already holds the loan token, and otherwise swaps credits to the loan token first. The swap path repays by assets, because round-trip fees mean the account cannot always cover every share.

AccountClient has prepareTradeQuote and prepareBorrowQuote. To repay, construct RepayQuoteClient with the loan id, account id, account address, quote data and chain id, then call execute().

Notes