@@ -64,6 +64,9 @@ pub struct EnvConfig {
6464 /// Defaults to locally running Redis server: [`DEFAULT_REDIS_URL`]
6565 #[ serde( deserialize_with = "redis_url" , default = "default_redis_url" ) ]
6666 pub redis_url : ConnectionInfo ,
67+ /// Whether or not to seed the database in [`Environment::Development`].
68+ #[ serde( default ) ]
69+ pub seed_db : bool ,
6770}
6871
6972impl EnvConfig {
@@ -307,6 +310,214 @@ async fn shutdown_signal(logger: Logger, handle: Handle) {
307310 info ! ( & logger, "Received Ctrl+C signal. Shutting down.." )
308311}
309312
313+ pub mod seed {
314+ use std:: sync:: Arc ;
315+
316+ use axum:: { Extension , Json } ;
317+
318+ use adapter:: {
319+ ethereum:: {
320+ test_util:: { Erc20Token , Outpace } ,
321+ ChainTransport ,
322+ } ,
323+ Dummy , Ethereum ,
324+ } ;
325+ use primitives:: {
326+ sentry:: campaign_create:: CreateCampaign ,
327+ spender:: Spendable ,
328+ test_util:: { ADVERTISER , ADVERTISER_2 , CAMPAIGNS , LEADER } ,
329+ unified_num:: FromWhole ,
330+ BigNum , Campaign , ChainOf , Deposit , UnifiedNum , ValidatorId ,
331+ } ;
332+
333+ use slog:: info;
334+
335+ use crate :: {
336+ db:: { insert_channel, spendable:: insert_spendable} ,
337+ routes:: {
338+ campaign:: create_campaign,
339+ channel:: { channel_dummy_deposit, ChannelDummyDeposit } ,
340+ } ,
341+ Application , Auth ,
342+ } ;
343+
344+ pub async fn seed_dummy ( app : Application < Dummy > ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
345+ info ! ( & app. logger, "Seeding sentry with Dummy adapter" ) ;
346+
347+ // create campaign
348+ // Chain 1337
349+ let campaign_1 = CAMPAIGNS [ 0 ] . clone ( ) ;
350+ // Chain 1337
351+ let campaign_2 = CAMPAIGNS [ 1 ] . clone ( ) ;
352+ // Chain 1
353+ let campaign_3 = CAMPAIGNS [ 2 ] . clone ( ) ;
354+
355+ async fn create_seed_campaign (
356+ app : Application < Dummy > ,
357+ campaign : & ChainOf < Campaign > ,
358+ ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
359+ let campaign_to_create = CreateCampaign :: from_campaign ( campaign. context . clone ( ) ) ;
360+ let auth = Auth {
361+ era : 0 ,
362+ uid : ValidatorId :: from ( campaign_to_create. creator ) ,
363+ chain : campaign. chain . clone ( ) ,
364+ } ;
365+ create_campaign (
366+ Json ( campaign_to_create) ,
367+ Extension ( auth) ,
368+ Extension ( Arc :: new ( app) ) ,
369+ )
370+ . await
371+ . expect ( "Should create seed campaigns" ) ;
372+
373+ Ok ( ( ) )
374+ }
375+
376+ async fn dummy_deposit (
377+ app : Application < Dummy > ,
378+ campaign : & ChainOf < Campaign > ,
379+ ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
380+ let channel = campaign. context . channel ;
381+ let auth = Auth {
382+ era : 0 ,
383+ uid : ValidatorId :: from ( campaign. context . creator ) ,
384+ chain : campaign. chain . clone ( ) ,
385+ } ;
386+
387+ let request = ChannelDummyDeposit {
388+ channel,
389+ deposit : Deposit {
390+ total : UnifiedNum :: from_whole ( 1_000_000 ) ,
391+ } ,
392+ } ;
393+
394+ let result =
395+ channel_dummy_deposit ( Extension ( Arc :: new ( app) ) , Extension ( auth) , Json ( request) )
396+ . await ;
397+
398+ assert ! ( result. is_ok( ) ) ;
399+
400+ Ok ( ( ) )
401+ }
402+ // chain 1337
403+ dummy_deposit ( app. clone ( ) , & campaign_1) . await ?;
404+ // chain 1337
405+ dummy_deposit ( app. clone ( ) , & campaign_2) . await ?;
406+ // chain 1
407+ dummy_deposit ( app. clone ( ) , & campaign_3) . await ?;
408+
409+ create_seed_campaign ( app. clone ( ) , & campaign_1) . await ?;
410+ create_seed_campaign ( app. clone ( ) , & campaign_2) . await ?;
411+ create_seed_campaign ( app. clone ( ) , & campaign_3) . await ?;
412+ Ok ( ( ) )
413+ }
414+
415+ pub async fn seed_ethereum (
416+ app : Application < Ethereum > ,
417+ ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
418+ info ! ( & app. logger, "Seeding sentry with Ethereum adapter" ) ;
419+ // Chain 1337
420+ let campaign_1 = CAMPAIGNS [ 0 ] . clone ( ) ;
421+ // Chain 1337
422+ let campaign_2 = CAMPAIGNS [ 1 ] . clone ( ) ;
423+ // Chain 1
424+ let campaign_3 = CAMPAIGNS [ 2 ] . clone ( ) ;
425+
426+ let web3_chain_1337 = campaign_1. chain . init_web3 ( ) ?;
427+ let token_1337 = Erc20Token :: new ( & web3_chain_1337, campaign_1. token . clone ( ) ) ;
428+ let outpace_1337 = Outpace :: new ( & web3_chain_1337, campaign_1. chain . outpace ) ;
429+ let web3_chain_1 = campaign_3. chain . init_web3 ( ) ?;
430+ let token_1 = Erc20Token :: new ( & web3_chain_1, campaign_3. token . clone ( ) ) ;
431+ let outpace_1 = Outpace :: new ( & web3_chain_1, campaign_3. chain . outpace ) ;
432+
433+ token_1337
434+ . set_balance (
435+ LEADER . to_bytes ( ) ,
436+ ADVERTISER . to_bytes ( ) ,
437+ & BigNum :: with_precision ( 3_000_000 , token_1337. info . precision . into ( ) ) ,
438+ )
439+ . await
440+ . expect ( "Failed to set balance" ) ;
441+ outpace_1337
442+ . deposit (
443+ & campaign_1. context . channel ,
444+ ADVERTISER . to_bytes ( ) ,
445+ & BigNum :: with_precision ( 1_000_000 , token_1337. info . precision . into ( ) ) ,
446+ )
447+ . await
448+ . expect ( "Should deposit funds" ) ;
449+ outpace_1337
450+ . deposit (
451+ & campaign_2. context . channel ,
452+ ADVERTISER . to_bytes ( ) ,
453+ & BigNum :: with_precision ( 1_000_000 , token_1337. info . precision . into ( ) ) ,
454+ )
455+ . await
456+ . expect ( "Should deposit funds" ) ;
457+
458+ token_1
459+ . set_balance (
460+ LEADER . to_bytes ( ) ,
461+ ADVERTISER_2 . to_bytes ( ) ,
462+ & BigNum :: with_precision ( 2_000_000 , token_1. info . precision . into ( ) ) ,
463+ )
464+ . await
465+ . expect ( "Failed to set balance" ) ;
466+
467+ outpace_1
468+ . deposit (
469+ & campaign_3. context . channel ,
470+ ADVERTISER_2 . to_bytes ( ) ,
471+ & BigNum :: with_precision ( 1_000_000 , token_1. info . precision . into ( ) ) ,
472+ )
473+ . await
474+ . expect ( "Should deposit funds" ) ;
475+
476+ async fn create_seed_campaign (
477+ app : Application < Ethereum > ,
478+ campaign : & ChainOf < Campaign > ,
479+ ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
480+ let channel_context = ChainOf :: of_channel ( campaign) ;
481+ let campaign_to_create = CreateCampaign :: from_campaign ( campaign. context . clone ( ) ) ;
482+
483+ let auth = Auth {
484+ era : 0 ,
485+ uid : ValidatorId :: from ( campaign. context . creator ) ,
486+ chain : campaign. chain . clone ( ) ,
487+ } ;
488+
489+ let spendable = Spendable {
490+ spender : campaign. context . creator ,
491+ channel : campaign. context . channel ,
492+ deposit : Deposit {
493+ total : UnifiedNum :: from_whole ( 10_000 ) ,
494+ } ,
495+ } ;
496+ insert_channel ( & app. pool , & channel_context)
497+ . await
498+ . expect ( "Should insert channel of seed campaign" ) ;
499+ insert_spendable ( app. pool . clone ( ) , & spendable)
500+ . await
501+ . expect ( "Should insert spendable for campaign creator" ) ;
502+
503+ create_campaign (
504+ Json ( campaign_to_create) ,
505+ Extension ( auth) ,
506+ Extension ( Arc :: new ( app. clone ( ) ) ) ,
507+ )
508+ . await
509+ . expect ( "should create campaign" ) ;
510+
511+ Ok ( ( ) )
512+ }
513+
514+ create_seed_campaign ( app. clone ( ) , & campaign_1) . await ?;
515+ create_seed_campaign ( app. clone ( ) , & campaign_2) . await ?;
516+ create_seed_campaign ( app. clone ( ) , & campaign_3) . await ?;
517+ Ok ( ( ) )
518+ }
519+ }
520+
310521#[ cfg( test) ]
311522mod test {
312523 use serde_json:: json;
0 commit comments