Skip to main content

Overview

The Swift order subscriber enables market makers to receive real-time off-chain order authorizations from takers. Swift orders are signed messages that can be placed and filled by any willing counter-party within specified time-price bounds.

subscribe_swift_orders

pub async fn subscribe_swift_orders(
    client: &DriftClient,
    markets: &[MarketId],
    accept_sanitized: bool,
    accept_deposit_trades: bool,
    swift_ws_override: Option<String>,
) -> SdkResult<SwiftOrderStream>
Subscribe to Swift WebSocket server for real-time order notifications. Parameters:
  • client - Drift client instance
  • markets - List of perp markets to listen on for new Swift orders
  • accept_sanitized - Set to true to receive sanitized order flow (default: false)
  • accept_deposit_trades - Set to true to receive deposit+trade order flow (default: false)
  • swift_ws_override - Optional custom Swift WebSocket endpoint
Returns:
  • SwiftOrderStream - Stream of new Swift order messages
Example:
use futures_util::StreamExt;
use drift::{DriftClient, MarketId};

let client = DriftClient::new(/* ... */).await?;
let markets = vec![MarketId::perp(0), MarketId::perp(1)];

let mut order_stream = subscribe_swift_orders(
    &client,
    &markets,
    false,  // don't accept sanitized orders
    false,  // don't accept deposit+trade orders
    None    // use default endpoint
).await?;

while let Some(order_info) = order_stream.next().await {
    println!("New Swift order: {}", order_info.order_uuid_str());
    // Process and potentially fill the order
}

Important Notes

Sanitized Orders: A sanitized order may have its auction parameters modified by the program when placed onchain. Market makers should understand the time/price implications before accepting these orders. Deposit+Trade Orders: These orders require fillers to send an attached, preceding deposit transaction before the Swift order transaction.

SignedOrderInfo

Represents a Swift order with metadata received from the WebSocket.
pub struct SignedOrderInfo {
    pub ts: u64,
    pub taker_authority: Pubkey,
    pub signer: Pubkey,
    pub signature: Signature,
    pub will_sanitize: bool,
    pub pre_deposit: Option<String>,
}
Fields:
  • ts - Order creation timestamp (unix milliseconds)
  • taker_authority - The taker’s authority public key
  • signer - The authority pubkey that verifies the signature (taker authority or delegate)
  • signature - Signature over the serialized order payload
  • will_sanitize - True if order params are likely to be sanitized by the program
  • pre_deposit - Taker signed pre-deposit transaction (for deposit+trade orders)

Methods

slot

pub fn slot(&self) -> Slot
Returns the slot number when the user signed the order.

order_uuid_str

pub fn order_uuid_str(&self) -> &str
Returns the order’s UUID as a string.

order_uuid

pub fn order_uuid(&self) -> [u8; 8]
Returns the raw 8-byte UUID.

order_params

pub fn order_params(&self) -> OrderParams
Returns the Drift order parameters of the message. Example:
let params = order_info.order_params();
println!("Market: {}, Direction: {:?}", 
    params.market_index, 
    params.direction
);

taker_subaccount

pub fn taker_subaccount(&self) -> Pubkey
Returns the taker’s sub-account pubkey for the order.

encode_for_signing

pub fn encode_for_signing(&self) -> Vec<u8>
Serializes the order message for onchain signature verification.

to_ix_data

pub fn to_ix_data(&self) -> Vec<u8>
Converts the Swift order into Anchor instruction data format. Example:
let ix_data = order_info.to_ix_data();
// Use ix_data in place_and_make instruction

using_delegate_signing

pub fn using_delegate_signing(&self) -> bool
Returns true if the order was signed using delegated authority.

has_builder

pub fn has_builder(&self) -> bool
Returns true if the order specifies a builder fee and index.

has_isolated_position_deposit

pub fn has_isolated_position_deposit(&self) -> bool
Returns true if the order uses isolated margin mode with a deposit.

isolated_position_deposit

pub fn isolated_position_deposit(&self) -> Option<u64>
Returns the isolated margin deposit amount, if any.

