Skip to content

Commit a2207d5

Browse files
committed
fixed seeding + returned seed.rs binary + fmt
1 parent 9de1638 commit a2207d5

File tree

2 files changed

+153
-8
lines changed

2 files changed

+153
-8
lines changed

sentry/bin/seed.rs

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
use adapter::{client::Unlocked, dummy::Options, Adapter, Dummy, UnlockedState};
2+
use primitives::{
3+
config::GANACHE_CONFIG,
4+
sentry::SuccessResponse,
5+
test_util::{ADVERTISER, ADVERTISER_2, CAMPAIGNS, DUMMY_AUTH, IDS, LEADER},
6+
unified_num::FromWhole,
7+
util::ApiUrl,
8+
Campaign, ChainOf, Channel, Deposit, UnifiedNum, ValidatorId,
9+
};
10+
use reqwest::Client;
11+
use sentry::routes::channel::ChannelDummyDeposit;
12+
13+
#[tokio::main]
14+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
15+
let advertiser_adapter = Adapter::with_unlocked(Dummy::init(Options {
16+
dummy_identity: IDS[&ADVERTISER],
17+
dummy_auth_tokens: DUMMY_AUTH.clone(),
18+
dummy_chains: GANACHE_CONFIG.chains.values().cloned().collect(),
19+
}));
20+
21+
let advertiser2_adapter = Adapter::with_unlocked(Dummy::init(Options {
22+
dummy_identity: IDS[&ADVERTISER_2],
23+
dummy_auth_tokens: DUMMY_AUTH.clone(),
24+
dummy_chains: GANACHE_CONFIG.chains.values().cloned().collect(),
25+
}));
26+
27+
let client = reqwest::Client::new();
28+
// add deposit for campaigns
29+
let intended_for = (
30+
"http://127.0.0.1:8005".parse::<ApiUrl>().unwrap(),
31+
IDS[&LEADER],
32+
);
33+
34+
// create campaign
35+
// Chain 1337
36+
let campaign_1 = CAMPAIGNS[0].clone();
37+
// Chain 1337
38+
let campaign_2 = CAMPAIGNS[1].clone();
39+
// Chain 1
40+
let campaign_3 = CAMPAIGNS[2].clone();
41+
// chain 1337
42+
dummy_deposit(
43+
&client,
44+
&advertiser_adapter,
45+
&campaign_1.of_channel(),
46+
&intended_for,
47+
)
48+
.await?;
49+
// chain 1337
50+
dummy_deposit(
51+
&client,
52+
&advertiser_adapter,
53+
&campaign_2.of_channel(),
54+
&intended_for,
55+
)
56+
.await?;
57+
// chain 1
58+
dummy_deposit(
59+
&client,
60+
&advertiser2_adapter,
61+
&campaign_3.of_channel(),
62+
&intended_for,
63+
)
64+
.await?;
65+
66+
create_campaign(&client, &advertiser_adapter, &campaign_1, &intended_for).await?;
67+
create_campaign(&client, &advertiser_adapter, &campaign_2, &intended_for).await?;
68+
create_campaign(&client, &advertiser2_adapter, &campaign_3, &intended_for).await?;
69+
70+
Ok(())
71+
}
72+
73+
async fn create_campaign(
74+
client: &Client,
75+
adapter: &Adapter<Dummy, UnlockedState>,
76+
campaign: &ChainOf<Campaign>,
77+
(sentry_url, intended_for): &(ApiUrl, ValidatorId),
78+
) -> Result<(), Box<dyn std::error::Error>> {
79+
let auth_token = adapter.get_auth(campaign.chain.chain_id, *intended_for)?;
80+
81+
let _result = client
82+
.post(sentry_url.join("/v5/campaign")?)
83+
.bearer_auth(auth_token)
84+
.json(&campaign.context)
85+
.send()
86+
.await?
87+
.error_for_status()?
88+
.json::<Campaign>()
89+
.await?;
90+
91+
Ok(())
92+
}
93+
94+
async fn dummy_deposit(
95+
client: &Client,
96+
adapter: &Adapter<Dummy, UnlockedState>,
97+
channel: &ChainOf<Channel>,
98+
(sentry_url, for_validator): &(ApiUrl, ValidatorId),
99+
) -> Result<(), Box<dyn std::error::Error>> {
100+
let auth_token = adapter.get_auth(channel.chain.chain_id, *for_validator)?;
101+
102+
let request = ChannelDummyDeposit {
103+
channel: channel.context,
104+
deposit: Deposit {
105+
total: UnifiedNum::from_whole(1_000_000),
106+
},
107+
};
108+
109+
let result = client
110+
.post(sentry_url.join("/v5/channel/dummy-deposit")?)
111+
.bearer_auth(auth_token)
112+
.json(&request)
113+
.send()
114+
.await?
115+
.error_for_status()?
116+
.json::<SuccessResponse>()
117+
.await?;
118+
119+
assert!(result.success);
120+
121+
Ok(())
122+
}

