1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
//! # Equilibrium Oracle Pallet
//!
//! Equilibrium's Oracle Pallet is a Substrate module for receiving external
//! prices information, aggregating it into currency prices and using them
//! in Substrate

#![cfg_attr(not(feature = "std"), no_std)]

mod mock;
mod regex_offsets;
mod tests;
extern crate alloc;

use crate::regex_offsets::{get_index_offsets, get_url_offset};
use alloc::format;
use alloc::string::{FromUtf8Error, String, ToString};
use core::str::FromStr;
use eq_primitives::currency::*;
use eq_utils::log::eq_log;
use eq_utils::{eq_ensure, ok_or_error};
use eq_whitelists::CheckWhitelisted;
use frame_support::{
    codec::{Decode, Encode},
    debug, decl_error, decl_event, decl_module, decl_storage,
    dispatch::DispatchResult,
    traits::{Get, UnixTime},
};
use impl_trait_for_tuples::impl_for_tuples;
use serde_json as json;
use sp_arithmetic::{FixedI64, FixedPointNumber};
use sp_core::crypto::KeyTypeId;
use sp_runtime::{
    offchain::{http, Duration, StorageKind},
    traits::IdentifyAccount,
    RuntimeAppPublic, RuntimeDebug,
};
use sp_std::iter::Iterator;
use sp_std::prelude::*;
use system as frame_system;
use system::{
    self as system, ensure_root, ensure_signed,
    offchain::{
        AppCrypto, CreateSignedTransaction,
        /*SendUnsignedTransaction,*/ SendSignedTransaction,
        /*SignedPayload, SigningTypes,*/ Signer, /*SubmitTransaction,*/
    },
};

pub const KEY_TYPE: KeyTypeId = KeyTypeId(*b"orac");

pub mod crypto {
    //! Module for signing operations

    use super::KEY_TYPE;
    use sp_runtime::MultiSignature;
    use sp_runtime::{
        app_crypto::{app_crypto, sr25519},
        traits::Verify,
    };
    app_crypto!(sr25519, KEY_TYPE);

    pub struct AuthId;
    impl system::offchain::AppCrypto<<MultiSignature as Verify>::Signer, MultiSignature> for AuthId {
        type RuntimeAppPublic = Public;
        type GenericSignature = sp_core::sr25519::Signature;
        type GenericPublic = sp_core::sr25519::Public;
    }

    use sp_core::sr25519::Signature as Sr25519Signature;

    pub struct TestAuthId;
    impl system::offchain::AppCrypto<<Sr25519Signature as Verify>::Signer, Sr25519Signature>
        for TestAuthId
    {
        type RuntimeAppPublic = Public;
        type GenericSignature = sp_core::sr25519::Signature;
        type GenericPublic = sp_core::sr25519::Public;
    }
}

/// Newly added price points processing logic
#[impl_for_tuples(5)]
pub trait OnNewPrice {
    fn on_new_price(currency: &eq_primitives::currency::Currency, price: FixedI64);
}

/// Substrate pallet configuration trait
pub trait Trait: CreateSignedTransaction<Call<Self>> {
    type AuthorityId: AppCrypto<Self::Public, Self::Signature>;
    type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
    type Call: From<Call<Self>>;

    /// Timestamp provider
    type UnixTime: UnixTime;
    /// Whitelist checks for price setters
    type Whitelist: CheckWhitelisted<Self::AccountId>;
    /// Pallet setting representing amount of time for which price median is valid
    type MedianPriceTimeout: Get<u64>;
    /// Pallet setting representing amount of time for which price point is valid
    type PriceTimeout: Get<u64>;
    /// Newly added price points processing logic
    type OnNewPrice: OnNewPrice;
}

/// Getter of an URL for currency price
trait WithUrl {
    /// Gets URL and JSON path for currency price
    fn get_url(&self, url_template: &str, path_template: &str) -> (String, String);
}

