@@ -4,7 +4,7 @@ use crate::db::{
4
4
latest_new_state_v5,
5
5
} ,
6
6
get_channel_by_id, insert_channel, insert_validator_messages, list_channels,
7
- spendable:: { fetch_spendable, update_spendable} ,
7
+ spendable:: { fetch_spendable, get_all_spendables_for_channel , update_spendable} ,
8
8
DbPool , PoolError ,
9
9
} ;
10
10
use crate :: { success_response, Application , Auth , ResponseError , RouteParams } ;
@@ -353,30 +353,32 @@ pub async fn get_all_spender_limits<A: Adapter + 'static>(
353
353
354
354
let mut all_spender_limits: HashMap < Address , Spender > = HashMap :: new ( ) ;
355
355
356
+ let all_spendables = get_all_spendables_for_channel ( app. pool . clone ( ) , & channel. id ( ) ) . await ?;
357
+
356
358
// Using for loop to avoid async closures
357
- for ( spender_addr , balance ) in new_state . msg . balances . spenders . iter ( ) {
358
- let latest_spendable =
359
- match fetch_spendable ( app . pool . clone ( ) , spender_addr , & channel . id ( ) ) . await ? {
360
- Some ( spendable ) => spendable ,
361
- None => continue , // skipping spender if not found
362
- } ;
363
-
364
- let total_deposited = latest_spendable . deposit . total ;
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
359
+ for spendable in all_spendables {
360
+ let spender = spendable . spender ;
361
+ let spender_leaf = if new_state . msg . balances . spenders . contains_key ( & spender ) {
362
+ let balance = new_state . msg . balances . spenders . get ( & spender ) . unwrap ( ) ;
363
+
364
+ Some ( SpenderLeaf {
365
+ total_spent : spendable
366
+ . deposit
367
+ . total
368
+ . checked_sub ( balance )
369
+ . unwrap_or_default ( ) ,
370
+ // merkle_proof: [u8; 32], // TODO
371
+ } )
372
+ } else {
373
+ None
372
374
} ;
373
375
374
376
let spender_info = Spender {
375
- total_deposited,
376
- spender_leaf : Some ( spender_leaf ) ,
377
+ total_deposited : spendable . deposit . total ,
378
+ spender_leaf,
377
379
} ;
378
380
379
- all_spender_limits. insert ( * spender_addr , spender_info) ;
381
+ all_spender_limits. insert ( spender , spender_info) ;
380
382
}
381
383
382
384
let res = AllSpendersResponse {
0 commit comments