@@ -33,103 +33,101 @@ use subtensor_macros::freeze_struct;
3333) ]
3434pub struct AlphaCurrency ( u64 ) ;
3535
36- impl TypeInfo for AlphaCurrency {
37- type Identity = <u64 as TypeInfo >:: Identity ;
38- fn type_info ( ) -> scale_info:: Type {
39- <u64 as TypeInfo >:: type_info ( )
40- }
41- }
42-
43- impl Display for AlphaCurrency {
44- fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
45- Display :: fmt ( & self . 0 , f)
46- }
47- }
48-
49- impl CompactAs for AlphaCurrency {
50- type As = u64 ;
51-
52- fn encode_as ( & self ) -> & Self :: As {
53- & self . 0
54- }
55-
56- fn decode_from ( v : Self :: As ) -> Result < Self , CodecError > {
57- Ok ( Self ( v) )
58- }
59- }
60-
61- impl From < Compact < AlphaCurrency > > for AlphaCurrency {
62- fn from ( c : Compact < AlphaCurrency > ) -> Self {
63- c. 0
64- }
65- }
66-
67- impl From < AlphaCurrency > for u64 {
68- fn from ( val : AlphaCurrency ) -> Self {
69- val. 0
70- }
71- }
36+ #[ freeze_struct( "4d1bcb31c40c2594" ) ]
37+ #[ repr( transparent) ]
38+ #[ derive(
39+ Deserialize ,
40+ Serialize ,
41+ Clone ,
42+ Copy ,
43+ Decode ,
44+ DecodeWithMemTracking ,
45+ Default ,
46+ Encode ,
47+ Eq ,
48+ Hash ,
49+ MaxEncodedLen ,
50+ Ord ,
51+ PartialEq ,
52+ PartialOrd ,
53+ RuntimeDebug ,
54+ ) ]
55+ pub struct TaoCurrency ( u64 ) ;
7256
73- impl From < u64 > for AlphaCurrency {
74- fn from ( value : u64 ) -> Self {
75- Self ( value)
76- }
77- }
57+ // implements traits required by the Currency trait (ToFixed + Into<u64> + From<u64>) and CompactAs,
58+ // TypeInfo and Display. It expects a wrapper structure for u64 (CurrencyT(u64)).
59+ macro_rules! impl_currency_reqs {
60+ ( $currency_type: ident) => {
61+ impl $currency_type {
62+ pub const fn new( inner: u64 ) -> Self {
63+ Self ( inner)
64+ }
65+ }
7866
79- impl ToFixed for AlphaCurrency {
80- fn to_fixed < F : Fixed > ( self ) -> F {
81- self . 0 . to_fixed ( )
82- }
67+ impl TypeInfo for $currency_type {
68+ type Identity = <u64 as TypeInfo >:: Identity ;
69+ fn type_info( ) -> scale_info:: Type {
70+ <u64 as TypeInfo >:: type_info( )
71+ }
72+ }
8373
84- fn checked_to_fixed < F : Fixed > ( self ) -> Option < F > {
85- self . 0 . checked_to_fixed ( )
86- }
74+ impl Display for $currency_type {
75+ fn fmt( & self , f: & mut Formatter <' _>) -> fmt:: Result {
76+ Display :: fmt( & self . 0 , f)
77+ }
78+ }
8779
88- fn saturating_to_fixed < F : Fixed > ( self ) -> F {
89- self . 0 . saturating_to_fixed ( )
90- }
91- fn wrapping_to_fixed < F : Fixed > ( self ) -> F {
92- self . 0 . wrapping_to_fixed ( )
93- }
80+ impl CompactAs for $currency_type {
81+ type As = u64 ;
9482
95- fn overflowing_to_fixed < F : Fixed > ( self ) -> ( F , bool ) {
96- self . 0 . overflowing_to_fixed ( )
97- }
98- }
83+ fn encode_as( & self ) -> & Self :: As {
84+ & self . 0
85+ }
9986
100- impl Currency for AlphaCurrency {
101- const MAX : Self = Self ( u64 :: MAX ) ;
102- const ZERO : Self = Self ( 0 ) ;
103- }
87+ fn decode_from ( v : Self :: As ) -> Result < Self , CodecError > {
88+ Ok ( Self ( v ) )
89+ }
90+ }
10491
105- pub trait Currency : ToFixed + Into < u64 > + From < u64 > + Clone + Copy {
106- const MAX : Self ;
107- const ZERO : Self ;
92+ impl From <Compact <$currency_type>> for $currency_type {
93+ fn from( c: Compact <$currency_type>) -> Self {
94+ c. 0
95+ }
96+ }
10897
109- fn is_zero ( & self ) -> bool {
110- Into :: < u64 > :: into ( * self ) == 0
111- }
98+ impl From <$currency_type> for u64 {
99+ fn from( val: $currency_type) -> Self {
100+ val. 0
101+ }
102+ }
112103
113- fn to_u64 ( & self ) -> u64 {
114- ( * self ) . into ( )
115- }
104+ impl From <u64 > for $currency_type {
105+ fn from( value: u64 ) -> Self {
106+ Self ( value)
107+ }
108+ }
116109
117- fn saturating_add ( & self , rhv : Self ) -> Self {
118- Into :: < u64 > :: into ( * self ) . saturating_add ( rhv. into ( ) ) . into ( )
119- }
110+ impl ToFixed for $currency_type {
111+ fn to_fixed<F : Fixed >( self ) -> F {
112+ self . 0 . to_fixed( )
113+ }
120114
121- #[ allow( clippy:: arithmetic_side_effects) ]
122- fn saturating_div ( & self , rhv : Self ) -> Self {
123- Into :: < u64 > :: into ( * self ) . saturating_div ( rhv. into ( ) ) . into ( )
124- }
115+ fn checked_to_fixed<F : Fixed >( self ) -> Option <F > {
116+ self . 0 . checked_to_fixed( )
117+ }
125118
126- fn saturating_sub ( & self , rhv : Self ) -> Self {
127- Into :: < u64 > :: into ( * self ) . saturating_sub ( rhv. into ( ) ) . into ( )
128- }
119+ fn saturating_to_fixed<F : Fixed >( self ) -> F {
120+ self . 0 . saturating_to_fixed( )
121+ }
122+ fn wrapping_to_fixed<F : Fixed >( self ) -> F {
123+ self . 0 . wrapping_to_fixed( )
124+ }
129125
130- fn saturating_mul ( & self , rhv : Self ) -> Self {
131- Into :: < u64 > :: into ( * self ) . saturating_mul ( rhv. into ( ) ) . into ( )
132- }
126+ fn overflowing_to_fixed<F : Fixed >( self ) -> ( F , bool ) {
127+ self . 0 . overflowing_to_fixed( )
128+ }
129+ }
130+ } ;
133131}
134132
135133macro_rules! impl_arithmetic_operators {
@@ -208,8 +206,6 @@ macro_rules! impl_arithmetic_operators {
208206 } ;
209207}
210208
211- impl_arithmetic_operators ! ( AlphaCurrency ) ;
212-
213209macro_rules! impl_approx {
214210 ( $currency_type: ident) => {
215211 #[ cfg( feature = "approx" ) ]
@@ -231,4 +227,50 @@ macro_rules! impl_approx {
231227 } ;
232228}
233229
230+ pub trait Currency : ToFixed + Into < u64 > + From < u64 > + Clone + Copy {
231+ const MAX : Self ;
232+ const ZERO : Self ;
233+
234+ fn is_zero ( & self ) -> bool {
235+ Into :: < u64 > :: into ( * self ) == 0
236+ }
237+
238+ fn to_u64 ( & self ) -> u64 {
239+ ( * self ) . into ( )
240+ }
241+
242+ fn saturating_add ( & self , rhv : Self ) -> Self {
243+ Into :: < u64 > :: into ( * self ) . saturating_add ( rhv. into ( ) ) . into ( )
244+ }
245+
246+ #[ allow( clippy:: arithmetic_side_effects) ]
247+ fn saturating_div ( & self , rhv : Self ) -> Self {
248+ Into :: < u64 > :: into ( * self ) . saturating_div ( rhv. into ( ) ) . into ( )
249+ }
250+
251+ fn saturating_sub ( & self , rhv : Self ) -> Self {
252+ Into :: < u64 > :: into ( * self ) . saturating_sub ( rhv. into ( ) ) . into ( )
253+ }
254+
255+ fn saturating_mul ( & self , rhv : Self ) -> Self {
256+ Into :: < u64 > :: into ( * self ) . saturating_mul ( rhv. into ( ) ) . into ( )
257+ }
258+ }
259+
260+ impl_arithmetic_operators ! ( AlphaCurrency ) ;
234261impl_approx ! ( AlphaCurrency ) ;
262+ impl_currency_reqs ! ( AlphaCurrency ) ;
263+
264+ impl_arithmetic_operators ! ( TaoCurrency ) ;
265+ impl_approx ! ( TaoCurrency ) ;
266+ impl_currency_reqs ! ( TaoCurrency ) ;
267+
268+ impl Currency for AlphaCurrency {
269+ const MAX : Self = Self ( u64:: MAX ) ;
270+ const ZERO : Self = Self ( 0 ) ;
271+ }
272+
273+ impl Currency for TaoCurrency {
274+ const MAX : Self = Self ( u64:: MAX ) ;
275+ const ZERO : Self = Self ( 0 ) ;
276+ }
0 commit comments