impl WithUrl for Currency {
    fn get_url(&self, url_template: &str, path_template: &str) -> (String, String) {
        let is_upper_case = match url_template.find("USD") {
            Some(_) => true,
            None => false,
        };

        let is_kraken = url_template.contains("api.kraken.com");

        let symbol = match self {
            Currency::Unknown => panic!("Nope!"),
            Currency::Usd => "usd",
            Currency::Eq => "eq",
            Currency::Eth => {
                if is_kraken {
                    "XETHZ"
                } else {
                    "eth"
                }
            }
            Currency::Btc => {
                if is_kraken {
                    "XXBTZ"
                } else {
                    "btc"
                }
            }
            Currency::Eos => "eos",
            Currency::Dot => "dot",
        };

        let upper_case_symbol = symbol.to_uppercase();

        (
            url_template.replace(
                "{$}",
                if is_upper_case {
                    upper_case_symbol.as_str()
                } else {
                    symbol
                },
            ),
            path_template.replace(
                "{$}",
                if is_upper_case {
                    upper_case_symbol.as_str()
                } else {
                    symbol
                },
            ),
        )
    }
}

/// Struct for storing added currency price data from one source
#[derive(Encode, Decode, Clone, Default, PartialEq, RuntimeDebug)]
pub struct DataPoint<AccountId, BlockNumber> {
    price: FixedI64,
    account_id: AccountId,
    block_number: BlockNumber,
    timestamp: u64,
}

impl<AccountId, BlockNumber> DataPoint<AccountId, BlockNumber> {
    pub fn get(&self) -> (&AccountId, i64) {
        (&self.account_id, self.price.into_inner())
    }
}

/// Struct for storing aggregated currency price data
#[derive(Encode, Decode, Clone, Default, PartialEq, RuntimeDebug)]
pub struct PricePoint<AccountId, BlockNumber> {
    block_number: BlockNumber,
    timestamp: u64,
    price: FixedI64,
    data_points: Vec<DataPoint<AccountId, BlockNumber>>,
}

impl<AccountId, BlockNumber> PricePoint<AccountId, BlockNumber> {
    pub fn get_block_number(&self) -> &BlockNumber {
        &self.block_number
    }

    pub fn get_timestamp(&self) -> u64 {
        self.timestamp
    }

    pub fn get_price(&self) -> i64 {
        self.price.into_inner()
    }

    pub fn get_data_points(&self) -> &Vec<DataPoint<AccountId, BlockNumber>> {
        &self.data_points
    }
}

/// Source of received price data points
#[derive(Debug)]
pub enum PriceSource {
    Custom,
    Cryptocompare,
}

impl PriceSource {
    /// Converts `resource_type` string into `PriceSource`
    pub fn get(resource_type: String) -> Option<PriceSource> {
        match resource_type.as_str() {
            "custom" => Some(PriceSource::Custom),
            "cryptocompare" => Some(PriceSource::Cryptocompare),
            _ => Option::None,
        }
    }

    /// Returns key for local storage that contains price source
    pub fn get_param_key(&self) -> &[u8] {
        match self {
            PriceSource::Custom => b"oracle::custom_query",
            PriceSource::Cryptocompare => b"oracle::cryptocompare_api_key",
        }
    }

    /// Gets query for price requests
    pub fn get_query(&self) -> Result<Option<String>, FromUtf8Error> {
        let query_param_raw =
            sp_io::offchain::local_storage_get(StorageKind::PERSISTENT, self.get_param_key());
        if let Some(query_param) = query_param_raw {
            let param = String::from_utf8(query_param)?;
            match self {
                PriceSource::Custom => return Ok(Some(param)),
                PriceSource::Cryptocompare => {
                    return Ok(Some(format!("json(https://min-api.cryptocompare.com/data/price?fsym={{$}}&tsyms=USD&api_key={}).USD", param)))
                },
            };
        }

        Ok(Option::None)
    }
}

decl_storage! {
    trait Store for Module<T: Trait> as Oracle
    {
        /// Pallet storage for added price points
        PricePoints get(fn price_points): map hasher(identity) Currency => PricePoint<<T as system::Trait>::AccountId, <T as system::Trait>::BlockNumber>;
    }
    add_extra_genesis {
        config(prices): Vec<(u8, u64, u64)>;
        config(update_date): u64;

        build(|config: &GenesisConfig| {
            let default_price_point = PricePoint {
                block_number: system::Module::<T>::block_number(),
                timestamp: 0,
                price: FixedI64::saturating_from_integer(-1),
                data_points: Vec::<DataPoint<T::AccountId, T::BlockNumber>>::new()
            };
            for currency in Currency::iterator() {
                <PricePoints<T>>::insert(currency, default_price_point.clone());
            }
            for &(currency, nom, denom) in config.prices.iter() {
                let currency_typed: Currency = currency.into();
                let price: FixedI64 = FixedI64::saturating_from_rational(nom, denom);

                <PricePoints<T>>::mutate(&currency_typed, |price_point| {
                    price_point.timestamp = config.update_date;
                    price_point.price = price;
                });
            }
        });
    }
}

