Skip to content

Commit

Permalink
program: anchor 0.29.0 (#48)
Browse files Browse the repository at this point in the history
* init

* remove unnecessary lifetimes

* update git commit
  • Loading branch information
crispheaney authored Jun 11, 2024
1 parent ee43233 commit a2073a3
Show file tree
Hide file tree
Showing 17 changed files with 1,024 additions and 1,872 deletions.
2,739 changes: 936 additions & 1,803 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions programs/drift_vaults/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ cpi = ["no-entrypoint"]
default = []

[dependencies]
anchor-lang = "0.27.0"
anchor-spl = "0.27.0"
drift = { git = "https://github.com/drift-labs/protocol-v2.git", tag = "v2.71.0", features = ["cpi", "mainnet-beta"], default-features = false}
drift-competitions = { git = "https://github.com/drift-labs/drift-competitions.git", rev = "678c7a", features = ["cpi"]}
anchor-lang = "0.29.0"
anchor-spl = "0.29.0"
drift = { git = "https://github.com/drift-labs/protocol-v2.git", rev = "a95009", features = ["cpi", "mainnet-beta"], default-features = false}
drift-competitions = { git = "https://github.com/drift-labs/drift-competitions.git", rev = "c8baa6", features = ["cpi"]}
bytemuck = { version = "1.4.0" }
static_assertions = "1.1.0"
drift-macros = { git = "https://github.com/drift-labs/drift-macros.git", rev = "c57d87" }
4 changes: 3 additions & 1 deletion programs/drift_vaults/src/instructions/apply_profit_share.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use drift::instructions::optional_accounts::AccountMaps;
use drift::program::Drift;
use drift::state::user::User;

pub fn apply_profit_share(ctx: Context<ApplyProfitShare>) -> Result<()> {
pub fn apply_profit_share<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, ApplyProfitShare<'info>>,
) -> Result<()> {
let clock = &Clock::get()?;

let mut vault = ctx.accounts.vault.load_mut()?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::constraints::{
use crate::AccountMapProvider;
use crate::{Vault, VaultDepositor};

pub fn cancel_withdraw_request<'info>(
ctx: Context<'_, '_, '_, 'info, CancelWithdrawRequest<'info>>,
pub fn cancel_withdraw_request<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, CancelWithdrawRequest<'info>>,
) -> Result<()> {
let clock = &Clock::get()?;
let vault = &mut ctx.accounts.vault.load_mut()?;
Expand Down
5 changes: 4 additions & 1 deletion programs/drift_vaults/src/instructions/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ use drift::instructions::optional_accounts::AccountMaps;
use drift::program::Drift;
use drift::state::user::User;

pub fn deposit<'info>(ctx: Context<'_, '_, '_, 'info, Deposit<'info>>, amount: u64) -> Result<()> {
pub fn deposit<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, Deposit<'info>>,
amount: u64,
) -> Result<()> {
let clock = &Clock::get()?;

let mut vault = ctx.accounts.vault.load_mut()?;
Expand Down
4 changes: 3 additions & 1 deletion programs/drift_vaults/src/instructions/force_withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use drift::instructions::optional_accounts::AccountMaps;
use drift::program::Drift;
use drift::state::user::User;

pub fn force_withdraw<'info>(ctx: Context<'_, '_, '_, 'info, ForceWithdraw<'info>>) -> Result<()> {
pub fn force_withdraw<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, ForceWithdraw<'info>>,
) -> Result<()> {
let clock = &Clock::get()?;
let mut vault = ctx.accounts.vault.load_mut()?;
let mut vault_depositor = ctx.accounts.vault_depositor.load_mut()?;
Expand Down
8 changes: 4 additions & 4 deletions programs/drift_vaults/src/instructions/initialize_vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn initialize_vault<'info>(
ctx: Context<'_, '_, '_, 'info, InitializeVault<'info>>,
params: VaultParams,
) -> Result<()> {
let bump = ctx.bumps.get("vault").ok_or(ErrorCode::Default)?;
let bump = ctx.bumps.vault;

let mut vault = ctx.accounts.vault.load_init()?;
vault.name = params.name;
Expand Down Expand Up @@ -55,13 +55,13 @@ pub fn initialize_vault<'info>(
"hurdle rate not implemented"
)?;
vault.hurdle_rate = params.hurdle_rate;
vault.bump = *bump;
vault.bump = bump;
vault.permissioned = params.permissioned;

drop(vault);

ctx.drift_initialize_user(params.name, *bump)?;
ctx.drift_initialize_user_stats(params.name, *bump)?;
ctx.drift_initialize_user(params.name, bump)?;
ctx.drift_initialize_user_stats(params.name, bump)?;

Ok(())
}
Expand Down
4 changes: 3 additions & 1 deletion programs/drift_vaults/src/instructions/liquidate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use drift::instructions::optional_accounts::AccountMaps;
use drift::program::Drift;
use drift::state::user::User;

pub fn liquidate<'info>(ctx: Context<'_, '_, '_, 'info, Liquidate<'info>>) -> Result<()> {
pub fn liquidate<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, Liquidate<'info>>,
) -> Result<()> {
let clock = &Clock::get()?;
let now = Clock::get()?.unix_timestamp;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::constraints::{is_manager_for_vault, is_user_for_vault, is_user_stats_
use crate::AccountMapProvider;
use crate::Vault;

pub fn manager_cancel_withdraw_request<'info>(
ctx: Context<'_, '_, '_, 'info, ManagerCancelWithdrawRequest<'info>>,
pub fn manager_cancel_withdraw_request<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, ManagerCancelWithdrawRequest<'info>>,
) -> Result<()> {
let clock = &Clock::get()?;
let vault = &mut ctx.accounts.vault.load_mut()?;
Expand Down
4 changes: 2 additions & 2 deletions programs/drift_vaults/src/instructions/manager_deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use drift::instructions::optional_accounts::AccountMaps;
use drift::program::Drift;
use drift::state::user::User;

pub fn manager_deposit<'info>(
ctx: Context<'_, '_, '_, 'info, ManagerDeposit<'info>>,
pub fn manager_deposit<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, ManagerDeposit<'info>>,
amount: u64,
) -> Result<()> {
let clock = &Clock::get()?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use anchor_lang::prelude::*;
use drift::instructions::optional_accounts::AccountMaps;
use drift::state::user::User;

pub fn manager_request_withdraw<'info>(
ctx: Context<'_, '_, '_, 'info, ManagerRequestWithdraw<'info>>,
pub fn manager_request_withdraw<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, ManagerRequestWithdraw<'info>>,
withdraw_amount: u64,
withdraw_unit: WithdrawUnit,
) -> Result<()> {
Expand Down
4 changes: 2 additions & 2 deletions programs/drift_vaults/src/instructions/manager_withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use drift::instructions::optional_accounts::AccountMaps;
use drift::program::Drift;
use drift::state::user::User;

pub fn manager_withdraw<'info>(
ctx: Context<'_, '_, '_, 'info, ManagerWithdraw<'info>>,
pub fn manager_withdraw<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, ManagerWithdraw<'info>>,
) -> Result<()> {
let clock = &Clock::get()?;
let mut vault = ctx.accounts.vault.load_mut()?;
Expand Down
4 changes: 2 additions & 2 deletions programs/drift_vaults/src/instructions/request_withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use drift::instructions::optional_accounts::AccountMaps;
use drift::math::casting::Cast;
use drift::state::user::User;

pub fn request_withdraw<'info>(
ctx: Context<'_, '_, '_, 'info, RequestWithdraw<'info>>,
pub fn request_withdraw<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, RequestWithdraw<'info>>,
withdraw_amount: u64,
withdraw_unit: WithdrawUnit,
) -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion programs/drift_vaults/src/instructions/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use drift::instructions::optional_accounts::AccountMaps;
use drift::program::Drift;
use drift::state::user::User;

pub fn withdraw<'info>(ctx: Context<'_, '_, '_, 'info, Withdraw<'info>>) -> Result<()> {
pub fn withdraw<'c: 'info, 'info>(ctx: Context<'_, '_, 'c, 'info, Withdraw<'info>>) -> Result<()> {
let clock = &Clock::get()?;
let mut vault = ctx.accounts.vault.load_mut()?;
let mut vault_depositor = ctx.accounts.vault_depositor.load_mut()?;
Expand Down
72 changes: 38 additions & 34 deletions programs/drift_vaults/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@ declare_id!("vAuLTsyrvSfZRuRB3XgvkPwNGgYSs9YRYymVebLKoxR");
pub mod drift_vaults {
use super::*;

pub fn initialize_vault<'info>(
ctx: Context<'_, '_, '_, 'info, InitializeVault<'info>>,
pub fn initialize_vault<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, InitializeVault<'info>>,
params: VaultParams,
) -> Result<()> {
instructions::initialize_vault(ctx, params)
}

pub fn update_delegate<'info>(
ctx: Context<'_, '_, '_, 'info, UpdateDelegate<'info>>,
pub fn update_delegate<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, UpdateDelegate<'info>>,
delegate: Pubkey,
) -> Result<()> {
instructions::update_delegate(ctx, delegate)
}