sentry/src/application.rs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,16 @@ pub mod seed {
316316
use axum::{Extension, Json};
317317

318318
use adapter::{
319-
ethereum::{test_util::{Erc20Token, Outpace}, ChainTransport},
319+
ethereum::{
320+
test_util::{Erc20Token, Outpace},
321+
ChainTransport,
322+
},
320323
Dummy, Ethereum,
321324
};
322325
use primitives::{
323326
sentry::campaign_create::CreateCampaign,
324327
spender::Spendable,
325-
test_util::{ADVERTISER, ADVERTISER_2, CAMPAIGNS, LEADER, FOLLOWER},
328+
test_util::{ADVERTISER, ADVERTISER_2, CAMPAIGNS, LEADER},
326329
unified_num::FromWhole,
327330
BigNum, Campaign, ChainOf, Deposit, UnifiedNum, ValidatorId,
328331
};
@@ -420,28 +423,48 @@ pub mod seed {
420423
let outpace_1337 = Outpace::new(&web3_chain_1337, campaign_1.chain.outpace);
421424
let web3_chain_1 = campaign_3.chain.init_web3()?;
422425
let token_1 = Erc20Token::new(&web3_chain_1, campaign_3.token.clone());
423-
let outpace_1 = Outpace::new(&web3_chain_1, campaign_1.chain.outpace);
426+
let outpace_1 = Outpace::new(&web3_chain_1, campaign_3.chain.outpace);
424427

425428
token_1337
426-
.set_balance(LEADER.to_bytes(), ADVERTISER.to_bytes(), &BigNum::with_precision(3_000_000, token_1337.info.precision.into()))
429+
.set_balance(
430+
LEADER.to_bytes(),
431+
ADVERTISER.to_bytes(),
432+
&BigNum::with_precision(3_000_000, token_1337.info.precision.into()),
433+
)
427434
.await
428435
.expect("Failed to set balance");
429436
outpace_1337
430-
.deposit(&campaign_1.context.channel, ADVERTISER.to_bytes(), &BigNum::with_precision(1_000_000, token_1337.info.precision.into()))
437+
.deposit(
438+
&campaign_1.context.channel,
439+
ADVERTISER.to_bytes(),
440+
&BigNum::with_precision(1_000_000, token_1337.info.precision.into()),
441+
)
431442
.await
432443
.expect("Should deposit funds");
433444
outpace_1337
434-
.deposit(&campaign_2.context.channel, ADVERTISER.to_bytes(), &BigNum::with_precision(1_000_000, token_1337.info.precision.into()))
445+
.deposit(
446+
&campaign_2.context.channel,
447+
ADVERTISER.to_bytes(),
448+
&BigNum::with_precision(1_000_000, token_1337.info.precision.into()),
449+
)
435450
.await
436451
.expect("Should deposit funds");
437452

438453
token_1
439-
.set_balance(LEADER.to_bytes(), ADVERTISER_2.to_bytes(), &BigNum::with_precision(2_000_000, token_1.info.precision.into()))
454+
.set_balance(
455+
LEADER.to_bytes(),
456+
ADVERTISER_2.to_bytes(),
457+
&BigNum::with_precision(2_000_000, token_1.info.precision.into()),
458+
)
440459
.await
441460
.expect("Failed to set balance");
442461

443462
outpace_1
444-
.deposit(&campaign_3.context.channel, ADVERTISER_2.to_bytes(), &BigNum::with_precision(1_000_000, token_1.info.precision.into()))
463+
.deposit(
464+
&campaign_3.context.channel,
465+
ADVERTISER_2.to_bytes(),
466+
&BigNum::with_precision(1_000_000, token_1.info.precision.into()),
467+
)
445468
.await
446469
.expect("Should deposit funds");
447470

0 commit comments

Comments
 (0)