Rust SDK

Example code to interact with Phoenix in Rust

Get all markets

You can get all markets by calling the getProgramAccounts RPC, filtering for accounts that match the market discriminant. Sample code available here.

Viewing the state of the order book

To get the state of a market's order book, call SDKClient.get_market_orderbook(&market_pubkey). The order book can be pretty-printed with orderbook.print_ladder().

let client = EllipsisClient::from_rpc(
    RpcClient::new_with_commitment(
        "https://api.mainnet-beta.solana.com".to_string(),
        CommitmentConfig::confirmed(),
    ),
    &payer,
)?;

let sdk_client = SDKClient::new_from_ellipsis_client(client).await?;
let sol_market = Pubkey::from_str("4DoNfFBfF7UokCC2FQzriy7yHK6DY6NVdYpuekQ5pRgg")?;
let orderbook = sdk_client.get_market_orderbook(&sol_market).await?;
orderbook.print_ladder(5, 4);
            19.9240   214.3670  
            19.9090   53.5920   
   46.8330  19.8860             
   37.4840  19.8710     

Fetching market events

All market events are logged for easy data indexing and trader convenience. Events are recorded in instruction data via an authorized self-CPI to ensure no logs are dropped.

By listening to all transactions that touch a given market, one can create a stream of all market events as they are confirmed by the blockchain with a polling loop. For latency sensitive operations, look into running a Geyser Plugin.

Sample output:

Placing cross orders

Directly call the send_ioc function exposed by the SDK.

In order to use these helper functions to send transactions, you must first add the market_key to an instance of the SDKClient with the add_market function

Alternatively, to batch multiple instructions into a single atomic transaction, use get_ioc_ix to create instructions and send them manually.

Placing limit orders

Directly call the send_limit_order function exposed by the SDK. Alternatively, use get_limit_order_ix to create instructions and send them manually.

Canceling orders

Use send_cancel_ids, send_cancel_multiple, and send_cancel_all functions to cancel orders.

These functions will never fail if there are no orders to be found for the user who attempted to cancel. Instead the Phoenix program will skip over all orders that are missing or already filled.

This enables greater composability as the full transaction will never revert from a failed cancel attempt.

Place and cancel example

Here is an end-to-end example of how to use a generated client_order_id to send a limit order and find its corresponding exchange order ID. After sending the order, we immediately cancel it using the ID that we fetched from the previous step.

Last updated