pub fn update_margin_trading_enabled<'info>(
ctx: Context<'_, '_, '_, 'info, UpdateMarginTradingEnabled<'info>>,
pub fn update_margin_trading_enabled<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, UpdateMarginTradingEnabled<'info>>,
enabled: bool,
) -> Result<()> {
instructions::update_margin_trading_enabled(ctx, enabled)
}

pub fn update_vault<'info>(
ctx: Context<'_, '_, '_, 'info, UpdateVault<'info>>,
pub fn update_vault<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, UpdateVault<'info>>,
params: UpdateVaultParams,
) -> Result<()> {
instructions::update_vault(ctx, params)
Expand All @@ -48,89 +48,93 @@ pub mod drift_vaults {
instructions::initialize_vault_depositor(ctx)
}

pub fn deposit<'info>(
ctx: Context<'_, '_, '_, 'info, Deposit<'info>>,
pub fn deposit<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, Deposit<'info>>,
amount: u64,
) -> Result<()> {
instructions::deposit(ctx, amount)
}

pub fn request_withdraw<'info>(
ctx: Context<'_, '_, '_, 'info, RequestWithdraw<'info>>,
pub fn request_withdraw<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, RequestWithdraw<'info>>,
withdraw_amount: u64,
withdraw_unit: WithdrawUnit,
) -> Result<()> {
instructions::request_withdraw(ctx, withdraw_amount, withdraw_unit)
}

