@@ -14,7 +14,7 @@ use crate::data::{
14
14
identity:: Identity ,
15
15
} ;
16
16
use crate :: external:: bitcoin:: BitcoinClientApi ;
17
- use crate :: external:: mint:: MintClientApi ;
17
+ use crate :: external:: mint:: { MintClientApi , QuoteStatusReply } ;
18
18
use crate :: get_config;
19
19
use crate :: persistence:: bill:: BillChainStoreApi ;
20
20
use crate :: persistence:: bill:: BillStoreApi ;
@@ -36,7 +36,7 @@ use bcr_ebill_core::constants::{
36
36
} ;
37
37
use bcr_ebill_core:: contact:: { BillAnonParticipant , BillParticipant , Contact } ;
38
38
use bcr_ebill_core:: identity:: { IdentityType , IdentityWithAll } ;
39
- use bcr_ebill_core:: mint:: { MintRequestState , MintRequestStatus } ;
39
+ use bcr_ebill_core:: mint:: { MintRequest , MintRequestState , MintRequestStatus } ;
40
40
use bcr_ebill_core:: notification:: ActionType ;
41
41
use bcr_ebill_core:: util:: currency;
42
42
use bcr_ebill_core:: { ServiceTraitBounds , Validate , ValidationError } ;
@@ -276,6 +276,86 @@ impl BillService {
276
276
}
277
277
Ok ( ( ) )
278
278
}
279
+
280
+ /// Checks a given mint quote and updates the bill mint state, if there was a change
281
+ pub ( super ) async fn check_mint_quote_and_update_bill_mint_state (
282
+ & self ,
283
+ mint_request : & MintRequest ,
284
+ ) -> Result < ( ) > {
285
+ debug ! (
286
+ "Checking mint request for quote {}" ,
287
+ & mint_request. mint_request_id
288
+ ) ;
289
+ // if it doesn't have a 'finished' state, we check the quote at the mint
290
+ if !matches ! (
291
+ mint_request. status,
292
+ MintRequestStatus :: Cancelled
293
+ | MintRequestStatus :: Rejected
294
+ | MintRequestStatus :: Expired
295
+ | MintRequestStatus :: Denied
296
+ | MintRequestStatus :: Accepted
297
+ ) {
298
+ let mint_cfg = & get_config ( ) . mint_config ;
299
+ // for now, we only support the default mint
300
+ if mint_request. mint_node_id == mint_cfg. default_mint_node_id {
301
+ let updated_status = self
302
+ . mint_client
303
+ . lookup_quote_for_mint (
304
+ & mint_cfg. default_mint_url ,
305
+ & mint_request. mint_request_id ,
306
+ )
307
+ . await ?;
308
+ // only update, if changed
309
+ match updated_status {
310
+ QuoteStatusReply :: Pending => {
311
+ if !matches ! ( mint_request. status, MintRequestStatus :: Pending ) {
312
+ self . mint_store
313
+ . update_request (
314
+ & mint_request. mint_request_id ,
315
+ & MintRequestStatus :: Pending ,
316
+ )
317
+ . await ?;
318
+ }
319
+ }
320
+ QuoteStatusReply :: Denied => {
321
+ // checked above, that it's not denied
322
+ self . mint_store
323
+ . update_request ( & mint_request. mint_request_id , & MintRequestStatus :: Denied )
324
+ . await ?;
325
+ }
326
+ QuoteStatusReply :: Offered {
327
+ ..
328
+ // keyset_id,
329
+ // expiration_date,
330
+ // discounted,
331
+ } => {
332
+ if !matches ! ( mint_request. status, MintRequestStatus :: Offered ) {
333
+ // TODO next task: if not offered yet, persist offer
334
+ self . mint_store
335
+ . update_request (
336
+ & mint_request. mint_request_id ,
337
+ & MintRequestStatus :: Offered ,
338
+ )
339
+ . await ?;
340
+ }
341
+ }
342
+ QuoteStatusReply :: Accepted { .. } => {
343
+ // checked above, that it's not accepted
344
+ self . mint_store
345
+ . update_request ( & mint_request. mint_request_id , & MintRequestStatus :: Accepted )
346
+ . await ?;
347
+ }
348
+ QuoteStatusReply :: Rejected { .. } => {
349
+ // checked above, that it's not rejected
350
+ self . mint_store
351
+ . update_request ( & mint_request. mint_request_id , & MintRequestStatus :: Rejected )
352
+ . await ?;
353
+ }
354
+ } ;
355
+ }
356
+ }
357
+ Ok ( ( ) )
358
+ }
279
359
}
280
360
281
361
#[ cfg_attr( target_arch = "wasm32" , async_trait( ?Send ) ) ]
@@ -691,7 +771,7 @@ impl BillServiceApi for BillService {
691
771
. mint_store
692
772
. get_requests ( & signer_public_data. node_id ( ) , bill_id, mint_node_id)
693
773
. await ?;
694
- // If there are any active (i.e. pending, accepted or offered) requests, we can't make another one
774
+ // If there are any active, or accepted (i.e. pending, accepted or offered) requests, we can't make another one
695
775
if requests_to_mint_for_bill_and_mint. iter ( ) . any ( |rtm| {
696
776
matches ! (
697
777
rtm. status,
@@ -1118,6 +1198,66 @@ impl BillServiceApi for BillService {
1118
1198
. collect ( ) )
1119
1199
}
1120
1200
1201
+ async fn cancel_request_to_mint (
1202
+ & self ,
1203
+ mint_request_id : & str ,
1204
+ current_identity_node_id : & str ,
1205
+ ) -> Result < ( ) > {
1206
+ debug ! ( "trying to cancel request to mint {mint_request_id}" ) ;
1207
+ match self . mint_store . get_request ( mint_request_id) . await {
1208
+ Ok ( Some ( req) ) => {
1209
+ if req. requester_node_id == current_identity_node_id {
1210
+ if matches ! ( req. status, MintRequestStatus :: Pending ) {
1211
+ // TODO next task: call endpoint on mint to cancel
1212
+ self . mint_store
1213
+ . update_request ( mint_request_id, & MintRequestStatus :: Cancelled )
1214
+ . await ?;
1215
+ Ok ( ( ) )
1216
+ } else {
1217
+ Err ( Error :: CancelMintRequestNotPending )
1218
+ }
1219
+ } else {
1220
+ Err ( Error :: NotFound )
1221
+ }
1222
+ }
1223
+ Ok ( None ) => Err ( Error :: NotFound ) ,
1224
+ Err ( e) => Err ( e. into ( ) ) ,
1225
+ }
1226
+ }
1227
+
1228
+ async fn check_mint_state ( & self , bill_id : & str , current_identity_node_id : & str ) -> Result < ( ) > {
1229
+ debug ! ( "checking mint requests for bill {bill_id}" ) ;
1230
+ let requests = self
1231
+ . mint_store
1232
+ . get_requests_for_bill ( current_identity_node_id, bill_id)
1233
+ . await ?;
1234
+ for req in requests {
1235
+ if let Err ( e) = self . check_mint_quote_and_update_bill_mint_state ( & req) . await {
1236
+ error ! (
1237
+ "Could not check mint state for {}: {e}" ,
1238
+ & req. mint_request_id
1239
+ ) ;
1240
+ }
1241
+ }
1242
+ Ok ( ( ) )
1243
+ }
1244
+
1245
+ async fn check_mint_state_for_all_bills ( & self ) -> Result < ( ) > {
1246
+ debug ! ( "checking all active mint requests" ) ;
1247
+ // get all active (offered, pending) requests
1248
+ let requests = self . mint_store . get_all_active_requests ( ) . await ?;
1249
+ debug ! ( "checking all active mint requests ({})" , requests. len( ) ) ;
1250
+ for req in requests {
1251
+ if let Err ( e) = self . check_mint_quote_and_update_bill_mint_state ( & req) . await {
1252
+ error ! (
1253
+ "Could not check mint state for {}: {e}" ,
1254
+ & req. mint_request_id
1255
+ ) ;
1256
+ }
1257
+ }
1258
+ Ok ( ( ) )
1259
+ }
1260
+
1121
1261
async fn clear_bill_cache ( & self ) -> Result < ( ) > {
1122
1262
self . store . clear_bill_cache ( ) . await ?;
1123
1263
Ok ( ( ) )
0 commit comments