Skip to content

Commit c6c63bc

Browse files
authored
Merge pull request #408 from AdExNetwork/v5-modify-submit-campaigns
V5: Authenticated POST route to modify/submit campaigns
2 parents b81584b + 19ccd17 commit c6c63bc

File tree

13 files changed

+1143
-106
lines changed

13 files changed

+1143
-106
lines changed

lib/protocol-eth

primitives/src/sentry.rs

+80-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::{
2-
targeting::Rules,
32
validator::{ApproveState, Heartbeat, MessageTypes, NewState, Type as MessageType},
4-
Address, BalancesMap, BigNum, Channel, ChannelId, ValidatorId, IPFS,
3+
Address, BigNum, Channel, ChannelId, ValidatorId, IPFS,
54
};
65
use chrono::{DateTime, Utc};
76
use serde::{Deserialize, Serialize};
@@ -361,6 +360,85 @@ pub mod campaign_create {
361360
}
362361
}
363362
}
363+
364+
/// This implementation helps with test setup
365+
/// **NOTE:** It erases the CampaignId, since the creation of the campaign gives it's CampaignId
366+
impl From<Campaign> for CreateCampaign {
367+
fn from(campaign: Campaign) -> Self {
368+
Self {
369+
channel: campaign.channel,
370+
creator: campaign.creator,
371+
budget: campaign.budget,
372+
validators: campaign.validators,
373+
title: campaign.title,
374+
pricing_bounds: campaign.pricing_bounds,
375+
event_submission: campaign.event_submission,
376+
ad_units: campaign.ad_units,
377+
targeting_rules: campaign.targeting_rules,
378+
created: campaign.created,
379+
active: campaign.active,
380+
}
381+
}
382+
}
383+
384+
// All editable fields stored in one place, used for checking when a budget is changed
385+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
386+
pub struct ModifyCampaign {
387+
pub budget: Option<UnifiedNum>,
388+
pub validators: Option<Validators>,
389+
pub title: Option<String>,
390+
pub pricing_bounds: Option<PricingBounds>,
391+
pub event_submission: Option<EventSubmission>,
392+
pub ad_units: Option<Vec<AdUnit>>,
393+
pub targeting_rules: Option<Rules>,
394+
}
395+
396+
impl ModifyCampaign {
397+
pub fn from_campaign(campaign: Campaign) -> Self {
398+
ModifyCampaign {
399+
budget: Some(campaign.budget),
400+
validators: Some(campaign.validators),
401+
title: campaign.title,
402+
pricing_bounds: campaign.pricing_bounds,
403+
event_submission: campaign.event_submission,
404+
ad_units: Some(campaign.ad_units),
405+
targeting_rules: Some(campaign.targeting_rules),
406+
}
407+
}
408+
409+
pub fn apply(self, mut campaign: Campaign) -> Campaign {
410+
if let Some(new_budget) = self.budget {
411+
campaign.budget = new_budget;
412+
}
413+
414+
if let Some(new_validators) = self.validators {
415+
campaign.validators = new_validators;
416+
}
417+
418+
// check if it was passed otherwise not sending a Title will result in clearing of the current one
419+
if let Some(new_title) = self.title {
420+
campaign.title = Some(new_title);
421+
}
422+
423+
if let Some(new_pricing_bounds) = self.pricing_bounds {
424+
campaign.pricing_bounds = Some(new_pricing_bounds);
425+
}
426+
427+
if let Some(new_event_submission) = self.event_submission {
428+
campaign.event_submission = Some(new_event_submission);
429+
}
430+
431+
if let Some(new_ad_units) = self.ad_units {
432+
campaign.ad_units = new_ad_units;
433+
}
434+
435+
if let Some(new_targeting_rules) = self.targeting_rules {
436+
campaign.targeting_rules = new_targeting_rules;
437+
}
438+
439+
campaign
440+
}
441+
}
364442
}
365443

366444
#[cfg(feature = "postgres")]

primitives/src/sentry/accounting.rs

+10
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ impl<S: BalancesState> Balances<S> {
6868

6969
Ok(())
7070
}
71+
72+
/// Adds the spender to the Balances with `UnifiedNum::from(0)` if he does not exist
73+
pub fn add_spender(&mut self, spender: Address) {
74+
self.spenders.entry(spender).or_insert(UnifiedNum::from(0));
75+
}
76+
77+
/// Adds the earner to the Balances with `UnifiedNum::from(0)` if he does not exist
78+
pub fn add_earner(&mut self, earner: Address) {
79+
self.earners.entry(earner).or_insert(UnifiedNum::from(0));
80+
}
7181
}
7282

7383
#[derive(Debug)]

primitives/src/validator.rs

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ impl From<&Address> for ValidatorId {
3939
}
4040
}
4141

42+
impl From<Address> for ValidatorId {
43+
fn from(address: Address) -> Self {
44+
Self(address)
45+
}
46+
}
47+
4248
impl From<&[u8; 20]> for ValidatorId {
4349
fn from(bytes: &[u8; 20]) -> Self {
4450
Self(Address::from(bytes))

sentry/src/db.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use lazy_static::lazy_static;
77

88
pub mod accounting;
99
pub mod analytics;
10-
mod campaign;
10+
pub mod campaign;
1111
mod channel;
1212
pub mod event_aggregate;
1313
pub mod spendable;

sentry/src/db/accounting.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ pub async fn get_accounting_spent(
3838
Ok(row.get("spent"))
3939
}
4040

41-
// TODO This is still WIP
42-
#[allow(dead_code)]
43-
async fn insert_accounting(
41+
pub async fn insert_accounting(
4442
pool: DbPool,
4543
channel: Channel,
4644
balances: Balances<CheckedState>,

0 commit comments

Comments
 (0)