@@ -7,13 +7,12 @@ use jsonrpsee::{
7
7
types:: ErrorObject ,
8
8
} ;
9
9
use thunder:: {
10
- node,
11
10
types:: { Address , PointedOutput , Txid , WithdrawalBundle } ,
12
- wallet:: { self , Balance } ,
11
+ wallet:: Balance ,
13
12
} ;
14
13
use thunder_app_rpc_api:: RpcServer ;
15
14
16
- use crate :: app:: { self , App } ;
15
+ use crate :: app:: App ;
17
16
18
17
pub struct RpcServerImpl {
19
18
app : App ,
@@ -30,25 +29,10 @@ where
30
29
let error = anyhow:: Error :: from ( error) ;
31
30
custom_err_msg ( format ! ( "{error:#}" ) )
32
31
}
33
-
34
- fn convert_app_err ( err : app:: Error ) -> ErrorObject < ' static > {
35
- let err = anyhow:: anyhow!( err) ;
36
- tracing:: error!( "{err:#}" ) ;
37
- custom_err ( err)
38
- }
39
-
40
- fn convert_node_err ( err : node:: Error ) -> ErrorObject < ' static > {
41
- custom_err ( err)
42
- }
43
-
44
- fn convert_wallet_err ( err : wallet:: Error ) -> ErrorObject < ' static > {
45
- custom_err ( err)
46
- }
47
-
48
32
#[ async_trait]
49
33
impl RpcServer for RpcServerImpl {
50
34
async fn balance ( & self ) -> RpcResult < Balance > {
51
- self . app . wallet . get_balance ( ) . map_err ( convert_wallet_err )
35
+ self . app . wallet . get_balance ( ) . map_err ( custom_err )
52
36
}
53
37
54
38
async fn create_deposit (
@@ -64,14 +48,14 @@ impl RpcServer for RpcServerImpl {
64
48
bitcoin:: Amount :: from_sat ( value_sats) ,
65
49
bitcoin:: Amount :: from_sat ( fee_sats) ,
66
50
)
67
- . map_err ( convert_app_err )
51
+ . map_err ( custom_err )
68
52
} )
69
53
. await
70
54
. unwrap ( )
71
55
}
72
56
73
57
async fn connect_peer ( & self , addr : SocketAddr ) -> RpcResult < ( ) > {
74
- self . app . node . connect_peer ( addr) . map_err ( convert_node_err )
58
+ self . app . node . connect_peer ( addr) . map_err ( custom_err )
75
59
}
76
60
77
61
async fn format_deposit_address (
@@ -118,25 +102,18 @@ impl RpcServer for RpcServerImpl {
118
102
}
119
103
120
104
async fn get_new_address ( & self ) -> RpcResult < Address > {
121
- self . app
122
- . wallet
123
- . get_new_address ( )
124
- . map_err ( convert_wallet_err)
105
+ self . app . wallet . get_new_address ( ) . map_err ( custom_err)
125
106
}
126
107
127
108
async fn get_wallet_addresses ( & self ) -> RpcResult < Vec < Address > > {
128
- let addrs = self
129
- . app
130
- . wallet
131
- . get_addresses ( )
132
- . map_err ( convert_wallet_err) ?;
109
+ let addrs = self . app . wallet . get_addresses ( ) . map_err ( custom_err) ?;
133
110
let mut res: Vec < _ > = addrs. into_iter ( ) . collect ( ) ;
134
111
res. sort_by_key ( |addr| addr. as_base58 ( ) ) ;
135
112
Ok ( res)
136
113
}
137
114
138
115
async fn get_wallet_utxos ( & self ) -> RpcResult < Vec < PointedOutput > > {
139
- let utxos = self . app . wallet . get_utxos ( ) . map_err ( convert_wallet_err ) ?;
116
+ let utxos = self . app . wallet . get_utxos ( ) . map_err ( custom_err ) ?;
140
117
let utxos = utxos
141
118
. into_iter ( )
142
119
. map ( |( outpoint, output) | PointedOutput { outpoint, output } )
@@ -145,8 +122,7 @@ impl RpcServer for RpcServerImpl {
145
122
}
146
123
147
124
async fn getblockcount ( & self ) -> RpcResult < u32 > {
148
- let height =
149
- self . app . node . try_get_height ( ) . map_err ( convert_node_err) ?;
125
+ let height = self . app . node . try_get_height ( ) . map_err ( custom_err) ?;
150
126
let block_count = height. map_or ( 0 , |height| height + 1 ) ;
151
127
Ok ( block_count)
152
128
}
@@ -158,7 +134,7 @@ impl RpcServer for RpcServerImpl {
158
134
. app
159
135
. node
160
136
. get_latest_failed_withdrawal_bundle_height ( )
161
- . map_err ( convert_node_err ) ?;
137
+ . map_err ( custom_err ) ?;
162
138
Ok ( height)
163
139
}
164
140
@@ -168,7 +144,7 @@ impl RpcServer for RpcServerImpl {
168
144
}
169
145
170
146
async fn list_utxos ( & self ) -> RpcResult < Vec < PointedOutput > > {
171
- let utxos = self . app . node . get_all_utxos ( ) . map_err ( convert_node_err ) ?;
147
+ let utxos = self . app . node . get_all_utxos ( ) . map_err ( custom_err ) ?;
172
148
let res = utxos
173
149
. into_iter ( )
174
150
. map ( |( outpoint, output) | PointedOutput { outpoint, output } )
@@ -178,10 +154,14 @@ impl RpcServer for RpcServerImpl {
178
154
179
155
async fn mine ( & self , fee : Option < u64 > ) -> RpcResult < ( ) > {
180
156
let fee = fee. map ( bitcoin:: Amount :: from_sat) ;
181
- self . app . local_pool . spawn_pinned ( {
182
- let app = self . app . clone ( ) ;
183
- move || async move { app. mine ( fee) . await . map_err ( convert_app_err) }
184
- } ) . await . unwrap ( )
157
+ self . app
158
+ . local_pool
159
+ . spawn_pinned ( {
160
+ let app = self . app . clone ( ) ;
161
+ move || async move { app. mine ( fee) . await . map_err ( custom_err) }
162
+ } )
163
+ . await
164
+ . unwrap ( )
185
165
}
186
166
187
167
async fn pending_withdrawal_bundle (
@@ -190,7 +170,7 @@ impl RpcServer for RpcServerImpl {
190
170
self . app
191
171
. node
192
172
. get_pending_withdrawal_bundle ( )
193
- . map_err ( convert_node_err )
173
+ . map_err ( custom_err )
194
174
}
195
175
196
176
async fn openapi_schema ( & self ) -> RpcResult < utoipa:: openapi:: OpenApi > {
@@ -199,10 +179,7 @@ impl RpcServer for RpcServerImpl {
199
179
}
200
180
201
181
async fn remove_from_mempool ( & self , txid : Txid ) -> RpcResult < ( ) > {
202
- self . app
203
- . node
204
- . remove_from_mempool ( txid)
205
- . map_err ( convert_node_err)
182
+ self . app . node . remove_from_mempool ( txid) . map_err ( custom_err)
206
183
}
207
184
208
185
async fn set_seed_from_mnemonic ( & self , mnemonic : String ) -> RpcResult < ( ) > {
@@ -213,18 +190,12 @@ impl RpcServer for RpcServerImpl {
213
190
let seed_bytes: [ u8 ; 64 ] = seed. as_bytes ( ) . try_into ( ) . map_err (
214
191
|err : <[ u8 ; 64 ] as TryFrom < & [ u8 ] > >:: Error | custom_err ( err) ,
215
192
) ?;
216
- self . app
217
- . wallet
218
- . set_seed ( & seed_bytes)
219
- . map_err ( convert_wallet_err)
193
+ self . app . wallet . set_seed ( & seed_bytes) . map_err ( custom_err)
220
194
}
221
195
222
196
async fn sidechain_wealth_sats ( & self ) -> RpcResult < u64 > {
223
- let sidechain_wealth = self
224
- . app
225
- . node
226
- . get_sidechain_wealth ( )
227
- . map_err ( convert_node_err) ?;
197
+ let sidechain_wealth =
198
+ self . app . node . get_sidechain_wealth ( ) . map_err ( custom_err) ?;
228
199
Ok ( sidechain_wealth. to_sat ( ) )
229
200
}
230
201
@@ -238,11 +209,8 @@ impl RpcServer for RpcServerImpl {
238
209
value_sats : u64 ,
239
210
fee_sats : u64 ,
240
211
) -> RpcResult < Txid > {
241
- let accumulator = self
242
- . app
243
- . node
244
- . get_tip_accumulator ( )
245
- . map_err ( convert_node_err) ?;
212
+ let accumulator =
213
+ self . app . node . get_tip_accumulator ( ) . map_err ( custom_err) ?;
246
214
let tx = self
247
215
. app
248
216
. wallet
@@ -252,9 +220,9 @@ impl RpcServer for RpcServerImpl {
252
220
Amount :: from_sat ( value_sats) ,
253
221
Amount :: from_sat ( fee_sats) ,
254
222
)
255
- . map_err ( convert_wallet_err ) ?;
223
+ . map_err ( custom_err ) ?;
256
224
let txid = tx. txid ( ) ;
257
- self . app . sign_and_send ( tx) . map_err ( convert_app_err ) ?;
225
+ self . app . sign_and_send ( tx) . map_err ( custom_err ) ?;
258
226
Ok ( txid)
259
227
}
260
228
@@ -265,11 +233,8 @@ impl RpcServer for RpcServerImpl {
265
233
fee_sats : u64 ,
266
234
mainchain_fee_sats : u64 ,
267
235
) -> RpcResult < Txid > {
268
- let accumulator = self
269
- . app
270
- . node
271
- . get_tip_accumulator ( )
272
- . map_err ( convert_node_err) ?;
236
+ let accumulator =
237
+ self . app . node . get_tip_accumulator ( ) . map_err ( custom_err) ?;
273
238
let tx = self
274
239
. app
275
240
. wallet
@@ -280,9 +245,9 @@ impl RpcServer for RpcServerImpl {
280
245
Amount :: from_sat ( mainchain_fee_sats) ,
281
246
Amount :: from_sat ( fee_sats) ,
282
247
)
283
- . map_err ( convert_wallet_err ) ?;
248
+ . map_err ( custom_err ) ?;
284
249
let txid = tx. txid ( ) ;
285
- self . app . sign_and_send ( tx) . map_err ( convert_app_err ) ?;
250
+ self . app . sign_and_send ( tx) . map_err ( custom_err ) ?;
286
251
Ok ( txid)
287
252
}
288
253
}
0 commit comments