Exit a lock early
An active lock can be cancelled at any time before maturity, at a price Certa signs. From maturity it can only settle. First see where the lock stands against the market, then get the price, then cancel.
1. Compare the lock with the market
const lock = await certa.locks.get(lockId);
// Any market rate you trust, EURC per USDC. Here, the spot that comes with indicative rates.
const { spot } = await certa.rates.get({
pair: lock.pair,
side: lock.side,
usdcNotional: lock.usdcNotional,
tenorSeconds: 3600,
});
const preview = await certa.locks.preview(lock.id, spot);
console.log(
`Locked at ${preview.lockedRate} until ${lock.maturity}: ${preview.receiveAmount} EURC ` +
`(fee ${lock.fee} paid, deposit ${lock.deposit} held, counterparty Certa (testnet)). ` +
`At ${preview.marketRate} the same USDC buys ${preview.valueAtMarket}: ` +
`the lock is ${preview.gainVsMarket} EURC minor units better.`,
);
GET /v1/locks/{id}/preview is read-only. For the marketRate you pass, it returns valueAtMarket, what the delivered amount would buy at that rate, and gainVsMarket, the locked receiveAmount minus that value. Negative means the market is better than the lock today.
2. Get the price
const price = await certa.locks.cancel(lock.id, { previewOnly: true });
const extraDue = BigInt(price.extraDue ?? "0");
console.log(
`Cancelling now costs ${price.cost} USDC minor units: ` +
`${BigInt(price.cost) - extraDue} from the deposit, ${extraDue} from the customer's wallet. ` +
`${price.refund} of the deposit comes back. Price firm until ${price.expiresAt}.`,
);
POST /v1/locks/{id}/cancel with previewOnly: true returns the price and nothing to sign. The price is:
- cost: how much better the locked terms are than today's forward rate to the same maturity (never below zero), plus the cost of unwinding the hedge (venue fee and slippage). It is never negative: if the market has moved in the customer's favour, the cost is the unwind alone.
- From the deposit first. The deposit pays up to its whole amount;
refundis what is left of it, returned to the customer. - From the customer's wallet:
extraDue, only when the cost is larger than the deposit. - The fee (0.3%) paid when the lock was created is not refunded. The cost goes to the pool, which bears the hedge unwind.
3. Cancel
// The customer agreed. Each call signs a new price, so check it again before sending.
try {
const final = await certa.locks.cancel(lock.id, { execution: "unsigned" });
if (BigInt(final.cost) > BigInt(price.cost)) {
console.log(`The price moved to ${final.cost}: ask the customer again.`);
} else {
// An approve first if extraDue > 0, then cancelLock, all before final.expiresAt.
await sendFromCustomerWallet(final.transactions);
const closed = await certa.locks.waitFor(lock.id, ["cancelled"]);
console.log(`Lock ${closed.id} is ${closed.status}.`);
}
} catch (e) {
// WRONG_STATE: the lock is no longer active, or it reached maturity and settles instead.
if (!(e instanceof CertaError && e.code === "WRONG_STATE")) throw e;
console.log(e.message);
}
Without previewOnly, the call signs a new price for this lock (its cost, a 30-second expiry and a nonce) and returns the transactions that use it: an approval for extraDue if there is one, then cancelLock. LockManager checks the signature, takes the cost from the deposit and then from the wallet, and returns the refund. Send them before expiresAt, or ask for a new price. In the sandbox, execution: "managed" sends them for you. When the chain confirms it, the lock is cancelled and you receive lock.cancelled.
Errors
- WRONG_STATE: the lock is not active, or it has reached maturity and settles instead.
- INSUFFICIENT_LIQUIDITY: the hedge venue cannot take the unwind now. Retry later.
- STALE_PRICE or FEEDS_DISAGREE: no safe price right now. Retry in a few seconds.
To see a costly exit in the sandbox, move the market against the lock first (see Test every outcome in the sandbox).