@@ -2,9 +2,6 @@ pub mod fee {
22
33 use primitives:: { Address , DomainError , UnifiedNum , ValidatorDesc } ;
44
5- /// Pro mile (per thousands, `1000`) in `UnifiedNum` precision, i.e. `1_000.00_000_000`.
6- pub const PRO_MILLE : UnifiedNum = UnifiedNum :: from_u64 ( 1_000 * UnifiedNum :: MULTIPLIER ) ;
7-
85 /// Calculates the fee for a given payout of the specified validator
96 /// This function will return None if the provided validator is not part of the Campaign / Channel
107 /// In the case of overflow when calculating the payout, an error will be returned
@@ -13,17 +10,16 @@ pub mod fee {
1310 validator : & ValidatorDesc ,
1411 ) -> Result < UnifiedNum , DomainError > {
1512 // should never overflow, but we guard against overflow
16- // `UnifiedNum::checked_div` will accurately floor the inner `u64` for the `UnifiedNum::PRECISION`
1713 payout
1814 . checked_mul ( & validator. fee )
19- . and_then ( |pro_mille_fee| pro_mille_fee. checked_div ( & PRO_MILLE ) )
2015 . ok_or_else ( || DomainError :: InvalidArgument ( "payout calculation overflow" . to_string ( ) ) )
2116 }
2217
2318 #[ cfg( test) ]
2419 mod test {
2520 use primitives:: {
2621 test_util:: { DUMMY_VALIDATOR_LEADER , PUBLISHER } ,
22+ unified_num:: FromWhole ,
2723 UnifiedNum , ValidatorDesc ,
2824 } ;
2925
@@ -32,36 +28,26 @@ pub mod fee {
3228 #[ test]
3329 fn test_calculation_of_fee ( ) {
3430 let mut dummy_leader = DUMMY_VALIDATOR_LEADER . clone ( ) ;
35- dummy_leader. fee = UnifiedNum :: from ( 10_000_000 ) ;
31+ dummy_leader. fee = UnifiedNum :: from_whole ( 0.1 ) ;
3632
3733 // normal payout - no flooring
3834 {
39- // 30 000 * 10 000 000 / 1 000 00 000 000 = 3
35+ // 30 000 * 10 000 000 / 100 000 000 = 3000
4036
41- // 0.0003 * 0.1 / 1000.0 = 0.00000003 = UnifiedNum(3)
42- // 0.00 030 000 * 0.10 000 000 / 1 000 = 0.00 000 003
43- let payout = ( * PUBLISHER , UnifiedNum :: from ( 30_000 ) ) ;
37+ // 0.0003 * 0.1 = 0.00000003 = UnifiedNum(3)
38+ // 0.00 030 000 * 0.10 000 000 = 0.00003
39+ let payout = ( * PUBLISHER , UnifiedNum :: from_whole ( 0.0003 ) ) ;
4440
4541 let validator_fee =
4642 calculate_fee ( payout, & dummy_leader) . expect ( "Should not overflow" ) ;
4743
4844 assert_eq ! (
49- UnifiedNum :: from ( 3 ) ,
45+ UnifiedNum :: from_whole ( 0.00003 ) ,
5046 validator_fee,
51- "fee should be 0.00000003 in UnifiedNum"
47+ "fee should be 0.00003 in UnifiedNum"
5248 ) ;
5349 }
5450
55- // payout with flooring
56- {
57- // 66 000 * 10 000 000 / 100 000 000 000 = 6.6 = 6
58- let payout = ( * PUBLISHER , UnifiedNum :: from ( 66_000 ) ) ;
59- let validator_fee =
60- calculate_fee ( payout, & dummy_leader) . expect ( "Should not overflow" ) ;
61-
62- assert_eq ! ( UnifiedNum :: from( 6 ) , validator_fee) ;
63- }
64-
6551 // Overflow - even using `Ratio` for `UnifiedNum`, this should overflow
6652 {
6753 let very_high_fee = ValidatorDesc {
@@ -79,13 +65,12 @@ pub mod fee {
7965 // e.g. 3 TOKENs
8066 let payout = ( * PUBLISHER , UnifiedNum :: from ( 300_000_000_u64 ) ) ;
8167
82- // 300 000 000 × 10 000 000 / 100 000 000 000
68+ // 300 000 000 × 10 000 000 / 100 000 000 = 30 000 000
8369 let validator_fee =
8470 calculate_fee ( payout, & dummy_leader) . expect ( "Should not overflow" ) ;
8571
86- // : 3 000 000 000 000
87- // 0.000003
88- assert_eq ! ( UnifiedNum :: from( 30_000 ) , validator_fee) ;
72+ // 0.3
73+ assert_eq ! ( UnifiedNum :: from_whole( 0.3 ) , validator_fee) ;
8974 }
9075 }
9176 }
0 commit comments