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