> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tayho.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Orders

> Prepare, submit, monitor, and cancel relayed Tayho market and conditional orders.

# Orders

Orders use the same relayer-only signing model as other writes. The session key signs an EIP-712
order; the API accepts it as an intent; and the relayer submits execution when its conditions are
met. Direct wallet broadcasting is not currently supported, including for market orders.

## Order types

| `orderType` | Meaning      | Price fields                                   |
| ----------: | ------------ | ---------------------------------------------- |
|         `0` | Market open  | `triggerPriceWad` and `limitPriceWad` are zero |
|         `1` | Limit open   | Set the execution bound in `limitPriceWad`     |
|         `2` | Market close | Identify the position and close quantity       |
|         `3` | Stop-loss    | Set the trigger in `triggerPriceWad`           |
|         `4` | Take-profit  | Set the trigger in `triggerPriceWad`           |

`direction` is `0` or `1`. `closeMode: 0` closes `qWad`; `closeMode: 1` closes the entire position.
Amounts and prices use protocol integer units. Use a 32-byte zero `ocoGroup` when the order is not
part of an OCO pair.

## Prepare and submit

```ts theme={null}
const prepared = await actionClient.orders.prepare({
  orderType: 1,
  marketId: 1n,
  direction: 0,
  positionId: 0n,
  qWad: 1_000_000_000_000_000_000n,
  marginTokens: 100_000_000n,
  triggerPriceWad: 0n,
  limitPriceWad: 620_000_000_000_000_000n,
  closeMode: 0,
  ocoGroup: `0x${"00".repeat(32)}`,
});

const receipt = await prepared.submit();
const terminal = await receipt.wait();
```

The signer address is the default `trader` and `sessionKey`. You may provide another trader when the
connected signer is its authorized session key. The SDK generates a nonce and five-minute deadline
unless supplied.

With a chain reader, preparation validates the unused nonce, active session, authorized market,
spend and size bounds, collateral and Permit2 funding, and—when closing—the live position identity.
Preparation fails before signing when the current on-chain state cannot satisfy the order.

## Intent lifecycle

The submission result includes `orderHash`, `state`, `duplicate`, `statusUrl`, the canonical order,
`get()`, and `wait()`. A repeated submission with the prepared order's stable idempotency key can
return `duplicate: true`; this is success, not a second order.

`wait()` uses the same bounded HTTP polling controls as action receipts. It stops on `filled`,
`rejected`, `cancelled`, `cancelled_by_oco`, `expired`, or `failed`. Realtime events improve
responsiveness but are not the source of lifecycle correctness.

## Cancellation

Prepare cancellation with the accepted order hash and the exact canonical order:

```ts theme={null}
const cancellation = await actionClient.orders.prepareCancel({
  orderHash: receipt.orderHash,
  order: receipt.order,
});

await cancellation.submit();
```

The order's session key must sign the cancellation. Its prepared payload also keeps a stable nonce,
deadline, signature, and idempotency key across retries.

Cancellation is best effort. It can stop an outstanding limit, stop-loss, or take-profit intent, but
cannot reverse an order that has already executed or won the execution race. An OCO execution may
also terminate its sibling as `cancelled_by_oco`.
