Skip to content

Commit 1af1380

Browse files
authored
Merge pull request #308 from AdExNetwork/supermarket-changes
Supermarket changes
2 parents 4e59a6d + 88df42a commit 1af1380

File tree

14 files changed

+333
-102
lines changed

14 files changed

+333
-102
lines changed

adapter/src/ethereum.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,15 +669,17 @@ mod test {
669669
deposit_asset: eth_checksum::checksum(&format!("{:?}", token_contract.address())),
670670
deposit_amount: 2_000.into(),
671671
valid_until: Utc::now() + Duration::days(2),
672+
targeting_rules: vec![],
672673
spec: ChannelSpec {
673674
title: None,
674675
validators: SpecValidators::new(leader_validator_desc, follower_validator_desc),
675676
max_per_impression: 10.into(),
676677
min_per_impression: 10.into(),
677678
targeting: vec![],
679+
targeting_rules: vec![],
678680
min_targeting_score: None,
679681
event_submission: Some(EventSubmission { allow: vec![] }),
680-
created: Some(Utc::now()),
682+
created: Utc::now(),
681683
active_from: None,
682684
nonce: None,
683685
withdraw_period_start: Utc::now() + Duration::days(1),

primitives/src/balances_map.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ impl BalancesMap {
3232
self.0.get(key)
3333
}
3434

35+
pub fn contains_key(&self, key: &ValidatorId) -> bool {
36+
self.0.contains_key(key)
37+
}
38+
3539
pub fn entry(&mut self, key: ValidatorId) -> Entry<'_, ValidatorId, BigNum> {
3640
self.0.entry(key)
3741
}

primitives/src/channel.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
use std::error::Error;
22
use std::fmt;
3+
use std::ops::Deref;
34

45
use chrono::serde::{ts_milliseconds, ts_milliseconds_option, ts_seconds};
56
use chrono::{DateTime, Utc};
67
use serde::{Deserialize, Deserializer, Serialize};
78
use serde_hex::{SerHex, StrictPfx};
89

9-
use crate::big_num::BigNum;
10-
use crate::{AdUnit, EventSubmission, TargetingTag, ValidatorDesc, ValidatorId};
10+
use crate::{
11+
targeting::Rule, AdUnit, BigNum, EventSubmission, TargetingTag, ValidatorDesc, ValidatorId,
12+
};
1113
use hex::{FromHex, FromHexError};
12-
use std::ops::Deref;
1314

1415
#[derive(Serialize, Deserialize, PartialEq, Eq, Copy, Clone, Hash)]
1516
#[serde(transparent)]
@@ -84,6 +85,8 @@ pub struct Channel {
8485
pub deposit_amount: BigNum,
8586
#[serde(with = "ts_seconds")]
8687
pub valid_until: DateTime<Utc>,
88+
#[serde(default)]
89+
pub targeting_rules: Vec<Rule>,
8790
pub spec: ChannelSpec,
8891
}
8992

@@ -116,6 +119,14 @@ impl PricingBounds {
116119

117120
vec
118121
}
122+
123+
pub fn get(&self, event_type: &str) -> Option<&Pricing> {
124+
match event_type {
125+
"IMPRESSION" => self.impression.as_ref(),
126+
"CLICK" => self.click.as_ref(),
127+
_ => None,
128+
}
129+
}
119130
}
120131

