@@ -4600,6 +4600,62 @@ pub fn handle_initialize_constituent<'info>(
46004600 Ok ( ( ) )
46014601}
46024602
4603+ #[ derive( AnchorSerialize , AnchorDeserialize , Clone , Default ) ]
4604+ pub struct InitializeAmmConstituentMappingDatum {
4605+ pub constituent_index : u16 ,
4606+ pub perp_market_index : u16 ,
4607+ }
4608+
4609+ pub fn handle_add_amm_constituent_data < ' info > (
4610+ ctx : Context < AddAmmConstituentMappingData > ,
4611+ init_amm_constituent_mapping_data : Vec < InitializeAmmConstituentMappingDatum > ,
4612+ ) -> Result < ( ) > {
4613+ let amm_mapping = & mut ctx. accounts . amm_constituent_mapping ;
4614+ let constituent_target_weights = & ctx. accounts . constituent_target_weights ;
4615+ let state = & ctx. accounts . state ;
4616+ let mut current_len = amm_mapping. data . len ( ) ;
4617+
4618+ for init_datum in init_amm_constituent_mapping_data {
4619+ let perp_market_index = init_datum. perp_market_index ;
4620+
4621+ validate ! (
4622+ perp_market_index < state. number_of_markets,
4623+ ErrorCode :: InvalidAmmConstituentMappingArgument ,
4624+ "perp_market_index too large compared to number of markets"
4625+ ) ?;
4626+
4627+ validate ! (
4628+ ( init_datum. constituent_index as usize ) < constituent_target_weights. data. len( ) ,
4629+ ErrorCode :: InvalidAmmConstituentMappingArgument ,
4630+ "constituent_index too large compared to number of constituents in target weights"
4631+ ) ?;
4632+
4633+ let constituent_index = init_datum. constituent_index ;
4634+ let mut datum = AmmConstituentDatum :: default ( ) ;
4635+ datum. perp_market_index = perp_market_index;
4636+ datum. constituent_index = constituent_index;
4637+
4638+ // Check if the datum already exists
4639+ let exists = amm_mapping. data . iter ( ) . any ( |d| {
4640+ d. perp_market_index == perp_market_index && d. constituent_index == constituent_index
4641+ } ) ;
4642+
4643+ validate ! (
4644+ !exists,
4645+ ErrorCode :: InvalidAmmConstituentMappingArgument ,
4646+ "AmmConstituentDatum already exists for perp_market_index {} and constituent_index {}" ,
4647+ perp_market_index,
4648+ constituent_index
4649+ ) ?;
4650+
4651+ // Add the new datum to the mapping
4652+ current_len += 1 ;
4653+ amm_mapping. data . resize ( current_len, datum) ;
4654+ }
4655+
4656+ Ok ( ( ) )
4657+ }
4658+
46034659#[ derive( Accounts ) ]
46044660pub struct Initialize < ' info > {
46054661 #[ account( mut ) ]
@@ -5409,8 +5465,43 @@ pub struct InitializeConstituent<'info> {
54095465 payer = admin,
54105466 ) ]
54115467 pub constituent : AccountLoader < ' info , Constituent > ,
5468+ pub rent : Sysvar < ' info , Rent > ,
5469+ pub system_program : Program < ' info , System > ,
5470+ }
54125471
5472+ #[ derive( Accounts ) ]
5473+ #[ instruction(
5474+ lp_pool_name: [ u8 ; 32 ] ,
5475+ market_index_constituent_index_pairs: Vec <( u16 , u16 ) >,
5476+ ) ]
5477+ pub struct AddAmmConstituentMappingData < ' info > {
54135478 #[ account( mut ) ]
5414- pub payer : Signer < ' info > ,
5479+ pub admin : Signer < ' info > ,
5480+
5481+ #[ account(
5482+ seeds = [ b"lp_pool" , lp_pool_name. as_ref( ) ] ,
5483+ bump,
5484+ ) ]
5485+ pub lp_pool : AccountLoader < ' info , LPPool > ,
5486+
5487+ #[ account(
5488+ mut ,
5489+ seeds = [ AMM_MAP_PDA_SEED . as_ref( ) , lp_pool. key( ) . as_ref( ) ] ,
5490+ bump,
5491+ realloc = AmmConstituentMapping :: space( amm_constituent_mapping. data. len( ) + market_index_constituent_index_pairs. len( ) ) ,
5492+ realloc:: payer = admin,
5493+ realloc:: zero = false ,
5494+ ) ]
5495+ pub amm_constituent_mapping : Box < Account < ' info , AmmConstituentMapping > > ,
5496+ #[ account(
5497+ mut ,
5498+ seeds = [ CONSTITUENT_TARGET_WEIGHT_PDA_SEED . as_ref( ) , lp_pool. key( ) . as_ref( ) ] ,
5499+ bump,
5500+ realloc = ConstituentTargetWeights :: space( constituent_target_weights. data. len( ) + 1 as usize ) ,
5501+ realloc:: payer = admin,
5502+ realloc:: zero = false ,
5503+ ) ]
5504+ pub constituent_target_weights : Box < Account < ' info , ConstituentTargetWeights > > ,
5505+ pub state : Box < Account < ' info , State > > ,
54155506 pub system_program : Program < ' info , System > ,
54165507}
0 commit comments