Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
open-runtime-module-library
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Noxim
open-runtime-module-library
Commits
8afaae67
Unverified
Commit
8afaae67
authored
5 years ago
by
Shaopeng Wang
Committed by
GitHub
5 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Prices module: update price type. (#48)
parent
335bef8b
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
prices/src/lib.rs
+10
-11
10 additions, 11 deletions
prices/src/lib.rs
prices/src/mock.rs
+3
-5
3 additions, 5 deletions
prices/src/mock.rs
prices/src/tests.rs
+2
-2
2 additions, 2 deletions
prices/src/tests.rs
with
15 additions
and
18 deletions
prices/src/lib.rs
+
10
−
11
View file @
8afaae67
...
...
@@ -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
)
}
}
This diff is collapsed.
Click to expand it.
prices/src/mock.rs
+
3
−
5
View file @
8afaae67
...
...
@@ -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
;
}
...
...
This diff is collapsed.
Click to expand it.
prices/src/tests.rs
+
2
−
2
View file @
8afaae67
...
...
@@ -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
)
));
});
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment