You get your own account, named by your IP address, with $100. Send money to a stranger and watch the Transfer move it. Send to bob to see a refund: his account starts frozen.
What’s running
| Object | One per | What it does |
|---|---|---|
BankAccount/<your IP> |
visitor | Holds the balance and sets money aside when you send. A frozen account bounces money sent to it. |
Transfer/<id> |
transfer | The saga: requested → review? → clearing → crediting → settling → completed, or → refunding → refunded. |
Bank/global |
site | The clearing house everyone shares. It clears one transfer every 2.5 s, so yours waits behind strangers’. |
The CI example runs in the same Worker and has its own Account, so this one is BankAccount. The page labels it “Account”.
- Timers: a transfer of $100 or more waits 12 s in
review. It’s anaftertimer, so it survives eviction. - Messages: every hop is a
deliver()from one object’s outbox. Each one waits 700 ms (thepaceoption) so you can see it travel. - Only you spend your money. Commands go through the Worker, which picks the account from your IP. Everyone can watch every object.
The code
Each object is a few lines. The machine does the work.
export class Transfer extends DurableObject<Env> {
machine = new DurableMachine(this, transferMachine, {
input: (transferId) => ({ transferId }),
actions: {
toAccount: ({ accountId, event }) => this.machine.deliver("BankAccount", accountId, event),
toBank: (event) => this.machine.deliver("Bank", "global", event),
},
pace: 700,
});
send(event: EventFromLogic<typeof transferMachine>, envelope?: Envelope) {
return this.machine.send(event, envelope);
}
}Source: examples/bank.