rippled
STCurrency.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2023 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/STCurrency.h>
21 #include <ripple/protocol/jss.h>
22 
23 #include <ripple/basics/contract.h>
24 
25 namespace ripple {
26 
27 STCurrency::STCurrency(SField const& name) : STBase{name}
28 {
29 }
30 
31 STCurrency::STCurrency(SerialIter& sit, SField const& name) : STBase{name}
32 {
33  currency_ = sit.get160();
34 }
35 
36 STCurrency::STCurrency(SField const& name, Currency const& currency)
37  : STBase{name}, currency_{currency}
38 {
39 }
40 
43 {
44  return STI_CURRENCY;
45 }
46 
49 {
50  return to_string(currency_);
51 }
52 
54 {
55  return to_string(currency_);
56 }
57 
58 void
60 {
62 }
63 
64 bool
66 {
67  const STCurrency* v = dynamic_cast<const STCurrency*>(&t);
68  return v && (*v == *this);
69 }
70 
71 bool
73 {
74  return isXRP(currency_);
75 }
76 
79 {
80  return std::make_unique<STCurrency>(sit, name);
81 }
82 
83 STBase*
84 STCurrency::copy(std::size_t n, void* buf) const
85 {
86  return emplace(n, buf, *this);
87 }
88 
89 STBase*
91 {
92  return emplace(n, buf, std::move(*this));
93 }
94 
96 currencyFromJson(SField const& name, Json::Value const& v)
97 {
98  if (!v.isString())
99  {
100  Throw<std::runtime_error>(
101  "currencyFromJson currency must be a string Json value");
102  }
103 
104  auto const currency = to_currency(v.asString());
105  if (currency == badCurrency() || currency == noCurrency())
106  {
107  Throw<std::runtime_error>(
108  "currencyFromJson currency must be a valid currency");
109  }
110 
111  return STCurrency{name, currency};
112 }
113 
114 } // namespace ripple
ripple::badCurrency
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
Definition: UintTypes.cpp:129
ripple::to_currency
bool to_currency(Currency &currency, std::string const &code)
Tries to convert a string to a Currency, returns true on success.
Definition: UintTypes.cpp:80
std::string
STL class.
Json::Value::isString
bool isString() const
Definition: json_value.cpp:1009
ripple::noCurrency
Currency const & noCurrency()
A placeholder for empty currencies.
Definition: UintTypes.cpp:122
ripple::STCurrency::add
void add(Serializer &s) const override
Definition: STCurrency.cpp:59
ripple::STCurrency::isEquivalent
bool isEquivalent(const STBase &t) const override
Definition: STCurrency.cpp:65
ripple::currencyFromJson
STCurrency currencyFromJson(SField const &name, Json::Value const &v)
Definition: STCurrency.cpp:96
ripple::SerializedTypeID
SerializedTypeID
Definition: SField.h:106
ripple::STCurrency::isDefault
bool isDefault() const override
Definition: STCurrency.cpp:72
ripple::STCurrency::getText
std::string getText() const override
Definition: STCurrency.cpp:48
ripple::base_uint< 160, detail::CurrencyTag >
ripple::STCurrency::STCurrency
STCurrency()=default
ripple::STBase::emplace
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition: STBase.h:220
ripple::STCurrency::currency_
Currency currency_
Definition: STCurrency.h:34
ripple::STCurrency::construct
static std::unique_ptr< STCurrency > construct(SerialIter &, SField const &name)
Definition: STCurrency.cpp:78
ripple::STCurrency::move
STBase * move(std::size_t n, void *buf) override
Definition: STCurrency.cpp:90
ripple::isXRP
bool isXRP(AccountID const &c)
Definition: AccountID.h:91
ripple::SerialIter
Definition: Serializer.h:311
ripple::STCurrency
Definition: STCurrency.h:31
ripple::Serializer
Definition: Serializer.h:40
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Serializer::addBitString
int addBitString(base_uint< Bits, Tag > const &v)
Definition: Serializer.h:98
ripple::JsonOptions
Note, should be treated as flags that can be | and &.
Definition: STBase.h:35
ripple::SField
Identifies fields.
Definition: SField.h:141
ripple::STBase
A type which can be exported to a well known binary format.
Definition: STBase.h:121
ripple::STCurrency::getSType
SerializedTypeID getSType() const override
Definition: STCurrency.cpp:42
ripple::STCurrency::copy
STBase * copy(std::size_t n, void *buf) const override
Definition: STCurrency.cpp:84
std::size_t
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:38
ripple::STCurrency::getJson
Json::Value getJson(JsonOptions) const override
Definition: STCurrency.cpp:53
std::unique_ptr
STL class.
Json::Value
Represents a JSON value.
Definition: json_value.h:145
Json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:469