Docs · Guides
Borrow and repay
Borrow against sealed collateral, read the position's health, repay, add or remove collateral, and close, in the app or from code.
A borrow locks collateral in a sealed position and pays the borrowed asset to your balance. Nobody can read the position's collateral or its liquidation price. It stays sealed for as long as it keeps proving it is healthy, once per heartbeat.
Before you borrow
- A Mesa balance of the collateral asset. See Deposit and withdraw.
- A plan for renewal. A position must be renewed before each deadline, by your open browser, by the watcher or by your own script. Read Keep positions renewed before you borrow.
Each pool's page shows its Collateral factor, the liquidation threshold beside it, the Liquidation bonus and the Heartbeat.
Borrow
1. Open Lend and choose the asset to borrow.
2. In the "Borrow" panel, choose the collateral asset.
3. Enter the collateral. Enter the amount to borrow, or press "Up to …" for the most the collateral allows.
4. Check "Health after" and the borrow rate. Press "Review borrow".
5. Press "Borrow".const config = await mesa.mesa.client.config()
const sol = config.assets.find((a) => a.symbol === 'SOL')!
const { position } = await mesa.borrow({
poolId: 'USDC',
collateralMint: sol.mint,
collateral: 10_000_000_000n, // 10 SOL
amount: 1_000_000_000n, // 1,000 USDC
})The most you can borrow is the collateral's value at the oracle price times the pool's collateral factor. The proof made when you place the borrow shows the position is within it, without revealing either amount.
The borrow goes through a batch. When the batch settles, the borrowed asset is in your balance and the position is listed under Your positions as Sealed. Until then it reads In its batch.
On the batch record, the pool's Borrowed total rises by the amount.
If the pool has less cash than the batch's borrows ask for, borrows are taken in the order they were placed and the rest are refused. A refused borrow pays nothing, and Close on its position returns the collateral. The same holds for a borrow that was never opened by the end of its batch's open window.
Read the position
| Shown | Meaning |
|---|---|
| Collateral | What the position holds |
| Health | 1.00 is the liquidation threshold. Above it the position can renew. |
| Liquidation price | The collateral price at which health reaches 1.00. Computed in your browser and shown only to you. |
| Next renewal | Time left until the position's deadline |
| Renewed by | This browser, the watcher, or your own script |
From code these are health, liquidationPrice, nextDeadlineAt and renewedBy on each entry of mesa.snapshot().positions.
Interest accrues on the debt at the pool's borrow rate, so health falls slowly even if the price does not move.
Repay and change collateral
On the pool's page, the Borrow panel's Repay tab holds three actions for a position.
1. Open Lend, choose the asset, and select "Repay" in the "Borrow" panel.
2. If you hold several positions, choose one.
3. Choose "Repay", "Add collateral" or "Remove collateral" and enter the amount.
4. Press "Review", check the debt or collateral after, and confirm.await mesa.repay({ positionId: position!.id, amount: 400_000_000n })
const account = mesa.mesa
await account.run({ kind: 'add_collateral', positionId: position!.id, amount: 2_000_000_000n }, console.log)
await account.run({ kind: 'remove_collateral', positionId: position!.id, amount: 1_000_000_000n }, console.log)These three do not wait for a batch. Each proves the position healthy after the change and moves its deadline one heartbeat on. Removing collateral is refused if it would leave the position below the threshold.
A repayment's amount is public, as an amount repaid to the pool. Which position it reduced is not.
Close
Press "Close" on the position.await mesa.close({ positionId: position!.id })Closing repays the whole debt at the pool's current index from your balance of the borrowed asset and returns all the collateral. You need the debt with interest in your balance when you close.
What can stop a borrow or a change
| You see | Cause | Do |
|---|---|---|
| "More than this collateral allows." | The amount exceeds the collateral factor | Borrow less or lock more collateral |
| "More than the pool has available." | The pool's cash is below the amount | Borrow less |
UnsupportedCollateral | The pool does not accept that collateral asset | Choose one the pool lists |
StaleOracle | The oracle price is older than the program accepts | Try again when the price updates |
ProofVerificationFailed | The oracle price or the pool's index moved between proving and landing | Try again. The proof is rebuilt at the new price. |
Insufficient Mesa balance for <mint> | Closing or repaying more than your balance of the borrowed asset | Deposit or buy the asset first |
If you stop renewing
A position that passes its deadline unrenewed lapses: its contents become readable. If it is still healthy, renewing seals it again. If it is not, anyone can liquidate it, repaying the debt and taking collateral worth the debt plus the liquidation bonus. The rest returns to your balance. See Liquidation.