# Order Placement

This example demonstrates the complete workflow for placing an order on the 21X platform. It involves initializing the REST client to discover trading pairs, retrieving the `OrderBook` address for a specific pair, setting up the `OrderBook` class, and submitting an order.

***

## Steps to Place an Order

### Initialize the REST API Client

Use the REST client to fetch trading pairs and locate the target trading pair.

```python
from x21_sdk import Client

# Initialize the REST client
client = Client()
```

### Fetch Trading Pairs

Retrieve a list of available trading pairs and identify the pair of interest.

```python
trading_pairs = get_trading_pairs.sync(client=client)

for pair in trading_pairs.items:
    print(pair)

# Select a trading pair
selected_pair = next(
    pair for pair in trading_pairs if pair.base_token_data.symbol == 'DEVAMDIII'
)

orderbook_address = selected_pair.smart_contract_order_book
print(f'OrderBook Contract Address: {orderbook_address}')
```

### Retrieve Price Information

Use the `getTradeInfo` endpoint to retrieve trade information for the selected trading pair.

```python
from x21_sdk.client.api.public_market_data import get_trade_info
from x21_sdk.client.models import TradingStatusEnum

trade_info = get_trade_info.sync(
    client=client,
    id=selected_pair.id,
)

assert trade_info.trading_status == TradingStatusEnum.CONTINUOUS_TRADING
print(f'current price: {trade_info.last_price}')
```

### Initialize the `OrderBook` Class

Use the retrieved `orderbook_address` to interact with the smart contract.

```python
from x21_sdk import OrderBook

# Initialize the OrderBook with Web3 provider and contract address
order_book = OrderBook(
  private_key='your_private_key',
  orderbook_addr=orderbook_address,
  rpc_url='https://polygon-mainnet.g.alchemy.com/v2/YOUR_API_KEY',
)
```

### Set Token Allowances

Before placing an order, ensure that the allowances for the base and quote tokens are set appropriately.

```python
from decimal import Decimal

order_book.set_base_allowance(amount=Decimal('1005'))
order_book.set_quote_allowance(amount=Decimal('10'))
```

### Place an Order

Use the `create_buy_order` or `create_sell_order` methods to submit your order.

```python
# Example: Place a buy order
order_book.create_buy_order(
    quantity=Decimal('10'),
    price=Decimal('99.75'),
    allowance=False,
)

print('Buy order placed successfully!')

# Example: Place a sell order
order_book.create_sell_order(
    quantity=Decimal('10'),
    price=Decimal('100.25'),
    allowance=False,
)

print('Sell order placed successfully!')
```

***

#### Summary of Workflow

1. Use the REST API client to fetch trading pairs and locate the `OrderBook` address.
2. Initialize the `OrderBook` class with the retrieved address.
3. Ensure sufficient token allowances are set for the transaction.
4. Submit a buy or sell order using the `OrderBook` class.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.21x.eu/sdk/04-contract/01-order_placement.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
