File tree 2 files changed +26
-1
lines changed
substrate-node/pallets/pallet-tft-price/src 2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -400,6 +400,9 @@ impl<T: Config> Pallet<T> {
400
400
fn calc_avg ( ) -> u32 {
401
401
let queue = Self :: queue_transient ( ) ;
402
402
let items = queue. get_all_values ( ) ;
403
- items. iter ( ) . fold ( 0_u32 , |a, b| a. saturating_add ( * b) ) / items. len ( ) as u32
403
+ let sum = items. iter ( ) . fold ( 0_u32 , |a, b| a. saturating_add ( * b) ) ;
404
+ ( U32F32 :: from_num ( sum) / U32F32 :: from_num ( items. len ( ) ) )
405
+ . round ( )
406
+ . to_num :: < u32 > ( )
404
407
}
405
408
}
Original file line number Diff line number Diff line change @@ -28,6 +28,28 @@ fn test_set_allowed_origin_by_wrong_origin_fails() {
28
28
} )
29
29
}
30
30
31
+ #[ test]
32
+ fn test_calc_avg_rounding_works ( ) {
33
+ let mut t = ExternalityBuilder :: build ( ) ;
34
+ t. execute_with ( || {
35
+ let mut queue = TFTPriceModule :: queue_transient ( ) ;
36
+ queue. push ( 499 ) ; // avg = 499.0
37
+ assert_eq ! ( TFTPriceModule :: calc_avg( ) , 499 ) ;
38
+ queue. push ( 500 ) ; // avg = 499.5
39
+ assert_eq ! ( TFTPriceModule :: calc_avg( ) , 500 ) ;
40
+ queue. push ( 500 ) ; // avg = 499.66
41
+ assert_eq ! ( TFTPriceModule :: calc_avg( ) , 500 ) ;
42
+ queue. push ( 500 ) ; // avg = 499.75
43
+ assert_eq ! ( TFTPriceModule :: calc_avg( ) , 500 ) ;
44
+ queue. push ( 499 ) ; // avg = 499.66
45
+ assert_eq ! ( TFTPriceModule :: calc_avg( ) , 500 ) ;
46
+ queue. push ( 499 ) ; // avg = 499.5
47
+ assert_eq ! ( TFTPriceModule :: calc_avg( ) , 500 ) ;
48
+ queue. push ( 499 ) ; // avg = 499.25
49
+ assert_eq ! ( TFTPriceModule :: calc_avg( ) , 499 ) ;
50
+ } )
51
+ }
52
+
31
53
#[ test]
32
54
fn test_set_prices_works ( ) {
33
55
let mut t = ExternalityBuilder :: build ( ) ;
You can’t perform that action at this time.
0 commit comments