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 from the blockchain containing market configs and state
The Drift sub-account address
Set true to build transactions for delegated signing
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.
Price per compute unit in microlamports
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.
Markets to include as readable
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.
let tx_builder = tx_builder.add_ix(custom_instruction);
set_ix
Replace an instruction at a specific index.
Index of the instruction to replace
let tx_builder = tx_builder.set_ix(0, new_instruction);
Order Management Methods
place_orders
Place one or more orders.
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.
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.
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.
Taker’s account address and data
ID of the taker’s order to match
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 to deposit in native token units
Spot market index to deposit into
If true, only reduces existing borrow
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 to withdraw in native token units
Spot market index to withdraw from
If true, only reduces existing deposit
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.
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();