decl_event!(
    pub enum Event<T>
    where
        AccountId = <T as system::Trait>::AccountId,
        Currency = eq_primitives::currency::Currency,
    {
        /// New price added to storage. Event contains: `Currency` for price,
        /// `FixedI64` for price value that was added, `FixedI64` for new
        /// aggregated price and `AccountId` of price submitter
        NewPrice(Currency, FixedI64, FixedI64, AccountId),
    }
);

decl_error! {
    /// Pallet's errors
    pub enum Error for Module<T: Trait> {
        /// Account is not allowed to set prices
        NotAllowedToSubmitPrice,
        /// The same price data point was already added
        PriceAlreadyAdded,
        /// Incorrect currency
        CurrencyNotFound,
        /// Attempting to submit price for constant price currencies
        WrongCurrency,
        /// Price cannot be zero
        PriceIsZero,
        /// Price cannot be negative
        PriceIsNegative,
        /// Price data point is too old and cannot be used
        PriceTimeout,
    }
}

decl_module! {
    pub struct Module<T: Trait> for enum Call where origin: T::Origin {
        fn deposit_event() = default;

        #[weight = 10_000]
        /// Adds new `DataPoint` containing `currency` price information. It
        /// would be used for `PricePoint` calculation. Only whitelisted
        /// accounts can add `DataPoint`s
        pub fn set_price(origin, currency: Currency, price: FixedI64) -> DispatchResult
        {
            let who = ensure_signed(origin)?;
            //debug::info!("Who: {:?}", &who);
            eq_ensure!(T::Whitelist::in_whitelist(&who), Error::<T>::NotAllowedToSubmitPrice,
            "{}:{}. Account not in whitelist. Who: {:?}.", file!(), line!(), who);
            eq_ensure!(price != FixedI64::from_inner(0), Error::<T>::PriceIsZero,
            "{}:{}. Price is equal to zero. Who: {:?}, price: {:?}, currency: {:?}.",
            file!(), line!(), who, price, currency);
            eq_ensure!(!price.is_negative(), Error::<T>::PriceIsNegative,
            "{}:{}. Price is negative. Who: {:?}, price: {:?}, currency: {:?}.",
            file!(), line!(), who, price, currency);
            eq_ensure!(currency != Currency::Usd, Error::<T>::WrongCurrency,
            "{}:{}. 'USD' is not allowed to set price. Who: {:?}, price: {:?}, currency: {:?}.",
            file!(), line!(), who, price, currency);
            eq_ensure!(currency != Currency::Unknown, Error::<T>::WrongCurrency,
            "{}:{}. 'Unknown' is not allowed to set price. Who: {:?}, price: {:?}, currency: {:?}.",
            file!(), line!(), who, price, currency);
            // eq_ensure!(<PricePoints<T>>::contains_key(&currency), Error::<T>::CurrencyNotFound,
            // "{:?}:{:?}. Currency is not contained in PricePoints. Who: {:?}, price: {:?}, currency: {:?}.",
            // file!(), line!(), who, price, currency);

            let mut new_price = price;
            <PricePoints<T>>::mutate(&currency, |price_point| {
                let current_block = system::Module::<T>::block_number();
                let current_time = T::UnixTime::now().as_secs(); // always same within block
                if price_point.block_number == current_block {
                    if price_point.data_points.iter().any(|x| x.account_id == who && x.block_number == current_block) {
                        debug::error!("{}:{}. Account already setted price. Who: {:?}, price: {:?}, block: {:?}, timestamp: {:?}.",
                        file!(), line!(), who, price_point.price, price_point.block_number, price_point.timestamp);
                        return Err(Error::<T>::PriceAlreadyAdded);
                    }
                }
                price_point.block_number = current_block;
                price_point.timestamp = current_time;
                let dp = DataPoint { account_id: who.clone(), price: price, block_number: current_block, timestamp: current_time };

                let white_list = T::Whitelist::accounts();
                let mut actual_data_points: Vec<_> = price_point.data_points.iter().filter(|x| current_time - x.timestamp < T::PriceTimeout::get()).collect();
                actual_data_points.push(&dp);

                let mut last_data_points: Vec<_> = Vec::new();
                for acc_id in white_list {
                    if let Some(i) = actual_data_points.iter().rposition(|x| x.account_id == acc_id) {
                        last_data_points.push(actual_data_points[i].clone());
                    }
                }

                last_data_points.sort_by(|a, b| a.price.cmp(&b.price));
                let len = last_data_points.len();
                if len % 2 == 0
                {
                    new_price= (last_data_points[len/2-1].price+last_data_points[len/2].price)/(FixedI64::one()+FixedI64::one());
                }
                else
                {
                    new_price= last_data_points[len/2].price;
                }
                price_point.price = new_price;
                price_point.data_points = last_data_points;
                eq_log!("Med(Avg) calc price:{:?} new_price:{:?} {:?}", price , new_price, &currency);
                Ok(())
            })?;

            T::OnNewPrice::on_new_price(&currency, price);
            Self::deposit_event(RawEvent::NewPrice(currency, price, new_price, who));

            Ok(())
        }

        /// Starts the off-chain task for given block number
        fn offchain_worker(_block_number: T::BlockNumber) {
            let publics = <T::AuthorityId as AppCrypto<T::Public, T::Signature>>::RuntimeAppPublic::all()
                .into_iter()
                .enumerate()
                .filter_map(|(_index, key)| {
                    let generic_public = <T::AuthorityId as AppCrypto<T::Public, T::Signature>>::GenericPublic::from(key);
                    let public = generic_public.into();
                    let account_id = public.clone().into_account();
                    if T::Whitelist::in_whitelist(&account_id) {
                        Option::Some(public.clone())
                    } else {
                        Option::None
                    }
                })
                .collect();

            let signer = Signer::<T, T::AuthorityId>::all_accounts().with_filter(publics);
            if !signer.can_sign() {
                // not in whitelist
                return;
            }

            let price_periodicity_o = Self::get_local_storage_val::<u32>("oracle::price_periodicity");
            if let Some(price_periodicity) = price_periodicity_o {
                if price_periodicity < 1 {
                    debug::warn!("Unexpected price periodicity {:?}, should be more than 1", price_periodicity);
                    return;
                }
                let counter = Self::get_local_storage_val::<u32>("oracle::counter");
                let counter = counter.unwrap_or(0_u32);
                let counter_next = counter + 1;
                if counter_next == price_periodicity {
                    sp_io::offchain::local_storage_set(StorageKind::PERSISTENT, b"oracle::counter", 0_u32.to_string().as_bytes());
                    let resource_type_o = Self::get_local_storage_val::<String>("oracle::resource_type");
                    if let Some(resource_type) = resource_type_o {
                        let rsrc = PriceSource::get(resource_type);
                        if let Some(resource) = rsrc {
                            match resource.get_query() {
                                Ok(qry) => {
                                    if let Some(query) = qry {
                                        for currency in Currency::iterator() {
                                            if currency == &Currency::Unknown || currency == &Currency::Usd {continue;}

                                            // testnet hardcode for EQ price = 0.17 Dot price
                                            // TODO: add feature and disable it in mainnet

                                            if currency == &Currency::Eq {
                                                continue;
                                            }

                                            let price = Self::fetch_price(currency, &query);

                                            if price.is_err()
                                            {
                                                debug::warn!("Can't fetch {:?} price from {}", currency, query);
                                                continue;
                                            }

                                            let price_unwrapped = price.unwrap();

                                            if currency == &Currency::Dot {
                                                let eq_price = price_unwrapped * FixedI64::saturating_from_rational(17, 100);

                                                signer.send_signed_transaction(|_account| {
                                                    // TODO check for if T::Whitelist::in_whitelist(&account)
                                                    Call::set_price(Currency::Eq, eq_price)
                                                });
                                            }

                                            signer.send_signed_transaction(|_account| {
                                                // TODO check for if T::Whitelist::in_whitelist(&account)
                                                Call::set_price(currency.clone(), price_unwrapped)
                                            });
                                        }
                                    }
                                },
                                Err(_e) => {
                                    debug::warn!("Can't decode {:?} price query", resource);
                                    return;
                                }
                            }
                        } else {
                            debug::warn!("Unexpected price resource type {:?}", rsrc);
                            return;
                        }
                    }
                } else if counter_next > price_periodicity {
                    sp_io::offchain::local_storage_set(StorageKind::PERSISTENT, b"oracle::counter", 0_u32.to_string().as_bytes());
                } else {
                    sp_io::offchain::local_storage_set(StorageKind::PERSISTENT, b"oracle::counter", counter_next.to_string().as_bytes());
                }
            }
        }

        #[weight = 10_000]
        pub fn print_node_message(origin, message: Vec<u8>) -> DispatchResult
        {
            ensure_root(origin)?;
            eq_log!("|{:?}", String::from_utf8(message));
            Ok(())
        }
    }
}

