@@ -3,7 +3,7 @@ pub mod fee {
3
3
4
4
use primitives:: { Address , DomainError , UnifiedNum , ValidatorDesc } ;
5
5
6
- /// Calculates the fee for a specified validator
6
+ /// Calculates the fee for a given payout of the specified validator
7
7
/// This function will return None if the provided validator is not part of the Campaign / Channel
8
8
/// In the case of overflow when calculating the payout, an error will be returned
9
9
pub fn calculate_fee (
@@ -16,4 +16,51 @@ pub mod fee {
16
16
. map ( |pro_mille_fee| pro_mille_fee. div_floor ( & PRO_MILLE ) )
17
17
. ok_or_else ( || DomainError :: InvalidArgument ( "payout calculation overflow" . to_string ( ) ) )
18
18
}
19
+
20
+ #[ cfg( test) ]
21
+ mod test {
22
+ use primitives:: {
23
+ test_util:: { PUBLISHER , DUMMY_VALIDATOR_LEADER } , UnifiedNum ,
24
+ } ;
25
+
26
+ use crate :: spender:: fee:: calculate_fee;
27
+
28
+ #[ test]
29
+ fn test_calcualtion_of_fee ( ) {
30
+ let dummy_leader = DUMMY_VALIDATOR_LEADER . clone ( ) ;
31
+ assert_eq ! (
32
+ UnifiedNum :: from( 100 ) ,
33
+ dummy_leader. fee,
34
+ "Dummy validator leader fee has changed, please revisit this test!"
35
+ ) ;
36
+
37
+ // normal payout - no flooring
38
+ {
39
+ let payout = ( * PUBLISHER , UnifiedNum :: from ( 300 ) ) ;
40
+
41
+ let validator_fee =
42
+ calculate_fee ( payout, & dummy_leader) . expect ( "Should not overflow" ) ;
43
+
44
+ assert_eq ! ( UnifiedNum :: from( 30 ) , validator_fee) ;
45
+ }
46
+
47
+ // payout with flooring
48
+ {
49
+ // 66 * 100 / 1000 = 6.6 = 6
50
+ let payout = ( * PUBLISHER , UnifiedNum :: from ( 66 ) ) ;
51
+ let validator_fee =
52
+ calculate_fee ( payout, & dummy_leader) . expect ( "Should not overflow" ) ;
53
+
54
+ assert_eq ! ( UnifiedNum :: from( 6 ) , validator_fee) ;
55
+ }
56
+
57
+ // Overflow
58
+ {
59
+ // u64::MAX * 100 (overflow) / 1000
60
+ let payout = ( * PUBLISHER , UnifiedNum :: from ( u64:: MAX ) ) ;
61
+
62
+ calculate_fee ( payout, & dummy_leader) . expect_err ( "Should overflow" ) ;
63
+ }
64
+ }
65
+ }
19
66
}
0 commit comments