rippled
Loading...
Searching...
No Matches
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 <xrpl/protocol/STCurrency.h>
21#include <xrpl/protocol/jss.h>
22
23#include <xrpl/basics/contract.h>
24
25namespace ripple {
26
28{
29}
30
32{
33 currency_ = sit.get160();
34}
35
36STCurrency::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
55{
56 return to_string(currency_);
57}
58
59void
61{
63}
64
65bool
67{
68 const STCurrency* v = dynamic_cast<const STCurrency*>(&t);
69 return v && (*v == *this);
70}
71
72bool
74{
75 return isXRP(currency_);
76}
77
80{
81 return std::make_unique<STCurrency>(sit, name);
82}
83
84STBase*
85STCurrency::copy(std::size_t n, void* buf) const
86{
87 return emplace(n, buf, *this);
88}
89
90STBase*
92{
93 return emplace(n, buf, std::move(*this));
94}
95
97currencyFromJson(SField const& name, Json::Value const& v)
98{
99 if (!v.isString())
100 {
101 Throw<std::runtime_error>(
102 "currencyFromJson currency must be a string Json value");
103 }
104
105 auto const currency = to_currency(v.asString());
106 if (currency == badCurrency() || currency == noCurrency())
107 {
108 Throw<std::runtime_error>(
109 "currencyFromJson currency must be a valid currency");
110 }
111
112 return STCurrency{name, currency};
113}
114
115} // namespace ripple
Represents a JSON value.
Definition: json_value.h:147
bool isString() const
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:469
Identifies fields.
Definition: SField.h:144
A type which can be exported to a well known binary format.
Definition: STBase.h:124
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition: STBase.h:222
bool isDefault() const override
Definition: STCurrency.cpp:73
Currency currency_
Definition: STCurrency.h:34
void add(Serializer &s) const override
Definition: STCurrency.cpp:60
SerializedTypeID getSType() const override
Definition: STCurrency.cpp:42
Json::Value getJson(JsonOptions) const override
Definition: STCurrency.cpp:54
STCurrency()=default
std::string getText() const override
Definition: STCurrency.cpp:48
bool isEquivalent(const STBase &t) const override
Definition: STCurrency.cpp:66
static std::unique_ptr< STCurrency > construct(SerialIter &, SField const &name)
Definition: STCurrency.cpp:79
STBase * move(std::size_t n, void *buf) override
Definition: STCurrency.cpp:91
STBase * copy(std::size_t n, void *buf) const override
Definition: STCurrency.cpp:85
uint160 get160()
Definition: Serializer.h:409
int addBitString(base_uint< Bits, Tag > const &v)
Definition: Serializer.h:131
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
Definition: UintTypes.cpp:129
bool isXRP(AccountID const &c)
Definition: AccountID.h:91
STCurrency currencyFromJson(SField const &name, Json::Value const &v)
Definition: STCurrency.cpp:97
SerializedTypeID
Definition: SField.h:108
Currency const & noCurrency()
A placeholder for empty currencies.
Definition: UintTypes.cpp:122
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:629
bool to_currency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition: UintTypes.cpp:80
Note, should be treated as flags that can be | and &.
Definition: STBase.h:36