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