Skip to main content

Overview

The TransactionBuilder provides a fluent API for constructing Solana transactions that interact with the Drift protocol. It handles account resolution, instruction building, and transaction packaging.

Constructor

new

Initialize a new TransactionBuilder instance.
program_data
&ProgramData
required
Program data from the blockchain containing market configs and state
sub_account
Pubkey
required
The Drift sub-account address
user
Cow<User>
required
The user’s account data
delegated
bool
required
Set true to build transactions for delegated signing
returns
TransactionBuilder
A new TransactionBuilder instance

Example

use drift_rs::TransactionBuilder;
use std::borrow::Cow;

let tx_builder = TransactionBuilder::new(
    &program_data,
    sub_account_pubkey,
    Cow::Borrowed(&user),
    false, // not delegated
);

Configuration Methods

These methods configure the transaction builder and return Self for chaining.

legacy

Use legacy transaction mode instead of versioned transactions.
let tx_builder = tx_builder.legacy();

lookup_tables

Extend the transaction lookup tables (always includes default Drift LUTs).
lookup_tables
&[AddressLookupTableAccount]
required
Additional lookup tables to include
let tx_builder = tx_builder.lookup_tables(&additional_luts);

with_priority_fee

Set the priority fee for the transaction.
microlamports_per_cu
u64
required
Price per compute unit in microlamports
cu_limit
Option<u32>
Optional compute unit limit
let tx_builder = tx_builder.with_priority_fee(1000, Some(200_000));

force_include_markets

Force specific markets to be included in transaction accounts.
readable
&[MarketId]
required
Markets to include as readable
writeable
&[MarketId]
required
Markets to include as writeable
tx_builder.force_include_markets(
    &[MarketId::perp(0)],
    &[MarketId::QUOTE_SPOT],
);

add_ix

Append a custom instruction to the transaction.
ix
Instruction
required
The instruction to add
let tx_builder = tx_builder.add_ix(custom_instruction);

set_ix

Replace an instruction at a specific index.
idx
usize
required
Index of the instruction to replace
ix
Instruction
required
The new instruction
let tx_builder = tx_builder.set_ix(0, new_instruction);

Order Management Methods

place_orders

Place one or more orders.
orders
Vec<OrderParams>
required
List of orders to place
let tx_builder = tx_builder.place_orders(vec![order_params]);

cancel_all_orders

Cancel all orders for the account.
let tx_builder = tx_builder.cancel_all_orders();

cancel_orders

Cancel orders matching specific criteria.
market
(u16, MarketType)
required
Market index and type to filter by
direction
Option<PositionDirection>
Optional direction filter (Long or Short)
use drift_rs::types::{MarketType, PositionDirection};

let tx_builder = tx_builder.cancel_orders(
    (0, MarketType::Perp),
    Some(PositionDirection::Long),
);

cancel_orders_by_id

Cancel orders by their order IDs.
order_ids
Vec<u32>
required
List of order IDs to cancel
let tx_builder = tx_builder.cancel_orders_by_id(vec![12345, 12346]);

cancel_orders_by_user_id

Cancel orders by user-defined order IDs.
user_order_ids
Vec<u8>
required
List of user order IDs to cancel
let tx_builder = tx_builder.cancel_orders_by_user_id(vec![1, 2, 3]);

modify_orders

Modify existing orders by order ID.
orders
&[(u32, ModifyOrderParams)]
required
List of (order_id, modify_params) tuples
use drift_rs::types::ModifyOrderParams;

let modifications = vec![
    (12345, ModifyOrderParams {
        price: Some(105_000_000),
        ..Default::default()
    }),
];

let tx_builder = tx_builder.modify_orders(&modifications);

modify_orders_by_user_id

Modify existing orders by user order ID.
orders
&[(u8, ModifyOrderParams)]
required
List of (user_order_id, modify_params) tuples
let modifications = vec![
    (1, ModifyOrderParams {
        price: Some(105_000_000),
        ..Default::default()
    }),
];

let tx_builder = tx_builder.modify_orders_by_user_id(&modifications);

place_and_make

Place a maker order to match with a specific taker order.
order
OrderParams
required
The order to place
taker_info
&(Pubkey, User)
required
Taker’s account address and data
taker_order_id
u32
required
ID of the taker’s order to match
referrer
Option<Pubkey>
Optional referrer account
fulfillment_type
Option<SpotFulfillmentType>
Fulfillment type for spot orders (ignored for perp)
let tx_builder = tx_builder.place_and_make(
    order_params,
    &(taker_pubkey, taker_user),
    taker_order_id,
    None,
    None,
);

Collateral Management Methods

deposit

Deposit collateral into the user’s account.
amount
u64
required
Amount to deposit in native token units
market_index
u16
required
Spot market index to deposit into
reduce_only
Option<bool>
If true, only reduces existing borrow
transfer_hook
Option<Pubkey>
Transfer hook program if required by token
let tx_builder = tx_builder.deposit(
    1_000_000_000, // 1 SOL
    1,             // SOL-PERP market
    None,
    None,
);

withdraw

Withdraw collateral from the user’s account.
amount
u64
required
Amount to withdraw in native token units
market_index
u16
required
Spot market index to withdraw from
reduce_only
Option<bool>
If true, only reduces existing deposit
transfer_hook
Option<Pubkey>
Transfer hook program if required by token
let tx_builder = tx_builder.withdraw(
    1_000_000_000, // 1 SOL
    1,             // SOL-PERP market
    None,
    None,
);

Query Methods

ixs

Get the instructions currently included in the transaction.
returns
&[Instruction]
Slice of instructions
let instructions = tx_builder.ixs();
println!("Transaction has {} instructions", instructions.len());

Complete Example

use drift_rs::{TransactionBuilder, types::*};
use std::borrow::Cow;

// Initialize builder
let mut tx_builder = TransactionBuilder::new(
    &program_data,
    sub_account,
    Cow::Borrowed(&user),
    false,
)
.with_priority_fee(1000, Some(200_000));

// Build order
let order = OrderParams {
    order_type: OrderType::Limit,
    market_type: MarketType::Perp,
    direction: PositionDirection::Long,
    user_order_id: 1,
    base_asset_amount: 1_000_000_000,
    price: 100_000_000,
    market_index: 0,
    reduce_only: false,
    post_only: PostOnlyParam::None,
    bit_flags: 0,
    max_ts: None,
    trigger_price: None,
    trigger_condition: OrderTriggerCondition::Above,
    oracle_price_offset: None,
    auction_duration: None,
    auction_start_price: None,
    auction_end_price: None,
};

// Add instructions
tx_builder = tx_builder
    .place_orders(vec![order])
    .cancel_orders_by_user_id(vec![0]); // Cancel old order

// Build and send transaction
let tx = tx_builder.build();

Build docs developers (and LLMs) love