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
- Borrow and repay no longer need a browser. They sign with the credential id of the account's own signer. If none is known, the passkey picker is shown.
- The swap leg is price-protected like trading. Borrow asks the router for the expected output and sets the minimum from
slippageTolerance. If the router returns no quote,execute()throws before signing. - Repay by the swap path repays the slippage-adjusted minimum. If that is less than the full debt, the final collateral withdrawal can revert, and because the batch is atomic nothing happens. Raise the tolerance or repay with the loan token in hand.
- On the swap path
sharesRepaidin the result is the quoted share count, not the shares Morpho burned. Read the loan afterwards for the exact figure.