Skip to content

Commit 7f60378

Browse files
committed
Refactored getPriceInUsd and its tests + fixed a bug in the testing
1 parent 565f882 commit 7f60378

File tree

2 files changed

+25
-32
lines changed

2 files changed

+25
-32
lines changed

primitives/src/targeting/eval.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use std::{
66
fmt,
77
ops::{Add, Div, Mul, Rem, Sub},
88
str::FromStr,
9+
collections::HashMap,
910
};
11+
use lazy_static::lazy_static;
1012

1113
pub type Map = serde_json::value::Map<String, SerdeValue>;
1214

@@ -21,13 +23,19 @@ pub enum Error {
2123
TypeError,
2224
UnknownVariable,
2325
}
24-
25-
fn get_deposit_asset_divisor(address: &String) -> Result<BigNum, Error> {
26-
match address.as_str() {
27-
"0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359" => Ok(BigNum::from(10u64.pow(18))), // DAI
28-
"0xdac17f958d2ee523a2206206994597c13d831ec7" => Ok(BigNum::from(10u64.pow(6))), // Tether
29-
_ => Err(Error::TypeError),
30-
}
26+
pub const DAI_ADDR: &str = "0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359";
27+
pub const USDT_ADDR: &str = "0xdac17f958d2ee523a2206206994597c13d831ec7";
28+
pub const USDC_ADDR: &str = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48";
29+
30+
lazy_static! {
31+
pub static ref DEPOSIT_ASSETS_MAP: HashMap<String, BigNum> = {
32+
let mut assets = HashMap::new();
33+
assets.insert(DAI_ADDR.into(), BigNum::from(10u64.pow(18)));
34+
assets.insert(USDT_ADDR.into(), BigNum::from(10u64.pow(6)));
35+
assets.insert(USDC_ADDR.into(), BigNum::from(10u64.pow(18)));
36+
37+
assets
38+
};
3139
}
3240

3341
trait Eval {
@@ -887,7 +895,7 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
887895
.try_bignum()?;
888896
let deposit_asset = &input.global.channel.deposit_asset;
889897

890-
let divisor = get_deposit_asset_divisor(&deposit_asset)?;
898+
let divisor = DEPOSIT_ASSETS_MAP.get(deposit_asset).unwrap();
891899
Some(Value::BigNum(amount.div(divisor)))
892900
}
893901
Function::Do(first_rule) => eval(input, output, first_rule)?,

primitives/src/targeting/eval_test.rs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ mod math_functions {
287287
Value::Number(Number::from_f64(100.0).expect("should create float number")),
288288
Value::Number(Number::from_f64(3.0).expect("should create float number")),
289289
Value::Number(
290-
Number::from_f64(33.333333333333336).expect("should create float number"),
290+
Number::from_f64(33.333_333_333_333_336).expect("should create float number"),
291291
),
292292
),
293293
(
@@ -1088,7 +1088,6 @@ mod control_flow_and_logic {
10881088

10891089
mod string_and_array {
10901090
use super::*;
1091-
10921091
#[test]
10931092
fn test_in_eval() {
10941093
let input = get_default_input();
@@ -1309,38 +1308,24 @@ mod string_and_array {
13091308
#[test]
13101309
fn test_get_dai_price_in_usd_eval() {
13111310
let mut input = get_default_input();
1312-
input.global.channel.deposit_asset =
1313-
"0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359".to_string();
1311+
13141312
let mut output = Output {
13151313
show: true,
13161314
boost: 1.0,
13171315
price: Default::default(),
13181316
};
1319-
1317+
for (key, value) in &*DEPOSIT_ASSETS_MAP {
1318+
input.global.channel.deposit_asset = key.to_string();
1319+
let amount_crypto = BigNum::from(100).mul(value);
1320+
let amount_usd = Some(Value::BigNum(BigNum::from(100)));
1321+
let rule = Rule::Function(Function::new_get_price_in_usd(Rule::Value(Value::BigNum(amount_crypto))));
1322+
assert_eq!(Ok(amount_usd), rule.eval(&input, &mut output));
1323+
}
13201324
let amount_dai = BigNum::from_str("100000000000000000000").expect("Should create BigNum"); // 100 DAI
13211325
let amount_usd = Some(Value::BigNum(BigNum::from(100)));
13221326
let rule = Rule::Function(Function::new_get_price_in_usd(Rule::Value(Value::BigNum(
13231327
amount_dai,
13241328
))));
13251329
assert_eq!(Ok(amount_usd), rule.eval(&input, &mut output));
13261330
}
1327-
1328-
#[test]
1329-
fn test_get_tether_price_in_usd_eval() {
1330-
let mut input = get_default_input();
1331-
input.global.channel.deposit_asset =
1332-
"0xdac17f958d2ee523a2206206994597c13d831ec7".to_string();
1333-
let mut output = Output {
1334-
show: true,
1335-
boost: 1.0,
1336-
price: Default::default(),
1337-
};
1338-
1339-
let amount_tether = BigNum::from_str("100000000").expect("Should create BigNum"); // 100 Tether
1340-
let amount_usd = Some(Value::BigNum(BigNum::from(100)));
1341-
let rule = Rule::Function(Function::new_get_price_in_usd(Rule::Value(Value::BigNum(
1342-
amount_tether,
1343-
))));
1344-
assert_eq!(Ok(amount_usd), rule.eval(&input, &mut output));
1345-
}
13461331
}

0 commit comments

Comments
 (0)