Skip to content

Commit

Permalink
manager-crank-profit-share
Browse files Browse the repository at this point in the history
  • Loading branch information
0xbigz committed Aug 8, 2023
1 parent ede1b70 commit c6ce293
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use crate::constraints::{is_manager_for_vault, is_user_for_vault, is_user_stats_for_vault};
use crate::{Vault, VaultDepositor};

use crate::AccountMapProvider;
use anchor_lang::prelude::*;
use drift::instructions::optional_accounts::AccountMaps;
use drift::program::Drift;
use drift::state::user::User;

pub fn manager_crank_profit_share(ctx: Context<ManagerCrankProfitShare>) -> Result<()> {
let clock = &Clock::get()?;

let mut vault = ctx.accounts.vault.load_mut()?;
let mut vault_depositor = ctx.accounts.vault_depositor.load_mut()?;

let user = ctx.accounts.drift_user.load()?;
let spot_market_index = vault.spot_market_index;

let AccountMaps {
perp_market_map,
spot_market_map,
mut oracle_map,
} = ctx.load_maps(clock.slot, Some(spot_market_index))?;

let vault_equity =
vault.calculate_equity(&user, &perp_market_map, &spot_market_map, &mut oracle_map)?;

vault_depositor.apply_profit_share(vault_equity, &mut vault)?;

Ok(())
}

#[derive(Accounts)]
pub struct ManagerCrankProfitShare<'info> {
#[account(
mut,
constraint = is_manager_for_vault(&vault, &manager)?

This comment has been minimized.

Copy link
@crispheaney

crispheaney Aug 9, 2023

Member

should be allowed to be delegate

)]
pub vault: AccountLoader<'info, Vault>,
#[account(mut)]
pub vault_depositor: AccountLoader<'info, VaultDepositor>,
pub manager: Signer<'info>,
#[account(
mut,
constraint = is_user_stats_for_vault(&vault, &drift_user_stats)?
)]
/// CHECK: checked in drift cpi
pub drift_user_stats: AccountInfo<'info>,
#[account(
mut,
constraint = is_user_for_vault(&vault, &drift_user.key())?
)]
/// CHECK: checked in drift cpi
pub drift_user: AccountLoader<'info, User>,
/// CHECK: checked in drift cpi
pub drift_state: AccountInfo<'info>,
/// CHECK: checked in drift cpi
pub drift_signer: AccountInfo<'info>,
pub drift_program: Program<'info, Drift>,
}
2 changes: 2 additions & 0 deletions programs/drift_vaults/src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub use deposit::*;
pub use initialize_vault::*;
pub use initialize_vault_depositor::*;
pub use liquidate::*;
pub use manager_crank_profit_share::*;
pub use manager_deposit::*;
pub use manager_withdraw::*;
pub use request_withdraw::*;
Expand All @@ -17,6 +18,7 @@ mod deposit;
mod initialize_vault;
mod initialize_vault_depositor;
mod liquidate;
mod manager_crank_profit_share;
mod manager_deposit;
mod manager_withdraw;
mod request_withdraw;
Expand Down
6 changes: 6 additions & 0 deletions programs/drift_vaults/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,10 @@ pub mod drift_vaults {
) -> Result<()> {
instructions::manager_withdraw(ctx, withdraw_amount, withdraw_unit)
}

pub fn manager_crank_profit_share<'info>(
ctx: Context<'_, '_, '_, 'info, ManagerCrankProfitShare<'info>>,
) -> Result<()> {
instructions::manager_crank_profit_share(ctx)
}
}

0 comments on commit c6ce293

Please sign in to comment.