Skip to content

Commit f7665c5

Browse files
authored
Merge pull request #288 from AdExNetwork/clean-up-marketing-campaign
MarketChannel changes
2 parents e1401ee + 527f1e9 commit f7665c5

File tree

8 files changed

+78
-61
lines changed

8 files changed

+78
-61
lines changed

adview-manager/src/lib.rs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#![deny(rust_2018_idioms)]
22
#![deny(clippy::all)]
33

4-
use adex_primitives::market_channel::{MarketChannel, MarketStatusType};
5-
use adex_primitives::{AdUnit, BigNum, SpecValidators, TargetingTag};
4+
use adex_primitives::market::{Campaign, StatusType};
5+
use adex_primitives::{AdUnit, BigNum, ChannelId, SpecValidators, TargetingTag};
66
use chrono::Utc;
77
use serde::{Deserialize, Serialize};
88

@@ -18,7 +18,7 @@ pub struct AdViewManagerOptions {
1818
#[serde(rename = "marketURL")]
1919
pub market_url: String,
2020
/// Defaulted
21-
pub accepted_states: Vec<MarketStatusType>,
21+
pub accepted_states: Vec<StatusType>,
2222
/// Defaulted
2323
pub min_per_impression: BigNum,
2424
/// Defaulted
@@ -51,7 +51,7 @@ impl AdViewManagerOptions {
5151
#[derive(Debug)]
5252
pub struct UnitByPrice {
5353
pub unit: AdUnit,
54-
pub channel_id: String,
54+
pub channel_id: ChannelId,
5555
pub validators: SpecValidators,
5656
pub min_targeting_score: MinTargetingScore,
5757
pub min_per_impression: BigNum,
@@ -60,7 +60,7 @@ pub struct UnitByPrice {
6060
#[derive(Debug)]
6161
pub struct Unit {
6262
pub unit: AdUnit,
63-
pub channel_id: String,
63+
pub channel_id: ChannelId,
6464
pub validators: SpecValidators,
6565
pub min_targeting_score: MinTargetingScore,
6666
pub min_per_impression: BigNum,
@@ -80,24 +80,26 @@ impl Unit {
8080
}
8181
}
8282

83-
pub fn apply_selection(campaigns: &[MarketChannel], options: &AdViewManagerOptions) -> Vec<Unit> {
83+
pub fn apply_selection(campaigns: &[Campaign], options: &AdViewManagerOptions) -> Vec<Unit> {
8484
let eligible = campaigns.iter().filter(|campaign| {
8585
options
8686
.accepted_states
8787
.contains(&campaign.status.status_type)
8888
&& campaign
89+
.channel
8990
.spec
9091
.active_from
9192
.map(|datetime| datetime < Utc::now())
9293
.unwrap_or(true)
93-
&& campaign.deposit_asset == options.whitelisted_token
94-
&& campaign.spec.min_per_impression >= options.min_per_impression
94+
&& campaign.channel.deposit_asset == options.whitelisted_token
95+
&& campaign.channel.spec.min_per_impression >= options.min_per_impression
9596
});
9697

9798
let mut units: Vec<UnitByPrice> = eligible
9899
// filter ad_units by whitelisted type, map then to UnitByPrice and flat_map them for each campaing
99100
.flat_map(|campaign| {
100101
campaign
102+
.channel
101103
.spec
102104
.ad_units
103105
.iter()
@@ -113,13 +115,13 @@ pub fn apply_selection(campaigns: &[MarketChannel], options: &AdViewManagerOptio
113115
})
114116
.map(move |ad_unit| UnitByPrice {
115117
unit: ad_unit.clone(),
116-
channel_id: campaign.id.clone(),
117-
validators: campaign.spec.validators.clone(),
118+
channel_id: campaign.channel.id,
119+
validators: campaign.channel.spec.validators.clone(),
118120
min_targeting_score: ad_unit
119121
.min_targeting_score
120-
.or(campaign.spec.min_targeting_score)
122+
.or(campaign.channel.spec.min_targeting_score)
121123
.unwrap_or_else(|| 0.into()),
122-
min_per_impression: campaign.spec.min_per_impression.clone(),
124+
min_per_impression: campaign.channel.spec.min_per_impression.clone(),
123125
})
124126
})
125127
.collect();
@@ -223,7 +225,7 @@ fn video_html(
223225
pub fn get_html(
224226
options: &AdViewManagerOptions,
225227
ad_unit: AdUnit,
226-
channel_id: &str,
228+
channel_id: ChannelId,
227229
validators: &SpecValidators,
228230
) -> String {
229231
let ev_body = EventBody {
@@ -267,19 +269,19 @@ fn get_unit_html(
267269
let adex_icon = "<a href=\"https://www.adex.network\" target=\"_blank\" rel=\"noopener noreferrer\"
268270
style=\"position: absolute; top: 0; right: 0;\"
269271
>
270-
<svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\" y=\"0px\" width=\"18px\"
271-
height=\"18px\" viewBox=\"0 0 18 18\" style=\"enable-background:new 0 0 18 18;\" xml:space=\"preserve\">
272-
<style type=\"text/css\">
273-
.st0{fill:#FFFFFF;}
274-
.st1{fill:#1B75BC;}
275-
</style>
276-
<defs>
277-
</defs>
278-
<rect class=\"st0\" width=\"18\" height=\"18\"/>
279-
<path class=\"st1\" d=\"M14,12.1L10.9,9L14,5.9L12.1,4L9,7.1L5.9,4L4,5.9L7.1,9L4,12.1L5.9,14L9,10.9l3.1,3.1L14,12.1z M7.9,2L6.4,3.5
280-
L7.9,5L9,3.9L10.1,5l1.5-1.5L10,1.9l-1-1L7.9,2 M7.9,16l-1.5-1.5L7.9,13L9,14.1l1.1-1.1l1.5,1.5L10,16.1l-1,1L7.9,16\"/>
281-
</svg>
282-
</a>";
272+
<svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\" y=\"0px\" width=\"18px\"
273+
height=\"18px\" viewBox=\"0 0 18 18\" style=\"enable-background:new 0 0 18 18;\" xml:space=\"preserve\">
274+
<style type=\"text/css\">
275+
.st0{fill:#FFFFFF;}
276+
.st1{fill:#1B75BC;}
277+
</style>
278+
<defs>
279+
</defs>
280+
<rect class=\"st0\" width=\"18\" height=\"18\"/>
281+
<path class=\"st1\" d=\"M14,12.1L10.9,9L14,5.9L12.1,4L9,7.1L5.9,4L4,5.9L7.1,9L4,12.1L5.9,14L9,10.9l3.1,3.1L14,12.1z M7.9,2L6.4,3.5
282+
L7.9,5L9,3.9L10.1,5l1.5-1.5L10,1.9l-1-1L7.9,2 M7.9,16l-1.5-1.5L7.9,13L9,14.1l1.1-1.1l1.5,1.5L10,16.1l-1,1L7.9,16\"/>
283+
</svg>
284+
</a>";
283285

284286
let result = format!("
285287
<div style=\"position: relative; overflow: hidden; {size}\">

primitives/src/balances_map.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::BTreeMap;
22

33
use crate::{BigNum, ValidatorId};
4-
use std::collections::btree_map::{Entry, Iter, Values};
4+
use std::collections::btree_map::{Entry, IntoIter, Iter, Values};
55

66
use serde::{Deserialize, Serialize};
77
use std::iter::FromIterator;
@@ -50,6 +50,15 @@ impl FromIterator<(ValidatorId, BigNum)> for BalancesMap {
5050
}
5151
}
5252

53+
impl IntoIterator for BalancesMap {
54+
type Item = (ValidatorId, BigNum);
55+
type IntoIter = IntoIter<ValidatorId, BigNum>;
56+
57+
fn into_iter(self) -> Self::IntoIter {
58+
self.0.into_iter()
59+
}
60+
}
61+
5362
#[cfg(test)]
5463
mod test {
5564
use super::*;

primitives/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub mod channel;
1111
pub mod channel_validator;
1212
pub mod config;
1313
pub mod event_submission;
14-
pub mod market_channel;
14+
pub mod market;
1515
pub mod merkle_tree;
1616
pub mod sentry;
1717
pub mod targeting_tag;
Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,57 @@
11
use chrono::serde::ts_milliseconds;
2+
use chrono::{DateTime, Utc};
23
use serde::{Deserialize, Serialize};
3-
use std::collections::HashMap;
44

5-
use chrono::{DateTime, Utc};
5+
use std::fmt;
66

7-
use crate::{BigNum, ChannelSpec};
7+
use crate::{BalancesMap, BigNum, Channel};
88

99
// Data structs specific to the market
1010
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
11-
pub enum MarketStatusType {
12-
Initializing,
13-
Ready,
11+
pub enum StatusType {
1412
Active,
13+
Ready,
14+
Pending,
15+
Initializing,
16+
Waiting,
1517
Offline,
1618
Disconnected,
1719
Unhealthy,
18-
Withdraw,
20+
Invalid,
1921
Expired,
22+
/// also called "Closed"
2023
Exhausted,
24+
Withdraw,
25+
}
26+
27+
impl fmt::Display for StatusType {
28+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29+
fmt::Debug::fmt(self, f)
30+
}
2131
}
2232

2333
#[derive(Deserialize, Clone, Debug)]
2434
#[serde(rename_all = "camelCase")]
25-
pub struct MarketStatus {
35+
pub struct Status {
2636
#[serde(rename = "name")]
27-
pub status_type: MarketStatusType,
28-
pub usd_estimate: f32,
37+
pub status_type: StatusType,
38+
pub usd_estimate: Option<f32>,
2939
#[serde(rename = "lastApprovedBalances")]
30-
pub balances: HashMap<String, BigNum>,
40+
pub balances: BalancesMap,
3141
#[serde(with = "ts_milliseconds")]
3242
pub last_checked: DateTime<Utc>,
3343
}
3444

35-
impl MarketStatus {
45+
impl Status {
3646
pub fn balances_sum(&self) -> BigNum {
3747
self.balances.values().sum()
3848
}
3949
}
4050

4151
#[derive(Deserialize, Clone, Debug)]
4252
#[serde(rename_all = "camelCase")]
43-
pub struct MarketChannel {
44-
pub id: String,
45-
pub creator: String,
46-
pub deposit_asset: String,
47-
pub deposit_amount: BigNum,
48-
pub status: MarketStatus,
49-
pub spec: ChannelSpec,
53+
pub struct Campaign {
54+
#[serde(flatten)]
55+
pub channel: Channel,
56+
pub status: Status,
5057
}

primitives/src/sentry.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::collections::HashMap;
66
use std::fmt;
77
use std::hash::Hash;
88

9-
#[derive(Serialize, Deserialize, Debug)]
9+
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
1010
#[serde(rename_all = "camelCase")]
1111
pub struct LastApproved {
1212
/// NewState can be None if the channel is brand new
@@ -15,21 +15,21 @@ pub struct LastApproved {
1515
pub approve_state: Option<ApproveStateValidatorMessage>,
1616
}
1717

18-
#[derive(Serialize, Deserialize, Debug)]
18+
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
1919
pub struct NewStateValidatorMessage {
2020
pub from: ValidatorId,
2121
pub received: DateTime<Utc>,
2222
pub msg: MessageTypes,
2323
}
2424

25-
#[derive(Serialize, Deserialize, Debug)]
25+
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
2626
pub struct ApproveStateValidatorMessage {
2727
pub from: ValidatorId,
2828
pub received: DateTime<Utc>,
2929
pub msg: MessageTypes,
3030
}
3131

32-
#[derive(Serialize, Deserialize, Debug)]
32+
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
3333
pub struct HeartbeatValidatorMessage {
3434
pub from: ValidatorId,
3535
pub received: DateTime<Utc>,
@@ -129,7 +129,7 @@ pub struct ChannelListResponse {
129129
pub total_pages: u64,
130130
}
131131

132-
#[derive(Serialize, Deserialize, Debug)]
132+
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
133133
#[serde(rename_all = "camelCase")]
134134
pub struct LastApprovedResponse {
135135
pub last_approved: Option<LastApproved>,

primitives/src/validator.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ pub struct ValidatorDesc {
137137

138138
// Validator Message Types
139139

140-
#[derive(Serialize, Deserialize, Debug, Clone)]
140+
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
141141
#[serde(rename_all = "camelCase")]
142142
pub struct Accounting {
143143
#[serde(rename = "lastEvAggr")]
@@ -146,23 +146,23 @@ pub struct Accounting {
146146
pub balances: BalancesMap,
147147
}
148148

149-
#[derive(Serialize, Deserialize, Debug, Clone)]
149+
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
150150
#[serde(rename_all = "camelCase")]
151151
pub struct ApproveState {
152152
pub state_root: String,
153153
pub signature: String,
154154
pub is_healthy: bool,
155155
}
156156

157-
#[derive(Serialize, Deserialize, Debug, Clone)]
157+
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
158158
#[serde(rename_all = "camelCase")]
159159
pub struct NewState {
160160
pub state_root: String,
161161
pub signature: String,
162162
pub balances: BalancesMap,
163163
}
164164

165-
#[derive(Default, Serialize, Deserialize, Debug, Clone)]
165+
#[derive(Default, Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
166166
#[serde(rename_all = "camelCase")]
167167
pub struct RejectState {
168168
pub reason: String,
@@ -172,7 +172,7 @@ pub struct RejectState {
172172
pub timestamp: Option<DateTime<Utc>>,
173173
}
174174

175-
#[derive(Serialize, Deserialize, Debug, Clone)]
175+
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
176176
#[serde(rename_all = "camelCase")]
177177
pub struct Heartbeat {
178178
pub signature: String,
@@ -190,7 +190,7 @@ impl Heartbeat {
190190
}
191191
}
192192

193-
#[derive(Serialize, Deserialize, Debug, Clone)]
193+
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
194194
#[serde(tag = "type")]
195195
pub enum MessageTypes {
196196
ApproveState(ApproveState),

sentry/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ impl<A: Adapter + 'static> Application<A> {
137137
}
138138
};
139139

140-
let path = req.uri().path().to_string();
141-
let mut response = match (path.as_ref(), req.method()) {
140+
let mut response = match (req.uri().path(), req.method()) {
142141
("/cfg", &Method::GET) => config(req, &self).await,
143142
("/channel", &Method::POST) => create_channel(req, &self).await,
144143
("/channel/list", &Method::GET) => channel_list(req, &self).await,

sentry/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ async fn run<A: Adapter + 'static>(app: Application<A>, port: u16) {
127127
let logger = app.logger.clone();
128128
info!(&logger, "Listening on port {}!", port);
129129

130-
let make_service = make_service_fn(move |_| {
130+
let make_service = make_service_fn(|_| {
131131
let server = app.clone();
132132
async move {
133133
Ok::<_, Error>(service_fn(move |req| {

0 commit comments

Comments
 (0)