121132
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -141,14 +152,10 @@ pub struct ChannelSpec {
141152
#[serde(default, skip_serializing_if = "Option::is_none")]
142153
pub event_submission: Option<EventSubmission>,
143154
/// A millisecond timestamp of when the campaign was created
144-
#[serde(
145-
default,
146-
skip_serializing_if = "Option::is_none",
147-
with = "ts_milliseconds_option"
148-
)]
149-
pub created: Option<DateTime<Utc>>,
155+
#[serde(with = "ts_milliseconds")]
156+
pub created: DateTime<Utc>,
150157
/// A millisecond timestamp representing the time you want this campaign to become active (optional)
151-
/// Used by the AdViewManager
158+
/// Used by the AdViewManager & Targeting AIP#31
152159
#[serde(
153160
default,
154161
skip_serializing_if = "Option::is_none",
@@ -167,6 +174,8 @@ pub struct ChannelSpec {
167174
/// An array of AdUnit (optional)
168175
#[serde(default, skip_serializing_if = "Vec::is_empty")]
169176
pub ad_units: Vec<AdUnit>,
177+
#[serde(default)]
178+
pub targeting_rules: Vec<Rule>,
170179
#[serde(default, skip_serializing_if = "Vec::is_empty")]
171180
pub price_multiplication_rules: Vec<PriceMultiplicationRules>,
172181
#[serde(default)]
@@ -335,6 +344,7 @@ impl Error for ChannelError {
335344
pub mod postgres {
336345
use super::ChannelId;
337346
use super::{Channel, ChannelSpec};
347+
use crate::targeting::Rule;
338348
use bytes::BytesMut;
339349
use hex::FromHex;
340350
use postgres_types::{accepts, to_sql_checked, FromSql, IsNull, Json, ToSql, Type};
@@ -349,6 +359,7 @@ pub mod postgres {
349359
deposit_asset: row.get("deposit_asset"),
350360
deposit_amount: row.get("deposit_amount"),
351361
valid_until: row.get("valid_until"),
362+
targeting_rules: row.get::<_, Json<Vec<Rule>>>("targeting_rules").0,
352363
spec: row.get::<_, Json<ChannelSpec>>("spec").0,
353364
}
354365
}

primitives/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ pub mod event_submission;
1515
pub mod market;
1616
pub mod merkle_tree;
1717
pub mod sentry;
18+
pub mod supermarket;
1819
pub mod targeting;
1920
pub mod targeting_tag;
21+
2022
pub mod util {
2123
pub mod tests {
2224
pub mod prep_db;

primitives/src/sentry.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -217,21 +217,16 @@ impl fmt::Display for ChannelReport {
217217

218218
pub mod channel_list {
219219
use crate::ValidatorId;
220-
use chrono::serde::ts_seconds::deserialize as ts_seconds;
221-
use chrono::{DateTime, Utc};
222-
use serde::Deserialize;
220+
use chrono::{serde::ts_seconds, DateTime, Utc};
221+
use serde::{Deserialize, Serialize};
223222

224-
#[derive(Debug, Deserialize)]
223+
#[derive(Debug, Serialize, Deserialize)]
225224
pub struct ChannelListQuery {
226225
#[serde(default = "default_page")]
227226
pub page: u64,
228227
/// filters the list on `valid_until >= valid_until_ge`
229228
/// It should be the same timestamp format as the `Channel.valid_until`: **seconds**
230-
#[serde(
231-
deserialize_with = "ts_seconds",
232-
default = "Utc::now",
233-
rename = "validUntil"
234-
)]
229+
#[serde(with = "ts_seconds", default = "Utc::now", rename = "validUntil")]
235230
pub valid_until_ge: DateTime<Utc>,
236231
pub creator: Option<String>,
237232
/// filters the channels containing a specific validator if provided

primitives/src/supermarket.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use crate::{BalancesMap, Channel};
2+
3+
#[derive(Debug, Clone)]
4+
pub struct Campaign {
5+
pub channel: Channel,
6+
pub status: Status,
7+
pub balances: BalancesMap,
8+
}
9+
10+
impl Campaign {
11+
pub fn new(channel: Channel, status: Status, balances: BalancesMap) -> Self {
12+
Self {
13+
channel,
14+
status,
15+
balances,
16+
}
17+
}
18+
}
19+
20+
#[derive(Debug, Eq, PartialEq, Clone)]
21+
pub enum Status {
22+
// Active and Ready
23+
Active,
24+
Pending,
25+
Initializing,
26+
Waiting,
27+
Finalized(Finalized),
28+
Unsound {
29+
disconnected: bool,
30+
offline: bool,
31+
rejected_state: bool,
32+
unhealthy: bool,
33+
},
34+
}
35+
36+
#[derive(Debug, PartialEq, Eq, Clone)]
37+
pub enum Finalized {
38+
Expired,
39+
Exhausted,
40+
Withdraw,
41+
}

0 commit comments

Comments
 (0)