impl<T: Trait> Module<T> {
    fn exec_query(url: String) -> Result<String, http::Error> {
        let request = http::Request::get(url.as_str());
        let deadline = sp_io::offchain::timestamp().add(Duration::from_millis(2_000));
        let pending = request.deadline(deadline).send().map_err(|_| {
            debug::error!(
                "{}:{}. Error sending request. Request: {:?}, deadline: {:?}.",
                file!(),
                line!(),
                url.as_str(),
                deadline
            );
            http::Error::IoError
        })?;

        let response = pending.try_wait(deadline).map_err(|_| {
            debug::error!(
                "{}:{}. Didn't receive response. Deadline: {:?}.",
                file!(),
                line!(),
                deadline
            );
            http::Error::DeadlineReached
        })??;

        if response.code != 200 {
            debug::error!(
                "{}:{}. Unexpected status code: {}",
                file!(),
                line!(),
                response.code
            );
            return Err(http::Error::Unknown);
        }
        let body = response.body();
        let str = String::from_utf8(body.collect()).unwrap_or(String::new());
        Ok(str)
    }

    fn fetch_price_from_json(body: String, path: &str) -> Result<FixedI64, http::Error> {
        let err = http::Error::Unknown;
        let mut val: &json::Value = &json::from_str(&body).or_else(|_| {
            debug::warn!(
                "{:?}:{:?} {:?}. Cannot deserialize an instance from a string of JSON text.",
                file!(),
                line!(),
                body
            );
            Err(err.clone())
        })?;

        let indices = path.split(".");
        for index in indices {
            let offsets = get_index_offsets(index.as_bytes());
            if offsets.len() == 0 {
                let option_value = val.get(index);
                val = ok_or_error!(
                    option_value,
                    err.clone(),
                    "{}:{}. Couldn't access a value in a map. Json: {:?}, index: {:?}.",
                    file!(),
                    line!(),
                    val,
                    index
                )?;
            } else {
                // arrays
                for (start, end) in offsets {
                    if start != 0 {
                        let option_value = val.get(&index[..start]);
                        val = ok_or_error!(option_value, err.clone(), "{}:{}. Couldn't access an element of an array. Json: {:?}, index: {:?}.", 
                        file!(), line!(), val, &index[..start])?;
                    }
                    let i = &index[start + 1..end - 1]
                        .parse::<usize>()
                        .expect("Expect a number as array index");
                    let option_value = val.get(i);
                    val = ok_or_error!(
                        option_value,
                        err.clone(),
                        "{}:{}. Couldn't access an element of an array. Json: {:?}, index: {:?}.",
                        file!(),
                        line!(),
                        val,
                        i
                    )?;
                }
            }
        }

        let option_price = match val {
            json::Value::Number(v) => Ok(v.as_f64()),
            json::Value::String(v) => Ok(v.parse::<f64>().ok()),
            _ => Err({
                debug::error!(
                    "{}:{}. Value received from json not number or string. Value: {:?}.",
                    file!(),
                    line!(),
                    val
                );
                http::Error::Unknown
            }),
        }?;
        let price = ok_or_error!(
            option_price,
            err,
            "{}:{}. Couldn't get value as f64. Value: {:?}.",
            file!(),
            line!(),
            val
        )?;
        Ok(FixedI64::from_inner(
            (price * (FixedI64::accuracy() as f64)) as i64,
        ))
    }

