rippled
Loading...
Searching...
No Matches
Asset.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2024 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/Asset.h>
21#include <xrpl/protocol/Indexes.h>
22#include <xrpl/protocol/jss.h>
23
24namespace ripple {
25
26AccountID const&
28{
29 return std::visit(
30 [&](auto&& issue) -> AccountID const& { return issue.getIssuer(); },
31 issue_);
32}
33
36{
37 return std::visit([&](auto&& issue) { return issue.getText(); }, issue_);
38}
39
40void
42{
43 std::visit([&](auto&& issue) { issue.setJson(jv); }, issue_);
44}
45
47to_string(Asset const& asset)
48{
49 return std::visit(
50 [&](auto const& issue) { return to_string(issue); }, asset.value());
51}
52
53bool
55{
56 if (jv.isMember(jss::mpt_issuance_id))
57 return !(jv.isMember(jss::currency) || jv.isMember(jss::issuer));
58 return jv.isMember(jss::currency);
59}
60
61Asset
63{
64 if (!v.isMember(jss::currency) && !v.isMember(jss::mpt_issuance_id))
65 Throw<std::runtime_error>(
66 "assetFromJson must contain currency or mpt_issuance_id");
67
68 if (v.isMember(jss::currency))
69 return issueFromJson(v);
70 return mptIssueFromJson(v);
71}
72
74to_json(Asset const& asset)
75{
76 return std::visit(
77 [&](auto const& issue) { return to_json(issue); }, asset.value());
78}
79
80} // namespace ripple
Represents a JSON value.
Definition: json_value.h:147
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:943
void setJson(Json::Value &jv) const
Definition: Asset.cpp:41
constexpr value_type const & value() const
Definition: Asset.h:143
AccountID const & getIssuer() const
Definition: Asset.cpp:27
value_type issue_
Definition: Asset.h:51
std::string getText() const
Definition: Asset.cpp:35
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition: AccountID.h:49
Asset assetFromJson(Json::Value const &jv)
Definition: Asset.cpp:62
MPTIssue mptIssueFromJson(Json::Value const &jv)
Definition: MPTIssue.cpp:75
Json::Value to_json(Asset const &asset)
Definition: Asset.cpp:74
bool validJSONAsset(Json::Value const &jv)
Definition: Asset.cpp:54
Issue issueFromJson(Json::Value const &v)
Definition: Issue.cpp:90
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:629
T visit(T... args)