@@ -695,7 +695,13 @@ where
695695mod tests {
696696 use std:: str:: FromStr ;
697697
698+ use namada_parameters:: storage:: get_epochs_per_year_key;
699+ use namada_state:: testing:: TestState ;
700+ use namada_trans_token:: storage_key:: minted_balance_key;
701+ use storage:: write_pos_params;
702+
698703 use super :: * ;
704+ use crate :: OwnedPosParams ;
699705
700706 #[ test]
701707 fn test_inflation_calc_up ( ) {
@@ -877,10 +883,19 @@ mod tests {
877883
878884 #[ test]
879885 fn test_pos_inflation_playground ( ) {
886+ let mut storage = TestState :: default ( ) ;
887+ let gov_params =
888+ namada_governance:: parameters:: GovernanceParameters :: default ( ) ;
889+ gov_params. init_storage ( & mut storage) . unwrap ( ) ;
890+ write_pos_params ( & mut storage, & OwnedPosParams :: default ( ) ) . unwrap ( ) ;
891+
880892 let epochs_per_year = 365_u64 ;
893+ let epy_key = get_epochs_per_year_key ( ) ;
894+ storage. write ( & epy_key, epochs_per_year) . unwrap ( ) ;
881895
882896 let init_locked_ratio = Dec :: from_str ( "0.1" ) . unwrap ( ) ;
883897 let mut last_locked_ratio = init_locked_ratio;
898+
884899 let total_native_tokens = 1_000_000_000_u64 ;
885900 let locked_amount = u64:: try_from (
886901 ( init_locked_ratio * total_native_tokens) . to_uint ( ) . unwrap ( ) ,
@@ -891,6 +906,13 @@ mod tests {
891906 let mut total_native_tokens =
892907 token:: Amount :: native_whole ( total_native_tokens) ;
893908
909+ update_state_for_pos_playground (
910+ & mut storage,
911+ last_locked_ratio,
912+ last_inflation_amount,
913+ total_native_tokens,
914+ ) ;
915+
894916 let max_reward_rate = Dec :: from_str ( "0.1" ) . unwrap ( ) ;
895917 let target_ratio = Dec :: from_str ( "0.66666666" ) . unwrap ( ) ;
896918 let p_gain_nom = Dec :: from_str ( "0.25" ) . unwrap ( ) ;
@@ -917,17 +939,42 @@ mod tests {
917939 let locked_ratio = Dec :: try_from ( locked_amount) . unwrap ( )
918940 / Dec :: try_from ( total_native_tokens) . unwrap ( ) ;
919941
920- let rate = Dec :: try_from ( inflation) . unwrap ( )
942+ let inflation_rate = Dec :: try_from ( inflation) . unwrap ( )
921943 * Dec :: from ( epochs_per_year)
922944 / Dec :: try_from ( total_native_tokens) . unwrap ( ) ;
945+ let staking_rate = inflation_rate / locked_ratio;
946+
923947 println ! (
924948 "Round {round}: Locked ratio: {locked_ratio}, inflation rate: \
925- {rate}",
949+ {inflation_rate}, staking rate: {staking_rate }",
926950 ) ;
927951
928952 last_inflation_amount = inflation;
929953 total_native_tokens += inflation;
930954 last_locked_ratio = locked_ratio;
955+ update_state_for_pos_playground (
956+ & mut storage,
957+ last_locked_ratio,
958+ last_inflation_amount,
959+ total_native_tokens,
960+ ) ;
961+
962+ let query_staking_rate = estimate_staking_reward_rate :: <
963+ _ ,
964+ namada_trans_token:: Store < _ > ,
965+ namada_parameters:: Store < _ > ,
966+ > ( & storage)
967+ . unwrap ( ) ;
968+ // println!(" ----> Query staking rate: {query_staking_rate}");
969+ if !staking_rate. is_zero ( ) && !query_staking_rate. is_zero ( ) {
970+ let ratio = staking_rate / query_staking_rate;
971+ let residual = ratio. abs_diff ( Dec :: one ( ) ) . unwrap ( ) ;
972+ assert ! ( residual < Dec :: from_str( "0.001" ) . unwrap( ) ) ;
973+ // println!(
974+ // " ----> Ratio: {}\n",
975+ // staking_rate / query_staking_rate
976+ // );
977+ }
931978
932979 // if rate.abs_diff(&controller.max_reward_rate)
933980 // < Dec::from_str("0.01").unwrap()
@@ -965,4 +1012,22 @@ mod tests {
9651012 // );
9661013 }
9671014 }
1015+
1016+ fn update_state_for_pos_playground < S > (
1017+ storage : & mut S ,
1018+ last_staked_ratio : Dec ,
1019+ last_inflation_amount : token:: Amount ,
1020+ total_native_amount : token:: Amount ,
1021+ ) where
1022+ S : StorageRead + StorageWrite ,
1023+ {
1024+ write_last_staked_ratio ( storage, last_staked_ratio) . unwrap ( ) ;
1025+ write_last_pos_inflation_amount ( storage, last_inflation_amount)
1026+ . unwrap ( ) ;
1027+ let total_native_tokens_key =
1028+ minted_balance_key ( & storage. get_native_token ( ) . unwrap ( ) ) ;
1029+ storage
1030+ . write ( & total_native_tokens_key, total_native_amount)
1031+ . unwrap ( ) ;
1032+ }
9681033}
0 commit comments