Getting Started
This section will guide you through the initial steps to set up and begin using the 21X SDK.
Prerequisites
Before you start, ensure you have the following:
Python Version: Ensure Python 3.97 or higher is installed on your system.
Access Credentials: Obtain an API key for the REST API and ensure your wallet is registered and whitelisted in the 21X platform.
Polygon Wallet: Use a funded wallet on the Polygon network to cover gas fees for smart contract interactions.
Polygon RPC Provider: Access a Polygon-compatible RPC endpoint (e.g., Infura, Alchemy, or a self-hosted node) for blockchain communication.
Installation
Install the SDK using pip:
pip install 21x-sdk
Basic Setup
1. Initialize the REST API Client
The REST API client is used to interact with platform features like fetching open orders and price information.
from x21_sdk import Client, AuthenticatedClient
# Initialize either a client for the public endpoints
client = Client(base_url="<insert the 21X API url here>")
# Or a Client to use the authenticated endpoints
client = AuthenticatedClient(
base_url="<insert the 21X API url here>",
client_id="<insert the client id here>",
client_secret="<insert the client secret here>",
token_endpoint="<insert the oidc token endpoint here>",
)
Alternatively, you can make use the following environment variables, to auto configure the clients.
export 21X_BASE_URL=<>
export 21X_AUTH_CLIENT_ID=<>
export 21X_AUTH_CLIENT_SECRET=<>
export 21X_AUTH_CLIENT_TOKEN_ENDPOINT=<>
from x21_sdk import Client, AuthenticatedClient
# Initialize either a client for the public endpoints
client = Client()
# Or a Client to use the authenticated endpoints
client = AuthenticatedClient()
2. Example: Fetch Available Trading Pairs
Use the REST API client to list all trading pairs:
from x21_sdk.client.api.public_market_data import get_trading_pairs
trading_pairs = get_trading_pairs.sync(client=client)
for pair in trading_pairs.items:
print("orderbook_addr: " + pair.smart_contract_order_book)
print("base: " + pair.base_token_data.symbol)
print("quote: " + pair.quote_token_symbol)
3. Initialize the OrderBook Class
The OrderBook
class is your entry point for interacting with the EVM-based smart contract.
from x21_sdk import OrderBook
# Initialize the OrderBook class with your private key and RPC URL
order_book = OrderBook(
private_key="your_private_key",
orderbook_addr="0xOrderBookAddress",
rpc_url="https://polygon-mainnet.g.alchemy.com/v2/YOUR_API_KEY",
)
4. Example: Check Token Balances
Here’s how you can check the token balances for a trading pair:
balances = order_book.get_balance()
print(f"Base Token Balance: {balances.base}")
print(f"Quote Token Balance: {balances.quote}")
Last updated