@@ -695,7 +695,13 @@ where
695
695
mod tests {
696
696
use std:: str:: FromStr ;
697
697
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
+
698
703
use super :: * ;
704
+ use crate :: OwnedPosParams ;
699
705
700
706
#[ test]
701
707
fn test_inflation_calc_up ( ) {
@@ -877,10 +883,19 @@ mod tests {
877
883
878
884
#[ test]
879
885
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
+
880
892
let epochs_per_year = 365_u64 ;
893
+ let epy_key = get_epochs_per_year_key ( ) ;
894
+ storage. write ( & epy_key, epochs_per_year) . unwrap ( ) ;
881
895
882
896
let init_locked_ratio = Dec :: from_str ( "0.1" ) . unwrap ( ) ;
883
897
let mut last_locked_ratio = init_locked_ratio;
898
+
884
899
let total_native_tokens = 1_000_000_000_u64 ;
885
900
let locked_amount = u64:: try_from (
886
901
( init_locked_ratio * total_native_tokens) . to_uint ( ) . unwrap ( ) ,
@@ -891,6 +906,13 @@ mod tests {
891
906
let mut total_native_tokens =
892
907
token:: Amount :: native_whole ( total_native_tokens) ;
893
908
909
+ update_state_for_pos_playground (
910
+ & mut storage,
911
+ last_locked_ratio,
912
+ last_inflation_amount,
913
+ total_native_tokens,
914
+ ) ;
915
+
894
916
let max_reward_rate = Dec :: from_str ( "0.1" ) . unwrap ( ) ;
895
917
let target_ratio = Dec :: from_str ( "0.66666666" ) . unwrap ( ) ;
896
918
let p_gain_nom = Dec :: from_str ( "0.25" ) . unwrap ( ) ;
@@ -917,17 +939,42 @@ mod tests {
917
939
let locked_ratio = Dec :: try_from ( locked_amount) . unwrap ( )
918
940
/ Dec :: try_from ( total_native_tokens) . unwrap ( ) ;
919
941
920
- let rate = Dec :: try_from ( inflation) . unwrap ( )
942
+ let inflation_rate = Dec :: try_from ( inflation) . unwrap ( )
921
943
* Dec :: from ( epochs_per_year)
922
944
/ Dec :: try_from ( total_native_tokens) . unwrap ( ) ;
945
+ let staking_rate = inflation_rate / locked_ratio;
946
+
923
947
println ! (
924
948
"Round {round}: Locked ratio: {locked_ratio}, inflation rate: \
925
- {rate}",
949
+ {inflation_rate}, staking rate: {staking_rate }",
926
950
) ;
927
951
928
952
last_inflation_amount = inflation;
929
953
total_native_tokens += inflation;
930
954
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
+ }
931
978
932
979
// if rate.abs_diff(&controller.max_reward_rate)
933
980
// < Dec::from_str("0.01").unwrap()
@@ -965,4 +1012,22 @@ mod tests {
965
1012
// );
966
1013
}
967
1014
}
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
+ }
968
1033
}
0 commit comments