Skip to content

Fix/tx builder hlm #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 65 additions & 4 deletions crates/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1705,7 +1705,9 @@ impl<'a> TransactionBuilder<'a> {
self.force_markets.writeable.iter(),
);

if orders.iter().any(|x| x.high_leverage_mode()) {
if self.account_data.margin_mode == MarginMode::HighLeverage
|| orders.iter().any(|x| x.high_leverage_mode())
{
accounts.push(AccountMeta::new(*high_leverage_mode_account(), false));
}

Expand Down Expand Up @@ -1935,7 +1937,7 @@ impl<'a> TransactionBuilder<'a> {
.chain(self.force_markets.writeable.iter()),
);

if order.high_leverage_mode() {
if order.high_leverage_mode() || taker_info.1.margin_mode == MarginMode::HighLeverage {
accounts.push(AccountMeta::new(*high_leverage_mode_account(), false));
}

Expand Down Expand Up @@ -2013,7 +2015,9 @@ impl<'a> TransactionBuilder<'a> {
.chain(self.force_markets.writeable.iter()),
);

if order.high_leverage_mode() {
if order.high_leverage_mode()
|| maker_info.is_some_and(|(_, m)| m.margin_mode == MarginMode::HighLeverage)
{
accounts.push(AccountMeta::new(*high_leverage_mode_account(), false));
}

Expand Down Expand Up @@ -2153,7 +2157,9 @@ impl<'a> TransactionBuilder<'a> {
self.force_markets.writeable.iter(),
);

if signed_order_info.order_params().high_leverage_mode() {
if signed_order_info.order_params().high_leverage_mode()
|| taker_account.margin_mode == MarginMode::HighLeverage
{
accounts.push(AccountMeta::new(*high_leverage_mode_account(), false));
}

Expand Down Expand Up @@ -2781,4 +2787,59 @@ mod tests {
assert_eq!(spot.len(), 1);
assert_eq!(perp.len(), 1);
}

#[tokio::test]
async fn test_place_orders_high_leverage() {
// Create a test user with high leverage mode
let mut user = User::default();
user.margin_mode = MarginMode::HighLeverage;
let user = Cow::Owned(user);

// Create program data
let program_data = ProgramData::new(
vec![SpotMarket::default()],
vec![PerpMarket::default()],
vec![],
State::default(),
);
let sub_account = Pubkey::new_unique();

// Create transaction builder
let builder = TransactionBuilder::new(&program_data, sub_account, user, false);

// Test case 1: Place orders with high leverage mode account included due to user margin mode
let orders = vec![OrderParams {
market_index: 0,
market_type: MarketType::Perp,
direction: PositionDirection::Long,
order_type: OrderType::Limit,
..Default::default()
}];

let tx = builder.place_orders(orders).build();

// Check that high leverage mode account is included
let high_leverage_account = *high_leverage_mode_account();
assert!(tx.static_account_keys().contains(&high_leverage_account));

// Test case 2: Place orders with high leverage mode account included due to order params
let mut user = User::default();
user.margin_mode = MarginMode::Default; // Not high leverage
let user = Cow::Owned(user);
let builder = TransactionBuilder::new(&program_data, sub_account, user, false);

let orders = vec![OrderParams {
market_index: 0,
market_type: MarketType::Perp,
direction: PositionDirection::Long,
order_type: OrderType::Limit,
bit_flags: OrderParams::HIGH_LEVERAGE_MODE_FLAG,
..Default::default()
}];

let tx = builder.place_orders(orders).build();

// Check that high leverage mode account is included
assert!(tx.static_account_keys().contains(&high_leverage_account));
}
}
2 changes: 1 addition & 1 deletion crates/src/math/account_list_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct AccountsListBuilder {
/// placeholder account values populated with real market & oracle account data
perp_accounts: ArrayVec<AccountWithKey, 16>,
spot_accounts: ArrayVec<AccountWithKey, 16>,
oracle_accounts: ArrayVec<AccountWithKey, 16>,
oracle_accounts: ArrayVec<AccountWithKey, 32>,
}

impl AccountsListBuilder {
Expand Down
6 changes: 4 additions & 2 deletions crates/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,13 +549,15 @@ impl ReferrerInfo {
}

impl OrderParams {
pub const IMMEDIATE_OR_CANCEL_FLAG: u8 = 0b0000_0001;
pub const HIGH_LEVERAGE_MODE_FLAG: u8 = 0b0000_0010;
/// true if 'immediate or cancel' bit is set
pub fn immediate_or_cancel(&self) -> bool {
(self.bit_flags & 0b0000_00001) > 0
(self.bit_flags & Self::IMMEDIATE_OR_CANCEL_FLAG) > 0
}
/// true if HLM bit is set
pub fn high_leverage_mode(&self) -> bool {
(self.bit_flags & 0b0000_00010) > 0
(self.bit_flags & Self::HIGH_LEVERAGE_MODE_FLAG) > 0
}
}

Expand Down
Loading