pub fn cancel_request_withdraw<'info>(
ctx: Context<'_, '_, '_, 'info, CancelWithdrawRequest<'info>>,
pub fn cancel_request_withdraw<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, CancelWithdrawRequest<'info>>,
) -> Result<()> {
instructions::cancel_withdraw_request(ctx)
}

pub fn withdraw<'info>(ctx: Context<'_, '_, '_, 'info, Withdraw<'info>>) -> Result<()> {
pub fn withdraw<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, Withdraw<'info>>,
) -> Result<()> {
instructions::withdraw(ctx)
}

pub fn liquidate<'info>(ctx: Context<'_, '_, '_, 'info, Liquidate<'info>>) -> Result<()> {
pub fn liquidate<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, Liquidate<'info>>,
) -> Result<()> {
instructions::liquidate(ctx)
}

pub fn reset_delegate<'info>(
ctx: Context<'_, '_, '_, 'info, ResetDelegate<'info>>,
pub fn reset_delegate<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, ResetDelegate<'info>>,
) -> Result<()> {
instructions::reset_delegate(ctx)
}

pub fn manager_deposit<'info>(
ctx: Context<'_, '_, '_, 'info, ManagerDeposit<'info>>,
pub fn manager_deposit<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, ManagerDeposit<'info>>,
amount: u64,
) -> Result<()> {
instructions::manager_deposit(ctx, amount)
}

pub fn manager_request_withdraw<'info>(
ctx: Context<'_, '_, '_, 'info, ManagerRequestWithdraw<'info>>,
pub fn manager_request_withdraw<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, ManagerRequestWithdraw<'info>>,
withdraw_amount: u64,
withdraw_unit: WithdrawUnit,
) -> Result<()> {
instructions::manager_request_withdraw(ctx, withdraw_amount, withdraw_unit)
}

