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/basics/contract.h>
21#include <xrpl/json/json_value.h>
22#include <xrpl/protocol/AccountID.h>
23#include <xrpl/protocol/Asset.h>
24#include <xrpl/protocol/Issue.h>
25#include <xrpl/protocol/MPTIssue.h>
26#include <xrpl/protocol/jss.h>
27
28#include <stdexcept>
29#include <string>
30#include <variant>
31
32namespace ripple {
33
34AccountID const&
36{
37 return std::visit(
38 [&](auto&& issue) -> AccountID const& { return issue.getIssuer(); },
39 issue_);
40}
41
44{
45 return std::visit([&](auto&& issue) { return issue.getText(); }, issue_);
46}
47
48void
50{
51 std::visit([&](auto&& issue) { issue.setJson(jv); }, issue_);
52}
53
55to_string(Asset const& asset)
56{
57 return std::visit(
58 [&](auto const& issue) { return to_string(issue); }, asset.value());
59}
60
61bool
63{
64 if (jv.isMember(jss::mpt_issuance_id))
65 return !(jv.isMember(jss::currency) || jv.isMember(jss::issuer));
66 return jv.isMember(jss::currency);
67}
68
69Asset
71{
72 if (!v.isMember(jss::currency) && !v.isMember(jss::mpt_issuance_id))
73 Throw<std::runtime_error>(
74 "assetFromJson must contain currency or mpt_issuance_id");
75
76 if (v.isMember(jss::currency))
77 return issueFromJson(v);
78 return mptIssueFromJson(v);
79}
80
82to_json(Asset const& asset)
83{
84 return std::visit(
85 [&](auto const& issue) { return to_json(issue); }, asset.value());
86}
87
88} // namespace ripple
Represents a JSON value.
Definition: json_value.h:148
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:949
void setJson(Json::Value &jv) const
Definition: Asset.cpp:49
constexpr value_type const & value() const
Definition: Asset.h:143
AccountID const & getIssuer() const
Definition: Asset.cpp:35
value_type issue_
Definition: Asset.h:51
std::string getText() const
Definition: Asset.cpp:43
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:70
MPTIssue mptIssueFromJson(Json::Value const &jv)
Definition: MPTIssue.cpp:84
Json::Value to_json(Asset const &asset)
Definition: Asset.cpp:82
bool validJSONAsset(Json::Value const &jv)
Definition: Asset.cpp:62
Issue issueFromJson(Json::Value const &v)
Definition: Issue.cpp:95
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:630
T visit(T... args)