2
2
#![ deny( clippy:: all) ]
3
3
4
4
use adex_primitives:: {
5
+ campaign:: Validators ,
5
6
supermarket:: units_for_slot,
6
7
supermarket:: units_for_slot:: response:: { AdUnit , Campaign } ,
7
8
targeting:: { self , input} ,
8
- BigNum , ChannelId , SpecValidators , ValidatorId , IPFS ,
9
+ Address , BigNum , CampaignId , ToHex , UnifiedNum , IPFS ,
9
10
} ;
10
11
use async_std:: { sync:: RwLock , task:: block_on} ;
11
12
use chrono:: { DateTime , Utc } ;
@@ -23,7 +24,6 @@ use std::{
23
24
sync:: Arc ,
24
25
} ;
25
26
use thiserror:: Error ;
26
- use units_for_slot:: response:: UnitsWithPrice ;
27
27
use url:: Url ;
28
28
29
29
const IPFS_GATEWAY : & str = "https://ipfs.moonicorn.network/ipfs/" ;
@@ -48,7 +48,7 @@ pub struct Options {
48
48
#[ serde( rename = "marketURL" ) ]
49
49
pub market_url : Url ,
50
50
pub market_slot : IPFS ,
51
- pub publisher_addr : ValidatorId ,
51
+ pub publisher_addr : Address ,
52
52
// All passed tokens must be of the same price and decimals, so that the amounts can be accurately compared
53
53
pub whitelisted_tokens : Vec < String > ,
54
54
pub width : Option < u64 > ,
@@ -70,7 +70,7 @@ impl Options {
70
70
pub struct HistoryEntry {
71
71
time : DateTime < Utc > ,
72
72
unit_id : IPFS ,
73
- campaign_id : ChannelId ,
73
+ campaign_id : CampaignId ,
74
74
slot_id : IPFS ,
75
75
}
76
76
@@ -79,7 +79,7 @@ pub struct HistoryEntry {
79
79
struct Event {
80
80
#[ serde( rename = "type" ) ]
81
81
event_type : String ,
82
- publisher : ValidatorId ,
82
+ publisher : Address ,
83
83
ad_unit : IPFS ,
84
84
ad_slot : IPFS ,
85
85
#[ serde( rename = "ref" ) ]
@@ -209,16 +209,16 @@ pub fn get_unit_html_with_events(
209
209
options : & Options ,
210
210
ad_unit : & AdUnit ,
211
211
hostname : & str ,
212
- channel_id : ChannelId ,
213
- validators : & SpecValidators ,
212
+ campaign_id : CampaignId ,
213
+ validators : & Validators ,
214
214
no_impression : impl Into < bool > ,
215
215
) -> String {
216
216
let get_body = |event_type : & str | EventBody {
217
217
events : vec ! [ Event {
218
218
event_type: event_type. to_string( ) ,
219
219
publisher: options. publisher_addr,
220
- ad_unit: ad_unit. id. clone ( ) ,
221
- ad_slot: options. market_slot. clone ( ) ,
220
+ ad_unit: ad_unit. id,
221
+ ad_slot: options. market_slot,
222
222
referrer: "document.referrer" . to_string( ) ,
223
223
} ] ,
224
224
} ;
@@ -234,7 +234,7 @@ pub fn get_unit_html_with_events(
234
234
. map ( |validator| {
235
235
let fetch_url = format ! (
236
236
"{}/channel/{}/events?pubAddr={}" ,
237
- validator. url, channel_id , options. publisher_addr
237
+ validator. url, campaign_id , options. publisher_addr
238
238
) ;
239
239
240
240
format ! ( "fetch('{}', fetchOpts)" , fetch_url)
@@ -304,7 +304,7 @@ impl Manager {
304
304
pub async fn get_targeting_input (
305
305
& self ,
306
306
mut input : input:: Input ,
307
- channel_id : ChannelId ,
307
+ campaign_id : CampaignId ,
308
308
) -> input:: Input {
309
309
let seconds_since_campaign_impression = self
310
310
. history
@@ -313,7 +313,7 @@ impl Manager {
313
313
. iter ( )
314
314
. rev ( )
315
315
. find_map ( |h| {
316
- if h. campaign_id == channel_id {
316
+ if h. campaign_id == campaign_id {
317
317
let last_impression: chrono:: Duration = Utc :: now ( ) - h. time ;
318
318
319
319
u64:: try_from ( last_impression. num_seconds ( ) ) . ok ( )
@@ -352,7 +352,7 @@ impl Manager {
352
352
353
353
let stick_campaign = campaigns
354
354
. iter ( )
355
- . find ( |c| c. channel . id == sticky_entry. campaign_id ) ?;
355
+ . find ( |c| c. campaign . id == sticky_entry. campaign_id ) ?;
356
356
357
357
let unit = stick_campaign
358
358
. units_with_price
@@ -370,8 +370,8 @@ impl Manager {
370
370
& self . options ,
371
371
& unit,
372
372
hostname,
373
- stick_campaign. channel . id ,
374
- & stick_campaign. channel . spec . validators ,
373
+ stick_campaign. campaign . id ,
374
+ & stick_campaign. campaign . validators ,
375
375
true ,
376
376
) ;
377
377
@@ -383,7 +383,7 @@ impl Manager {
383
383
} )
384
384
}
385
385
386
- async fn is_campaign_sticky ( & self , campaign_id : ChannelId ) -> bool {
386
+ async fn is_campaign_sticky ( & self , campaign_id : CampaignId ) -> bool {
387
387
if self . options . disabled_sticky {
388
388
false
389
389
} else {
@@ -400,7 +400,7 @@ impl Manager {
400
400
pub async fn get_market_demand_resp (
401
401
& self ,
402
402
) -> Result < units_for_slot:: response:: Response , Error > {
403
- let pub_prefix: String = self . options . publisher_addr . to_hex_non_prefix_string ( ) ;
403
+ let pub_prefix = self . options . publisher_addr . to_hex ( ) ;
404
404
405
405
let deposit_asset = self
406
406
. options
@@ -432,7 +432,7 @@ impl Manager {
432
432
433
433
pub async fn get_next_ad_unit ( & self ) -> Result < Option < NextAdUnit > , Error > {
434
434
let units_for_slot = self . get_market_demand_resp ( ) . await ?;
435
- let campaigns = & units_for_slot. campaigns ;
435
+ let m_campaigns = & units_for_slot. campaigns ;
436
436
let fallback_unit = units_for_slot. fallback_unit ;
437
437
let targeting_input = units_for_slot. targeting_input_base ;
438
438
@@ -444,7 +444,7 @@ impl Manager {
444
444
445
445
// Stickiness is when we keep showing an ad unit for a slot for some time in order to achieve fair impression value
446
446
// see https://github.com/AdExNetwork/adex-adview-manager/issues/65
447
- let sticky_result = self . get_sticky_ad_unit ( campaigns , & hostname) . await ;
447
+ let sticky_result = self . get_sticky_ad_unit ( m_campaigns , & hostname) . await ;
448
448
if let Some ( sticky) = sticky_result {
449
449
return Ok ( Some ( NextAdUnit {
450
450
unit : sticky. unit ,
@@ -461,36 +461,36 @@ impl Manager {
461
461
let seed = BigNum :: from ( random as u64 ) ;
462
462
463
463
// Apply targeting, now with adView.* variables, and sort the resulting ad units
464
- let mut units_with_price: Vec < ( UnitsWithPrice , ChannelId ) > = campaigns
464
+ let mut units_with_price = m_campaigns
465
465
. iter ( )
466
- . map ( |campaign | {
466
+ . map ( |m_campaign | {
467
467
// since we are in a Iterator.map(), we can't use async, so we block
468
- if block_on ( self . is_campaign_sticky ( campaign . channel . id ) ) {
468
+ if block_on ( self . is_campaign_sticky ( m_campaign . campaign . id ) ) {
469
469
return vec ! [ ] ;
470
470
}
471
471
472
- let campaign_id = campaign . channel . id ;
472
+ let campaign_id = m_campaign . campaign . id ;
473
473
474
- let mut unit_input = targeting_input. clone ( ) . with_market_channel ( campaign . channel . clone ( ) ) ;
474
+ let mut unit_input = targeting_input. clone ( ) . with_campaign ( m_campaign . campaign . clone ( ) ) ;
475
475
476
- campaign
476
+ m_campaign
477
477
. units_with_price
478
478
. iter ( )
479
479
. filter ( |unit_with_price| {
480
- unit_input. ad_unit_id = Some ( unit_with_price. unit . id . clone ( ) ) ;
480
+ unit_input. ad_unit_id = Some ( unit_with_price. unit . id ) ;
481
481
482
482
let mut output = targeting:: Output {
483
483
show : true ,
484
484
boost : 1.0 ,
485
- price : vec ! [ ( "IMPRESSION" . to_string( ) , unit_with_price. price. clone ( ) ) ]
485
+ price : vec ! [ ( "IMPRESSION" . to_string( ) , unit_with_price. price) ]
486
486
. into_iter ( )
487
487
. collect ( ) ,
488
488
} ;
489
489
490
490
let on_type_error = |error, rule| error ! ( & self . logger, "Rule evaluation error for {:?}" , campaign_id; "error" => ?error, "rule" => ?rule) ;
491
491
492
492
targeting:: eval_with_callback (
493
- & campaign. targeting_rules ,
493
+ & m_campaign . campaign . targeting_rules ,
494
494
& unit_input,
495
495
& mut output,
496
496
Some ( on_type_error)
@@ -503,7 +503,7 @@ impl Manager {
503
503
} )
504
504
. flatten ( )
505
505
. filter ( |x| !( self . options . disabled_video && is_video ( & x. 0 . unit ) ) )
506
- . collect ( ) ;
506
+ . collect :: < Vec < _ > > ( ) ;
507
507
508
508
units_with_price. sort_by ( |b, a| match ( & a. 0 . price ) . cmp ( & b. 0 . price ) {
509
509
Ordering :: Equal => randomized_sort_pos ( & a. 0 . unit , seed. clone ( ) )
@@ -519,9 +519,9 @@ impl Manager {
519
519
520
520
let new_entry = HistoryEntry {
521
521
time : Utc :: now ( ) ,
522
- unit_id : unit_with_price. unit . id . clone ( ) ,
522
+ unit_id : unit_with_price. unit . id ,
523
523
campaign_id : * campaign_id,
524
- slot_id : self . options . market_slot . clone ( ) ,
524
+ slot_id : self . options . market_slot ,
525
525
} ;
526
526
527
527
* self . history . write ( ) . await = history
@@ -539,11 +539,11 @@ impl Manager {
539
539
540
540
// Return the results, with a fallback unit if there is one
541
541
if let Some ( ( unit_with_price, campaign_id) ) = auction_winner {
542
- let validators = campaigns
542
+ let validators = m_campaigns
543
543
. iter ( )
544
- . find_map ( |campaign | {
545
- if & campaign . channel . id == campaign_id {
546
- Some ( & campaign . channel . spec . validators )
544
+ . find_map ( |m_campaign | {
545
+ if & m_campaign . campaign . id == campaign_id {
546
+ Some ( & m_campaign . campaign . validators )
547
547
} else {
548
548
None
549
549
}
@@ -562,7 +562,7 @@ impl Manager {
562
562
563
563
Ok ( Some ( NextAdUnit {
564
564
unit : unit_with_price. unit . clone ( ) ,
565
- price : unit_with_price. price . clone ( ) ,
565
+ price : unit_with_price. price ,
566
566
accepted_referrers : units_for_slot. accepted_referrers ,
567
567
html,
568
568
} ) )
@@ -582,14 +582,14 @@ impl Manager {
582
582
583
583
pub struct NextAdUnit {
584
584
pub unit : AdUnit ,
585
- pub price : BigNum ,
585
+ pub price : UnifiedNum ,
586
586
pub accepted_referrers : Vec < Url > ,
587
587
pub html : String ,
588
588
}
589
589
590
590
pub struct StickyAdUnit {
591
591
pub unit : AdUnit ,
592
- pub price : BigNum ,
592
+ pub price : UnifiedNum ,
593
593
pub html : String ,
594
594
pub is_sticky : bool ,
595
595
}
0 commit comments