@@ -610,6 +610,40 @@ pub async fn get_accounting_for_channel<C: Locked + 'static>(
610
610
Ok ( success_response ( serde_json:: to_string ( & res) ?) )
611
611
}
612
612
613
+ pub async fn get_accounting_for_channel_axum < C : Locked + ' static > (
614
+ Extension ( app) : Extension < Arc < Application < C > > > ,
615
+ Extension ( channel_context) : Extension < ChainOf < Channel > > ,
616
+ ) -> Result < Json < AccountingResponse < CheckedState > > , ResponseError > {
617
+ let channel = channel_context. context ;
618
+
619
+ let accountings = get_all_accountings_for_channel ( app. pool . clone ( ) , channel. id ( ) ) . await ?;
620
+
621
+ let mut unchecked_balances: Balances < UncheckedState > = Balances :: default ( ) ;
622
+
623
+ for accounting in accountings {
624
+ match accounting. side {
625
+ Side :: Earner => unchecked_balances
626
+ . earners
627
+ . insert ( accounting. address , accounting. amount ) ,
628
+ Side :: Spender => unchecked_balances
629
+ . spenders
630
+ . insert ( accounting. address , accounting. amount ) ,
631
+ } ;
632
+ }
633
+
634
+ let balances = match unchecked_balances. check ( ) {
635
+ Ok ( balances) => balances,
636
+ Err ( error) => {
637
+ error ! ( & app. logger, "{}" , & error; "module" => "channel_accounting" ) ;
638
+ return Err ( ResponseError :: FailedValidation (
639
+ "Earners sum is not equal to spenders sum for channel" . to_string ( ) ,
640
+ ) ) ;
641
+ }
642
+ } ;
643
+
644
+ Ok ( Json ( AccountingResponse :: < CheckedState > { balances } ) )
645
+ }
646
+
613
647
pub async fn channel_payout_axum < C : Locked + ' static > (
614
648
Extension ( app) : Extension < Arc < Application < C > > > ,
615
649
Extension ( channel_context) : Extension < ChainOf < Channel > > ,
@@ -1087,7 +1121,6 @@ mod test {
1087
1121
ethereum:: test_util:: { GANACHE_INFO_1 , GANACHE_INFO_1337 } ,
1088
1122
primitives:: Deposit as AdapterDeposit ,
1089
1123
} ;
1090
- use hyper:: StatusCode ;
1091
1124
use primitives:: {
1092
1125
channel:: Nonce ,
1093
1126
test_util:: {
@@ -1170,19 +1203,11 @@ mod test {
1170
1203
assert_eq ! ( updated_spendable. spender, * CREATOR ) ;
1171
1204
}
1172
1205
1173
- async fn res_to_accounting_response ( res : Response < Body > ) -> AccountingResponse < CheckedState > {
1174
- let json = hyper:: body:: to_bytes ( res. into_body ( ) )
1175
- . await
1176
- . expect ( "Should get json" ) ;
1177
-
1178
- let accounting_response: AccountingResponse < CheckedState > =
1179
- serde_json:: from_slice ( & json) . expect ( "Should get AccountingResponse" ) ;
1180
- accounting_response
1181
- }
1182
-
1183
1206
#[ tokio:: test]
1184
1207
async fn get_accountings_for_channel ( ) {
1185
- let app = setup_dummy_app ( ) . await ;
1208
+ let app_guard = setup_dummy_app ( ) . await ;
1209
+
1210
+ let app = Extension ( Arc :: new ( app_guard. app . clone ( ) ) ) ;
1186
1211
let channel_context = app
1187
1212
. config
1188
1213
. find_chain_of ( DUMMY_CAMPAIGN . channel . token )
@@ -1192,20 +1217,15 @@ mod test {
1192
1217
insert_channel ( & app. pool , & channel_context)
1193
1218
. await
1194
1219
. expect ( "should insert channel" ) ;
1195
- let build_request = |channel_context : & ChainOf < Channel > | {
1196
- Request :: builder ( )
1197
- . extension ( channel_context. clone ( ) )
1198
- . body ( Body :: empty ( ) )
1199
- . expect ( "Should build Request" )
1200
- } ;
1220
+
1201
1221
// Testing for no accounting yet
1202
1222
{
1203
- let res = get_accounting_for_channel ( build_request ( & channel_context) , & app)
1204
- . await
1205
- . expect ( "should get response" ) ;
1206
- assert_eq ! ( StatusCode :: OK , res. status( ) ) ;
1223
+ let res = get_accounting_for_channel_axum ( app. clone ( ) , Extension ( channel_context. clone ( ) ) )
1224
+ . await ;
1225
+ assert ! ( res. is_ok( ) ) ;
1226
+
1227
+ let accounting_response = res. unwrap ( ) ;
1207
1228
1208
- let accounting_response = res_to_accounting_response ( res) . await ;
1209
1229
assert_eq ! ( accounting_response. balances. earners. len( ) , 0 ) ;
1210
1230
assert_eq ! ( accounting_response. balances. spenders. len( ) , 0 ) ;
1211
1231
}
@@ -1227,12 +1247,11 @@ mod test {
1227
1247
. await
1228
1248
. expect ( "should spend" ) ;
1229
1249
1230
- let res = get_accounting_for_channel ( build_request ( & channel_context) , & app)
1231
- . await
1232
- . expect ( "should get response" ) ;
1233
- assert_eq ! ( StatusCode :: OK , res. status( ) ) ;
1250
+ let res = get_accounting_for_channel_axum ( app. clone ( ) , Extension ( channel_context. clone ( ) ) )
1251
+ . await ;
1252
+ assert ! ( res. is_ok( ) ) ;
1234
1253
1235
- let accounting_response = res_to_accounting_response ( res) . await ;
1254
+ let accounting_response = res. unwrap ( ) ;
1236
1255
1237
1256
assert_eq ! ( balances, accounting_response. balances) ;
1238
1257
}
@@ -1263,15 +1282,11 @@ mod test {
1263
1282
. await
1264
1283
. expect ( "should spend" ) ;
1265
1284
1266
- let res = get_accounting_for_channel (
1267
- build_request ( & channel_context. clone ( ) . with ( second_channel) ) ,
1268
- & app,
1269
- )
1270
- . await
1271
- . expect ( "should get response" ) ;
1272
- assert_eq ! ( StatusCode :: OK , res. status( ) ) ;
1285
+ let res = get_accounting_for_channel_axum ( app. clone ( ) , Extension ( channel_context. clone ( ) ) )
1286
+ . await ;
1287
+ assert ! ( res. is_ok( ) ) ;
1273
1288
1274
- let accounting_response = res_to_accounting_response ( res) . await ;
1289
+ let accounting_response = res. unwrap ( ) ;
1275
1290
1276
1291
assert_eq ! ( balances, accounting_response. balances)
1277
1292
}
@@ -1289,7 +1304,8 @@ mod test {
1289
1304
. await
1290
1305
. expect ( "should spend" ) ;
1291
1306
1292
- let res = get_accounting_for_channel ( build_request ( & channel_context) , & app) . await ;
1307
+ let res = get_accounting_for_channel_axum ( app. clone ( ) , Extension ( channel_context. clone ( ) ) )
1308
+ . await ;
1293
1309
let expected = ResponseError :: FailedValidation (
1294
1310
"Earners sum is not equal to spenders sum for channel" . to_string ( ) ,
1295
1311
) ;
@@ -1320,13 +1336,6 @@ mod test {
1320
1336
. await
1321
1337
. expect ( "should insert channel" ) ;
1322
1338
1323
- let get_accounting_request = |channel_context : & ChainOf < Channel > | {
1324
- Request :: builder ( )
1325
- . extension ( channel_context. clone ( ) )
1326
- . body ( Body :: empty ( ) )
1327
- . expect ( "Should build Request" )
1328
- } ;
1329
-
1330
1339
// Calling with non existent accounting
1331
1340
let res = add_spender_leaf_axum (
1332
1341
app. clone ( ) ,
@@ -1336,12 +1345,11 @@ mod test {
1336
1345
. await ;
1337
1346
assert ! ( res. is_ok( ) ) ;
1338
1347
1339
- let res = get_accounting_for_channel ( get_accounting_request ( & channel_context) , & app)
1340
- . await
1341
- . expect ( "should get response" ) ;
1342
- assert_eq ! ( StatusCode :: OK , res. status( ) ) ;
1348
+ let res = get_accounting_for_channel_axum ( app. clone ( ) , Extension ( channel_context. clone ( ) ) )
1349
+ . await ;
1350
+ assert ! ( res. is_ok( ) ) ;
1343
1351
1344
- let accounting_response = res_to_accounting_response ( res) . await ;
1352
+ let accounting_response = res. unwrap ( ) ;
1345
1353
1346
1354
// Making sure a new entry has been created
1347
1355
assert_eq ! (
@@ -1364,12 +1372,11 @@ mod test {
1364
1372
. await
1365
1373
. expect ( "should spend" ) ;
1366
1374
1367
- let res = get_accounting_for_channel ( get_accounting_request ( & channel_context) , & app)
1368
- . await
1369
- . expect ( "should get response" ) ;
1370
- assert_eq ! ( StatusCode :: OK , res. status( ) ) ;
1375
+ let res = get_accounting_for_channel_axum ( app. clone ( ) , Extension ( channel_context. clone ( ) ) )
1376
+ . await ;
1377
+ assert ! ( res. is_ok( ) ) ;
1371
1378
1372
- let accounting_response = res_to_accounting_response ( res) . await ;
1379
+ let accounting_response = res. unwrap ( ) ;
1373
1380
1374
1381
assert_eq ! ( balances, accounting_response. balances) ;
1375
1382
@@ -1381,12 +1388,11 @@ mod test {
1381
1388
. await ;
1382
1389
assert ! ( res. is_ok( ) ) ;
1383
1390
1384
- let res = get_accounting_for_channel ( get_accounting_request ( & channel_context) , & app)
1385
- . await
1386
- . expect ( "should get response" ) ;
1387
- assert_eq ! ( StatusCode :: OK , res. status( ) ) ;
1391
+ let res = get_accounting_for_channel_axum ( app. clone ( ) , Extension ( channel_context. clone ( ) ) )
1392
+ . await ;
1393
+ assert ! ( res. is_ok( ) ) ;
1388
1394
1389
- let accounting_response = res_to_accounting_response ( res) . await ;
1395
+ let accounting_response = res. unwrap ( ) ;
1390
1396
1391
1397
// Balances shouldn't change
1392
1398
assert_eq ! (
0 commit comments