    fn fetch_price(currency: &Currency, query: &String) -> Result<FixedI64, http::Error> {
        let url_bytes_offset = get_url_offset(query.as_bytes());

        if let Some((start, end)) = url_bytes_offset {
            // regex is \(.+\)\.
            let url_template = &query[start + 1..end - 2];
            if !url_template.contains("{$}") {
                debug::error!("{}:{}. Incorrect query format, doesn't have {{$}}. Query: {}, url template: {:?}.", 
                file!(), line!(), query, url_template);
                Err(http::Error::Unknown)
            } else {
                let path_template = &query[end..];
                let (url, path) = currency.get_url(url_template, path_template);
                let s = Self::exec_query(url)?;
                Self::fetch_price_from_json(s, path.as_str())
            }
        } else {
            debug::error!(
                "{}:{}. Incorrect query format, can't parse. Query: {}",
                file!(),
                line!(),
                query
            );
            Err(http::Error::Unknown)
        }
    }

    fn get_local_storage_val<R: FromStr>(key: &str) -> Option<R> {
        let raw_val = sp_io::offchain::local_storage_get(StorageKind::PERSISTENT, key.as_bytes());
        match raw_val {
            Some(val_bytes) => match String::from_utf8(val_bytes.clone()) {
                Ok(val_decoded) => match val_decoded.parse::<R>() {
                    Ok(val) => return Some(val),
                    Err(_e) => {
                        debug::warn!("Can't parse local storage value {:?}", val_decoded);
                        return None;
                    }
                },
                Err(_e) => {
                    debug::warn!("Can't decode local storage key {:?}: {:?}", key, val_bytes);
                    return None;
                }
            },
            None => {
                debug::warn!("Uninitialized local storage key: {:?}", key);
                return None;
            }
        }
    }
}

