Skip to content

feature detect sanitization #128

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 13 commits into from
Apr 8, 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ tokio-stream = "0.1.17"
tokio-tungstenite = { version = "0.26", features = ["native-tls"] }

drift-pubsub-client = { version = "0.1.1", path = "crates/pubsub-client" }
arrayvec = "0.7.6"

[dev-dependencies]
bytes = "1"
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,11 @@ git clone https://github.com/drift-labs/drift-rs &&\
cd drift-rs &&\
git submodule update --init --recursive
```

**build**
```bash
# Build from source (default)
CARGO_DRIFT_FFI_STATIC=1
# on linux distros may need to run `ldconfig` to link
ldconfig

# Provide a prebuilt drift_ffi_sys lib
CARGO_DRIFT_FFI_PATH="/path/to/libdrift_ffi_sys"
Expand Down
18 changes: 17 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,29 @@ fn build_ffi_lib(current_dir: &Path) -> Result<(), Box<dyn std::error::Error>> {
// Build the library
let profile = std::env::var("PROFILE")?;
let drift_ffi_sys_crate = current_dir.join("crates/drift-ffi-sys");

// Check if submodule was cloned/exists
check_submodule_exists(&drift_ffi_sys_crate)?;
build_with_toolchain(&drift_ffi_sys_crate, lib_target, &profile)?;
install_library(&drift_ffi_sys_crate, &profile, lib_ext)?;

Ok(())
}

fn check_submodule_exists(crate_path: &Path) -> Result<(), Box<dyn std::error::Error>> {
// Check if the Cargo.toml file exists in the submodule directory
let cargo_toml_path = crate_path.join("Cargo.toml");

if !cargo_toml_path.exists() {
println!("cargo:warning=drift-ffi-sys submodule not found");
return Err(format!(
"drift-ffi-sys submodule not initialized: {} not found. Run 'git submodule update --init --recursive'",
cargo_toml_path.display()
).into());
}

Ok(())
}

fn get_platform_details(
host_target: &str,
) -> Result<(&'static str, &'static str), Box<dyn std::error::Error>> {
Expand Down
28 changes: 28 additions & 0 deletions crates/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ extern "C" {
order_params: &types::OrderParams,
accounts: &mut AccountsList,
) -> FfiResult<bool>;
#[allow(improper_ctypes)]
pub fn order_params_will_auction_params_sanitize(
order_params: &types::OrderParams,
perp_market: &accounts::PerpMarket,
oracle_price: i64,
is_signed_msg: bool,
) -> FfiResult<bool>;
}

//
Expand Down Expand Up @@ -188,6 +195,27 @@ pub fn simulate_place_perp_order(
to_sdk_result(res)
}

/// Simulates using the program's update_perp_auction_params func to determine if
/// an order's auction params will get sanitized
///
/// Returns `true` if the order's auctions will get sanitized
pub fn simulate_will_auction_params_sanitize(
order_params: &mut types::OrderParams,
perp_market: &accounts::PerpMarket,
oracle_price: i64,
is_signed_msg: bool,
) -> SdkResult<bool> {
let res = unsafe {
order_params_will_auction_params_sanitize(
order_params,
perp_market,
oracle_price,
is_signed_msg,
)
};
to_sdk_result(res)
}

impl types::SpotPosition {
pub fn is_available(&self) -> bool {
unsafe { spot_position_is_available(self) }
Expand Down
24 changes: 9 additions & 15 deletions crates/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,7 @@ pub struct TransactionBuilder<'a> {
ixs: Vec<Instruction>,
/// use legacy transaction mode
legacy: bool,
/// add additional lookup tables (v0 only)
/// Tx lookup tables (v0 only)
lookup_tables: Vec<AddressLookupTableAccount>,
/// some markets forced to include in the tx accounts list
force_markets: ForceMarkets,
Expand Down Expand Up @@ -1712,10 +1712,7 @@ impl<'a> TransactionBuilder<'a> {
authority: self.authority,
user: self.sub_account,
user_stats: Wallet::derive_stats_account(&self.authority),
taker: Wallet::derive_user_account(
&taker_account.authority,
signed_order_info.taker_subaccount_id(),
),
taker: signed_order_info.taker_subaccount(),
taker_stats: Wallet::derive_stats_account(&taker_account.authority),
taker_signed_msg_user_orders: Wallet::derive_swift_order_account(
&taker_account.authority,
Expand All @@ -1729,11 +1726,11 @@ impl<'a> TransactionBuilder<'a> {
);

if taker_account_referrer != &DEFAULT_PUBKEY {
accounts.push(AccountMeta::new(*taker_account_referrer, false));
accounts.push(AccountMeta::new(
Wallet::derive_stats_account(taker_account_referrer),
false,
));
accounts.push(AccountMeta::new(*taker_account_referrer, false));
}

self.ixs.push(Instruction {
Expand All @@ -1755,7 +1752,7 @@ impl<'a> TransactionBuilder<'a> {
/// or see `place_and_make_swift_order`
///
/// * `signed_order_info` - the signed swift order info
/// * `taker_account` - taker account data (authority of the swift order)
/// * `taker_account` - taker subaccount data
///
pub fn place_swift_order(
mut self,
Expand All @@ -1768,27 +1765,24 @@ impl<'a> TransactionBuilder<'a> {
"only swift perps are supported"
);

let perp_writable = [MarketId::perp(order_params.market_index)];
let perp_readable = [MarketId::perp(order_params.market_index)];
let accounts = build_accounts(
self.program_data,
types::accounts::PlaceSignedMsgTakerOrder {
state: *state_account(),
authority: self.authority,
user: Wallet::derive_user_account(
&taker_account.authority,
signed_order_info.taker_subaccount_id(),
),
user: signed_order_info.taker_subaccount(),
user_stats: Wallet::derive_stats_account(&taker_account.authority),
signed_msg_user_orders: Wallet::derive_swift_order_account(
&taker_account.authority,
),
ix_sysvar: SYSVAR_INSTRUCTIONS_PUBKEY,
},
&[taker_account],
self.force_markets.readable.iter(),
perp_writable
perp_readable
.iter()
.chain(self.force_markets.writeable.iter()),
.chain(self.force_markets.readable.iter()),
self.force_markets.writeable.iter(),
);

let swift_taker_ix_data = signed_order_info.to_ix_data();
Expand Down
Loading
Loading