Skip to content

Commit 94808ba

Browse files
committed
Remove drift support
1 parent db5c904 commit 94808ba

2 files changed

Lines changed: 1 addition & 259 deletions

File tree

src/bin/sys-lend.rs

Lines changed: 1 addition & 258 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use {
2828
send_transaction_until_expired,
2929
token::*,
3030
vendor::{
31-
drift, kamino, marginfi_v2,
31+
kamino, marginfi_v2,
3232
solend::{self, math::TryMul},
3333
},
3434
*,
@@ -77,9 +77,6 @@ lazy_static::lazy_static! {
7777
Token::WIF,
7878
Token::BONK,
7979
])),
80-
("drift", HashSet::from([
81-
Token::USDC,
82-
])),
8380
]);
8481
}
8582

@@ -327,8 +324,6 @@ fn pool_supply_apr(
327324
solend_apr(pool, token, account_data_cache)?
328325
} else if pool == "mfi" {
329326
mfi_apr(token, account_data_cache)?
330-
} else if pool == "drift" {
331-
drift_apr(token, account_data_cache)?
332327
} else {
333328
unreachable!()
334329
})
@@ -361,8 +356,6 @@ async fn pool_supply_balance(
361356
solend_deposited_amount(pool, address, token, account_data_cache)?
362357
} else if pool == "mfi" {
363358
mfi_deposited_amount(address, token, account_data_cache).await?
364-
} else if pool == "drift" {
365-
drift_deposited_amount(address, token, account_data_cache)?
366359
} else {
367360
unreachable!()
368361
})
@@ -441,8 +434,6 @@ async fn build_instructions_for_ops(
441434
solend_deposit_or_withdraw(*op, pool, address, token, amount, account_data_cache)?
442435
} else if *pool == "mfi" {
443436
mfi_deposit_or_withdraw(*op, address, token, amount, false, account_data_cache).await?
444-
} else if *pool == "drift" {
445-
drift_deposit_or_withdraw(*op, address, token, amount, false, account_data_cache)?
446437
} else {
447438
unreachable!();
448439
};
@@ -2916,251 +2907,3 @@ fn solend_deposit_or_withdraw(
29162907
address_lookup_table: Some(pubkey!["89ig7Cu6Roi9mJMqpY8sBkPYL2cnqzpgP16sJxSUbvct"]),
29172908
})
29182909
}
2919-
2920-
//////////////////////////////////////////////////////////////////////////////
2921-
// [ Drift Stuff ] ///////////////////////////////////////////////////////////
2922-
//////////////////////////////////////////////////////////////////////////////
2923-
2924-
const DRIFT_PROGRAM: Pubkey = pubkey!["dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH"];
2925-
2926-
fn drift_load_user(
2927-
user_address: Pubkey,
2928-
account_data_cache: &mut AccountDataCache,
2929-
) -> Result<Option<drift::user::User>, Box<dyn std::error::Error>> {
2930-
let (account_data, _context_slot) = account_data_cache.get(user_address)?;
2931-
2932-
if account_data.is_empty() {
2933-
Ok(None)
2934-
} else {
2935-
const LEN: usize = std::mem::size_of::<drift::user::User>();
2936-
assert_eq!(LEN, drift::user::SIZE - 8);
2937-
let account_data: [u8; LEN] = account_data[8..LEN + 8].try_into().unwrap();
2938-
let user = unsafe { std::mem::transmute::<[u8; LEN], drift::user::User>(account_data) };
2939-
Ok(Some(user))
2940-
}
2941-
}
2942-
2943-
fn drift_find_and_load_user(
2944-
wallet_address: Pubkey,
2945-
account_data_cache: &mut AccountDataCache,
2946-
) -> Result<Option<(Pubkey, drift::user::User)>, Box<dyn std::error::Error>> {
2947-
for subaccount in 0..3 {
2948-
let user_address = Pubkey::find_program_address(
2949-
&[b"user", &wallet_address.to_bytes(), &[subaccount, 0]],
2950-
&DRIFT_PROGRAM,
2951-
)
2952-
.0;
2953-
2954-
let user = drift_load_user(user_address, account_data_cache)?;
2955-
if let Some(user) = user {
2956-
assert_eq!(user.authority, wallet_address);
2957-
return Ok(Some((user_address, user)));
2958-
}
2959-
}
2960-
Ok(None)
2961-
}
2962-
2963-
fn drift_load_spot_market(
2964-
spot_market_address: Pubkey,
2965-
account_data_cache: &mut AccountDataCache,
2966-
) -> Result<drift::spot_market::SpotMarket, Box<dyn std::error::Error>> {
2967-
let (account_data, _context_slot) = account_data_cache.get(spot_market_address)?;
2968-
2969-
const LEN: usize = std::mem::size_of::<drift::spot_market::SpotMarket>();
2970-
assert_eq!(LEN, drift::spot_market::SIZE - 8);
2971-
let account_data: [u8; LEN] = account_data[8..LEN + 8].try_into().unwrap();
2972-
let spot_market =
2973-
unsafe { std::mem::transmute::<[u8; LEN], drift::spot_market::SpotMarket>(account_data) };
2974-
Ok(spot_market)
2975-
}
2976-
2977-
fn drift_find_and_load_spot_market(
2978-
token: Token,
2979-
account_data_cache: &mut AccountDataCache,
2980-
) -> Result<drift::spot_market::SpotMarket, Box<dyn std::error::Error>> {
2981-
let market_index: u16 = match token {
2982-
Token::USDC => 0,
2983-
_ => return Err(format!("Drift support for {} not added yet", token.name()).into()),
2984-
};
2985-
2986-
let spot_market_address = Pubkey::find_program_address(
2987-
&[b"spot_market", &market_index.to_le_bytes()],
2988-
&DRIFT_PROGRAM,
2989-
)
2990-
.0;
2991-
2992-
let spot_market = drift_load_spot_market(spot_market_address, account_data_cache)?;
2993-
assert_eq!(spot_market.mint, token.mint());
2994-
assert_eq!(spot_market.pubkey, spot_market_address);
2995-
Ok(spot_market)
2996-
}
2997-
2998-
fn drift_apr(
2999-
token: Token,
3000-
account_data_cache: &mut AccountDataCache,
3001-
) -> Result<f64, Box<dyn std::error::Error>> {
3002-
let spot_market = drift_find_and_load_spot_market(token, account_data_cache)?;
3003-
Ok(drift::calculate_accumulated_interest(&spot_market).deposit_rate)
3004-
}
3005-
3006-
fn drift_deposited_amount(
3007-
wallet_address: Pubkey,
3008-
token: Token,
3009-
account_data_cache: &mut AccountDataCache,
3010-
) -> Result<(/*balance: */ u64, /* available_balance: */ u64), Box<dyn std::error::Error>> {
3011-
let spot_market = drift_find_and_load_spot_market(token, account_data_cache)?;
3012-
let user_address_and_data = drift_find_and_load_user(wallet_address, account_data_cache)?;
3013-
let deposited_amount = match user_address_and_data {
3014-
None => 0,
3015-
Some((_user_address, data)) => {
3016-
let scaled_balance = data
3017-
.spot_positions
3018-
.iter()
3019-
.find_map(|spot_position| {
3020-
if spot_position.market_index == spot_market.market_index
3021-
&& spot_position.scaled_balance > 0
3022-
&& spot_position.balance_type == drift::user::SpotBalanceType::Deposit
3023-
{
3024-
Some(spot_position.scaled_balance as u128)
3025-
} else {
3026-
None
3027-
}
3028-
})
3029-
.unwrap_or_default();
3030-
3031-
drift::scaled_balance_to_token_amount(
3032-
scaled_balance,
3033-
&spot_market,
3034-
drift::user::SpotBalanceType::Deposit,
3035-
)
3036-
}
3037-
};
3038-
3039-
let remaining_outflow = u64::MAX;
3040-
Ok((deposited_amount, deposited_amount.min(remaining_outflow)))
3041-
}
3042-
3043-
fn drift_deposit_or_withdraw(
3044-
op: Operation,
3045-
wallet_address: Pubkey,
3046-
token: Token,
3047-
amount: u64,
3048-
_verbose: bool,
3049-
account_data_cache: &mut AccountDataCache,
3050-
) -> Result<DepositOrWithdrawResult, Box<dyn std::error::Error>> {
3051-
let state_address = Pubkey::find_program_address(&[b"drift_state"], &DRIFT_PROGRAM).0;
3052-
3053-
let (user_address, _user) = drift_find_and_load_user(wallet_address, account_data_cache).ok().flatten()
3054-
.ok_or_else(|| format!("No Drift account found for {wallet_address}. Manually deposit once into Drift and retry"))?;
3055-
3056-
let user_stats_address =
3057-
Pubkey::find_program_address(&[b"user_stats", &wallet_address.to_bytes()], &DRIFT_PROGRAM)
3058-
.0;
3059-
3060-
let spot_market = drift_find_and_load_spot_market(token, account_data_cache)?;
3061-
3062-
let (instructions, required_compute_units, amount) = match op {
3063-
Operation::Deposit => {
3064-
// Drift: Deposit
3065-
let deposit_data = {
3066-
let mut v = vec![0xf2, 0x23, 0xc6, 0x89, 0x52, 0xe1, 0xf2, 0xb6];
3067-
v.extend(spot_market.market_index.to_le_bytes());
3068-
v.extend(amount.to_le_bytes());
3069-
v.extend([/* Reduce Only = */ 0]);
3070-
v
3071-
};
3072-
3073-
let account_meta = vec![
3074-
// State
3075-
AccountMeta::new_readonly(state_address, false),
3076-
// User
3077-
AccountMeta::new(user_address, false),
3078-
// User Stats
3079-
AccountMeta::new(user_stats_address, false),
3080-
// Authority
3081-
AccountMeta::new(wallet_address, false),
3082-
// Spot Market Vault
3083-
AccountMeta::new(spot_market.vault, false),
3084-
// User Token Account
3085-
AccountMeta::new(
3086-
spl_associated_token_account::get_associated_token_address(
3087-
&wallet_address,
3088-
&token.mint(),
3089-
),
3090-
false,
3091-
),
3092-
// Token Program
3093-
AccountMeta::new_readonly(token.program_id(), false),
3094-
// Spot Market Oracle
3095-
AccountMeta::new_readonly(spot_market.oracle, false),
3096-
// Spot Market
3097-
AccountMeta::new(spot_market.pubkey, false),
3098-
];
3099-
3100-
let instructions = vec![Instruction::new_with_bytes(
3101-
DRIFT_PROGRAM,
3102-
&deposit_data,
3103-
account_meta,
3104-
)];
3105-
3106-
(instructions, 100_000, amount)
3107-
}
3108-
Operation::Withdraw => {
3109-
let drift_signer_address =
3110-
Pubkey::find_program_address(&[b"drift_signer"], &DRIFT_PROGRAM).0;
3111-
3112-
// Drift: Withdraw
3113-
let withdraw_data = {
3114-
let mut v = vec![0xb7, 0x12, 0x46, 0x9c, 0x94, 0x6d, 0xa1, 0x22];
3115-
v.extend(spot_market.market_index.to_le_bytes());
3116-
v.extend(amount.to_le_bytes());
3117-
v.extend([/* Reduce Only = */ 1]);
3118-
v
3119-
};
3120-
3121-
let account_meta = vec![
3122-
// State
3123-
AccountMeta::new_readonly(state_address, false),
3124-
// User
3125-
AccountMeta::new(user_address, false),
3126-
// User Stats
3127-
AccountMeta::new(user_stats_address, false),
3128-
// Authority
3129-
AccountMeta::new(wallet_address, false),
3130-
// Spot Market Vault
3131-
AccountMeta::new(spot_market.vault, false),
3132-
// Drift Signer
3133-
AccountMeta::new(drift_signer_address, false),
3134-
// User Token Account
3135-
AccountMeta::new(
3136-
spl_associated_token_account::get_associated_token_address(
3137-
&wallet_address,
3138-
&token.mint(),
3139-
),
3140-
false,
3141-
),
3142-
// Token Program
3143-
AccountMeta::new_readonly(token.program_id(), false),
3144-
// Spot Market Oracle
3145-
AccountMeta::new_readonly(spot_market.oracle, false),
3146-
// Spot Market
3147-
AccountMeta::new(spot_market.pubkey, false),
3148-
];
3149-
3150-
let instructions = vec![Instruction::new_with_bytes(
3151-
DRIFT_PROGRAM,
3152-
&withdraw_data,
3153-
account_meta,
3154-
)];
3155-
3156-
(instructions, 200_000, amount)
3157-
}
3158-
};
3159-
3160-
Ok(DepositOrWithdrawResult {
3161-
instructions,
3162-
required_compute_units,
3163-
amount,
3164-
address_lookup_table: Some(pubkey!["D9cnvzswDikQDf53k4HpQ3KJ9y1Fv3HGGDFYMXnK5T6c"]),
3165-
})
3166-
}

src/vendor/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/// These projects don't provide a usable Rust SDK..
2-
pub mod drift;
32
pub mod kamino;
43
pub mod marginfi_v2;
54
pub mod solend;

0 commit comments

Comments
 (0)