Hedge a recurring payment automatically
A customer pays the same euro bill every month, such as a payroll. Instead of locking each one by hand, create a hedge rule: "hedge 70% of the monthly euro payroll, locked 30 days before payday". Certa then quotes and locks each payday on schedule, through the same checks as a lock you make yourself, and records every run.
1. Create the rule
const rule = await certa.hedgeRules.create({
customerId,
name: "Payroll",
pair: "USDC/EURC",
side: "sell_base", // deliver USDC, receive EURC: a euro payment made from USDC
amount: "45000000000", // €45,000 a month (EURC minor units)
amountCurrency: "EURC",
hedgeBps: 7000, // hedge 70% of it
cadence: { type: "monthly", dayOfMonth: 25, hourUtc: 9 }, // payday, UTC
tenorSeconds: 30 * 86_400, // lock 30 days before each payday
execution: "managed", // sandbox: Certa signs with the customer's sandbox wallet
});
console.log(`${rule.id}: next payday ${rule.nextPaymentAt}, locks at ${rule.nextRunAt}`);
cadenceis when the payments fall, in UTC:monthlyon a day (shorter months use their last day) orweeklyon a weekday. The sandbox also takesminutes, every N minutes, for demos.tenorSecondsis how long before each payment the rule locks: every lock matures on its payment date. It runs from 1 hour plus a quote's 30 seconds and five minutes, up to 365 days.- With
amountCurrency: "EURC", each lock's USDC notional is sized at the forward rate a moment before the quote, so the EURC it receives lands within a few basis points of the hedged share. WithUSDC, the notional is the hedged share.maxPerRunUsdccaps a run; a capped run says so. execution: "managed"locks from the customer's sandbox wallet, which needs the deposit (5%) and fee (0.3%) each time. Withnotify, Certa sends ahedge_rule.dueevent at each run instead, and you quote and lock with the customer's own wallet.
2. Try it now in the sandbox
// Sandbox only: run the next payday now instead of waiting a month.
const run = await certa.sandbox.runHedgeRuleNext(rule.id);
The next payday runs at once, as the schedule would run it; the rule then waits for the payday after. Outside the sandbox this is NOT_AVAILABLE_IN_MODE.
3. Read what each run did
const report = (r: HedgeRuleRun) =>
r.outcome === "ok"
? `${r.paymentAt}: locked ${r.usdcNotional} USDC for ${r.targetAmount} EURC (lock ${r.lockId})`
: `${r.paymentAt}: ${r.outcome} ${r.errorCode ?? ""} ${r.reason ?? ""}`;
console.log(report(run));
const { data: runs } = await certa.hedgeRules.runs(rule.id, { limit: 12 });
for (const r of runs) console.log(report(r)); // newest first, one per payday
| Outcome | Meaning |
|---|---|
ok | Locked (the run has its quote and lock), or for notify rules the event sent |
refused | The API refused it, for example LIMIT_EXCEEDED or KYB_REQUIRED. Not tried again: errorCode and reason say why. MISSED is a payday that passed while the scheduler was down. |
failed | The chain refused the lock, or three tries ran out |
running | In progress, or waiting to be tried again after a short outage (STALE_PRICE, an expired quote) |
Each payday is run once, even with several schedulers or a crash halfway: a run that resumes checks whether its quote was already locked before trying again. Every lock's reference is the rule's name and the payment date, so it shows in your lock list.
4. Listen for the events
// In your webhook worker (see "Handle webhooks safely"): one event per run.
function onEvent(event: CertaEvent) {
const r = event.data as HedgeRuleRun;
if (event.type === "hedge_rule.fired") console.log(`Locked ${r.paymentAt}: ${r.lockId}`);
if (event.type === "hedge_rule.failed") console.log(`Not locked ${r.paymentAt}: ${r.reason}`);
}
hedge_rule.fired follows each lock, hedge_rule.failed each run that did not lock, and hedge_rule.due each run of a notify rule. The data is the run. The lock's own events (lock.created…) follow as usual.
5. Pause, resume or stop
await certa.hedgeRules.pause(rule.id); // no more runs until it is resumed
await certa.hedgeRules.resume(rule.id); // from the next payday still far enough away
await certa.hedgeRules.archive(rule.id); // stop for good; its runs and locks stay readable
A paused rule does nothing; resuming starts from the next payday that is still at least tenorSeconds away, so it never locks late. You can change the amount, share, name and cap with update; to change the schedule, archive the rule and create a new one.