@@ -307,22 +307,47 @@ pub async fn add_spender_leaf<C: Locked + 'static>(
307
307
let channel = req
308
308
. extensions ( )
309
309
. get :: < ChainOf < Channel > > ( )
310
- . ok_or ( ResponseError :: NotFound ) ?
311
- . context ;
310
+ . ok_or ( ResponseError :: NotFound ) ?;
312
311
313
312
update_accounting (
314
313
app. pool . clone ( ) ,
315
- channel. id ( ) ,
314
+ channel. context . id ( ) ,
316
315
spender,
317
316
Side :: Spender ,
318
317
UnifiedNum :: from_u64 ( 0 ) ,
319
318
)
320
319
. await ?;
321
320
322
- // TODO: Replace with SpenderResponse
323
- Ok ( success_response ( serde_json:: to_string ( & SuccessResponse {
324
- success : true ,
325
- } ) ?) )
321
+ let latest_spendable =
322
+ fetch_spendable ( app. pool . clone ( ) , & spender, & channel. context . id ( ) ) . await ?;
323
+
324
+ let latest_spendable = match latest_spendable {
325
+ Some ( spendable) => spendable,
326
+ None => {
327
+ create_or_update_spendable_document ( & app. adapter , app. pool . clone ( ) , & channel, spender)
328
+ . await ?
329
+ }
330
+ } ;
331
+
332
+ let new_state =
333
+ match get_corresponding_new_state ( & app. pool , & app. logger , & channel. context ) . await ? {
334
+ Some ( new_state) => new_state,
335
+ None => return spender_response_without_leaf ( latest_spendable. deposit . total ) ,
336
+ } ;
337
+
338
+ let total_spent = new_state
339
+ . balances
340
+ . spenders
341
+ . get ( & spender)
342
+ . map ( |spent| spent. to_owned ( ) ) ;
343
+
344
+ let res = SpenderResponse {
345
+ spender : Spender {
346
+ total_deposited : latest_spendable. deposit . total ,
347
+ total_spent,
348
+ } ,
349
+ } ;
350
+ Ok ( success_response ( serde_json:: to_string ( & res) ?) )
326
351
}
327
352
328
353
async fn get_corresponding_new_state (
@@ -902,12 +927,21 @@ mod test {
902
927
#[ tokio:: test]
903
928
async fn adds_and_retrieves_spender_leaf ( ) {
904
929
let app = setup_dummy_app ( ) . await ;
930
+
905
931
let channel_context = app
906
932
. config
907
933
. find_chain_of ( DUMMY_CAMPAIGN . channel . token )
908
934
. expect ( "Dummy channel Token should be present in config!" )
909
935
. with ( DUMMY_CAMPAIGN . channel ) ;
910
936
937
+ let deposit = AdapterDeposit {
938
+ total : BigNum :: from_str ( "100000000000000000000" ) . expect ( "should convert" ) , // 100 DAI
939
+ still_on_create2 : BigNum :: from_str ( "1000000000000000000" ) . expect ( "should convert" ) , // 1 DAI
940
+ } ;
941
+ app. adapter
942
+ . client
943
+ . add_deposit_call ( channel_context. context . id ( ) , * CREATOR , deposit. clone ( ) ) ;
944
+
911
945
insert_channel ( & app. pool , channel_context. context )
912
946
. await
913
947
. expect ( "should insert channel" ) ;
0 commit comments