# Build Order

> Build an EIP-712 order structure for a user to sign.

## Usage

```python
from opinion_clob_sdk.chain.py_order_utils.model.sides import BUY, SELL
from opinion_clob_sdk.chain.py_order_utils.model.order_type import LIMIT_ORDER, MARKET_ORDER

build_result = builder.build_order_for_signing(
    market_id=123,
    token_id="outcome_token_id",
    user_wallet_address="0xSafeAddress...",    # Maker (Safe wallet)
    side=BUY,
    order_type=LIMIT_ORDER,
    amount=10.0,
    price="0.5",
    signer_address="0xUserEOAAddress...",      # Signer (EOA)
)

print(build_result["order"])           # Order struct dict
print(build_result["struct_hash"])     # Hash for the user to sign
print(build_result["typed_data"])      # Full EIP-712 typed data (for reference)
```

## Parameters

| Parameter             | Type      | Required   | Description                                                                                       |
| --------------------- | --------- | ---------- | ------------------------------------------------------------------------------------------------- |
| `market_id`           | int       | Yes        | Market ID                                                                                         |
| `token_id`            | str       | Yes        | Outcome token ID to trade                                                                         |
| `user_wallet_address` | str       | Yes        | Safe address (maker, where funds are held)                                                        |
| `side`                | OrderSide | Yes        | `BUY` or `SELL`                                                                                   |
| `order_type`          | int       | Yes        | `LIMIT_ORDER` (2) or `MARKET_ORDER` (1)                                                           |
| `amount`              | float     | Yes        | Amount in human-readable units                                                                    |
| `price`               | str       | Limit only | Price per share (e.g., `"0.5"` for 50 cents). Required for `LIMIT_ORDER`.                         |
| `amount_type`         | str       | No         | `"quote"` (default) or `"base"`                                                                   |
| `signer_address`      | str       | No         | EOA signer address. If different from `user_wallet_address`, Safe mode (signatureType=2) is used. |

## Response

```python
{
    "order": {                        # Order struct for submission
        "salt": "123...",
        "maker": "0xSafeAddress...",
        "signer": "0xEOAAddress...",
        "taker": "0x0000...0000",
        "tokenId": "...",
        "makerAmount": "10000000",
        "takerAmount": "20000000",
        "expiration": "...",
        "nonce": "0",
        "feeRateBps": "0",
        "side": "0",
        "signatureType": "2",
    },
    "struct_hash": "0xabcdef...",     # Hash for user to sign
    "typed_data": { ... },            # Full EIP-712 typed data
    "domain": { ... },                # EIP-712 domain
    "exchange_address": "0x...",
    "currency_address": "0x...",
    "currency_decimal": 6,
    "market_id": 123,
    "order_type": 2,
    "price": "0.5",
}
```

## Market Order Restrictions

* Market BUY: `amount_type` must be `"quote"` (how much to spend)
* Market SELL: `amount_type` must be `"base"` (how many tokens to sell)
* Market orders set `price` to `"0"` internally.

## Signing the Order

Order signing uses `sign_hash(struct_hash)`, **not** `sign_typed_data`. This is different from Safe TX signing.

```python
# Using UserClient (testing helper)
from opinion_clob_sdk.user_client import UserClient

user = UserClient("0xPrivateKey...")
signature = user.sign_hash(build_result["struct_hash"])
```

For testing, you can also use the static helper method:

```python
signature = BuilderClient.sign_order_with_private_key(
    build_result["typed_data"],
    "0xPrivateKey...",
)
```

## Notes

* In production, users sign the `struct_hash` using their wallet (e.g., `personal_sign` or equivalent).
* The `sign_order_with_private_key()` static method signs the full `typed_data` (EIP-712). This is for testing convenience only.
* Orders expire after 30 days by default.
* The signature type (EOA=0 or Safe=2) is set automatically based on whether `signer_address` differs from `user_wallet_address`.
