Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
beem
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
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
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
hive
beem
Commits
e64523e1
Commit
e64523e1
authored
7 years ago
by
Fabian Schuh
Browse files
Options
Downloads
Patches
Plain Diff
[price] fix multiplication of prices
parent
ff7178c1
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
bitshares/price.py
+17
-7
17 additions, 7 deletions
bitshares/price.py
tests/test_price.py
+5
-5
5 additions, 5 deletions
tests/test_price.py
with
22 additions
and
12 deletions
bitshares/price.py
+
17
−
7
View file @
e64523e1
...
@@ -238,14 +238,24 @@ class Price(dict):
...
@@ -238,14 +238,24 @@ class Price(dict):
a
=
self
.
copy
()
a
=
self
.
copy
()
if
isinstance
(
other
,
Price
):
if
isinstance
(
other
,
Price
):
# Rotate/invert other
# Rotate/invert other
if
self
[
"
quote
"
][
"
symbol
"
]
in
other
.
symbols
():
if
(
other
=
other
.
as_quote
(
self
[
"
quote
"
][
"
symbol
"
])
self
[
"
quote
"
][
"
symbol
"
]
not
in
other
.
symbols
()
and
elif
self
[
"
base
"
][
"
symbol
"
]
in
other
.
symbols
():
self
[
"
base
"
][
"
symbol
"
]
not
in
other
.
symbols
()
other
=
other
.
as_quote
(
self
[
"
base
"
][
"
symbol
"
])
):
else
:
raise
InvalidAssetException
raise
InvalidAssetException
a
[
"
base
"
]
=
Amount
(
float
(
self
[
"
base
"
]
*
other
[
"
base
"
]),
other
[
"
base
"
][
"
symbol
"
])
a
[
"
quote
"
]
=
Amount
(
float
(
self
[
"
quote
"
]
*
other
[
"
quote
"
]),
self
[
"
quote
"
][
"
symbol
"
])
# base/quote = a/b
# a/b * b/c = a/c
a
=
self
.
copy
()
if
self
[
"
quote
"
][
"
symbol
"
]
==
other
[
"
base
"
][
"
symbol
"
]:
a
[
"
base
"
]
=
Amount
(
float
(
self
[
"
base
"
])
*
float
(
other
[
"
base
"
]),
self
[
"
base
"
][
"
symbol
"
])
a
[
"
quote
"
]
=
Amount
(
float
(
self
[
"
quote
"
])
*
float
(
other
[
"
quote
"
]),
other
[
"
quote
"
][
"
symbol
"
])
# a/b * c/a = c/b
elif
self
[
"
base
"
][
"
symbol
"
]
==
other
[
"
quote
"
][
"
symbol
"
]:
a
[
"
base
"
]
=
Amount
(
float
(
self
[
"
base
"
])
*
float
(
other
[
"
base
"
]),
other
[
"
base
"
][
"
symbol
"
])
a
[
"
quote
"
]
=
Amount
(
float
(
self
[
"
quote
"
])
*
float
(
other
[
"
quote
"
]),
self
[
"
quote
"
][
"
symbol
"
])
else
:
raise
ValueError
(
"
Wrong rotation of prices
"
)
elif
isinstance
(
other
,
Amount
):
elif
isinstance
(
other
,
Amount
):
assert
other
[
"
asset
"
][
"
id
"
]
==
self
[
"
quote
"
][
"
asset
"
][
"
id
"
]
assert
other
[
"
asset
"
][
"
id
"
]
==
self
[
"
quote
"
][
"
asset
"
][
"
id
"
]
a
=
other
.
copy
()
*
self
[
"
price
"
]
a
=
other
.
copy
()
*
self
[
"
price
"
]
...
...
This diff is collapsed.
Click to expand it.
tests/test_price.py
+
5
−
5
View file @
e64523e1
...
@@ -35,14 +35,14 @@ class Testcases(unittest.TestCase):
...
@@ -35,14 +35,14 @@ class Testcases(unittest.TestCase):
def
test_multiplication
(
self
):
def
test_multiplication
(
self
):
p1
=
Price
(
10.0
,
"
USD/GOLD
"
)
p1
=
Price
(
10.0
,
"
USD/GOLD
"
)
p2
=
Price
(
5.0
,
"
USD/EUR
"
)
p2
=
Price
(
5.0
,
"
EUR/USD
"
)
p3
=
p1
*
p2
p3
=
p1
*
p2
p4
=
p3
.
as_base
(
"
GOLD
"
)
p4
=
p3
.
as_base
(
"
GOLD
"
)
self
.
assertEqual
(
p4
[
"
quote
"
][
"
symbol
"
],
"
EUR
"
)
self
.
assertEqual
(
p4
[
"
quote
"
][
"
symbol
"
],
"
EUR
"
)
self
.
assertEqual
(
p4
[
"
base
"
][
"
symbol
"
],
"
GOLD
"
)
self
.
assertEqual
(
p4
[
"
base
"
][
"
symbol
"
],
"
GOLD
"
)
# 10 USD/GOLD * 0.2 EUR/USD =
2
EUR/GOLD = 0.
5
GOLD/EUR
# 10 USD/GOLD * 0.2 EUR/USD =
50
EUR/GOLD = 0.
02
GOLD/EUR
self
.
assertEqual
(
float
(
p4
),
0.
5
)
self
.
assertEqual
(
float
(
p4
),
0.
02
)
# Inline multiplication
# Inline multiplication
p5
=
p1
p5
=
p1
...
@@ -50,8 +50,8 @@ class Testcases(unittest.TestCase):
...
@@ -50,8 +50,8 @@ class Testcases(unittest.TestCase):
p4
=
p5
.
as_base
(
"
GOLD
"
)
p4
=
p5
.
as_base
(
"
GOLD
"
)
self
.
assertEqual
(
p4
[
"
quote
"
][
"
symbol
"
],
"
EUR
"
)
self
.
assertEqual
(
p4
[
"
quote
"
][
"
symbol
"
],
"
EUR
"
)
self
.
assertEqual
(
p4
[
"
base
"
][
"
symbol
"
],
"
GOLD
"
)
self
.
assertEqual
(
p4
[
"
base
"
][
"
symbol
"
],
"
GOLD
"
)
# 10 USD/GOLD * 0.2 EUR/USD = 2 EUR/GOLD = 0.
5
GOLD/EUR
# 10 USD/GOLD * 0.2 EUR/USD = 2 EUR/GOLD = 0.
02
GOLD/EUR
self
.
assertEqual
(
float
(
p4
),
0.
5
)
self
.
assertEqual
(
float
(
p4
),
0.
02
)
def
test_div
(
self
):
def
test_div
(
self
):
p1
=
Price
(
10.0
,
"
USD/GOLD
"
)
p1
=
Price
(
10.0
,
"
USD/GOLD
"
)
...
...
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