rippled
Rate2.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2015 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/protocol/Quality.h>
21 #include <ripple/protocol/Rate.h>
22 
23 namespace ripple {
24 
25 Rate const parityRate (QUALITY_ONE);
26 
27 namespace detail {
28 
29 STAmount as_amount (Rate const& rate)
30 {
31  return { noIssue(), rate.value, -9, false };
32 }
33 
34 }
35 
36 STAmount
38  STAmount const& amount,
39  Rate const& rate)
40 {
41  assert (rate.value != 0);
42 
43  if (rate == parityRate)
44  return amount;
45 
46  return multiply (
47  amount,
48  detail::as_amount(rate),
49  amount.issue());
50 }
51 
52 STAmount
54  STAmount const& amount,
55  Rate const& rate,
56  bool roundUp)
57 {
58  assert (rate.value != 0);
59 
60  if (rate == parityRate)
61  return amount;
62 
63  return mulRound (
64  amount,
65  detail::as_amount(rate),
66  amount.issue(),
67  roundUp);
68 }
69 
70 STAmount
72  STAmount const& amount,
73  Rate const& rate,
74  Issue const& issue,
75  bool roundUp)
76 {
77  assert (rate.value != 0);
78 
79  if (rate == parityRate)
80  {
81  return amount;
82  }
83 
84  return mulRound (
85  amount,
86  detail::as_amount(rate),
87  issue,
88  roundUp);
89 }
90 
91 STAmount
93  STAmount const& amount,
94  Rate const& rate)
95 {
96  assert (rate.value != 0);
97 
98  if (rate == parityRate)
99  return amount;
100 
101  return divide (
102  amount,
103  detail::as_amount(rate),
104  amount.issue());
105 }
106 
107 STAmount
109  STAmount const& amount,
110  Rate const& rate,
111  bool roundUp)
112 {
113  assert (rate.value != 0);
114 
115  if (rate == parityRate)
116  return amount;
117 
118  return divRound (
119  amount,
120  detail::as_amount(rate),
121  amount.issue(),
122  roundUp);
123 }
124 
125 STAmount
127  STAmount const& amount,
128  Rate const& rate,
129  Issue const& issue,
130  bool roundUp)
131 {
132  assert (rate.value != 0);
133 
134  if (rate == parityRate)
135  return amount;
136 
137  return divRound (
138  amount,
139  detail::as_amount(rate),
140  issue,
141  roundUp);
142 }
143 
144 }
ripple::Issue
A currency issued by an account.
Definition: Issue.h:34
ripple::Rate
Represents a transfer rate.
Definition: Rate.h:37
ripple::multiplyRound
STAmount multiplyRound(STAmount const &amount, Rate const &rate, bool roundUp)
Definition: Rate2.cpp:53
ripple::noIssue
Issue const & noIssue()
Returns an asset specifier that represents no account and currency.
Definition: Issue.h:104
ripple::divideRound
STAmount divideRound(STAmount const &amount, Rate const &rate, bool roundUp)
Definition: Rate2.cpp:108
ripple::parityRate
const Rate parityRate(QUALITY_ONE)
A transfer rate signifying a 1:1 exchange.
Definition: Rate.h:110
ripple::mulRound
STAmount mulRound(STAmount const &v1, STAmount const &v2, Issue const &issue, bool roundUp)
Definition: STAmount.cpp:1173
ripple::divide
STAmount divide(STAmount const &amount, Rate const &rate)
Definition: Rate2.cpp:92
ripple::divRound
STAmount divRound(STAmount const &num, STAmount const &den, Issue const &issue, bool roundUp)
Definition: STAmount.cpp:1257
ripple::STAmount
Definition: STAmount.h:42
ripple::multiply
STAmount multiply(STAmount const &amount, Rate const &rate)
Definition: Rate2.cpp:37
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::STAmount::issue
Issue const & issue() const
Definition: STAmount.h:156
ripple::detail::as_amount
STAmount as_amount(Rate const &rate)
Definition: Rate2.cpp:29