# Order lifecycle (v2)

> The five steps of an order, its eight states, and when to stop polling.

> API v2 is deprecated. It stays supported, but new integrations should use the [Khalani API](/) (v3). See [Migrate to v3](/v2/migrate/).

## The five steps

| Step | Call | Who |
|---|---|---|
| 1. Quote | `GET /v2/quote` prices the trade and returns the build request, signed and ready. | Your backend |
| 2. Build | `POST /v2/build` creates the order and returns the transactions to send. | Your backend |
| 3. Deposit | Send the approvals, then the deposit, on the source chain. | The user's wallet |
| 4. Track | Poll `GET /v2/orders/{orderId}` every 10 to 15 seconds until delivery succeeds or fails. | Your backend |
| 5. Cancel (optional) | `POST /v2/orders/{orderId}/cancel` settles a published order early. | The user signs; your backend relays |

`GET /v2/params` is optional. The quote is not: it is the only place the price comes from.

## States

`state` is a closed set of eight values. Client logic should depend on these strings only. Orders move forward and can skip states: `partially_filled` is skipped when the first fill completes the order.

| State | Meaning | Stop polling? |
|---|---|---|
| `pending_deposit` | Built. Waiting for the user's deposit. | No |
| `deposited` | The deposit was seen on the source chain and is on its way to the hub. | No |
| `published` | The order is live as an intent and open to fills. `traces.rootIntentId` and `fullFee` are now set, and the order can be cancelled from here on unless its quote returned `earlyCancelDisabled: true`. | No |
| `partially_filled` | Part of the order filled and the rest is still open. Set on the first fill only. | No |
| `terminal_chain` | No more fills are expected: the order filled completely, reached its deadline, or was cancelled. Settlement follows automatically. | No |
| `withdrawn_on_hub` | Settled on the hub. Payout and refund delivery is not yet fully recorded. | No |
| `delivered` | Every owed payout and refund leg was recorded by tx in `deliver.<leg>` or by reason in `deliver.withoutTx.<leg>`. | **Yes** |
| `delivery_failed` | At least one transfer could not be delivered after retries. The order is settled on the hub; `deliveryFailed` says which transfer. If the failed transfer is recovered, `deliveryFailed` clears and the order moves on to `delivered`, or back to `withdrawn_on_hub` while the other transfer is still in flight. | **Yes** |

Count a leg as delivered when `deliver.<leg>` is non-null **or** `deliver.withoutTx.<leg>` is set. `sub_unit_amount` means the amount was smaller than the destination chain's smallest unit, so nothing was transferred. `tx_unknown` means delivery completed without a known destination tx. Without a tx, `deliver.payout` or `deliver.refund` stays `null`. `deliver.at` changes only when another leg is recorded.

The terminal delivery states for client polling are **`delivered` and `delivery_failed`**. Keep polling through `withdrawn_on_hub`: settlement alone does not confirm receipt of the payout or refund.

Use a local polling timeout and retain the `orderId` so tracking can resume. A timeout means the result is unconfirmed; it does not cancel the order or imply `delivery_failed`. There is no expired state, so an order that is never deposited stays at `pending_deposit`.

Delivery tracking is best effort: an order can stay at `withdrawn_on_hub` after funds arrive if their delivery is never observed. A settlement with no payout or refund to transfer also stays at `withdrawn_on_hub`. Neither case is proof of a failed transfer; inspect `deliver` and the settlement amounts when reconciling a timed-out order.

## Settlement

An order settles in one on-chain transaction, triggered by whichever comes first:

- it fills completely,
- its deadline passes, or
- a cancel is relayed.

Settlement is usually submitted within a minute of the trigger; a relayed cancel submits it immediately. It pays the filled part, priced between the order's rate and its quote, to `payoutAddr` on the destination chain, and refunds the unfilled share of the deposit, after the flat fee, to `refundAddr` on the source chain. An order that never fills gets its deposit back, less the flat fee.

Orders can fill in parts, from more than one fill. `traces.chainObservations` gets one entry per fill, plus one for a cancellation or expiry.
