Skip to content

Commit 58d03e9

Browse files
committed
sentry - move seeding functions to own module
add todos for ethereum token set_balance
1 parent 0c5217e commit 58d03e9

File tree

3 files changed

+145
-113
lines changed

3 files changed

+145
-113
lines changed

primitives/src/test_util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ pub static DUMMY_IPFS: Lazy<[IPFS; 5]> = Lazy::new(|| {
300300
]
301301
});
302302

303-
/// List of test campaigns with keys `Campaign {Chain id} #{Number campaign}`
303+
/// List of test campaigns
304304
pub static CAMPAIGNS: Lazy<[ChainOf<Campaign>; 3]> = Lazy::new(|| {
305305
let campaign_1337_1 = {
306306
let ganache_chain_info = GANACHE_CONFIG.chains["Ganache #1337"].clone();

sentry/src/application.rs

Lines changed: 140 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use axum::{
99
http::{Method, StatusCode},
1010
middleware,
1111
routing::get,
12-
Extension, Json, Router,
12+
Extension, Router,
1313
};
1414
use axum_server::{tls_rustls::RustlsConfig, Handle};
1515
use once_cell::sync::Lazy;
@@ -19,23 +19,14 @@ use slog::{error, info, Logger};
1919
use tower::ServiceBuilder;
2020
use tower_http::cors::CorsLayer;
2121

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};
2824

2925
use crate::{
30-
db::{
31-
campaign::insert_campaign, insert_channel, spendable::insert_spendable, CampaignRemaining,
32-
DbPool,
33-
},
26+
db::{CampaignRemaining, DbPool},
3427
middleware::auth::authenticate,
3528
platform::PlatformApi,
3629
routes::{
37-
campaign::create_campaign,
38-
channel::{channel_dummy_deposit, ChannelDummyDeposit},
3930
get_cfg,
4031
routers::{analytics_router, campaigns_router, channels_router, units_for_slot_router},
4132
},
@@ -76,7 +67,6 @@ pub struct EnvConfig {
7667
/// Whether or not to seed the database in [`Environment::Development`].
7768
#[serde(default)]
7869
pub seed_db: bool,
79-
8070
}
8171

8272
impl EnvConfig {
@@ -320,113 +310,152 @@ async fn shutdown_signal(logger: Logger, handle: Handle) {
320310
info!(&logger, "Received Ctrl+C signal. Shutting down..")
321311
}
322312

323-
pub async fn seed_dummy(app: Application<Dummy>) -> Result<(), Box<dyn std::error::Error>> {
324-
// create campaign
325-
// Chain 1337
326-
let campaign_1 = CAMPAIGNS[0].clone();
327-
// Chain 1337
328-
let campaign_2 = CAMPAIGNS[1].clone();
329-
// Chain 1
330-
let campaign_3 = CAMPAIGNS[2].clone();
331-
332-
async fn create_seed_campaign(
333-
app: Application<Dummy>,
334-
campaign: &ChainOf<Campaign>,
335-
) -> Result<(), Box<dyn std::error::Error>> {
336-
let campaign_to_create = CreateCampaign::from_campaign(campaign.context.clone());
337-
let auth = Auth {
338-
era: 0,
339-
uid: ValidatorId::from(campaign_to_create.creator),
340-
chain: campaign.chain.clone(),
341-
};
342-
create_campaign(
343-
Json(campaign_to_create),
344-
Extension(auth),
345-
Extension(Arc::new(app)),
346-
)
347-
.await
348-
.expect("Should create seed campaigns");
349-
350-
Ok(())
351-
}
352-
353-
async fn dummy_deposit(
354-
app: Application<Dummy>,
355-
campaign: &ChainOf<Campaign>,
356-
) -> Result<(), Box<dyn std::error::Error>> {
357-
let channel = campaign.context.channel;
358-
let auth = Auth {
359-
era: 0,
360-
uid: ValidatorId::from(campaign.context.creator),
361-
chain: campaign.chain.clone(),
362-
};
363-
364-
let request = ChannelDummyDeposit {
365-
channel,
366-
deposit: Deposit {
367-
total: UnifiedNum::from_whole(1_000_000),
368-
},
369-
};
370-
371-
let result =
372-
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");
373362

374-
assert!(result.is_ok());
363+
Ok(())
364+
}
375365

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?;
376402
Ok(())
377403
}
378-
// chain 1337
379-
dummy_deposit(app.clone(), &campaign_1).await?;
380-
// chain 1337
381-
dummy_deposit(app.clone(), &campaign_2).await?;
382-
// chain 1
383-
dummy_deposit(app.clone(), &campaign_3).await?;
384-
385-
create_seed_campaign(app.clone(), &campaign_1).await?;
386-
create_seed_campaign(app.clone(), &campaign_2).await?;
387-
create_seed_campaign(app.clone(), &campaign_3).await?;
388-
Ok(())
389-
}
390-
391-
pub async fn seed_ethereum(app: Application<Ethereum>) -> Result<(), Box<dyn std::error::Error>> {
392-
// create campaign
393-
// Chain 1337
394-
let campaign_1 = CAMPAIGNS[0].clone();
395-
// Chain 1337
396-
let campaign_2 = CAMPAIGNS[1].clone();
397-
// Chain 1
398-
let campaign_3 = CAMPAIGNS[2].clone();
399404

400-
async fn create_seed_campaign(
405+
pub async fn seed_ethereum(
401406
app: Application<Ethereum>,
402-
campaign: &ChainOf<Campaign>,
403407
) -> Result<(), Box<dyn std::error::Error>> {
404-
let channel_context = ChainOf::of_channel(campaign);
405-
406-
let spendable = Spendable {
407-
spender: campaign.context.creator,
408-
channel: campaign.context.channel,
409-
deposit: Deposit {
410-
total: UnifiedNum::from_u64(10_000_000),
411-
},
412-
};
413-
insert_channel(&app.pool, &channel_context)
414-
.await
415-
.expect("Should insert channel of seed campaign");
416-
insert_campaign(&app.pool, &campaign.context)
417-
.await
418-
.expect("Should insert seed campaign");
419-
insert_spendable(app.pool.clone(), &spendable)
420-
.await
421-
.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+
}
422453

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?;
423457
Ok(())
424458
}
425-
426-
create_seed_campaign(app.clone(), &campaign_1).await?;
427-
create_seed_campaign(app.clone(), &campaign_2).await?;
428-
create_seed_campaign(app.clone(), &campaign_3).await?;
429-
Ok(())
430459
}
431460

432461
#[cfg(test)]

sentry/src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ use primitives::{
1717
ValidatorId,
1818
};
1919
use sentry::{
20-
application::{seed_dummy, seed_ethereum, EnableTls, EnvConfig},
20+
application::{
21+
seed::{seed_dummy, seed_ethereum},
22+
EnableTls, EnvConfig,
23+
},
2124
db::{postgres_connection, redis_connection, setup_migrations, CampaignRemaining, DbPool},
2225
platform::PlatformApi,
2326
Application,

0 commit comments

Comments
 (0)