@@ -5,17 +5,17 @@ use crate::db::{
55 } ,
66 get_channel_by_id, insert_channel, insert_validator_messages, list_channels,
77 spendable:: { fetch_spendable, update_spendable} ,
8- DbPool , PoolError
8+ DbPool , PoolError ,
99} ;
1010use crate :: { success_response, Application , Auth , ResponseError , RouteParams } ;
1111use futures:: future:: try_join_all;
1212use hex:: FromHex ;
1313use hyper:: { Body , Request , Response } ;
1414use primitives:: {
1515 adapter:: Adapter ,
16+ balances:: UncheckedState ,
1617 channel_v5:: Channel as ChannelV5 ,
1718 config:: TokenInfo ,
18- balances:: UncheckedState ,
1919 sentry:: {
2020 channel_list:: { ChannelListQuery , LastApprovedQuery } ,
2121 LastApproved , LastApprovedResponse , SpenderResponse , SuccessResponse ,
@@ -225,7 +225,7 @@ pub async fn create_validator_messages<A: Adapter + 'static>(
225225 }
226226}
227227
228- async fn create_spendable_document (
228+ async fn create_or_update_spendable_document (
229229 adapter : & impl Adapter ,
230230 token_info : & TokenInfo ,
231231 pool : DbPool ,
@@ -300,7 +300,7 @@ pub async fn get_spender_limits<A: Adapter + 'static>(
300300 let latest_spendable = match latest_spendable {
301301 Some ( spendable) => spendable,
302302 None => {
303- create_spendable_document (
303+ create_or_update_spendable_document (
304304 & app. adapter ,
305305 token_info,
306306 app. pool . clone ( ) ,
@@ -345,54 +345,42 @@ pub async fn get_spender_limits<A: Adapter + 'static>(
345345#[ cfg( test) ]
346346mod test {
347347 use super :: * ;
348- use crate :: db:: tests_postgres:: { setup_test_migrations, DATABASE_POOL } ;
349- use adapter:: DummyAdapter ;
348+ use crate :: test_util:: setup_dummy_app;
350349 use primitives:: {
351- adapter:: { Deposit , DummyAdapterOptions } ,
352- config:: configuration,
353- util:: tests:: prep_db:: { ADDRESSES , DUMMY_CAMPAIGN , IDS } ,
350+ adapter:: Deposit ,
351+ util:: tests:: prep_db:: { ADDRESSES , DUMMY_CAMPAIGN } ,
354352 BigNum ,
355353 } ;
356354
357355 #[ tokio:: test]
358356 async fn create_and_fetch_spendable ( ) {
359- let config = configuration ( "development" , None ) . expect ( "Should get Config" ) ;
360- let adapter = DummyAdapter :: init (
361- DummyAdapterOptions {
362- dummy_identity : IDS [ "leader" ] ,
363- dummy_auth : Default :: default ( ) ,
364- dummy_auth_tokens : Default :: default ( ) ,
365- } ,
366- & config,
367- ) ;
368- let database = DATABASE_POOL . get ( ) . await . expect ( "Should get a DB pool" ) ;
369- setup_test_migrations ( database. pool . clone ( ) )
370- . await
371- . expect ( "Migrations should succeed" ) ;
357+ let app = setup_dummy_app ( ) . await ;
372358
373359 let channel = DUMMY_CAMPAIGN . channel . clone ( ) ;
374360
375- let token_info = config
361+ let token_info = app
362+ . config
376363 . token_address_whitelist
377364 . get ( & channel. token )
378365 . expect ( "should retrieve address" ) ;
379366 let precision: u8 = token_info. precision . into ( ) ;
380367 let deposit = Deposit {
381- total : BigNum :: from ( 1000000000 ) , // 100 USDT
382- still_on_create2 : BigNum :: from ( 1000000 ) , // 1 USDT
368+ total : BigNum :: from_str ( "100000000000000000000" ) . expect ( "should convert" ) , // 100 DAI
369+ still_on_create2 : BigNum :: from_str ( "1000000000000000000" ) . expect ( "should convert" ) , // 1 DAI
383370 } ;
384- adapter. add_deposit_call ( channel. id ( ) , ADDRESSES [ "creator" ] , deposit. clone ( ) ) ;
371+ app. adapter
372+ . add_deposit_call ( channel. id ( ) , ADDRESSES [ "creator" ] , deposit. clone ( ) ) ;
385373 // Making sure spendable does not yet exist
386- let spendable = fetch_spendable ( database . clone ( ) , & ADDRESSES [ "creator" ] , & channel. id ( ) )
374+ let spendable = fetch_spendable ( app . pool . clone ( ) , & ADDRESSES [ "creator" ] , & channel. id ( ) )
387375 . await
388376 . expect ( "should return None" ) ;
389377 assert ! ( spendable. is_none( ) ) ;
390378
391- // Call create_spendable
392- let new_spendable = create_spendable_document (
393- & adapter,
394- & token_info,
395- database . clone ( ) ,
379+ // Call create_or_update_spendable
380+ let new_spendable = create_or_update_spendable_document (
381+ & app . adapter ,
382+ token_info,
383+ app . pool . clone ( ) ,
396384 & channel,
397385 ADDRESSES [ "creator" ] ,
398386 )
@@ -413,23 +401,23 @@ mod test {
413401 assert_eq ! ( new_spendable. spender, ADDRESSES [ "creator" ] ) ;
414402
415403 // Make sure spendable NOW exists
416- let spendable = fetch_spendable ( database . clone ( ) , & ADDRESSES [ "creator" ] , & channel. id ( ) )
404+ let spendable = fetch_spendable ( app . pool . clone ( ) , & ADDRESSES [ "creator" ] , & channel. id ( ) )
417405 . await
418406 . expect ( "should return a spendable" ) ;
419407 assert ! ( spendable. is_some( ) ) ;
420408
421- // Updating our deposit by doubling it
422409 let updated_deposit = Deposit {
423- total : BigNum :: from ( 110000000 ) , // 101 USDT
424- still_on_create2 : BigNum :: from ( 1100000 ) , // 1.1 USDT
410+ total : BigNum :: from_str ( "110000000000000000000" ) . expect ( "should convert" ) , // 110 DAI
411+ still_on_create2 : BigNum :: from_str ( "1100000000000000000" ) . expect ( "should convert" ) , // 1.1 DAI
425412 } ;
426413
427- adapter. add_deposit_call ( channel. id ( ) , ADDRESSES [ "creator" ] , updated_deposit. clone ( ) ) ;
414+ app. adapter
415+ . add_deposit_call ( channel. id ( ) , ADDRESSES [ "creator" ] , updated_deposit. clone ( ) ) ;
428416
429- let updated_spendable = create_spendable_document (
430- & adapter,
431- & token_info,
432- database . clone ( ) ,
417+ let updated_spendable = create_or_update_spendable_document (
418+ & app . adapter ,
419+ token_info,
420+ app . pool . clone ( ) ,
433421 & channel,
434422 ADDRESSES [ "creator" ] ,
435423 )
0 commit comments