Skip to content

Commit 04ea51c

Browse files
committed
Added getPriceInUsd function
1 parent e7c3d6a commit 04ea51c

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

primitives/src/targeting.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl Input {
3636
"advertiserId" => Ok(Value::String(self.global.advertiser_id.clone())),
3737
"country" => Ok(Value::String(self.global.country.clone())),
3838
"eventType" => Ok(Value::String(self.global.event_type.clone())),
39-
"campaignId" => Ok(Value::String(self.global.campiagn_id.clone())),
39+
"campaignId" => Ok(Value::String(self.global.campaign_id.clone())),
4040
"campaignTotalSpent" => Ok(Value::String(self.global.campaign_total_spent.clone())),
4141
"campaignSecondsActive" => {
4242
Ok(Value::Number(self.global.campaign_seconds_active.into()))
@@ -45,6 +45,7 @@ impl Input {
4545
Ok(Value::Number(self.global.campaign_seconds_duration.into()))
4646
}
4747
"campaignBudget" => Ok(Value::BigNum(self.global.campaign_budget.clone())),
48+
"depositAsset" => Ok(Value::String(self.global.deposit_asset.clone())),
4849
"eventMinPrice" => Ok(Value::BigNum(self.global.event_min_price.clone())),
4950
"eventMaxPrice" => Ok(Value::BigNum(self.global.event_max_price.clone())),
5051
"publisherEarnedFromCampaign" => Ok(Value::BigNum(
@@ -102,11 +103,12 @@ pub struct Global {
102103
pub advertiser_id: String,
103104
pub country: String,
104105
pub event_type: String,
105-
pub campiagn_id: String,
106+
pub campaign_id: String,
106107
pub campaign_total_spent: String,
107108
pub campaign_seconds_active: u64,
108109
pub campaign_seconds_duration: u64,
109110
pub campaign_budget: BigNum,
111+
pub deposit_asset: String,
110112
pub event_min_price: BigNum,
111113
pub event_max_price: BigNum,
112114
pub publisher_earned_from_campaign: BigNum,

primitives/src/targeting/eval.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ pub enum Error {
2222
UnknownVariable,
2323
}
2424

25+
fn get_deposit_asset_divisor(address: &String) -> Result<BigNum, Error> {
26+
match address.as_str() {
27+
"0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359" => Ok(BigNum::from(10u64.pow(18))),
28+
"0xdac17f958d2ee523a2206206994597c13d831ec7" => Ok(BigNum::from(10u64.pow(6))),
29+
_ => Err(Error::TypeError)
30+
}
31+
}
32+
2533
trait Eval {
2634
fn eval(self, input: &Input, output: &mut Output) -> Result<Option<Value>, Error>;
2735
}
@@ -154,6 +162,7 @@ pub enum Function {
154162
StartsWith(Box<Rule>, Box<Rule>),
155163
EndsWith(Box<Rule>, Box<Rule>),
156164
OnlyShowIf(Box<Rule>),
165+
GetPriceInUsd(Box<Rule>),
157166
Intersects(Box<Rule>, Box<Rule>),
158167
/// Evaluates rule
159168
Do(Box<Rule>),
@@ -325,6 +334,10 @@ impl Function {
325334
pub fn new_bn(value: impl Into<Value>) -> Self {
326335
Self::Bn(value.into())
327336
}
337+
338+
pub fn new_get_price_in_usd(amount: impl Into<Rule>) -> Self {
339+
Self::GetPriceInUsd(Box::new(amount.into()))
340+
}
328341
}
329342

330343
impl Value {
@@ -867,6 +880,13 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
867880
let new_rule = Box::new(Rule::Value(Value::Bool(first_eval)));
868881
Function::Set(String::from("show"), new_rule).eval(input, output)?
869882
}
883+
Function::GetPriceInUsd(second_rule) => {
884+
let amount = second_rule.eval(input, output)?.ok_or(Error::TypeError)?.try_bignum()?;
885+
let deposit_asset = Function::Get("deposit_asset".to_string()).eval(input, output)?.ok_or(Error::TypeError)?.try_string()?;
886+
887+
let divisor = get_deposit_asset_divisor(&deposit_asset)?;
888+
Some(Value::BigNum(amount.div(divisor)))
889+
}
870890
Function::Do(first_rule) => eval(input, output, first_rule)?,
871891
Function::Set(key, rule) => {
872892
// Output variables can be set any number of times by different rules, except `show`

0 commit comments

Comments
 (0)