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
30 as_amount(Rate const& rate)
31 {
32  return {noIssue(), rate.value, -9, false};
33 }
34 
35 } // namespace detail
36 
37 namespace nft {
38 Rate
40 {
41  return Rate{static_cast<std::uint32_t>(fee) * 10000};
42 }
43 
44 } // namespace nft
45 
46 STAmount
47 multiply(STAmount const& amount, Rate const& rate)
48 {
49  assert(rate.value != 0);
50 
51  if (rate == parityRate)
52  return amount;
53 
54  return multiply(amount, detail::as_amount(rate), amount.issue());
55 }
56 
57 STAmount
58 multiplyRound(STAmount const& amount, Rate const& rate, bool roundUp)
59 {
60  assert(rate.value != 0);
61 
62  if (rate == parityRate)
63  return amount;
64 
65  return mulRound(amount, detail::as_amount(rate), amount.issue(), roundUp);
66 }
67 
68 STAmount
70  STAmount const& amount,
71  Rate const& rate,
72  Issue const& issue,
73  bool roundUp)
74 {
75  assert(rate.value != 0);
76 
77  if (rate == parityRate)
78  {
79  return amount;
80  }
81 
82  return mulRound(amount, detail::as_amount(rate), issue, roundUp);
83 }
84 
85 STAmount
86 divide(STAmount const& amount, Rate const& rate)
87 {
88  assert(rate.value != 0);
89 
90  if (rate == parityRate)
91  return amount;
92 
93  return divide(amount, detail::as_amount(rate), amount.issue());
94 }
95 
96 STAmount
97 divideRound(STAmount const& amount, Rate const& rate, bool roundUp)
98 {
99  assert(rate.value != 0);
100 
101  if (rate == parityRate)
102  return amount;
103 
104  return divRound(amount, detail::as_amount(rate), amount.issue(), roundUp);
105 }
106 
107 STAmount
109  STAmount const& amount,
110  Rate const& rate,
111  Issue const& issue,
112  bool roundUp)
113 {
114  assert(rate.value != 0);
115 
116  if (rate == parityRate)
117  return amount;
118 
119  return divRound(amount, detail::as_amount(rate), issue, roundUp);
120 }
121 
122 } // namespace ripple
ripple::Issue
A currency issued by an account.
Definition: Issue.h:34
ripple::Rate
Represents a transfer rate.
Definition: Rate.h:37
ripple::STAmount::issue
Issue const & issue() const
Definition: STAmount.h:347
ripple::nft::transferFeeAsRate
Rate transferFeeAsRate(std::uint16_t fee)
Given a transfer fee (in basis points) convert it to a transfer rate.
Definition: Rate2.cpp:39
ripple::multiplyRound
STAmount multiplyRound(STAmount const &amount, Rate const &rate, bool roundUp)
Definition: Rate2.cpp:58
ripple::noIssue
Issue const & noIssue()
Returns an asset specifier that represents no account and currency.
Definition: Issue.h:103
ripple::divideRound
STAmount divideRound(STAmount const &amount, Rate const &rate, bool roundUp)
Definition: Rate2.cpp:97
ripple::parityRate
const Rate parityRate(QUALITY_ONE)
A transfer rate signifying a 1:1 exchange.
Definition: Rate.h:101
ripple::mulRound
STAmount mulRound(STAmount const &v1, STAmount const &v2, Issue const &issue, bool roundUp)
Definition: STAmount.cpp:1305
ripple::divide
STAmount divide(STAmount const &amount, Rate const &rate)
Definition: Rate2.cpp:86
ripple::divRound
STAmount divRound(STAmount const &num, STAmount const &den, Issue const &issue, bool roundUp)
Definition: STAmount.cpp:1391
ripple::STAmount
Definition: STAmount.h:45
std::uint16_t
ripple::multiply
STAmount multiply(STAmount const &amount, Rate const &rate)
Definition: Rate2.cpp:47
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::detail::as_amount
STAmount as_amount(Rate const &rate)
Definition: Rate2.cpp:30