Docs · Guides
Keep positions renewed
The three ways to renew a borrow or perp before each deadline: your open browser, the watcher, or your own script, with how to set up and check each.
A position stays sealed only while it is renewed before each deadline. A renewal proves the position healthy at the current oracle price and moves its deadline to the end of the next heartbeat. It needs the position's keys and no spend secret, so it can be handed to something other than you.
Pick one of three for each position. The Renewed by line on the position says which is in effect.
| Renewed by | Runs | Holds | Use it when |
|---|---|---|---|
| Your browser | While the app is open in a visible tab | Your whole account, as always | You are at the screen for the life of the position |
| The watcher | Always | That one position's keys and contents | You want it kept without running anything |
| Your own script | While your process runs | Whatever key file you give it | You run infrastructure and want nothing held by Mesa |
The heartbeat's length is heartbeatSeconds in GET /v1/config, and it is shown as Heartbeat on each pool's page.
Your browser
There is nothing to set up. A position that is not enrolled with the watcher reads Renewed by "this browser, while it is open", and the app renews it for you under three conditions: the app is open in a tab, the tab is visible, and your account is read.
Every 30 seconds the app checks your positions. Once a position is inside the last half of its heartbeat it is renewed, and a notice reads "Renewing a position", then "Renewed. It stays sealed." A renewal that fails is tried again after two minutes. The proof is made in the browser and the renewal is relayed, so it costs nothing.
To renew by hand at any time, press Renew now on the position.
Close the tab, hide it, or disconnect, and renewal stops. A position left alone passes its deadline and lapses.
The watcher
The watcher is Mesa's service that renews positions for their owners.
Press "Let the watcher renew" on the position.
To take it back, press "Renew it myself".const account = mesa.mesa
await account.run({ kind: 'enrol_watcher', positionId: position.id }, console.log)
// take it back
await account.run({ kind: 'remove_watcher', positionId: position.id }, console.log)Enrolling sends the watcher that position's holder keys and its contents. With them the watcher can read that one position and renew it. It cannot read your balances, orders or other positions, and a renewal can only replace the position with the same position and a later deadline, so the watcher can move nothing.
Check an enrolment without any key:
curl "https://api.mesa.markets/v1/watcher/enrolments?positionId=<position id>"| Field | Meaning |
|---|---|
status | active, renewing, lapsed or removed |
lastRenewedAt | When the watcher last renewed it |
nextDeadlineAt | The deadline it is renewed to |
renewCount | How many times the watcher has renewed it |
The watcher can only renew a healthy position. If the position's health falls below 1.00, no renewal proof exists for it, whoever holds the keys. Add collateral or repay before that point.
Your own script
import { openMesaScript } from '@mesa/sdk/node'
const mesa = await openMesaScript({
apiUrl: 'https://api.mesa.markets',
keyFile: './mesa-key.json',
})
const { heartbeatSeconds } = await mesa.mesa.client.config()
const loop = mesa.renewLoop({
intervalMs: 30_000,
marginSeconds: Math.floor(heartbeatSeconds / 2),
onRenew: (position, result) => console.log('renewed', position.id, result.signature),
onError: (id, error) => console.error('renew failed', id, error),
})
process.on('SIGINT', () => loop.stop())
await loop.done
mesa.destroy()On each pass the loop reads the account and renews every position whose deadline is within marginSeconds. With the margin at half a heartbeat and a pass every 30 seconds, each position is renewed once per heartbeat with many passes to spare. Left out, the margin is a third of the heartbeat.
The key file can be the wallet's keypair or the account's 32-byte master secret. A master secret is enough to renew, and it cannot sign a deposit. Run the script wherever it will stay up, and watch onError: a renewal that keeps failing is a position that will lapse.
To renew only some positions, pass positionIds. Renewals are relayed, so the script needs no SOL.
Check that it is working
- Next renewal on the position moves a heartbeat ahead after each renewal.
- Your account's history on Portfolio lists each renewal.
- From code,
nextDeadlineAton the position is in the future andstateissealed.
| State | In the app | Meaning |
|---|---|---|
sealed | Sealed | Renewed, with time in hand |
due | Renewal due | The deadline is close |
lapsed | Open: deadline missed | The deadline passed. The position is readable. |
If a deadline is missed
A lapsed position is readable: its collateral, debt or size, and entry are readable by anyone. What happens next depends on its health.
- Healthy. Renew it, by any of the three ways or with Renew now. It is replaced by a fresh sealed note with a new deadline.
- Unhealthy. Anyone can liquidate it. Adding collateral or repaying, if you get there first, renews it too.
What can stop a renewal
| You see | Cause | Do |
|---|---|---|
ProofVerificationFailed | The position is not healthy at the current price, or the price moved between proving and landing | Check health. If it is below 1.00, add collateral or repay. Otherwise try again. |
StaleOracle | The oracle price is older than the program accepts | Try again when the price updates |
UNAUTHORIZED on enrolment | The position is already enrolled with a different key | Remove the earlier enrolment first |
PROVER_FAILED | The prover did not answer | Try again. The loop does, on its next pass. |