Skip to content

Commit d6ed850

Browse files
authored
Merge pull request #321 from AdExNetwork/issue-296-logic-and-control-flow
getPriceInUsd now returns f64 instead of BigNum
2 parents 5d1a34f + c433514 commit d6ed850

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

primitives/src/targeting/eval.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -895,11 +895,10 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
895895
.try_bignum()?;
896896
let deposit_asset = &input.global.channel.deposit_asset;
897897

898-
let divisor = match DEPOSIT_ASSETS_MAP.get(deposit_asset) {
899-
Some(d) => Ok(d),
900-
None => Err(Error::TypeError),
901-
};
902-
Some(Value::BigNum(amount.div(divisor?)))
898+
let divisor = DEPOSIT_ASSETS_MAP.get(deposit_asset).ok_or(Error::TypeError)?;
899+
let amount_in_usd = amount.div(divisor).to_f64().ok_or(Error::TypeError)?;
900+
let amount_as_number = Number::from_f64(amount_in_usd).ok_or(Error::TypeError)?;
901+
Some(Value::Number(amount_as_number))
903902
}
904903
Function::Do(first_rule) => eval(input, output, first_rule)?,
905904
Function::Set(key, rule) => {

primitives/src/targeting/eval_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1317,7 +1317,7 @@ mod string_and_array {
13171317
for (key, value) in &*DEPOSIT_ASSETS_MAP {
13181318
input.global.channel.deposit_asset = key.to_string();
13191319
let amount_crypto = BigNum::from(100).mul(value);
1320-
let amount_usd = Some(Value::BigNum(BigNum::from(100)));
1320+
let amount_usd = Some(Value::Number(Number::from_f64(100.0).expect("should create a float")));
13211321
let rule = Rule::Function(Function::new_get_price_in_usd(Rule::Value(Value::BigNum(
13221322
amount_crypto,
13231323
))));

0 commit comments

Comments
 (0)