LogoLogo
  • Getting Started
  • How to Connect
  • REST API
    • Order
    • PublicMarketData
    • Wallet
    • Trade
    • Models
  • Smart Contract ABI
    • OrderBook
    • Order Data (explanation)
  • Websocket API
    • Orderbook
    • Ticker
  • SDK Documentation
    • Getting Started
    • REST API Interaction
    • Smart Contract Interaction
      • Order Placement
      • Order Cancelation
Powered by GitBook

Privacy

  • Privacy Policy

All rights reserved. 21X AG

On this page
  • Steps to Place an Order
  • Initialize the REST API Client
  • Fetch Trading Pairs
  • Retrieve Price Information
  • Initialize the OrderBook Class
  • Set Token Allowances
  • Place an Order
Export as PDF
  1. Sample Use Cases & Code

Using the Python-SDK

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 RestClient

# Initialize the REST client
client = RestClient()

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['baseTokenData']['symbol'] == 'DEVAMDIII'
)

orderbook_address = selected_pair['smartContractOrderBook']
print(f'OrderBook Contract Address: {orderbook_address}')

Retrieve Price Information

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

from x21_sdk.client.api.public_market_data import get_trade_info

trade_info = get_trade_info.sync(
    client=client,
    id=selected_pair['id'],
)

assert trade_info['lastPrice'] == 'CONTINUOUS_TRADING'
print(f'current price: {trade_info['lastPrice']}')

Initialize the OrderBook Class

Use the retrieved orderbook_address to interact with the smart contract.

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.

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.

# 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.

Last updated 2 months ago