@@ -26,12 +26,13 @@ use rayon::iter::{
2626} ;
2727#[ cfg( feature = "wasm-runtime" ) ]
2828use rayon:: prelude:: ParallelSlice ;
29+ use rust_decimal:: Decimal ;
2930use thiserror:: Error ;
3031pub use traits:: { Sha256Hasher , StorageHasher } ;
3132
3233use crate :: ledger:: gas:: MIN_STORAGE_GAS ;
34+ use crate :: ledger:: inflation:: { mint_tokens, RewardsController , ValsToUpdate } ;
3335use crate :: ledger:: parameters:: { self , EpochDuration , Parameters } ;
34- use crate :: ledger:: inflation:: mint_tokens;
3536use crate :: ledger:: storage:: merkle_tree:: {
3637 Error as MerkleTreeError , MerkleRoot ,
3738} ;
@@ -804,8 +805,104 @@ where
804805 . expect ( "unable to derive asset identifier" )
805806 }
806807
808+ fn calculate_masp_rewards (
809+ & mut self ,
810+ addr : & Address ,
811+ ) -> storage_api:: Result < ( u64 , u64 ) > {
812+ let masp_addr = masp ( ) ;
813+ // Query the storage for information
814+
815+ //// information about the amount of tokens on the chain
816+ let total_tokens: token:: Amount =
817+ StorageRead :: read ( self , & token:: total_supply_key ( addr) ) ?. expect ( "" ) ;
818+
819+ // total staked amount in the Shielded pool
820+ let total_token_in_masp: token:: Amount =
821+ StorageRead :: read ( self , & token:: balance_key ( addr, & masp_addr) ) ?
822+ . expect ( "" ) ;
823+
824+ let epochs_per_year: u64 = StorageRead :: read (
825+ self ,
826+ & parameters:: storage:: get_epochs_per_year_key ( ) ,
827+ ) ?
828+ . expect ( "" ) ;
829+
830+ //// Values from the last epoch
831+ let last_inflation: u64 =
832+ StorageRead :: read ( self , & token:: last_inflation ( addr) ) ?. expect ( "" ) ;
833+
834+ let last_locked_ratio: Decimal =
835+ StorageRead :: read ( self , & token:: last_locked_ratio ( addr) ) ?
836+ . expect ( "" ) ;
837+
838+ //// Parameters for each token
839+ let max_reward_rate: Decimal =
840+ StorageRead :: read ( self , & token:: parameters:: max_reward_rate ( addr) ) ?
841+ . expect ( "" ) ;
842+
843+ let kp_gain_nom: Decimal =
844+ StorageRead :: read ( self , & token:: parameters:: kp_sp_gain ( addr) ) ?
845+ . expect ( "" ) ;
846+
847+ let kd_gain_nom: Decimal =
848+ StorageRead :: read ( self , & token:: parameters:: kd_sp_gain ( addr) ) ?
849+ . expect ( "" ) ;
850+
851+ let locked_target_ratio: Decimal = StorageRead :: read (
852+ self ,
853+ & token:: parameters:: locked_token_ratio ( addr) ,
854+ ) ?
855+ . expect ( "" ) ;
856+
857+ // Creating the PD controller for handing out tokens
858+ let controller = RewardsController :: new (
859+ total_token_in_masp,
860+ total_tokens,
861+ locked_target_ratio,
862+ last_locked_ratio,
863+ max_reward_rate,
864+ token:: Amount :: from ( last_inflation) ,
865+ kp_gain_nom,
866+ kd_gain_nom,
867+ epochs_per_year,
868+ ) ;
869+
870+ let ValsToUpdate {
871+ locked_ratio,
872+ inflation,
873+ } = RewardsController :: run ( & controller) ;
874+
875+ // Is it fine to write the inflation rate, this is accruate,
876+ // but we should make sure the return value's ratio matches
877+ // this new inflation rate in 'update_allowed_conversions',
878+ // otherwise we will have an inaccurate view of inflation
879+ StorageWrite :: write (
880+ self ,
881+ & token:: last_inflation ( addr) ,
882+ inflation. try_to_vec ( ) . expect ( "encode new reward rat" ) ,
883+ )
884+ . expect ( "unable to encode new inflation rate (Decimal)" ) ;
885+
886+ StorageWrite :: write (
887+ self ,
888+ & token:: last_locked_ratio ( addr) ,
889+ locked_ratio. try_to_vec ( ) . expect ( "encode new reward rat" ) ,
890+ )
891+ . expect ( "unable to encode new locked ratio (Decimal)" ) ;
892+
893+ // to make it conform with the expected output, we need to
894+ // move it to a ratio of x/100 to match the masp_rewards
895+ // function This may be unneeded, as we could describe it as a
896+ // ratio of x/1
897+
898+ // inflation-per-token = inflation / locked tokens = n/100
899+ // ∴ n = (inflation * 100) / locked tokens
900+ Ok ( ( inflation * 100 / total_token_in_masp. change ( ) as u64 , 100 ) )
901+ }
902+
807903 #[ cfg( feature = "wasm-runtime" ) ]
808- /// Update the MASP's allowed conversions
904+ /// Update the MASP's allowed conversions. Further applies the
905+ /// shield pool inflation amount per currency.
809906 fn update_allowed_conversions ( & mut self ) -> Result < ( ) > {
810907 use masp_primitives:: ff:: PrimeField ;
811908 use masp_primitives:: transaction:: components:: Amount as MaspAmount ;
@@ -832,8 +929,13 @@ where
832929 // Conversions from the previous to current asset for each address
833930 let mut current_convs = BTreeMap :: < Address , AllowedConversion > :: new ( ) ;
834931 // Reward all tokens according to above reward rates
835- for ( addr, reward) in & masp_rewards {
836- // Dispence a transparent reward in parallel to the shielded rewards
932+ // Masp inflation
933+ for ( addr, _reward) in & masp_rewards {
934+ let reward = self
935+ . calculate_masp_rewards ( addr)
936+ . expect ( "Calculating the masp rewards should not fail" ) ;
937+ // Dispense a transparent reward in parallel to the shielded rewards
938+ ///////// Gets the total shielded amount total
837939 let token_key = self . read ( & token:: balance_key ( addr, & masp_addr) ) ;
838940 if let Ok ( ( Some ( addr_balance) , _) ) = token_key {
839941 // The reward for each reward.1 units of the current asset is
@@ -842,7 +944,7 @@ where
842944 types:: decode ( addr_balance) . expect ( "invalid balance" ) ;
843945 // Since floor(a) + floor(b) <= floor(a+b), there will always be
844946 // enough rewards to reimburse users
845- total_reward += ( addr_bal * * reward) . 0 ;
947+ total_reward += ( addr_bal * reward) . 0 ;
846948 }
847949 // Provide an allowed conversion from previous timestamp. The
848950 // negative sign allows each instance of the old asset to be
0 commit comments