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/basics/contract.h>
21#include <xrpl/json/json_value.h>
22#include <xrpl/protocol/SField.h>
23#include <xrpl/protocol/STBase.h>
24#include <xrpl/protocol/STCurrency.h>
25#include <xrpl/protocol/Serializer.h>
26#include <xrpl/protocol/UintTypes.h>
27
28#include <cstddef>
29#include <memory>
30#include <stdexcept>
31#include <string>
32#include <utility>
33
34namespace ripple {
35
37{
38}
39
41{
42 currency_ = sit.get160();
43}
44
45STCurrency::STCurrency(SField const& name, Currency const& currency)
46 : STBase{name}, currency_{currency}
47{
48}
49
52{
53 return STI_CURRENCY;
54}
55
58{
59 return to_string(currency_);
60}
61
67
68void
73
74bool
76{
77 STCurrency const* v = dynamic_cast<STCurrency const*>(&t);
78 return v && (*v == *this);
79}
80
81bool
83{
84 return isXRP(currency_);
85}
86
89{
90 return std::make_unique<STCurrency>(sit, name);
91}
92
93STBase*
94STCurrency::copy(std::size_t n, void* buf) const
95{
96 return emplace(n, buf, *this);
97}
98
99STBase*
101{
102 return emplace(n, buf, std::move(*this));
103}
104
106currencyFromJson(SField const& name, Json::Value const& v)
107{
108 if (!v.isString())
109 {
110 Throw<std::runtime_error>(
111 "currencyFromJson currency must be a string Json value");
112 }
113
114 auto const currency = to_currency(v.asString());
115 if (currency == badCurrency() || currency == noCurrency())
116 {
117 Throw<std::runtime_error>(
118 "currencyFromJson currency must be a valid currency");
119 }
120
121 return STCurrency{name, currency};
122}
123
124} // namespace ripple
Represents a JSON value.
Definition json_value.h:149
bool isString() const
std::string asString() const
Returns the unquoted string value.
Identifies fields.
Definition SField.h:146
A type which can be exported to a well known binary format.
Definition STBase.h:135
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition STBase.h:233
bool isEquivalent(STBase const &t) const override
bool isDefault() const override
void add(Serializer &s) const override
SerializedTypeID getSType() const override
Json::Value getJson(JsonOptions) const override
STCurrency()=default
std::string getText() const override
static std::unique_ptr< STCurrency > construct(SerialIter &, SField const &name)
STBase * move(std::size_t n, void *buf) override
STBase * copy(std::size_t n, void *buf) const override
int addBitString(base_uint< Bits, Tag > const &v)
Definition Serializer.h:131
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
bool isXRP(AccountID const &c)
Definition AccountID.h:90
STCurrency currencyFromJson(SField const &name, Json::Value const &v)
SerializedTypeID
Definition SField.h:110
Currency const & noCurrency()
A placeholder for empty currencies.
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:630
bool to_currency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition UintTypes.cpp:84
Note, should be treated as flags that can be | and &.
Definition STBase.h:37