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/STAmount.h>
27#include <xrpl/protocol/jss.h>
28
29#include <stdexcept>
30#include <string>
31#include <variant>
32
33namespace ripple {
34
35AccountID const&
37{
38 return std::visit(
39 [&](auto&& issue) -> AccountID const& { return issue.getIssuer(); },
40 issue_);
41}
42
45{
46 return std::visit([&](auto&& issue) { return issue.getText(); }, issue_);
47}
48
49void
51{
52 std::visit([&](auto&& issue) { issue.setJson(jv); }, issue_);
53}
54
56Asset::operator()(Number const& number) const
57{
58 return STAmount{*this, number};
59}
60
62to_string(Asset const& asset)
63{
64 return std::visit(
65 [&](auto const& issue) { return to_string(issue); }, asset.value());
66}
67
68bool
70{
71 if (jv.isMember(jss::mpt_issuance_id))
72 return !(jv.isMember(jss::currency) || jv.isMember(jss::issuer));
73 return jv.isMember(jss::currency);
74}
75
76Asset
78{
79 if (!v.isMember(jss::currency) && !v.isMember(jss::mpt_issuance_id))
80 Throw<std::runtime_error>(
81 "assetFromJson must contain currency or mpt_issuance_id");
82
83 if (v.isMember(jss::currency))
84 return issueFromJson(v);
85 return mptIssueFromJson(v);
86}
87
88} // namespace ripple
Represents a JSON value.
Definition json_value.h:149
bool isMember(char const *key) const
Return true if the object has a member named key.
STAmount operator()(Number const &) const
Definition Asset.cpp:56
void setJson(Json::Value &jv) const
Definition Asset.cpp:50
constexpr value_type const & value() const
Definition Asset.h:156
AccountID const & getIssuer() const
Definition Asset.cpp:36
value_type issue_
Definition Asset.h:53
std::string getText() const
Definition Asset.cpp:44
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:48
Asset assetFromJson(Json::Value const &jv)
Definition Asset.cpp:77
MPTIssue mptIssueFromJson(Json::Value const &jv)
Definition MPTIssue.cpp:78
bool validJSONAsset(Json::Value const &jv)
Definition Asset.cpp:69
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)