@@ -9,7 +9,9 @@ use fvm_shared::bigint::bigint_ser::BigIntDe;
9
9
use fvm_shared:: econ:: TokenAmount ;
10
10
use num_traits:: { Signed , Zero } ;
11
11
12
- use fil_actors_runtime:: { make_empty_map, make_map_with_root_and_bitwidth, Map } ;
12
+ use fil_actors_runtime:: {
13
+ actor_error, make_empty_map, make_map_with_root_and_bitwidth, ActorError , Map ,
14
+ } ;
13
15
14
16
pub const BALANCE_TABLE_BITWIDTH : u32 = 6 ;
15
17
@@ -25,17 +27,17 @@ where
25
27
}
26
28
27
29
/// Initializes a balance table from a root Cid
28
- pub fn from_root ( bs : & ' a BS , cid : & Cid ) -> Result < Self , HamtError > {
30
+ pub fn from_root ( bs : & ' a BS , cid : & Cid ) -> Result < Self , HamtError < BS :: Error > > {
29
31
Ok ( Self ( make_map_with_root_and_bitwidth ( cid, bs, BALANCE_TABLE_BITWIDTH ) ?) )
30
32
}
31
33
32
34
/// Retrieve root from balance table
33
- pub fn root ( & mut self ) -> Result < Cid , HamtError > {
35
+ pub fn root ( & mut self ) -> Result < Cid , HamtError < BS :: Error > > {
34
36
self . 0 . flush ( )
35
37
}
36
38
37
39
/// Gets token amount for given address in balance table
38
- pub fn get ( & self , key : & Address ) -> Result < TokenAmount , HamtError > {
40
+ pub fn get ( & self , key : & Address ) -> Result < TokenAmount , HamtError < BS :: Error > > {
39
41
if let Some ( v) = self . 0 . get ( & key. to_bytes ( ) ) ? {
40
42
Ok ( v. 0 . clone ( ) )
41
43
} else {
@@ -44,12 +46,17 @@ where
44
46
}
45
47
46
48
/// Adds token amount to previously initialized account.
47
- pub fn add ( & mut self , key : & Address , value : & TokenAmount ) -> Result < ( ) , HamtError > {
49
+ pub fn add ( & mut self , key : & Address , value : & TokenAmount ) -> Result < ( ) , ActorError > {
48
50
let prev = self . get ( key) ?;
49
51
let sum = & prev + value;
50
52
if sum. is_negative ( ) {
51
- Err ( format ! ( "New balance in table cannot be negative: {}" , sum) . into ( ) )
52
- } else if sum. is_zero ( ) && !prev. is_zero ( ) {
53
+ return Err ( actor_error ! (
54
+ illegal_argument,
55
+ "new balance in table cannot be negative: {}" ,
56
+ sum
57
+ ) ) ;
58
+ }
59
+ if sum. is_zero ( ) && !prev. is_zero ( ) {
53
60
self . 0 . delete ( & key. to_bytes ( ) ) ?;
54
61
Ok ( ( ) )
55
62
} else {
66
73
key : & Address ,
67
74
req : & TokenAmount ,
68
75
floor : & TokenAmount ,
69
- ) -> Result < TokenAmount , HamtError > {
76
+ ) -> Result < TokenAmount , ActorError > {
70
77
let prev = self . get ( key) ?;
71
78
let available = std:: cmp:: max ( TokenAmount :: zero ( ) , prev - floor) ;
72
79
let sub: TokenAmount = std:: cmp:: min ( & available, req) . clone ( ) ;
@@ -79,24 +86,24 @@ where
79
86
}
80
87
81
88
/// Subtracts value from a balance, and errors if full amount was not substracted.
82
- pub fn must_subtract ( & mut self , key : & Address , req : & TokenAmount ) -> Result < ( ) , HamtError > {
89
+ pub fn must_subtract ( & mut self , key : & Address , req : & TokenAmount ) -> Result < ( ) , ActorError > {
83
90
let prev = self . get ( key) ?;
84
91
85
92
if req > & prev {
86
- Err ( "couldn't subtract the requested amount" . into ( ) )
87
- } else {
88
- self . add ( key, & -req)
93
+ return Err ( actor_error ! ( illegal_argument, "couldn't subtract the requested amount" ) ) ;
89
94
}
95
+ self . add ( key, & -req) ?;
96
+
97
+ Ok ( ( ) )
90
98
}
91
99
92
100
/// Returns total balance held by this balance table
93
101
#[ allow( dead_code) ]
94
- pub fn total ( & self ) -> Result < TokenAmount , HamtError > {
102
+ pub fn total ( & self ) -> Result < TokenAmount , HamtError < BS :: Error > > {
95
103
let mut total = TokenAmount :: default ( ) ;
96
104
97
105
self . 0 . for_each ( |_, v : & BigIntDe | {
98
106
total += & v. 0 ;
99
- Ok ( ( ) )
100
107
} ) ?;
101
108
102
109
Ok ( total)
0 commit comments