/// Interface for getting current system price of a `currency`
pub trait PriceGetter {
    /// Gets current price for given `currency`
    fn get_price(currency: &Currency) -> Result<FixedI64, sp_runtime::DispatchError>;
}

impl<T: Trait> PriceGetter for Module<T> {
    // Mb change to Option?
    fn get_price(currency: &Currency) -> Result<FixedI64, sp_runtime::DispatchError> {
        if currency == &Currency::Usd {
            return Ok(FixedI64::saturating_from_integer(1));
        }
        eq_ensure!(
            <PricePoints<T>>::contains_key(&currency),
            Error::<T>::CurrencyNotFound,
            "{}:{}. Currency not found in PricePoints. Currency: {:?}.",
            file!(),
            line!(),
            currency
        );
        let item = <PricePoints<T>>::get(&currency);
        let price = item.price;
        // eq_utils::log::eq_log!(" price is {:?}", price);
        eq_ensure!(
            price != FixedI64::from_inner(0),
            Error::<T>::PriceIsZero,
            "{}:{}. Price is equal to zero. Price: {:?}, currency: {:?}.",
            file!(),
            line!(),
            price,
            currency
        );
        eq_ensure!(
            !price.is_negative(),
            Error::<T>::PriceIsNegative,
            "{}:{}. Price is negative. Price: {:?}, currency: {:?}.",
            file!(),
            line!(),
            price,
            currency
        );
        /*let current_block = system::Module::<T>::block_number();
        eq_ensure!(
            (current_block<item.block_number),
            Error::<T>::PriceTimeout
        );*/
        let current_time = T::UnixTime::now().as_secs();
        eq_ensure!(
            (current_time < item.timestamp + T::MedianPriceTimeout::get()),
            Error::<T>::PriceTimeout,
            "{}:{}. Price received after time is out. Current time: {:?}, price_point timestamp + {:?} seconds: {:?}.", 
            file!(), line!(), current_time, T::MedianPriceTimeout::get(), item.timestamp + T::MedianPriceTimeout::get()
        );
        Ok(price)
    }
}