Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
crispheaney committed Jan 21, 2024
1 parent da6b50c commit 4f8d1e2
Show file tree
Hide file tree
Showing 22 changed files with 1,027 additions and 404 deletions.
922 changes: 730 additions & 192 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions deploy-scripts/verified-build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
solana-verify build --library-name drift
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "",
"license": "ISC",
"devDependencies": {
"@coral-xyz/anchor": "0.28.1-beta.2",
"@coral-xyz/anchor": "0.29.0",
"@project-serum/common": "0.0.1-beta.3",
"@project-serum/serum": "^0.13.38",
"@pythnetwork/client": "^2.5.1",
Expand Down
8 changes: 4 additions & 4 deletions programs/drift/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ mainnet-beta=[]
default=["mainnet-beta"]

[dependencies]
anchor-lang = "0.27.0"
solana-program = "=1.14.16"
anchor-spl = "0.27.0"
anchor-lang = "0.29.0"
solana-program = "1.16"
anchor-spl = "0.29.0"
pyth-client = "0.2.2"
bytemuck = { version = "1.4.0" }
borsh = "0.9.1"
Expand All @@ -31,7 +31,7 @@ arrayref = "0.3.6"
base64 = "0.13.0"
serum_dex = { git = "https://github.com/project-serum/serum-dex", rev = "85b4f14", version = "0.5.6", features = ["no-entrypoint"] }
enumflags2 = "0.6.4"
phoenix-v1 = { git = "https://github.com/drift-labs/phoenix-v1", rev = "4c65c9", version = "0.2.4", features = ["no-entrypoint"] }
phoenix-v1 = { git = "https://github.com/drift-labs/phoenix-v1", rev = "d60b17", version = "0.2.4", features = ["no-entrypoint"] }
solana-security-txt = "1.1.0"
static_assertions = "1.1.0"
drift-macros = { git = "https://github.com/drift-labs/drift-macros.git", rev = "c57d87" }
Expand Down
86 changes: 58 additions & 28 deletions programs/drift/src/instructions/keeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ use crate::{validate, QUOTE_PRECISION_I128};
#[access_control(
fill_not_paused(&ctx.accounts.state)
)]
pub fn handle_fill_perp_order<'info>(ctx: Context<FillOrder>, order_id: Option<u32>) -> Result<()> {
pub fn handle_fill_perp_order<'a, 'b, 'c: 'info, 'info>(
ctx: Context<'a, 'b, 'c, 'info, FillOrder<'info>>,
order_id: Option<u32>,
) -> Result<()> {
let (order_id, market_index) = {
let user = &load!(ctx.accounts.user)?;
// if there is no order id, use the users last order id
Expand Down Expand Up @@ -67,7 +70,11 @@ pub fn handle_fill_perp_order<'info>(ctx: Context<FillOrder>, order_id: Option<u
Ok(())
}

fn fill_order(ctx: Context<FillOrder>, order_id: u32, market_index: u16) -> Result<()> {
fn fill_order<'a, 'b, 'c: 'info, 'info>(
ctx: Context<'a, 'b, 'c, 'info, FillOrder<'info>>,
order_id: u32,
market_index: u16,
) -> Result<()> {
let clock = &Clock::get()?;
let state = &ctx.accounts.state;

Expand Down Expand Up @@ -149,7 +156,7 @@ impl Default for SpotFulfillmentType {
#[access_control(
fill_not_paused(&ctx.accounts.state)
)]
pub fn handle_fill_spot_order<'a, 'b, 'c, 'info>(
pub fn handle_fill_spot_order<'a, 'b, 'c: 'info, 'info>(
ctx: Context<'a, 'b, 'c, 'info, FillOrder<'info>>,
order_id: Option<u32>,
fulfillment_type: Option<SpotFulfillmentType>,
Expand Down Expand Up @@ -183,8 +190,8 @@ pub fn handle_fill_spot_order<'a, 'b, 'c, 'info>(
Ok(())
}

fn fill_spot_order<'info>(
ctx: Context<'_, '_, '_, 'info, FillOrder<'info>>,
fn fill_spot_order<'a, 'b, 'c: 'info, 'info>(
ctx: Context<'a, 'b, 'c, 'info, FillOrder<'info>>,
order_id: u32,
market_index: u16,
fulfillment_type: SpotFulfillmentType,
Expand Down Expand Up @@ -275,7 +282,10 @@ fn fill_spot_order<'info>(
#[access_control(
exchange_not_paused(&ctx.accounts.state)
)]
pub fn handle_trigger_order<'info>(ctx: Context<TriggerOrder>, order_id: u32) -> Result<()> {
pub fn handle_trigger_order<'a, 'b, 'c: 'info, 'info>(
ctx: Context<'a, 'b, 'c, 'info, TriggerOrder<'info>>,
order_id: u32,
) -> Result<()> {
let AccountMaps {
perp_market_map,
spot_market_map,
Expand Down Expand Up @@ -325,7 +335,9 @@ pub fn handle_trigger_order<'info>(ctx: Context<TriggerOrder>, order_id: u32) ->
#[access_control(
exchange_not_paused(&ctx.accounts.state)
)]
pub fn handle_force_cancel_orders<'info>(ctx: Context<ForceCancelOrder>) -> Result<()> {
pub fn handle_force_cancel_orders<'a, 'b, 'c: 'info, 'info>(
ctx: Context<'a, 'b, 'c, 'info, ForceCancelOrder>,
) -> Result<()> {
let AccountMaps {
perp_market_map,
spot_market_map,
Expand Down Expand Up @@ -354,7 +366,9 @@ pub fn handle_force_cancel_orders<'info>(ctx: Context<ForceCancelOrder>) -> Resu
#[access_control(
exchange_not_paused(&ctx.accounts.state)
)]
pub fn handle_update_user_idle<'info>(ctx: Context<UpdateUserIdle>) -> Result<()> {
pub fn handle_update_user_idle<'a, 'b, 'c: 'info, 'info>(
ctx: Context<'a, 'b, 'c, 'info, UpdateUserIdle<'info>>,
) -> Result<()> {
let mut user = load_mut!(ctx.accounts.user)?;
let clock = Clock::get()?;

Expand Down Expand Up @@ -413,7 +427,10 @@ pub fn handle_update_user_open_orders_count<'info>(ctx: Context<UpdateUserIdle>)
#[access_control(
settle_pnl_not_paused(&ctx.accounts.state)
)]
pub fn handle_settle_pnl(ctx: Context<SettlePNL>, market_index: u16) -> Result<()> {
pub fn handle_settle_pnl<'a, 'b, 'c: 'info, 'info>(
ctx: Context<'a, 'b, 'c, 'info, SettlePNL>,
market_index: u16,
) -> Result<()> {
let clock = Clock::get()?;
let state = &ctx.accounts.state;

Expand Down Expand Up @@ -485,7 +502,9 @@ pub fn handle_settle_pnl(ctx: Context<SettlePNL>, market_index: u16) -> Result<(
#[access_control(
funding_not_paused(&ctx.accounts.state)
)]
pub fn handle_settle_funding_payment(ctx: Context<SettleFunding>) -> Result<()> {
pub fn handle_settle_funding_payment<'a, 'b, 'c: 'info, 'info>(
ctx: Context<'a, 'b, 'c, 'info, SettleFunding>,
) -> Result<()> {
let clock = Clock::get()?;
let now = clock.unix_timestamp;

Expand All @@ -510,7 +529,10 @@ pub fn handle_settle_funding_payment(ctx: Context<SettleFunding>) -> Result<()>
#[access_control(
amm_not_paused(&ctx.accounts.state)
)]
pub fn handle_settle_lp<'info>(ctx: Context<SettleLP>, market_index: u16) -> Result<()> {
pub fn handle_settle_lp<'a, 'b, 'c: 'info, 'info>(
ctx: Context<'a, 'b, 'c, 'info, SettleLP>,
market_index: u16,
) -> Result<()> {
let user_key = ctx.accounts.user.key();
let user = &mut load_mut!(ctx.accounts.user)?;

Expand Down Expand Up @@ -538,7 +560,10 @@ pub fn handle_settle_lp<'info>(ctx: Context<SettleLP>, market_index: u16) -> Res
#[access_control(
settle_pnl_not_paused(&ctx.accounts.state)
)]
pub fn handle_settle_expired_market(ctx: Context<UpdateAMM>, market_index: u16) -> Result<()> {
pub fn handle_settle_expired_market<'a, 'b, 'c: 'info, 'info>(
ctx: Context<'a, 'b, 'c, 'info, UpdateAMM<'info>>,
market_index: u16,
) -> Result<()> {
let clock = Clock::get()?;
let _now = clock.unix_timestamp;
let state = &ctx.accounts.state;
Expand Down Expand Up @@ -578,8 +603,8 @@ pub fn handle_settle_expired_market(ctx: Context<UpdateAMM>, market_index: u16)
#[access_control(
liq_not_paused(&ctx.accounts.state)
)]
pub fn handle_liquidate_perp(
ctx: Context<LiquidatePerp>,
pub fn handle_liquidate_perp<'a, 'b, 'c: 'info, 'info>(
ctx: Context<'a, 'b, 'c, 'info, LiquidatePerp<'info>>,
market_index: u16,
liquidator_max_base_asset_amount: u64,
limit_price: Option<u64>,
Expand Down Expand Up @@ -638,8 +663,8 @@ pub fn handle_liquidate_perp(
#[access_control(
liq_not_paused(&ctx.accounts.state)
)]
pub fn handle_liquidate_spot(
ctx: Context<LiquidateSpot>,
pub fn handle_liquidate_spot<'a, 'b, 'c: 'info, 'info>(
ctx: Context<'a, 'b, 'c, 'info, LiquidateSpot<'info>>,
asset_market_index: u16,
liability_market_index: u16,
liquidator_max_liability_transfer: u128,
Expand Down Expand Up @@ -695,8 +720,8 @@ pub fn handle_liquidate_spot(
#[access_control(
liq_not_paused(&ctx.accounts.state)
)]
pub fn handle_liquidate_borrow_for_perp_pnl(
ctx: Context<LiquidateBorrowForPerpPnl>,
pub fn handle_liquidate_borrow_for_perp_pnl<'a, 'b, 'c: 'info, 'info>(
ctx: Context<'a, 'b, 'c, 'info, LiquidateBorrowForPerpPnl<'info>>,
perp_market_index: u16,
spot_market_index: u16,
liquidator_max_liability_transfer: u128,
Expand Down Expand Up @@ -754,8 +779,8 @@ pub fn handle_liquidate_borrow_for_perp_pnl(
#[access_control(
liq_not_paused(&ctx.accounts.state)
)]
pub fn handle_liquidate_perp_pnl_for_deposit(
ctx: Context<LiquidatePerpPnlForDeposit>,
pub fn handle_liquidate_perp_pnl_for_deposit<'a, 'b, 'c: 'info, 'info>(
ctx: Context<'a, 'b, 'c, 'info, LiquidatePerpPnlForDeposit<'info>>,
perp_market_index: u16,
spot_market_index: u16,
liquidator_max_pnl_transfer: u128,
Expand Down Expand Up @@ -813,8 +838,8 @@ pub fn handle_liquidate_perp_pnl_for_deposit(
#[access_control(
withdraw_not_paused(&ctx.accounts.state)
)]
pub fn handle_resolve_perp_pnl_deficit(
ctx: Context<ResolvePerpPnlDeficit>,
pub fn handle_resolve_perp_pnl_deficit<'a, 'b, 'c: 'info, 'info>(
ctx: Context<'a, 'b, 'c, 'info, ResolvePerpPnlDeficit<'info>>,
spot_market_index: u16,
perp_market_index: u16,
) -> Result<()> {
Expand Down Expand Up @@ -936,8 +961,8 @@ pub fn handle_resolve_perp_pnl_deficit(
#[access_control(
withdraw_not_paused(&ctx.accounts.state)
)]
pub fn handle_resolve_perp_bankruptcy(
ctx: Context<ResolveBankruptcy>,
pub fn handle_resolve_perp_bankruptcy<'a, 'b, 'c: 'info, 'info>(
ctx: Context<'a, 'b, 'c, 'info, ResolveBankruptcy<'info>>,
quote_spot_market_index: u16,
market_index: u16,
) -> Result<()> {
Expand Down Expand Up @@ -1048,8 +1073,8 @@ pub fn handle_resolve_perp_bankruptcy(
#[access_control(
withdraw_not_paused(&ctx.accounts.state)
)]
pub fn handle_resolve_spot_bankruptcy(
ctx: Context<ResolveBankruptcy>,
pub fn handle_resolve_spot_bankruptcy<'a, 'b, 'c: 'info, 'info>(
ctx: Context<'a, 'b, 'c, 'info, ResolveBankruptcy<'info>>,
market_index: u16,
) -> Result<()> {
let state = &ctx.accounts.state;
Expand Down Expand Up @@ -1211,7 +1236,9 @@ pub fn handle_update_funding_rate(
funding_not_paused(&ctx.accounts.state)
valid_oracle_for_perp_market(&ctx.accounts.oracle, &ctx.accounts.perp_market)
)]
pub fn handle_update_perp_bid_ask_twap(ctx: Context<UpdatePerpBidAskTwap>) -> Result<()> {
pub fn handle_update_perp_bid_ask_twap<'a, 'b, 'c: 'info, 'info>(
ctx: Context<'a, 'b, 'c, 'info, UpdatePerpBidAskTwap<'info>>,
) -> Result<()> {
let perp_market = &mut load_mut!(ctx.accounts.perp_market)?;
let clock = Clock::get()?;
let now = clock.unix_timestamp;
Expand Down Expand Up @@ -1400,7 +1427,10 @@ pub fn handle_update_spot_market_cumulative_interest(
#[access_control(
exchange_not_paused(&ctx.accounts.state)
)]
pub fn handle_update_amms(ctx: Context<UpdateAMM>, market_indexes: [u16; 5]) -> Result<()> {
pub fn handle_update_amms<'a, 'b, 'c: 'info, 'info>(
ctx: Context<'a, 'b, 'c, 'info, UpdateAMM<'info>>,
market_indexes: [u16; 5],
) -> Result<()> {
// up to ~60k compute units (per amm) worst case

let clock = Clock::get()?;
Expand Down
22 changes: 11 additions & 11 deletions programs/drift/src/instructions/optional_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub struct AccountMaps<'a> {
pub oracle_map: OracleMap<'a>,
}

