@@ -6,7 +6,8 @@ use adex_primitives::{
6
6
} ,
7
7
targeting:: { self , input} ,
8
8
util:: ApiUrl ,
9
- Address , BigNum , CampaignId , ToHex , UnifiedNum , IPFS ,
9
+ validator:: ValidatorDesc ,
10
+ Address , BigNum , CampaignId , UnifiedNum , IPFS ,
10
11
} ;
11
12
use async_std:: { sync:: RwLock , task:: block_on} ;
12
13
use chrono:: { DateTime , Duration , Utc } ;
@@ -49,10 +50,12 @@ pub static DEFAULT_TOKENS: Lazy<HashSet<Address>> = Lazy::new(|| {
49
50
50
51
#[ derive( Debug , Error ) ]
51
52
pub enum Error {
52
- #[ error( "Request to the Market failed: status {status} at url {url}" ) ]
53
- Market { status : StatusCode , url : Url } ,
53
+ #[ error( "Request to the Sentry failed: status {status} at url {url}" ) ]
54
+ Sentry { status : StatusCode , url : Url } ,
54
55
#[ error( transparent) ]
55
56
Request ( #[ from] reqwest:: Error ) ,
57
+ #[ error( "No validators provided" ) ]
58
+ NoValidators ,
56
59
}
57
60
58
61
/// The Ad [`Manager`]'s options for showing ads.
@@ -77,6 +80,11 @@ pub struct Options {
77
80
/// default: `false`
78
81
#[ serde( default ) ]
79
82
pub disabled_sticky : bool ,
83
+ /// List of validators to query /units-for-slot from
84
+ ///
85
+ /// default: `[]`
86
+ #[ serde( default ) ]
87
+ pub validators : Vec < ValidatorDesc > ,
80
88
}
81
89
82
90
/// [`AdSlot`](adex_primitives::AdSlot) size `width x height` in pixels (`px`)
@@ -240,36 +248,45 @@ impl Manager {
240
248
}
241
249
}
242
250
251
+ // Test with different units with price
252
+ // Test if first campaign is not overwritten
243
253
pub async fn get_market_demand_resp ( & self ) -> Result < Response , Error > {
244
- let pub_prefix = self . options . publisher_addr . to_hex ( ) ;
245
-
246
254
let deposit_asset = self
247
255
. options
248
256
. whitelisted_tokens
249
257
. iter ( )
250
- . map ( |token| format ! ( "depositAsset ={}" , token) )
258
+ . map ( |token| format ! ( "depositAssets[] ={}" , token) )
251
259
. collect :: < Vec < _ > > ( )
252
260
. join ( "&" ) ;
253
261
254
- // ApiUrl handles endpoint path (with or without `/`)
255
- let url = self
262
+ let first_validator = self
256
263
. options
257
- . market_url
258
- . join ( & format ! (
259
- "units-for-slot/{ad_slot}?pubPrefix={pub_prefix}&{deposit_asset}" ,
260
- ad_slot = self . options. market_slot
261
- ) )
262
- . expect ( "Valid URL endpoint!" ) ;
263
-
264
- let market_response = self . client . get ( url. clone ( ) ) . send ( ) . await ?;
265
-
266
- match market_response. status ( ) {
267
- StatusCode :: OK => Ok ( market_response. json ( ) . await ?) ,
268
- _ => Err ( Error :: Market {
269
- status : market_response. status ( ) ,
270
- url,
271
- } ) ,
264
+ . validators
265
+ . clone ( )
266
+ . into_iter ( )
267
+ . next ( )
268
+ . ok_or ( Error :: NoValidators ) ?;
269
+
270
+ let url = format ! (
271
+ "{}/units-for-slot/{}?{}" ,
272
+ first_validator. url, self . options. market_slot, deposit_asset
273
+ ) ;
274
+ let mut first_res: Response = self . client . get ( url) . send ( ) . await ?. json ( ) . await ?;
275
+
276
+ for validator in self . options . validators . iter ( ) . skip ( 1 ) {
277
+ let url = format ! (
278
+ "{}/units-for-slot/{}?{}" ,
279
+ validator. url, self . options. market_slot, deposit_asset
280
+ ) ;
281
+ let new_res: Response = self . client . get ( url) . send ( ) . await ?. json ( ) . await ?;
282
+ for campaign in new_res. campaigns {
283
+ if !first_res. campaigns . contains ( & campaign) {
284
+ first_res. campaigns . push ( campaign) ;
285
+ }
286
+ }
272
287
}
288
+
289
+ Ok ( first_res)
273
290
}
274
291
275
292
pub async fn get_next_ad_unit ( & self ) -> Result < Option < NextAdUnit > , Error > {
0 commit comments