Skip to content

Commit e2a2f62

Browse files
authored
Merge pull request #560 from AmbireTech/seeding-improvements
Seeding improvements
2 parents 2c6a333 + 5c7fc3f commit e2a2f62

File tree

10 files changed

+198
-145
lines changed

10 files changed

+198
-145
lines changed

adapter/src/ethereum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use web3::signing::keccak256;
88
use crate::{Adapter, LockedState, UnlockedState};
99

1010
pub use {
11-
client::{Ethereum, Options},
11+
client::{ChainTransport, Ethereum, Options},
1212
error::Error,
1313
};
1414

adapter/src/ethereum/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub struct Ethereum<S = LockedWallet> {
3737
pub(crate) state: S,
3838
}
3939

40-
pub(crate) trait ChainTransport {
40+
pub trait ChainTransport {
4141
fn init_web3(&self) -> web3::Result<Web3<Http>>;
4242
}
4343

docs/config/ganache.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ admins = [
4747
'0x80690751969B234697e9059e04ed72195c3507fa'
4848
]
4949

50-
seed_db = true
51-
5250
[platform]
5351
# This should be changed for tests and use the wiremock url
5452
url = "https://platform.adex.network"

docs/config/prod.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ validators_whitelist = []
2828
# Galya (for analytics)
2929
admins = ['0x5d6A3F1AD7b124ecDFDf4841D9bB246eD5fBF04c']
3030

31-
seed_db = false
32-
3331
[platform]
3432
# This should be changed for tests and use the wiremock url
3533
url = "https://platform.adex.network"

primitives/src/config.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ pub struct Config {
182182
pub chains: HashMap<String, ChainInfo>,
183183
pub platform: PlatformConfig,
184184
pub limits: Limits,
185-
pub seed_db: bool,
186185
}
187186

188187
impl Config {

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/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ futures = "0.3"
2525
async-trait = "0.1"
2626
# Primitives
2727
primitives = { version = "0.2", path = "../primitives", features = ["postgres"] }
28-
adapter = { version = "0.2", path = "../adapter" }
28+
adapter = { version = "0.2", path = "../adapter", features = ["test-util"] }
2929
chrono = { version = "0.4", features = ["serde"] }
3030
# used for redis test pool
3131
dashmap = { version = "5", optional = true }
@@ -72,7 +72,6 @@ reqwest = { version = "0.11", features = ["json", "cookies"] }
7272

7373
[dev-dependencies]
7474
primitives = { version = "0.2", path = "../primitives", features = ["postgres", "test-util"] }
75-
adapter = { version = "0.2", path = "../adapter", features = ["test-util"] }
7675
pretty_assertions = "1"
7776

7877
# we only require `hyper` for `hyper::body::to_bytes` function

sentry/src/application.rs

Lines changed: 143 additions & 110 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
},
@@ -73,6 +64,9 @@ pub struct EnvConfig {
7364
/// Defaults to locally running Redis server: [`DEFAULT_REDIS_URL`]
7465
#[serde(deserialize_with = "redis_url", default = "default_redis_url")]
7566
pub redis_url: ConnectionInfo,
67+
/// Whether or not to seed the database in [`Environment::Development`].
68+
#[serde(default)]
69+
pub seed_db: bool,
7670
}
7771

7872
impl EnvConfig {
@@ -316,113 +310,152 @@ async fn shutdown_signal(logger: Logger, handle: Handle) {
316310
info!(&logger, "Received Ctrl+C signal. Shutting down..")
317311
}
318312

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");
369362

370-
assert!(result.is_ok());
363+
Ok(())
364+
}
371365

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?;
372402
Ok(())
373403
}
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();
395404

396-
async fn create_seed_campaign(
405+
pub async fn seed_ethereum(
397406
app: Application<Ethereum>,
398-
campaign: &ChainOf<Campaign>,
399407
) -> 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+
}
418453

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?;
419457
Ok(())
420458
}
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(())
426459
}
427460

428461
#[cfg(test)]

0 commit comments

Comments
 (0)