@@ -3,7 +3,7 @@ use anchor_lang::Discriminator;
33use anchor_spl:: token_interface:: { TokenAccount , TokenInterface } ;
44
55use crate :: error:: ErrorCode ;
6- use crate :: ids:: if_rebalance_wallet;
6+ use crate :: ids:: { admin_hot_wallet , if_rebalance_wallet} ;
77use crate :: instructions:: constraints:: * ;
88use crate :: instructions:: optional_accounts:: { load_maps, AccountMaps } ;
99use crate :: optional_accounts:: get_token_mint;
@@ -143,6 +143,7 @@ pub fn handle_add_insurance_fund_stake<'c: 'info, 'info>(
143143 user_stats,
144144 spot_market,
145145 clock. unix_timestamp ,
146+ false ,
146147 ) ?;
147148
148149 controller:: token:: receive (
@@ -821,6 +822,114 @@ pub fn handle_transfer_protocol_if_shares_to_revenue_pool<'c: 'info, 'info>(
821822 Ok ( ( ) )
822823}
823824
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+ true ,
914+ ) ?;
915+
916+ controller:: token:: receive (
917+ & ctx. accounts . token_program ,
918+ & ctx. accounts . user_token_account ,
919+ & ctx. accounts . insurance_fund_vault ,
920+ & ctx. accounts . signer . to_account_info ( ) ,
921+ amount,
922+ & mint,
923+ if spot_market. has_transfer_hook ( ) {
924+ Some ( remaining_accounts_iter)
925+ } else {
926+ None
927+ } ,
928+ ) ?;
929+
930+ Ok ( ( ) )
931+ }
932+
824933#[ derive( Accounts ) ]
825934#[ instruction(
826935 market_index: u16 ,
@@ -1082,3 +1191,49 @@ pub struct TransferProtocolIfSharesToRevenuePool<'info> {
10821191 /// CHECK: forced drift_signer
10831192 pub drift_signer : AccountInfo < ' info > ,
10841193}
1194+
1195+ #[ derive( Accounts ) ]
1196+ #[ instruction( market_index: u16 , ) ]
1197+ pub struct DepositIntoInsuranceFundStake < ' info > {
1198+ pub signer : Signer < ' info > ,
1199+ #[ account(
1200+ mut ,
1201+ constraint = signer. key( ) == admin_hot_wallet:: id( ) || signer. key( ) == state. admin
1202+ ) ]
1203+ pub state : Box < Account < ' info , State > > ,
1204+ #[ account(
1205+ mut ,
1206+ seeds = [ b"spot_market" , market_index. to_le_bytes( ) . as_ref( ) ] ,
1207+ bump
1208+ ) ]
1209+ pub spot_market : AccountLoader < ' info , SpotMarket > ,
1210+ #[ account(
1211+ mut ,
1212+ seeds = [ b"insurance_fund_stake" , user_stats. load( ) ?. authority. as_ref( ) , market_index. to_le_bytes( ) . as_ref( ) ] ,
1213+ bump,
1214+ ) ]
1215+ pub insurance_fund_stake : AccountLoader < ' info , InsuranceFundStake > ,
1216+ #[ account( mut ) ]
1217+ pub user_stats : AccountLoader < ' info , UserStats > ,
1218+ #[ account(
1219+ mut ,
1220+ seeds = [ b"spot_market_vault" . as_ref( ) , market_index. to_le_bytes( ) . as_ref( ) ] ,
1221+ bump,
1222+ ) ]
1223+ pub spot_market_vault : Box < InterfaceAccount < ' info , TokenAccount > > ,
1224+ #[ account(
1225+ mut ,
1226+ seeds = [ b"insurance_fund_vault" . as_ref( ) , market_index. to_le_bytes( ) . as_ref( ) ] ,
1227+ bump,
1228+ ) ]
1229+ pub insurance_fund_vault : Box < InterfaceAccount < ' info , TokenAccount > > ,
1230+ #[ account(
1231+ mut ,
1232+ token:: mint = insurance_fund_vault. mint,
1233+ token:: authority = signer
1234+ ) ]
1235+ pub user_token_account : Box < InterfaceAccount < ' info , TokenAccount > > ,
1236+ pub token_program : Interface < ' info , TokenInterface > ,
1237+ /// CHECK: forced drift_signer
1238+ pub drift_signer : AccountInfo < ' info > ,
1239+ }
0 commit comments