Skip to content
Snippets Groups Projects
Unverified Commit 8afaae67 authored by Shaopeng Wang's avatar Shaopeng Wang Committed by GitHub
Browse files

Prices module: update price type. (#48)

parent 335bef8b
No related branches found
No related tags found
No related merge requests found
......@@ -2,12 +2,14 @@
use frame_support::{decl_module, decl_storage, Parameter};
use orml_traits::{DataProvider, PriceProvider};
use sr_primitives::traits::{MaybeSerializeDeserialize, Member, SimpleArithmetic, Zero};
use orml_utilities::FixedU128;
use sr_primitives::traits::{MaybeSerializeDeserialize, Member};
pub type Price = FixedU128;
pub trait Trait: frame_system::Trait {
type CurrencyId: Parameter + Member + Copy + MaybeSerializeDeserialize;
type Price: Parameter + Member + Zero + SimpleArithmetic + Copy + Ord;
type Source: DataProvider<Self::CurrencyId, Self::Price>;
type Source: DataProvider<Self::CurrencyId, Price>;
}
mod mock;
......@@ -23,14 +25,11 @@ decl_module! {
impl<T: Trait> Module<T> {}
impl<T: Trait> PriceProvider<T::CurrencyId, T::Price> for Module<T> {
fn get_price(base: T::CurrencyId, quote: T::CurrencyId) -> Option<T::Price> {
if let (Some(base_price), Some(quote_price)) = (T::Source::get(&base), (T::Source::get(&quote))) {
if !base_price.is_zero() {
return Some(quote_price / base_price);
}
}
impl<T: Trait> PriceProvider<T::CurrencyId, Price> for Module<T> {
fn get_price(base_currency_id: T::CurrencyId, quote_currency_id: T::CurrencyId) -> Option<Price> {
let base_price = T::Source::get(&base_currency_id)?;
let quote_price = T::Source::get(&quote_currency_id)?;
None
quote_price.checked_div(&base_price)
}
}
......@@ -44,16 +44,15 @@ impl frame_system::Trait for Runtime {
type Version = ();
}
type Price = u32;
type CurrencyId = u32;
pub struct MockDataProvider;
impl DataProvider<CurrencyId, Price> for MockDataProvider {
fn get(currency: &CurrencyId) -> Option<Price> {
match currency {
0 => Some(0),
1 => Some(1),
2 => Some(2),
0 => Some(Price::from_parts(0)),
1 => Some(Price::from_parts(1)),
2 => Some(Price::from_parts(2)),
_ => None,
}
}
......@@ -61,7 +60,6 @@ impl DataProvider<CurrencyId, Price> for MockDataProvider {
impl Trait for Runtime {
type CurrencyId = CurrencyId;
type Price = Price;
type Source = MockDataProvider;
}
......
......@@ -8,7 +8,7 @@ use mock::{ExtBuilder, PricesModule};
#[test]
fn get_price_should_work() {
ExtBuilder::default().build().execute_with(|| {
assert_eq!(PricesModule::get_price(1, 2), Some(2));
assert_eq!(PricesModule::get_price(1, 2), Some(Price::from_rational(2, 1)));
});
}
......@@ -25,6 +25,6 @@ fn price_is_none_should_not_panic() {
fn price_is_zero_should_not_panic() {
ExtBuilder::default().build().execute_with(|| {
assert_eq!(PricesModule::get_price(0, 0), None);
assert_eq!(PricesModule::get_price(1, 0), Some(0));
assert_eq!(PricesModule::get_price(1, 0), Some(Price::from_parts(0)));
});
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment