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 byRunsHoldsUse it when
Your browserWhile the app is open in a visible tabYour whole account, as alwaysYou are at the screen for the life of the position
The watcherAlwaysThat one position's keys and contentsYou want it kept without running anything
Your own scriptWhile your process runsWhatever key file you give itYou 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".

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:

bash
curl "https://api.mesa.markets/v1/watcher/enrolments?positionId=<position id>"
FieldMeaning
statusactive, renewing, lapsed or removed
lastRenewedAtWhen the watcher last renewed it
nextDeadlineAtThe deadline it is renewed to
renewCountHow 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

renew.ts
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, nextDeadlineAt on the position is in the future and state is sealed.
StateIn the appMeaning
sealedSealedRenewed, with time in hand
dueRenewal dueThe deadline is close
lapsedOpen: deadline missedThe 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 seeCauseDo
ProofVerificationFailedThe position is not healthy at the current price, or the price moved between proving and landingCheck health. If it is below 1.00, add collateral or repay. Otherwise try again.
StaleOracleThe oracle price is older than the program acceptsTry again when the price updates
UNAUTHORIZED on enrolmentThe position is already enrolled with a different keyRemove the earlier enrolment first
PROVER_FAILEDThe prover did not answerTry again. The loop does, on its next pass.