How it works
1. Escrow registration
When a settlement reachesREGISTERING_ESCROW, the engine registers the expected deposits on the escrow smart contract. The contract knows exactly what each party should deposit.
2. Deposit monitoring
AtAWAITING_DEPOSITS, KeyStone monitors the escrow contract for incoming deposits using two mechanisms:
| Method | Latency | Role |
|---|---|---|
| Alchemy webhooks | Sub-second | Primary detection |
| Fallback poller | ~5 minutes | Safety net for missed webhooks |
DepositReceived event is detected on-chain, KeyStone checks if ALL expected deposits are now complete. If so, the settlement advances automatically.
3. Atomic execution
When all deposits are confirmed, the engine callsexecute() on the escrow contract. This performs the atomic swap in a single transaction:
- Tokens move to the buyer’s wallet
- Payment moves to the seller’s wallet
- The settlement transitions to
SETTLED
4. Rollback protection
If the settlement times out or fails:- The engine calls
rollback()on the escrow contract - All deposits are returned to their original owners
- No partial execution is possible
Deposit flow for platforms
From your platform’s perspective:- You receive a webhook:
settlement.state.awaiting_deposits - Instruct the settlement parties to deposit to the escrow contract address
- KeyStone detects the deposits automatically (no API call needed from you)
- You receive a webhook:
settlement.state.executing_swap - You receive a webhook:
settlement.state.finalized
The escrow contract address is included in the settlement details once registration is complete. Query the settlement to get the contract address and expected deposit amounts.
Monitoring deposit status
Check deposit progress by querying the settlement:Timeout behavior
Every settlement has atimeout_at deadline. If all deposits aren’t received by this time:
- Settlement transitions to
TIMED_OUT - Escrow contract returns any received deposits
- All platforms are notified via webhook