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.
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.
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.
Initialize the OrderBook Class
OrderBook ClassUse the retrieved orderbook_address to interact with the smart contract.
Set Token Allowances
Before placing an order, ensure that the allowances for the base and quote tokens are set appropriately.
Place an Order
Use the create_buy_order or create_sell_order methods to submit your order.
Summary of Workflow
Use the REST API client to fetch trading pairs and locate the
OrderBookaddress.Initialize the
OrderBookclass with the retrieved address.Ensure sufficient token allowances are set for the transaction.
Submit a buy or sell order using the
OrderBookclass.
Last updated

