1
- pub mod errors;
2
1
pub mod receiver;
3
2
mod state;
4
3
mod types;
5
- use std:: ops:: Neg ;
6
4
7
- use self :: state:: TokenState ;
5
+ use self :: state:: { StateError , TokenState } ;
8
6
pub use self :: types:: * ;
9
7
10
- use anyhow:: bail;
11
- use anyhow:: Ok ;
12
- use anyhow:: Result ;
13
8
use cid:: Cid ;
14
9
use fvm_ipld_blockstore:: Blockstore as IpldStore ;
15
10
use fvm_shared:: econ:: TokenAmount ;
16
11
use fvm_shared:: ActorID ;
17
12
use num_traits:: Signed ;
13
+ use std:: ops:: Neg ;
14
+ use thiserror:: Error ;
15
+
16
+ #[ derive( Error , Debug ) ]
17
+ pub enum TokenError {
18
+ #[ error( "error in underlying state {0}" ) ]
19
+ State ( #[ from] StateError ) ,
20
+ #[ error( "invalid negative: {0}" ) ]
21
+ InvalidNegative ( String ) ,
22
+ }
23
+
24
+ type Result < T > = std:: result:: Result < T , TokenError > ;
18
25
19
26
/// Library functions that implement core FRC-??? standards
20
27
///
44
51
/// Constructs the token state tree and saves it at a CID
45
52
pub fn init_state ( & self ) -> Result < Cid > {
46
53
let init_state = TokenState :: new ( & self . bs ) ?;
47
- init_state. save ( & self . bs )
54
+ Ok ( init_state. save ( & self . bs ) ? )
48
55
}
49
56
50
57
/// Helper function that loads the root of the state tree related to token-accounting
60
67
/// The mint amount must be non-negative or the method returns an error
61
68
pub fn mint ( & self , initial_holder : ActorID , value : TokenAmount ) -> Result < ( ) > {
62
69
if value. is_negative ( ) {
63
- bail ! ( "value of mint was negative {}" , value) ;
70
+ return Err ( TokenError :: InvalidNegative ( format ! (
71
+ "mint amount {} cannot be negative" ,
72
+ value
73
+ ) ) ) ;
64
74
}
65
75
66
76
// Increase the balance of the actor and increase total supply
92
102
pub fn balance_of ( & self , holder : ActorID ) -> Result < TokenAmount > {
93
103
// Load the HAMT holding balances
94
104
let state = self . load_state ( ) ;
95
- state. get_balance ( & self . bs , holder)
105
+ Ok ( state. get_balance ( & self . bs , holder) ? )
96
106
}
97
107
98
108
/// Gets the allowance between owner and spender
@@ -116,7 +126,10 @@ where
116
126
delta : TokenAmount ,
117
127
) -> Result < TokenAmount > {
118
128
if delta. is_negative ( ) {
119
- bail ! ( "value of delta was negative {}" , delta) ;
129
+ return Err ( TokenError :: InvalidNegative ( format ! (
130
+ "increase allowance delta {} cannot be negative" ,
131
+ delta
132
+ ) ) ) ;
120
133
}
121
134
122
135
let mut state = self . load_state ( ) ;
@@ -138,7 +151,10 @@ where
138
151
delta : TokenAmount ,
139
152
) -> Result < TokenAmount > {
140
153
if delta. is_negative ( ) {
141
- bail ! ( "value of delta was negative {}" , delta) ;
154
+ return Err ( TokenError :: InvalidNegative ( format ! (
155
+ "decrease allowance delta {} cannot be negative" ,
156
+ delta
157
+ ) ) ) ;
142
158
}
143
159
144
160
let mut state = self . load_state ( ) ;
@@ -186,7 +202,10 @@ where
186
202
value : TokenAmount ,
187
203
) -> Result < TokenAmount > {
188
204
if value. is_negative ( ) {
189
- bail ! ( "cannot burn a negative amount" ) ;
205
+ return Err ( TokenError :: InvalidNegative ( format ! (
206
+ "burn amount {} cannot be negative" ,
207
+ value
208
+ ) ) ) ;
190
209
}
191
210
192
211
let mut state = self . load_state ( ) ;
@@ -235,7 +254,10 @@ where
235
254
value : TokenAmount ,
236
255
) -> Result < ( ) > {
237
256
if value. is_negative ( ) {
238
- bail ! ( "cannot transfer a negative amount" ) ;
257
+ return Err ( TokenError :: InvalidNegative ( format ! (
258
+ "transfer amount {} cannot be negative" ,
259
+ value
260
+ ) ) ) ;
239
261
}
240
262
241
263
let mut state = self . load_state ( ) ;
0 commit comments