@@ -22,6 +22,14 @@ pub enum Error {
22
22
UnknownVariable ,
23
23
}
24
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 ) ) ) ,
28
+ "0xdac17f958d2ee523a2206206994597c13d831ec7" => Ok ( BigNum :: from ( 10u64 . pow ( 6 ) ) ) ,
29
+ _ => Err ( Error :: TypeError )
30
+ }
31
+ }
32
+
25
33
trait Eval {
26
34
fn eval ( self , input : & Input , output : & mut Output ) -> Result < Option < Value > , Error > ;
27
35
}
@@ -154,6 +162,7 @@ pub enum Function {
154
162
StartsWith ( Box < Rule > , Box < Rule > ) ,
155
163
EndsWith ( Box < Rule > , Box < Rule > ) ,
156
164
OnlyShowIf ( Box < Rule > ) ,
165
+ GetPriceInUsd ( Box < Rule > ) ,
157
166
Intersects ( Box < Rule > , Box < Rule > ) ,
158
167
/// Evaluates rule
159
168
Do ( Box < Rule > ) ,
@@ -325,6 +334,10 @@ impl Function {
325
334
pub fn new_bn ( value : impl Into < Value > ) -> Self {
326
335
Self :: Bn ( value. into ( ) )
327
336
}
337
+
338
+ pub fn new_get_price_in_usd ( amount : impl Into < Rule > ) -> Self {
339
+ Self :: GetPriceInUsd ( Box :: new ( amount. into ( ) ) )
340
+ }
328
341
}
329
342
330
343
impl Value {
@@ -867,6 +880,13 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
867
880
let new_rule = Box :: new ( Rule :: Value ( Value :: Bool ( first_eval) ) ) ;
868
881
Function :: Set ( String :: from ( "show" ) , new_rule) . eval ( input, output) ?
869
882
}
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
+ }
870
890
Function :: Do ( first_rule) => eval ( input, output, first_rule) ?,
871
891
Function :: Set ( key, rule) => {
872
892
// Output variables can be set any number of times by different rules, except `show`
0 commit comments