Constructors

authority

pub fn authority(
    taker_authority: Pubkey,
    signed_order: SignedOrder,
    signature: Signature,
) -> Self
Create a SignedOrderInfo for an authority-signed order.

delegated

pub fn delegated(
    taker_authority: Pubkey,
    signing_authority: Pubkey,
    delegated_order: SignedDelegateOrder,
    signature: Signature,
) -> Self
Create a SignedOrderInfo for a delegate-signed order.

SignedOrderType

Enum representing either authority-signed or delegate-signed Swift orders.
pub enum SignedOrderType {
    Authority {
        inner: SignedOrder,
        raw: Option<String>,
    },
    Delegated {
        inner: SignedDelegateOrder,
        raw: Option<String>,
    },
}

Methods

is_delegated

pub fn is_delegated(&self) -> bool
Returns true if this is a delegated signed message order.

to_borsh

pub fn to_borsh(&self) -> Vec<u8>
Serializes the order as a borsh buffer (without enum discriminator).

info

pub fn info(&self, taker_authority: &Pubkey) -> SignedMessageInfo
Extracts common order details.

Message Types

SignedOrder (Authority)

Swift order signed by the taker’s authority keypair.
pub struct SignedMsgOrderParamsMessage {
    pub signed_msg_order_params: OrderParams,
    pub sub_account_id: u16,
    pub slot: Slot,
    pub uuid: [u8; 8],
    pub take_profit_order_params: Option<OrderParams>,
    pub stop_loss_order_params: Option<OrderParams>,
    pub max_margin_ratio: Option<u32>,
    pub builder_idx: Option<u16>,
    pub builder_fee_tenth_bps: Option<u32>,
    pub isolated_position_deposit: Option<u64>,
}

SignedDelegateOrder

Swift order signed by an authorized delegate keypair.
pub struct SignedMsgOrderParamsDelegateMessage {
    pub signed_msg_order_params: OrderParams,
    pub taker_pubkey: Pubkey,
    pub slot: Slot,
    pub uuid: [u8; 8],
    pub take_profit_order_params: Option<OrderParams>,
    pub stop_loss_order_params: Option<OrderParams>,
    pub max_margin_ratio: Option<u32>,
    pub builder_idx: Option<u16>,
    pub builder_fee_tenth_bps: Option<u32>,
    pub isolated_position_deposit: Option<u64>,
}

Constants

Message Discriminators

pub const SWIFT_MSG_PREFIX: [u8; 8] = [0xc8, 0xd5, 0xa6, 0x5e, 0x22, 0x34, 0xf5, 0x5d];
pub const SWIFT_DELEGATE_MSG_PREFIX: [u8; 8] = [0x42, 0x65, 0x66, 0x38, 0xc7, 0x25, 0x9e, 0x23];
Anchor discriminators for Swift message types (first 8 bytes of sha256 hash).

WebSocket Endpoints

pub const SWIFT_DEVNET_WS_URL: &str = "wss://master.swift.drift.trade";
pub const SWIFT_MAINNET_WS_URL: &str = "wss://swift.drift.trade";
Default Swift WebSocket endpoints for devnet and mainnet.

SwiftOrderStream

pub type SwiftOrderStream = ReceiverStream<SignedOrderInfo>;
A stream type that emits Swift orders from the WebSocket server. Implements the Stream trait from futures. Example:
use futures_util::StreamExt;

let mut stream = subscribe_swift_orders(&client, &markets, false, false, None).await?;

while let Some(order) = stream.next().await {
    let params = order.order_params();
    println!("Received order for market {}: {} @ {}",
        params.market_index,
        params.base_asset_amount,
        params.price
    );
}

Authentication Flow

The Swift WebSocket connection automatically handles:
  1. Challenge-Response Authentication: Signs a server-provided nonce with the maker’s wallet
  2. Market Subscription: Subscribes to specified perp markets after authentication
  3. Heartbeat Handling: Maintains connection with periodic heartbeat messages
No manual intervention required - the subscriber handles all protocol details.

Build docs developers (and LLMs) love