pub fn manger_cancel_withdraw_request<'info>(
ctx: Context<'_, '_, '_, 'info, ManagerCancelWithdrawRequest<'info>>,
pub fn manger_cancel_withdraw_request<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, ManagerCancelWithdrawRequest<'info>>,
) -> Result<()> {
instructions::manager_cancel_withdraw_request(ctx)
}

pub fn manager_withdraw<'info>(
ctx: Context<'_, '_, '_, 'info, ManagerWithdraw<'info>>,
pub fn manager_withdraw<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, ManagerWithdraw<'info>>,
) -> Result<()> {
instructions::manager_withdraw(ctx)
}

pub fn apply_profit_share<'info>(
ctx: Context<'_, '_, '_, 'info, ApplyProfitShare<'info>>,
pub fn apply_profit_share<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, ApplyProfitShare<'info>>,
) -> Result<()> {
instructions::apply_profit_share(ctx)
}

pub fn force_withdraw<'info>(
ctx: Context<'_, '_, '_, 'info, ForceWithdraw<'info>>,
pub fn force_withdraw<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, ForceWithdraw<'info>>,
) -> Result<()> {
instructions::force_withdraw(ctx)
}

pub fn initialize_insurance_fund_stake<'info>(
ctx: Context<'_, '_, '_, 'info, InitializeInsuranceFundStake<'info>>,
pub fn initialize_insurance_fund_stake<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, InitializeInsuranceFundStake<'info>>,
market_index: u16,
) -> Result<()> {
instructions::initialize_insurance_fund_stake(ctx, market_index)
}

pub fn initialize_competitor<'info>(
ctx: Context<'_, '_, '_, 'info, InitializeCompetitor<'info>>,
pub fn initialize_competitor<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, InitializeCompetitor<'info>>,
) -> Result<()> {
instructions::initialize_competitor(ctx)
}
Expand Down
10 changes: 6 additions & 4 deletions programs/drift_vaults/src/state/account_maps.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use anchor_lang::prelude::Context;
use drift::error::DriftResult;
use drift::instructions::optional_accounts::{load_maps, AccountMaps};
use drift::state::perp_market_map::MarketSet;
use drift::state::spot_market_map::get_writable_spot_market_set;
use std::collections::BTreeSet;

pub trait AccountMapProvider<'a> {
fn load_maps(
Expand All @@ -12,16 +12,18 @@ pub trait AccountMapProvider<'a> {
) -> DriftResult<AccountMaps<'a>>;
}

impl<'info, T> AccountMapProvider<'info> for Context<'_, '_, '_, 'info, T> {
impl<'a: 'info, 'info, T: anchor_lang::Bumps> AccountMapProvider<'a>
for Context<'_, '_, 'a, 'info, T>
{
fn load_maps(
&self,
slot: u64,
writable_spot_market_index: Option<u16>,
) -> DriftResult<AccountMaps<'info>> {
) -> DriftResult<AccountMaps<'a>> {
let remaining_accounts_iter = &mut self.remaining_accounts.iter().peekable();
load_maps(
remaining_accounts_iter,
&MarketSet::new(),
&BTreeSet::new(),
&writable_spot_market_index
.map(get_writable_spot_market_set)
.unwrap_or_default(),
Expand Down
16 changes: 10 additions & 6 deletions programs/drift_vaults/src/state/vault_depositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,12 +602,16 @@ impl VaultDepositor {
oracle_map,
)?;

let margin_trading_ok =
match validate_spot_margin_trading(drift_user, spot_market_map, oracle_map) {
Ok(_) => true,
Err(DriftErrorCode::MarginTradingDisabled) => false,
Err(e) => return Err(e),
};
let margin_trading_ok = match validate_spot_margin_trading(
drift_user,
perp_market_map,
spot_market_map,
oracle_map,
) {
Ok(_) => true,
Err(DriftErrorCode::MarginTradingDisabled) => false,
Err(e) => return Err(e),
};

if sufficient_collateral && margin_trading_ok {
msg!(
Expand Down

0 comments on commit a2073a3

Please sign in to comment.