Build on Asentum
Scheduled Contract Calls
Native cron on chain. Your contract fires itself on a schedule, no keeper needed. Estimated read time: 6 minutes
TL;DR
Scheduling on Asentum goes through the cron registry, a system contract at ase1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqxgkr943 that holds a calendar of jobs (the ARC-21 standard). There is no cron global inside contracts. You register a job by calling the registry's schedule method with ASE attached as gas escrow, either in a transaction from a wallet or from a contract through E().
At the start of every block the chain asks the registry which jobs are due and calls each one's method. No off-chain keeper, no oracle, no bot to keep running. Each run is billed against the escrow.
The schedule method
schedule(target, method, args, nextRunAt, intervalSec, maxRuns, gasLimit) -> jobId| target | The contract to call, an address. From the SDK pass ase1; inside a contract, msg.contractAddress for "this contract". |
| method | The method name to call, a string. |
| args | The array of arguments passed to that method on every run. |
| nextRunAt | Absolute time of the first run, in milliseconds since the Unix epoch. Must be later than the current block time. |
| intervalSec | Despite the name, milliseconds between runs. 0 means run once. Otherwise 60,000 (one minute) to 31,536,000,000 (365 days). |
| maxRuns | 0 runs until the escrow runs out or you cancel. N stops after N runs. |
| gasLimit | Gas per run, 21,000 to 5,000,000. |
| msg.value | The ASE you send with the call is the gas escrow. It must cover at least one run. |
Every time value is in milliseconds, the same unit as chain.blockTimestamp. Numbers can be passed as numbers or decimal strings. There is no cron-expression syntax such as @daily: work out the first run time and the interval yourself.
Schedule from a wallet
The simplest route: one transaction to the registry with @asentum/sdk. The wallet that sends it owns the job, so only that wallet can cancel it.
import { AsentumClient, AsentumWallet } from '@asentum/sdk';
const client = new AsentumClient('https://testnet.asentum.com');
const wallet = AsentumWallet.fromSecretKey(client, SECRET_KEY_HEX, PUBLIC_KEY_HEX);
const CRON = 'ase1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqxgkr943';
const HOUR_MS = 3_600_000;
await wallet.sendCall({
to: CRON,
method: 'schedule',
args: [
'ase1...yourContract', 'tick', [], // target, method, args
Date.now() + HOUR_MS, // first run, ms
HOUR_MS, // every hour
0, // no run limit
500_000, // gas per run
],
valueWei: 500_000n * 1000n, // escrow
});
// The receipt's JobScheduled event carries the job id.The SDK turns the ase1 target into the hex form contracts use. The scheduled method should check that msg.sender is the registry (see below), or anyone could call it directly.
Schedule from a contract
A contract can schedule itself by calling the registry with E() and forwarding ASE as escrow with E(cron, { value }). The contract then owns the job. E() takes the hex form of an address, so pass the registry's ase1 address to init through the SDK, which converts it, rather than writing it into the source.
const DAY = 86_400_000n; // ms
({
// cron: the cron registry's ase1 address, passed in at init.
init(cron) {
assert(!storage.get('owner'), 'already initialised');
storage.set('owner', msg.sender);
storage.set('cron', cron);
},
// Send ASE with this call. It becomes the job's gas escrow.
start() {
assert(msg.sender === storage.get('owner'), 'not owner');
const id = E(storage.get('cron'), { value: msg.value }).schedule(
msg.contractAddress, // target: this contract
'tick', // method the chain will call
[], // its arguments
(chain.blockTimestamp + DAY).toString(), // first run, ms
DAY.toString(), // then every day (0 = run once)
'0', // max runs (0 = until escrow runs out)
'500000', // gas limit per run
);
storage.set('jobId', id);
return id;
},
// Called by the chain once a day.
tick() {
assert(msg.sender === storage.get('cron'), 'only the cron registry');
const n = BigInt(storage.get('ticks') || '0') + 1n;
storage.set('ticks', n.toString());
emit('Ticked', { n: n.toString(), at: chain.blockTimestamp.toString() });
},
stop() {
assert(msg.sender === storage.get('owner'), 'not owner');
E(storage.get('cron')).cancel(storage.get('jobId')); // escrow goes back to this contract
},
});Call init with the registry address, then start with the escrow attached as the call's value. From then on the chain calls tick once a day.
What a run looks like
- A job is due in the first block whose timestamp is at or after
nextRunAt. After each run the registry movesnextRunAton by the interval. If the job fell behind (a full block, downtime), missed slots are skipped rather than run back to back, and the schedule keeps its phase. - A one-shot job (interval 0) is removed after its run.
- During the run
msg.senderis the cron registry andmsg.valueis 0. The method can useE(),transfer()and everything else a normal call can. - A target that runs several jobs can ask the registry which one is running:
E(cron).currentJob()returns{ id, owner, target, method }during a run andnullotherwise. - If the method throws, its changes are rolled back, the run is still billed, and the job stays scheduled. Make scheduled methods safe to run again.
Gas escrow
The ASE sent with schedule is the job's escrow. After each run the registry deducts the gas the run used, times its gas price. When what is left can no longer pay for one more run at gasLimit, the job is removed and the rest is refunded to the owner. A job also ends, with a refund, when it reaches maxRuns.
The registry prices gas with chain.baseFee, which the VM does not provide to contracts, so it uses its fallback of 1 wei per unit of gas today. At schedule time the escrow must be at least gasLimit wei. Send more if you want the job to run for a long time; the unused part comes back.
Cancel, top up, inspect
cancel(jobId) // owner only; refunds the remaining escrow to the owner
topUp(jobId) // anyone; the ASE sent with the call is added to the escrow
getJob(jobId) // free view: the job record, or nullOnly the job's owner can cancel: the wallet that scheduled it, or the contract that did. A contract-owned job is cancelled by the contract itself, with E(cron).cancel(jobId), and the refund goes to the contract. A job that cancels itself from inside its own run is removed once that run has been billed.
Read a job for free with POST /view: {"contract":"ase1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqxgkr943","method":"getJob","args":["1"]}. Times, gas and escrow come back as decimal strings, with nextRunAt and intervalSec in milliseconds.
With Vault and Workers
Cron plus Vault and Workers is the killer combo. Schedule a method on your contract. Have that method call E(vault).requestDecryption(sid, jobId, payload) with a payload telling the Worker to hit Slack, Resend, OpenAI, whatever. Now you have a contract that triggers real-world API calls on a schedule, with the schedule, the authorization, and the credential all on chain.
No other blockchain has this combination natively.
Limits
- Per-block budget: all due jobs share 15,000,000 gas per block. Jobs that do not fit wait for the next block.
- Due jobs per block: the chain looks at up to 100 due jobs per block, in order of due time.
- Interval: 0 (run once) or 60,000 ms to 365 days.
- Gas per run: 21,000 to 5,000,000.
- Targets: a job cannot target the cron registry itself.
- Failed runs are billed: if your method throws, the gas it used is still deducted and the job is rescheduled.
Full source for the cron registry lives at packages/node/src/chain/cron-registry.ts in the AsentumChain repo.