pub fn load_maps<'a, 'b>(
account_info_iter: &mut Peekable<Iter<AccountInfo<'a>>>,
pub fn load_maps<'a, 'b, 'c: 'a>(
account_info_iter: &mut Peekable<Iter<'c, AccountInfo<'a>>>,
writable_perp_markets: &'b MarketSet,
writable_spot_markets: &'b MarketSet,
slot: u64,
Expand All @@ -43,8 +43,8 @@ pub fn load_maps<'a, 'b>(
})
}

pub fn get_maker_and_maker_stats<'a>(
account_info_iter: &mut Peekable<Iter<AccountInfo<'a>>>,
pub fn get_maker_and_maker_stats<'a, 'b>(
account_info_iter: &mut Peekable<Iter<'a, AccountInfo<'a>>>,
) -> DriftResult<(AccountLoader<'a, User>, AccountLoader<'a, UserStats>)> {
let maker_account_info =
next_account_info(account_info_iter).or(Err(ErrorCode::MakerNotFound))?;
Expand Down Expand Up @@ -73,11 +73,11 @@ pub fn get_maker_and_maker_stats<'a>(
}

#[allow(clippy::type_complexity)]
pub fn get_referrer_and_referrer_stats<'a>(
account_info_iter: &mut Peekable<Iter<AccountInfo<'a>>>,
pub fn get_referrer_and_referrer_stats<'a: 'b, 'b>(
account_info_iter: &mut Peekable<Iter<'a, AccountInfo<'b>>>,
) -> DriftResult<(
Option<AccountLoader<'a, User>>,
Option<AccountLoader<'a, UserStats>>,
Option<AccountLoader<'b, User>>,
Option<AccountLoader<'b, UserStats>>,
)> {
let referrer_account_info = account_info_iter.peek();

Expand Down Expand Up @@ -146,9 +146,9 @@ pub fn get_referrer_and_referrer_stats<'a>(
Ok((Some(referrer), Some(referrer_stats)))
}

pub fn get_whitelist_token<'a>(
account_info_iter: &mut Peekable<Iter<AccountInfo<'a>>>,
) -> DriftResult<Account<'a, TokenAccount>> {
pub fn get_whitelist_token<'a: 'b, 'b>(
account_info_iter: &mut Peekable<Iter<'a, AccountInfo<'b>>>,
) -> DriftResult<Account<'b, TokenAccount>> {
let token_account_info = account_info_iter.peek();
if token_account_info.is_none() {
msg!("Could not find whitelist token");
Expand Down
Loading

0 comments on commit 4f8d1e2

Please sign in to comment.