Skip to content
CertadocsGet a sandbox key

Guides

Use a rate in parts: window locks

A regular lock settles once, at maturity. A window lock fixes the rate now and lets the customer use it in parts: any amount, as often as needed, from the start of a window until maturity. Whatever is left settles at maturity, like a regular lock. Use it when the payment date is not known to the day, or when one amount is paid out in several parts.

1. Quote with a window

Add windowStart to the quote: when drawdowns may start. It must be before maturity; now is fine (the whole tenor is usable). The quote carries it back.

// 30 days, usable from day 20: the customer pays suppliers in parts over the last 10 days.
const day = 86_400_000;
const quote = await certa.quotes.create({
  pair: "USDC/EURC",
  side: "sell_base", // the customer delivers USDC, receives EURC
  usdcNotional: "30000000000", // 30,000 USDC in minor units
  tenorSeconds: 30 * 86_400,
  windowStart: new Date(Date.now() + 20 * day).toISOString(),
  customerId,
});
console.log(
  `${quote.usdcNotional} USDC for ${quote.receiveAmount} EURC at ${quote.rate}, usable from ` +
    `${quote.windowStart} until maturity ${quote.maturity}; fee ${quote.fee}, deposit ` +
    `${quote.deposit} (minor units); counterparty Certa (testnet).`,
);

2. Lock

const lock = await certa.locks.create({ quoteId: quote.id, execution: "managed" });
await certa.locks.waitFor(lock.id, ["active"]);

The lock carries a window object: windowStart, the number of drawdowns, drawnUsdc, and remaining (the USDC leg, the amounts to deliver and receive, and the deposit still to settle). A regular lock has window: null.

3. Draw down inside the window

In the sandbox, move the window to now with time travel ("window_start", or "mid_window" to be halfway through it; the window keeps its length):

await certa.sandbox.advance(lock.id, "window_start"); // the window opens now

Then draw down part of the USDC leg with POST /v1/locks/{id}/drawdowns:

// Use 12,000 USDC of the rate now. "managed" sends from the customer's sandbox wallet; in live
// mode use "unsigned" and send d.transactions (approve, then drawdown) from their wallet.
const d = await certa.locks.drawdown(lock.id, {
  usdcAmount: "12000000000",
  execution: "managed",
});
console.log(
  `Delivered ${d.deliverAmount}, received ${d.receiveAmount}, ` +
    `deposit back ${d.depositReleased}; ${d.remaining.usdcNotional} USDC left to settle`,
);

In Python:

from certa import Certa

certa = Certa(api_key=os.environ["CERTA_API_KEY"], base_url=os.environ["CERTA_API_URL"])
d = certa.locks.drawdown(os.environ["CERTA_LOCK_ID"], "12000000000", execution="managed")
print(d["receiveAmount"], d["depositReleased"], d["remaining"]["usdcNotional"])
try {
  await certa.locks.drawdown(lock.id, { usdcAmount: "99000000000", execution: "managed" });
} catch (e) {
  // More than is left: INVALID_REQUEST, with what is left. Before the window or from
  // maturity: WRONG_STATE.
  if (!(e instanceof CertaError && e.code === "INVALID_REQUEST")) throw e;
  console.log(e.message);
}

More than is left is refused with INVALID_REQUEST (its detail gives what is left); before the window or from maturity, with WRONG_STATE.

4. See what was drawn

const drawn = await certa.locks.get(lock.id);
console.log(`${drawn.window?.drawdowns} drawdowns, ${drawn.window?.drawnUsdc} USDC drawn`);
for (const row of (await certa.locks.drawdowns(lock.id)).data) {
  console.log(`#${row.seq} ${row.usdcAmount} USDC at ${row.at}, tx ${row.txHash}`);
}

5. Settle the rest at maturity

From maturity no more drawdowns are taken: settle what is left with POST /v1/locks/{id}/settle, as for a regular lock. It delivers window.remaining, not the original amounts, and returns the rest of the deposit. Without delivery 48 hours after maturity, the remainder defaults (see Defaults): only the deposit still held is kept.

await certa.sandbox.advance(lock.id, "maturity"); // in the sandbox: maturity now
await certa.locks.settle(lock.id, { execution: "managed" }); // the rest, at the locked rate

Cancelling a window lock early prices the cancellation on what is left, the same way as exiting a regular lock.