Skip to content

Commit 4352f1a

Browse files
committed
program: deposit into if stake from admin
1 parent 107f0a8 commit 4352f1a

File tree

2 files changed

+162
-1
lines changed

2 files changed

+162
-1
lines changed

programs/drift/src/instructions/if_staker.rs

Lines changed: 154 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use anchor_lang::Discriminator;
33
use anchor_spl::token_interface::{TokenAccount, TokenInterface};
44

55
use crate::error::ErrorCode;
6-
use crate::ids::if_rebalance_wallet;
6+
use crate::ids::{admin_hot_wallet, if_rebalance_wallet};
77
use crate::instructions::constraints::*;
88
use crate::instructions::optional_accounts::{load_maps, AccountMaps};
99
use crate::optional_accounts::get_token_mint;
@@ -821,6 +821,114 @@ pub fn handle_transfer_protocol_if_shares_to_revenue_pool<'c: 'info, 'info>(
821821
Ok(())
822822
}
823823

824+
825+
pub fn handle_deposit_into_insurance_fund_stake<'c: 'info, 'info>(
826+
ctx: Context<'_, '_, 'c, 'info, DepositIntoInsuranceFundStake<'info>>,
827+
market_index: u16,
828+
amount: u64,
829+
) -> Result<()> {
830+
if amount == 0 {
831+
return Err(ErrorCode::InsufficientDeposit.into());
832+
}
833+
834+
let clock = Clock::get()?;
835+
let now = clock.unix_timestamp;
836+
let insurance_fund_stake = &mut load_mut!(ctx.accounts.insurance_fund_stake)?;
837+
let user_stats = &mut load_mut!(ctx.accounts.user_stats)?;
838+
let spot_market = &mut load_mut!(ctx.accounts.spot_market)?;
839+
let state = &ctx.accounts.state;
840+
841+
let remaining_accounts_iter = &mut ctx.remaining_accounts.iter().peekable();
842+
let mint = get_token_mint(remaining_accounts_iter)?;
843+
844+
validate!(
845+
!spot_market.is_insurance_fund_operation_paused(InsuranceFundOperation::Add),
846+
ErrorCode::InsuranceFundOperationPaused,
847+
"if staking add disabled",
848+
)?;
849+
850+
validate!(
851+
insurance_fund_stake.market_index == market_index,
852+
ErrorCode::IncorrectSpotMarketAccountPassed,
853+
"insurance_fund_stake does not match market_index"
854+
)?;
855+
856+
validate!(
857+
spot_market.status != MarketStatus::Initialized,
858+
ErrorCode::InvalidSpotMarketState,
859+
"spot market = {} not active for insurance_fund_stake",
860+
spot_market.market_index
861+
)?;
862+
863+
validate!(
864+
insurance_fund_stake.last_withdraw_request_shares == 0
865+
&& insurance_fund_stake.last_withdraw_request_value == 0,
866+
ErrorCode::IFWithdrawRequestInProgress,
867+
"withdraw request in progress"
868+
)?;
869+
870+
{
871+
if spot_market.has_transfer_hook() {
872+
controller::insurance::attempt_settle_revenue_to_insurance_fund(
873+
&ctx.accounts.spot_market_vault,
874+
&ctx.accounts.insurance_fund_vault,
875+
spot_market,
876+
now,
877+
&ctx.accounts.token_program,
878+
&ctx.accounts.drift_signer,
879+
state,
880+
&mint,
881+
Some(&mut remaining_accounts_iter.clone()),
882+
)?;
883+
} else {
884+
controller::insurance::attempt_settle_revenue_to_insurance_fund(
885+
&ctx.accounts.spot_market_vault,
886+
&ctx.accounts.insurance_fund_vault,
887+
spot_market,
888+
now,
889+
&ctx.accounts.token_program,
890+
&ctx.accounts.drift_signer,
891+
state,
892+
&mint,
893+
None,
894+
)?;
895+
};
896+
897+
// reload the vault balances so they're up-to-date
898+
ctx.accounts.spot_market_vault.reload()?;
899+
ctx.accounts.insurance_fund_vault.reload()?;
900+
math::spot_withdraw::validate_spot_market_vault_amount(
901+
spot_market,
902+
ctx.accounts.spot_market_vault.amount,
903+
)?;
904+
}
905+
906+
controller::insurance::add_insurance_fund_stake(
907+
amount,
908+
ctx.accounts.insurance_fund_vault.amount,
909+
insurance_fund_stake,
910+
user_stats,
911+
spot_market,
912+
clock.unix_timestamp,
913+
)?;
914+
915+
controller::token::receive(
916+
&ctx.accounts.token_program,
917+
&ctx.accounts.user_token_account,
918+
&ctx.accounts.insurance_fund_vault,
919+
&ctx.accounts.signer.to_account_info(),
920+
amount,
921+
&mint,
922+
if spot_market.has_transfer_hook() {
923+
Some(remaining_accounts_iter)
924+
} else {
925+
None
926+
},
927+
)?;
928+
929+
Ok(())
930+
}
931+
824932
#[derive(Accounts)]
825933
#[instruction(
826934
market_index: u16,
@@ -1082,3 +1190,48 @@ pub struct TransferProtocolIfSharesToRevenuePool<'info> {
10821190
/// CHECK: forced drift_signer
10831191
pub drift_signer: AccountInfo<'info>,
10841192
}
1193+
1194+
#[derive(Accounts)]
1195+
#[instruction(market_index: u16,)]
1196+
pub struct DepositIntoInsuranceFundStake<'info> {
1197+
pub signer: Signer<'info>,
1198+
#[account(
1199+
mut,
1200+
constraint = signer.key() == admin_hot_wallet::id() || signer.key() == state.admin
1201+
)]
1202+
pub state: Box<Account<'info, State>>,
1203+
#[account(
1204+
mut,
1205+
seeds = [b"spot_market", market_index.to_le_bytes().as_ref()],
1206+
bump
1207+
)]
1208+
pub spot_market: AccountLoader<'info, SpotMarket>,
1209+
#[account(
1210+
mut,
1211+
seeds = [b"insurance_fund_stake", user_stats.load()?.authority.as_ref(), market_index.to_le_bytes().as_ref()],
1212+
bump,
1213+
)]
1214+
pub insurance_fund_stake: AccountLoader<'info, InsuranceFundStake>,
1215+
#[account(mut)]
1216+
pub user_stats: AccountLoader<'info, UserStats>,
1217+
#[account(
1218+
mut,
1219+
seeds = [b"spot_market_vault".as_ref(), market_index.to_le_bytes().as_ref()],
1220+
bump,
1221+
)]
1222+
pub spot_market_vault: Box<InterfaceAccount<'info, TokenAccount>>,
1223+
#[account(
1224+
seeds = [b"insurance_fund_vault".as_ref(), market_index.to_le_bytes().as_ref()],
1225+
bump,
1226+
)]
1227+
pub insurance_fund_vault: Box<InterfaceAccount<'info, TokenAccount>>,
1228+
#[account(
1229+
mut,
1230+
token::mint = insurance_fund_vault.mint,
1231+
token::authority = signer
1232+
)]
1233+
pub user_token_account: Box<InterfaceAccount<'info, TokenAccount>>,
1234+
pub token_program: Interface<'info, TokenInterface>,
1235+
/// CHECK: forced drift_signer
1236+
pub drift_signer: AccountInfo<'info>,
1237+
}

programs/drift/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,14 @@ pub mod drift {
813813
handle_transfer_protocol_if_shares_to_revenue_pool(ctx, market_index, amount)
814814
}
815815

816+
pub fn deposit_into_insurance_fund_stake<'c: 'info, 'info>(
817+
ctx: Context<'_, '_, 'c, 'info, DepositIntoInsuranceFundStake<'info>>,
818+
market_index: u16,
819+
amount: u64,
820+
) -> Result<()> {
821+
handle_deposit_into_insurance_fund_stake(ctx, market_index, amount)
822+
}
823+
816824
pub fn update_pyth_pull_oracle(
817825
ctx: Context<UpdatePythPullOraclePriceFeed>,
818826
feed_id: [u8; 32],

0 commit comments

Comments
 (0)