Skip to content

Commit 47fea89

Browse files
committed
Readded seed.rs
1 parent 2d9f928 commit 47fea89

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

sentry/src/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+
}

0 commit comments

Comments
 (0)