@@ -9,7 +9,7 @@ use axum::{
9
9
http:: { Method , StatusCode } ,
10
10
middleware,
11
11
routing:: get,
12
- Extension , Json , Router ,
12
+ Extension , Router ,
13
13
} ;
14
14
use axum_server:: { tls_rustls:: RustlsConfig , Handle } ;
15
15
use once_cell:: sync:: Lazy ;
@@ -19,23 +19,14 @@ use slog::{error, info, Logger};
19
19
use tower:: ServiceBuilder ;
20
20
use tower_http:: cors:: CorsLayer ;
21
21
22
- use adapter:: { client:: Locked , Adapter , Dummy , Ethereum } ;
23
- use primitives:: {
24
- config:: Environment , sentry:: campaign_create:: CreateCampaign , spender:: Spendable ,
25
- test_util:: CAMPAIGNS , unified_num:: FromWhole , Campaign , ChainOf , Deposit , UnifiedNum ,
26
- ValidatorId ,
27
- } ;
22
+ use adapter:: { client:: Locked , Adapter } ;
23
+ use primitives:: { config:: Environment , ValidatorId } ;
28
24
29
25
use crate :: {
30
- db:: {
31
- campaign:: insert_campaign, insert_channel, spendable:: insert_spendable, CampaignRemaining ,
32
- DbPool ,
33
- } ,
26
+ db:: { CampaignRemaining , DbPool } ,
34
27
middleware:: auth:: authenticate,
35
28
platform:: PlatformApi ,
36
29
routes:: {
37
- campaign:: create_campaign,
38
- channel:: { channel_dummy_deposit, ChannelDummyDeposit } ,
39
30
get_cfg,
40
31
routers:: { analytics_router, campaigns_router, channels_router, units_for_slot_router} ,
41
32
} ,
@@ -73,6 +64,9 @@ pub struct EnvConfig {
73
64
/// Defaults to locally running Redis server: [`DEFAULT_REDIS_URL`]
74
65
#[ serde( deserialize_with = "redis_url" , default = "default_redis_url" ) ]
75
66
pub redis_url : ConnectionInfo ,
67
+ /// Whether or not to seed the database in [`Environment::Development`].
68
+ #[ serde( default ) ]
69
+ pub seed_db : bool ,
76
70
}
77
71
78
72
impl EnvConfig {
@@ -316,113 +310,152 @@ async fn shutdown_signal(logger: Logger, handle: Handle) {
316
310
info ! ( & logger, "Received Ctrl+C signal. Shutting down.." )
317
311
}
318
312
319
- pub async fn seed_dummy ( app : Application < Dummy > ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
320
- // create campaign
321
- // Chain 1337
322
- let campaign_1 = CAMPAIGNS [ 0 ] . clone ( ) ;
323
- // Chain 1337
324
- let campaign_2 = CAMPAIGNS [ 1 ] . clone ( ) ;
325
- // Chain 1
326
- let campaign_3 = CAMPAIGNS [ 2 ] . clone ( ) ;
327
-
328
- async fn create_seed_campaign (
329
- app : Application < Dummy > ,
330
- campaign : & ChainOf < Campaign > ,
331
- ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
332
- let campaign_to_create = CreateCampaign :: from_campaign ( campaign. context . clone ( ) ) ;
333
- let auth = Auth {
334
- era : 0 ,
335
- uid : ValidatorId :: from ( campaign_to_create. creator ) ,
336
- chain : campaign. chain . clone ( ) ,
337
- } ;
338
- create_campaign (
339
- Json ( campaign_to_create) ,
340
- Extension ( auth) ,
341
- Extension ( Arc :: new ( app) ) ,
342
- )
343
- . await
344
- . expect ( "Should create seed campaigns" ) ;
345
-
346
- Ok ( ( ) )
347
- }
348
-
349
- async fn dummy_deposit (
350
- app : Application < Dummy > ,
351
- campaign : & ChainOf < Campaign > ,
352
- ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
353
- let channel = campaign. context . channel ;
354
- let auth = Auth {
355
- era : 0 ,
356
- uid : ValidatorId :: from ( campaign. context . creator ) ,
357
- chain : campaign. chain . clone ( ) ,
358
- } ;
359
-
360
- let request = ChannelDummyDeposit {
361
- channel,
362
- deposit : Deposit {
363
- total : UnifiedNum :: from_whole ( 1_000_000 ) ,
364
- } ,
365
- } ;
366
-
367
- let result =
368
- channel_dummy_deposit ( Extension ( Arc :: new ( app) ) , Extension ( auth) , Json ( request) ) . await ;
313
+ pub mod seed {
314
+ use std:: sync:: Arc ;
315
+
316
+ use axum:: { Extension , Json } ;
317
+
318
+ use adapter:: {
319
+ ethereum:: { test_util:: Erc20Token , ChainTransport } ,
320
+ Dummy , Ethereum ,
321
+ } ;
322
+ use primitives:: {
323
+ sentry:: campaign_create:: CreateCampaign , spender:: Spendable , test_util:: CAMPAIGNS ,
324
+ unified_num:: FromWhole , Campaign , ChainOf , Deposit , UnifiedNum , ValidatorId ,
325
+ } ;
326
+
327
+ use crate :: {
328
+ db:: { campaign:: insert_campaign, insert_channel, spendable:: insert_spendable} ,
329
+ routes:: {
330
+ campaign:: create_campaign,
331
+ channel:: { channel_dummy_deposit, ChannelDummyDeposit } ,
332
+ } ,
333
+ Application , Auth ,
334
+ } ;
335
+
336
+ pub async fn seed_dummy ( app : Application < Dummy > ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
337
+ // create campaign
338
+ // Chain 1337
339
+ let campaign_1 = CAMPAIGNS [ 0 ] . clone ( ) ;
340
+ // Chain 1337
341
+ let campaign_2 = CAMPAIGNS [ 1 ] . clone ( ) ;
342
+ // Chain 1
343
+ let campaign_3 = CAMPAIGNS [ 2 ] . clone ( ) ;
344
+
345
+ async fn create_seed_campaign (
346
+ app : Application < Dummy > ,
347
+ campaign : & ChainOf < Campaign > ,
348
+ ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
349
+ let campaign_to_create = CreateCampaign :: from_campaign ( campaign. context . clone ( ) ) ;
350
+ let auth = Auth {
351
+ era : 0 ,
352
+ uid : ValidatorId :: from ( campaign_to_create. creator ) ,
353
+ chain : campaign. chain . clone ( ) ,
354
+ } ;
355
+ create_campaign (
356
+ Json ( campaign_to_create) ,
357
+ Extension ( auth) ,
358
+ Extension ( Arc :: new ( app) ) ,
359
+ )
360
+ . await
361
+ . expect ( "Should create seed campaigns" ) ;
369
362
370
- assert ! ( result. is_ok( ) ) ;
363
+ Ok ( ( ) )
364
+ }
371
365
366
+ async fn dummy_deposit (
367
+ app : Application < Dummy > ,
368
+ campaign : & ChainOf < Campaign > ,
369
+ ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
370
+ let channel = campaign. context . channel ;
371
+ let auth = Auth {
372
+ era : 0 ,
373
+ uid : ValidatorId :: from ( campaign. context . creator ) ,
374
+ chain : campaign. chain . clone ( ) ,
375
+ } ;
376
+
377
+ let request = ChannelDummyDeposit {
378
+ channel,
379
+ deposit : Deposit {
380
+ total : UnifiedNum :: from_whole ( 1_000_000 ) ,
381
+ } ,
382
+ } ;
383
+
384
+ let result =
385
+ channel_dummy_deposit ( Extension ( Arc :: new ( app) ) , Extension ( auth) , Json ( request) )
386
+ . await ;
387
+
388
+ assert ! ( result. is_ok( ) ) ;
389
+
390
+ Ok ( ( ) )
391
+ }
392
+ // chain 1337
393
+ dummy_deposit ( app. clone ( ) , & campaign_1) . await ?;
394
+ // chain 1337
395
+ dummy_deposit ( app. clone ( ) , & campaign_2) . await ?;
396
+ // chain 1
397
+ dummy_deposit ( app. clone ( ) , & campaign_3) . await ?;
398
+
399
+ create_seed_campaign ( app. clone ( ) , & campaign_1) . await ?;
400
+ create_seed_campaign ( app. clone ( ) , & campaign_2) . await ?;
401
+ create_seed_campaign ( app. clone ( ) , & campaign_3) . await ?;
372
402
Ok ( ( ) )
373
403
}
374
- // chain 1337
375
- dummy_deposit ( app. clone ( ) , & campaign_1) . await ?;
376
- // chain 1337
377
- dummy_deposit ( app. clone ( ) , & campaign_2) . await ?;
378
- // chain 1
379
- dummy_deposit ( app. clone ( ) , & campaign_3) . await ?;
380
-
381
- create_seed_campaign ( app. clone ( ) , & campaign_1) . await ?;
382
- create_seed_campaign ( app. clone ( ) , & campaign_2) . await ?;
383
- create_seed_campaign ( app. clone ( ) , & campaign_3) . await ?;
384
- Ok ( ( ) )
385
- }
386
-
387
- pub async fn seed_ethereum ( app : Application < Ethereum > ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
388
- // create campaign
389
- // Chain 1337
390
- let campaign_1 = CAMPAIGNS [ 0 ] . clone ( ) ;
391
- // Chain 1337
392
- let campaign_2 = CAMPAIGNS [ 1 ] . clone ( ) ;
393
- // Chain 1
394
- let campaign_3 = CAMPAIGNS [ 2 ] . clone ( ) ;
395
404
396
- async fn create_seed_campaign (
405
+ pub async fn seed_ethereum (
397
406
app : Application < Ethereum > ,
398
- campaign : & ChainOf < Campaign > ,
399
407
) -> Result < ( ) , Box < dyn std:: error:: Error > > {
400
- let channel_context = ChainOf :: of_channel ( campaign) ;
401
-
402
- let spendable = Spendable {
403
- spender : campaign. context . creator ,
404
- channel : campaign. context . channel ,
405
- deposit : Deposit {
406
- total : UnifiedNum :: from_u64 ( 10_000_000 ) ,
407
- } ,
408
- } ;
409
- insert_channel ( & app. pool , & channel_context)
410
- . await
411
- . expect ( "Should insert channel of seed campaign" ) ;
412
- insert_campaign ( & app. pool , & campaign. context )
413
- . await
414
- . expect ( "Should insert seed campaign" ) ;
415
- insert_spendable ( app. pool . clone ( ) , & spendable)
416
- . await
417
- . expect ( "Should insert spendable for campaign creator" ) ;
408
+ // Chain 1337
409
+ let campaign_1 = CAMPAIGNS [ 0 ] . clone ( ) ;
410
+ // Chain 1337
411
+ let campaign_2 = CAMPAIGNS [ 1 ] . clone ( ) ;
412
+ // Chain 1
413
+ let campaign_3 = CAMPAIGNS [ 2 ] . clone ( ) ;
414
+
415
+ let web3_chain_1337 = campaign_1. chain . init_web3 ( ) ?;
416
+ let token_1337 = Erc20Token :: new ( & web3_chain_1337, campaign_1. token . clone ( ) ) ;
417
+ let web3_chain_1 = campaign_3. chain . init_web3 ( ) ?;
418
+ let token_1 = Erc20Token :: new ( & web3_chain_1, campaign_3. token . clone ( ) ) ;
419
+
420
+ // TODO: Call set_balance() and set balance for ADVERTISER & ADVERTISER_2
421
+ // large enough for the campaigns + extra on top
422
+
423
+ // token_1337.set_balance(from, address, amount)
424
+ // token_1.set_balance(from, address, amount)
425
+
426
+ async fn create_seed_campaign (
427
+ app : Application < Ethereum > ,
428
+ campaign : & ChainOf < Campaign > ,
429
+ ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
430
+ let channel_context = ChainOf :: of_channel ( campaign) ;
431
+
432
+ // TODO: call create_campaign()
433
+
434
+ let spendable = Spendable {
435
+ spender : campaign. context . creator ,
436
+ channel : campaign. context . channel ,
437
+ deposit : Deposit {
438
+ total : UnifiedNum :: from_u64 ( 10_000_000 ) ,
439
+ } ,
440
+ } ;
441
+ insert_channel ( & app. pool , & channel_context)
442
+ . await
443
+ . expect ( "Should insert channel of seed campaign" ) ;
444
+ insert_campaign ( & app. pool , & campaign. context )
445
+ . await
446
+ . expect ( "Should insert seed campaign" ) ;
447
+ insert_spendable ( app. pool . clone ( ) , & spendable)
448
+ . await
449
+ . expect ( "Should insert spendable for campaign creator" ) ;
450
+
451
+ Ok ( ( ) )
452
+ }
418
453
454
+ create_seed_campaign ( app. clone ( ) , & campaign_1) . await ?;
455
+ create_seed_campaign ( app. clone ( ) , & campaign_2) . await ?;
456
+ create_seed_campaign ( app. clone ( ) , & campaign_3) . await ?;
419
457
Ok ( ( ) )
420
458
}
421
-
422
- create_seed_campaign ( app. clone ( ) , & campaign_1) . await ?;
423
- create_seed_campaign ( app. clone ( ) , & campaign_2) . await ?;
424
- create_seed_campaign ( app. clone ( ) , & campaign_3) . await ?;
425
- Ok ( ( ) )
426
459
}
427
460
428
461
#[ cfg( test) ]
0 commit comments