← Playground / Payments

Escrow state machine

An idempotent, ordered event consumer you can break on purpose. Fire duplicate and out-of-order webhook deliveries and watch the ledger refuse to double-spend.

  1. draft
  2. funded
  3. confirmed
  4. disputed
  5. released
  6. refunded

Statedraft

Next sequence#1

Parked0

Applied ids0

  1. No deliveries yet. Send an event, then redeliver it.

A teaching model of the general problem, written for this page. It is not Middleman's implementation and describes no private system.

01 / How it works

Payment webhooks arrive twice, arrive late, and arrive out of order. A consumer that assumes otherwise corrupts money state. This is that problem reduced to something you can click.

A reducer holds the agreement state, the set of event ids already applied, and the sequence number it expects next. A duplicate id is acknowledged and dropped. A future sequence number is parked in a pending buffer and replayed the moment the gap fills. An event that is not legal from the current state is rejected outright.

  • TypeScript
  • React
  • Reducer