@@ -312,7 +312,7 @@ pub async fn get_spender_limits<A: Adapter + 'static>(
312
312
}
313
313
} ;
314
314
315
- let new_state = match get_corresponding_new_state ( & app. pool , & channel) . await {
315
+ let new_state = match get_corresponding_new_state ( & app. pool , & channel) . await ? {
316
316
Some ( new_state) => new_state,
317
317
None => return spender_response_without_leaf ( latest_spendable. deposit . total ) ,
318
318
} ;
@@ -346,9 +346,10 @@ pub async fn get_all_spender_limits<A: Adapter + 'static>(
346
346
. expect ( "Request should have Channel" )
347
347
. to_owned ( ) ;
348
348
349
- let new_state = get_corresponding_new_state ( & app. pool , & channel)
350
- . await
351
- . ok_or ( ResponseError :: NotFound ) ?;
349
+ let new_state = match get_corresponding_new_state ( & app. pool , & channel) . await ? {
350
+ Some ( new_state) => new_state,
351
+ None => return Err ( ResponseError :: NotFound ) ,
352
+ } ;
352
353
353
354
let mut all_spender_limits: HashMap < Address , Spender > = HashMap :: new ( ) ;
354
355
@@ -357,15 +358,22 @@ pub async fn get_all_spender_limits<A: Adapter + 'static>(
357
358
let latest_spendable =
358
359
match fetch_spendable ( app. pool . clone ( ) , spender_addr, & channel. id ( ) ) . await ? {
359
360
Some ( spendable) => spendable,
360
- None => return Err ( ResponseError :: NotFound ) ,
361
+ None => continue , // skipping spender if not found
361
362
} ;
362
363
363
364
let total_deposited = latest_spendable. deposit . total ;
364
- let spender_leaf = get_spender_leaf_for_spender ( balance, & total_deposited) ;
365
+ let total_spent = total_deposited. checked_sub ( balance) . ok_or_else ( || {
366
+ ResponseError :: FailedValidation ( "Couldn't calculate total_spent" . to_string ( ) )
367
+ } ) ?;
368
+
369
+ let spender_leaf = SpenderLeaf {
370
+ total_spent,
371
+ // merkle_proof: [u8; 32], // TODO
372
+ } ;
365
373
366
374
let spender_info = Spender {
367
375
total_deposited,
368
- spender_leaf,
376
+ spender_leaf : Some ( spender_leaf ) ,
369
377
} ;
370
378
371
379
all_spender_limits. insert ( * spender_addr, spender_info) ;
@@ -381,35 +389,17 @@ pub async fn get_all_spender_limits<A: Adapter + 'static>(
381
389
async fn get_corresponding_new_state (
382
390
pool : & DbPool ,
383
391
channel : & ChannelV5 ,
384
- ) -> Option < MessageResponse < NewState < UncheckedState > > > {
385
- let approve_state = match latest_approve_state_v5 ( pool, channel) . await . ok ( ) ? {
392
+ ) -> Result < Option < MessageResponse < NewState < UncheckedState > > > , ResponseError > {
393
+ let approve_state = match latest_approve_state_v5 ( pool, channel) . await ? {
386
394
Some ( approve_state) => approve_state,
387
- None => return None ,
395
+ None => return Err ( ResponseError :: NotFound ) ,
388
396
} ;
389
397
390
398
let state_root = approve_state. msg . state_root . clone ( ) ;
391
399
392
- let new_state = latest_new_state_v5 ( pool, channel, & state_root) . await . ok ( ) ?;
393
-
394
- new_state
395
- }
396
-
397
- fn get_spender_leaf_for_spender (
398
- spender_balance : & UnifiedNum ,
399
- total_deposited : & UnifiedNum ,
400
- ) -> Option < SpenderLeaf > {
401
- let total_spent = match total_deposited. checked_sub ( spender_balance) {
402
- Some ( spent) => spent,
403
- None => return None ,
404
- } ;
405
-
406
- // Return
407
- let leaf = SpenderLeaf {
408
- total_spent,
409
- // merkle_proof: [u8; 32], // TODO
410
- } ;
400
+ let new_state = latest_new_state_v5 ( pool, channel, & state_root) . await ?;
411
401
412
- Some ( leaf )
402
+ Ok ( new_state )
413
403
}
414
404
415
405
#[ cfg( test) ]
0 commit comments