@@ -5,7 +5,7 @@ use anchor_spl::token_interface::{Mint, TokenAccount, TokenInterface};
5
5
6
6
use crate :: {
7
7
controller:: {
8
- self , lp ,
8
+ self ,
9
9
spot_balance:: update_spot_balances,
10
10
token:: { burn_tokens, mint_tokens} ,
11
11
} ,
@@ -16,7 +16,7 @@ use crate::{
16
16
self ,
17
17
casting:: Cast ,
18
18
constants:: {
19
- BASE_PRECISION_I128 , PERCENTAGE_PRECISION_I128 , PERCENTAGE_PRECISION_I64 ,
19
+ PERCENTAGE_PRECISION , PERCENTAGE_PRECISION_I128 , PERCENTAGE_PRECISION_I64 ,
20
20
PERCENTAGE_PRECISION_U64 , PRICE_PRECISION_I128 , QUOTE_PRECISION_I128 ,
21
21
} ,
22
22
oracle:: { is_oracle_valid_for_action, oracle_validity, DriftAction } ,
@@ -25,13 +25,12 @@ use crate::{
25
25
math_error, msg, safe_decrement, safe_increment,
26
26
state:: {
27
27
constituent_map:: { ConstituentMap , ConstituentSet } ,
28
- events:: { LPMintRedeemRecord , LPSwapRecord } ,
28
+ events:: { emit_stack , LPMintRedeemRecord , LPSwapRecord } ,
29
29
lp_pool:: {
30
30
calculate_target_weight, AmmConstituentDatum , AmmConstituentMappingFixed , Constituent ,
31
31
ConstituentCorrelationsFixed , ConstituentTargetBaseFixed , LPPool , TargetsDatum ,
32
- WeightValidationFlags , CONSTITUENT_CORRELATIONS_PDA_SEED ,
33
- LP_POOL_SWAP_AUM_UPDATE_DELAY , MAX_AMM_CACHE_STALENESS_FOR_TARGET_CALC ,
34
- MAX_CONSTITUENT_ORACLE_SLOT_STALENESS_FOR_AUM ,
32
+ WeightValidationFlags , LP_POOL_SWAP_AUM_UPDATE_DELAY ,
33
+ MAX_AMM_CACHE_STALENESS_FOR_TARGET_CALC , MAX_CONSTITUENT_ORACLE_SLOT_STALENESS_FOR_AUM ,
35
34
} ,
36
35
oracle:: OraclePriceData ,
37
36
oracle_map:: OracleMap ,
@@ -40,6 +39,7 @@ use crate::{
40
39
spot_market:: { SpotBalanceType , SpotMarket } ,
41
40
spot_market_map:: get_writable_spot_market_set_from_many,
42
41
state:: State ,
42
+ traits:: Size ,
43
43
user:: MarketType ,
44
44
zero_copy:: { AccountZeroCopy , AccountZeroCopyMut , ZeroCopyLoader } ,
45
45
} ,
@@ -53,16 +53,14 @@ use crate::controller::spot_balance::update_spot_market_cumulative_interest;
53
53
use crate :: controller:: token:: { receive, send_from_program_vault} ;
54
54
use crate :: instructions:: constraints:: * ;
55
55
use crate :: state:: lp_pool:: {
56
- AMM_MAP_PDA_SEED , CONSTITUENT_PDA_SEED , CONSTITUENT_TARGET_BASE_PDA_SEED ,
57
- LP_POOL_TOKEN_VAULT_PDA_SEED ,
56
+ CONSTITUENT_PDA_SEED , CONSTITUENT_TARGET_BASE_PDA_SEED , LP_POOL_TOKEN_VAULT_PDA_SEED ,
58
57
} ;
59
58
60
59
pub fn handle_update_constituent_target_base < ' c : ' info , ' info > (
61
60
ctx : Context < ' _ , ' _ , ' c , ' info , UpdateConstituentTargetBase < ' info > > ,
62
61
) -> Result < ( ) > {
63
62
let slot = Clock :: get ( ) ?. slot ;
64
63
65
- let lp_pool = & ctx. accounts . lp_pool . load ( ) ?;
66
64
let lp_pool_key: & Pubkey = & ctx. accounts . lp_pool . key ( ) ;
67
65
let amm_cache_key: & Pubkey = & ctx. accounts . amm_cache . key ( ) ;
68
66
@@ -87,29 +85,15 @@ pub fn handle_update_constituent_target_base<'c: 'info, 'info>(
87
85
) ?;
88
86
89
87
let state = & ctx. accounts . state ;
90
- let constituent_target_base_key = & ctx. accounts . constituent_target_base . key ( ) ;
91
- let amm_mapping_key = & ctx. accounts . amm_constituent_mapping . key ( ) ;
92
-
93
88
let mut constituent_target_base: AccountZeroCopyMut <
94
89
' _ ,
95
90
TargetsDatum ,
96
91
ConstituentTargetBaseFixed ,
97
92
> = ctx. accounts . constituent_target_base . load_zc_mut ( ) ?;
98
-
99
- let bump = constituent_target_base. fixed . bump ;
100
- let expected_pda = & Pubkey :: create_program_address (
101
- & [
102
- CONSTITUENT_TARGET_BASE_PDA_SEED . as_ref ( ) ,
103
- lp_pool. pubkey . as_ref ( ) ,
104
- bump. to_le_bytes ( ) . as_ref ( ) ,
105
- ] ,
106
- & crate :: ID ,
107
- )
108
- . map_err ( |_| ErrorCode :: InvalidPDA ) ?;
109
93
validate ! (
110
- expected_pda . eq( constituent_target_base_key ) ,
94
+ constituent_target_base . fixed . lp_pool . eq( lp_pool_key ) ,
111
95
ErrorCode :: InvalidPDA ,
112
- "Constituent target weights PDA does not match expected PDA"
96
+ "Constituent target base lp pool pubkey does not match lp pool pubkey" ,
113
97
) ?;
114
98
115
99
let num_constituents = constituent_target_base. len ( ) ;
@@ -124,24 +108,18 @@ pub fn handle_update_constituent_target_base<'c: 'info, 'info>(
124
108
AmmConstituentDatum ,
125
109
AmmConstituentMappingFixed ,
126
110
> = ctx. accounts . amm_constituent_mapping . load_zc ( ) ?;
127
-
128
- let amm_mapping_bump = amm_constituent_mapping. fixed . bump ;
129
- let expected_map_pda = & Pubkey :: create_program_address (
130
- & [
131
- AMM_MAP_PDA_SEED . as_ref ( ) ,
132
- lp_pool. pubkey . as_ref ( ) ,
133
- amm_mapping_bump. to_le_bytes ( ) . as_ref ( ) ,
134
- ] ,
135
- & crate :: ID ,
136
- )
137
- . map_err ( |_| ErrorCode :: InvalidPDA ) ?;
138
111
validate ! (
139
- expected_map_pda . eq( amm_mapping_key ) ,
112
+ amm_constituent_mapping . fixed . lp_pool . eq( lp_pool_key ) ,
140
113
ErrorCode :: InvalidPDA ,
141
- "Amm mapping PDA does not match expected PDA"
114
+ "Amm constituent mapping lp pool pubkey does not match lp pool pubkey" ,
142
115
) ?;
143
116
144
- let mut amm_inventories: Vec < ( u16 , i64 , i64 ) > = vec ! [ ] ;
117
+ let remaining_accounts = & mut ctx. remaining_accounts . iter ( ) . peekable ( ) ;
118
+ let constituent_map =
119
+ ConstituentMap :: load ( & ConstituentSet :: new ( ) , & lp_pool_key, remaining_accounts) ?;
120
+
121
+ let mut amm_inventories: Vec < ( u16 , i64 , i64 ) > =
122
+ Vec :: with_capacity ( amm_constituent_mapping. len ( ) as usize ) ;
145
123
for ( _, datum) in amm_constituent_mapping. iter ( ) . enumerate ( ) {
146
124
let cache_info = amm_cache. get ( datum. perp_market_index as u32 ) ;
147
125
@@ -183,11 +161,8 @@ pub fn handle_update_constituent_target_base<'c: 'info, 'info>(
183
161
return Ok ( ( ) ) ;
184
162
}
185
163
186
- let remaining_accounts = & mut ctx. remaining_accounts . iter ( ) . peekable ( ) ;
187
- let constituent_map =
188
- ConstituentMap :: load ( & ConstituentSet :: new ( ) , & lp_pool_key, remaining_accounts) ?;
189
-
190
- let mut constituent_indexes_and_decimals_and_prices: Vec < ( u16 , u8 , i64 ) > = vec ! [ ] ;
164
+ let mut constituent_indexes_and_decimals_and_prices: Vec < ( u16 , u8 , i64 ) > =
165
+ Vec :: with_capacity ( constituent_map. 0 . len ( ) ) ;
191
166
for ( index, loader) in & constituent_map. 0 {
192
167
let constituent_ref = loader. load ( ) ?;
193
168
constituent_indexes_and_decimals_and_prices. push ( (
@@ -248,25 +223,15 @@ pub fn handle_update_lp_pool_aum<'c: 'info, 'info>(
248
223
"Constituent map length does not match lp pool constituent count"
249
224
) ?;
250
225
251
- let constituent_target_base_key = & ctx. accounts . constituent_target_base . key ( ) ;
252
226
let mut constituent_target_base: AccountZeroCopyMut <
253
227
' _ ,
254
228
TargetsDatum ,
255
229
ConstituentTargetBaseFixed ,
256
230
> = ctx. accounts . constituent_target_base . load_zc_mut ( ) ?;
257
- let expected_pda = & Pubkey :: create_program_address (
258
- & [
259
- CONSTITUENT_TARGET_BASE_PDA_SEED . as_ref ( ) ,
260
- lp_pool. pubkey . as_ref ( ) ,
261
- constituent_target_base. fixed . bump . to_le_bytes ( ) . as_ref ( ) ,
262
- ] ,
263
- & crate :: ID ,
264
- )
265
- . map_err ( |_| ErrorCode :: InvalidPDA ) ?;
266
231
validate ! (
267
- expected_pda . eq( constituent_target_base_key ) ,
232
+ constituent_target_base . fixed . lp_pool . eq( & lp_pool . pubkey ) ,
268
233
ErrorCode :: InvalidPDA ,
269
- "Constituent target weights PDA does not match expected PDA"
234
+ "Constituent target base lp pool pubkey does not match lp pool pubkey" ,
270
235
) ?;
271
236
272
237
let amm_cache_key: & Pubkey = & ctx. accounts . amm_cache . key ( ) ;
@@ -417,8 +382,8 @@ pub fn handle_update_lp_pool_aum<'c: 'info, 'info>(
417
382
if constituent. last_oracle_price
418
383
< parent_constituent
419
384
. last_oracle_price
420
- . safe_mul ( 9 ) ?
421
- . safe_div ( 10 ) ?
385
+ . safe_mul ( constituent . constituent_derivative_depeg_threshold as i64 ) ?
386
+ . safe_div ( PERCENTAGE_PRECISION_I64 ) ?
422
387
{
423
388
msg ! (
424
389
"Constituent {} last oracle price {} is too low compared to parent constituent {} last oracle price {}. Assuming depegging and setting target base to 0." ,
@@ -504,40 +469,20 @@ pub fn handle_lp_pool_swap<'c: 'info, 'info>(
504
469
let mut in_constituent = ctx. accounts . in_constituent . load_mut ( ) ?;
505
470
let mut out_constituent = ctx. accounts . out_constituent . load_mut ( ) ?;
506
471
507
- let constituent_target_base_key = & ctx. accounts . constituent_target_base . key ( ) ;
508
472
let constituent_target_base: AccountZeroCopy < ' _ , TargetsDatum , ConstituentTargetBaseFixed > =
509
473
ctx. accounts . constituent_target_base . load_zc ( ) ?;
510
- let expected_pda = & Pubkey :: create_program_address (
511
- & [
512
- CONSTITUENT_TARGET_BASE_PDA_SEED . as_ref ( ) ,
513
- lp_pool. pubkey . as_ref ( ) ,
514
- constituent_target_base. fixed . bump . to_le_bytes ( ) . as_ref ( ) ,
515
- ] ,
516
- & crate :: ID ,
517
- )
518
- . map_err ( |_| ErrorCode :: InvalidPDA ) ?;
519
474
validate ! (
520
- expected_pda . eq( constituent_target_base_key ) ,
475
+ constituent_target_base . fixed . lp_pool . eq( & lp_pool . pubkey ) ,
521
476
ErrorCode :: InvalidPDA ,
522
- "Constituent target weights PDA does not match expected PDA"
477
+ "Constituent target base lp pool pubkey does not match lp pool pubkey" ,
523
478
) ?;
524
479
525
- let constituent_correlation_key = & ctx. accounts . constituent_correlations . key ( ) ;
526
480
let constituent_correlations: AccountZeroCopy < ' _ , i64 , ConstituentCorrelationsFixed > =
527
481
ctx. accounts . constituent_correlations . load_zc ( ) ?;
528
- let expected_correlation_pda = & Pubkey :: create_program_address (
529
- & [
530
- CONSTITUENT_CORRELATIONS_PDA_SEED . as_ref ( ) ,
531
- lp_pool. pubkey . as_ref ( ) ,
532
- constituent_correlations. fixed . bump . to_le_bytes ( ) . as_ref ( ) ,
533
- ] ,
534
- & crate :: ID ,
535
- )
536
- . map_err ( |_| ErrorCode :: InvalidPDA ) ?;
537
482
validate ! (
538
- expected_correlation_pda . eq( constituent_correlation_key ) ,
483
+ constituent_correlations . fixed . lp_pool . eq( & lp_pool . pubkey ) ,
539
484
ErrorCode :: InvalidPDA ,
540
- "Constituent correlations PDA does not match expected PDA"
485
+ "Constituent correlations lp pool pubkey does not match lp pool pubkey" ,
541
486
) ?;
542
487
543
488
let AccountMaps {
@@ -665,7 +610,7 @@ pub fn handle_lp_pool_swap<'c: 'info, 'info>(
665
610
let in_swap_id = get_then_update_id ! ( in_constituent, next_swap_id) ;
666
611
let out_swap_id = get_then_update_id ! ( out_constituent, next_swap_id) ;
667
612
668
- emit ! ( LPSwapRecord {
613
+ emit_stack :: < _ , { LPSwapRecord :: SIZE } > ( LPSwapRecord {
669
614
ts : now,
670
615
slot,
671
616
authority : ctx. accounts . authority . key ( ) ,
@@ -687,19 +632,19 @@ pub fn handle_lp_pool_swap<'c: 'info, 'info>(
687
632
in_oracle. price ,
688
633
& in_spot_market,
689
634
0 ,
690
- lp_pool. last_aum
635
+ lp_pool. last_aum ,
691
636
) ?,
692
637
in_market_target_weight : in_target_weight,
693
638
out_market_current_weight : out_constituent. get_weight (
694
639
out_oracle. price ,
695
640
& out_spot_market,
696
641
0 ,
697
- lp_pool. last_aum
642
+ lp_pool. last_aum ,
698
643
) ?,
699
644
out_market_target_weight : out_target_weight,
700
645
in_swap_id,
701
646
out_swap_id,
702
- } ) ;
647
+ } ) ? ;
703
648
704
649
receive (
705
650
& ctx. accounts . token_program ,
@@ -896,11 +841,11 @@ pub fn handle_lp_pool_add_liquidity<'c: 'info, 'info>(
896
841
} ;
897
842
898
843
let mint_redeem_id = get_then_update_id ! ( lp_pool, next_mint_redeem_id) ;
899
- emit ! ( LPMintRedeemRecord {
844
+ emit_stack :: < _ , { LPMintRedeemRecord :: SIZE } > ( LPMintRedeemRecord {
900
845
ts : now,
901
846
slot,
902
847
authority : ctx. accounts . authority . key ( ) ,
903
- is_minting : true ,
848
+ description : 1 ,
904
849
amount : in_amount,
905
850
fee : in_fee_amount,
906
851
spot_market_index : in_market_index,
@@ -918,10 +863,10 @@ pub fn handle_lp_pool_add_liquidity<'c: 'info, 'info>(
918
863
in_oracle. price ,
919
864
& in_spot_market,
920
865
0 ,
921
- lp_pool. last_aum
866
+ lp_pool. last_aum ,
922
867
) ?,
923
868
in_market_target_weight : in_target_weight,
924
- } ) ;
869
+ } ) ? ;
925
870
926
871
Ok ( ( ) )
927
872
}
@@ -1093,11 +1038,11 @@ pub fn handle_lp_pool_remove_liquidity<'c: 'info, 'info>(
1093
1038
} ;
1094
1039
1095
1040
let mint_redeem_id = get_then_update_id ! ( lp_pool, next_mint_redeem_id) ;
1096
- emit ! ( LPMintRedeemRecord {
1041
+ emit_stack :: < _ , { LPMintRedeemRecord :: SIZE } > ( LPMintRedeemRecord {
1097
1042
ts : now,
1098
1043
slot,
1099
1044
authority : ctx. accounts . authority . key ( ) ,
1100
- is_minting : false ,
1045
+ description : 0 ,
1101
1046
amount : out_amount,
1102
1047
fee : out_fee_amount,
1103
1048
spot_market_index : out_market_index,
@@ -1115,10 +1060,10 @@ pub fn handle_lp_pool_remove_liquidity<'c: 'info, 'info>(
1115
1060
out_oracle. price ,
1116
1061
& out_spot_market,
1117
1062
0 ,
1118
- lp_pool. last_aum
1063
+ lp_pool. last_aum ,
1119
1064
) ?,
1120
1065
in_market_target_weight : out_target_weight,
1121
- } ) ;
1066
+ } ) ? ;
1122
1067
1123
1068
Ok ( ( ) )
1124
1069
}
0 commit comments