xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
xrpld
rpc
handlers
VaultInfo.cpp
1
#include <xrpld/rpc/Context.h>
2
#include <xrpld/rpc/detail/RPCLedgerHelpers.h>
3
4
#include <xrpl/basics/base_uint.h>
5
#include <xrpl/beast/utility/Zero.h>
6
#include <xrpl/json/json_value.h>
7
#include <xrpl/protocol/AccountID.h>
8
#include <xrpl/protocol/ErrorCodes.h>
9
#include <xrpl/protocol/Indexes.h>
10
#include <xrpl/protocol/SField.h>
11
#include <xrpl/protocol/SeqProxy.h>
12
#include <xrpl/protocol/jss.h>
13
14
#include <
memory
>
15
#include <
optional
>
16
17
namespace
xrpl
{
18
19
static
std::optional<UInt256>
20
parseVault
(
json::Value
const
& params,
json::Value
& jvResult)
21
{
22
auto
const
hasVaultId = params.
isMember
(jss::vault_id);
23
auto
const
hasOwner = params.
isMember
(jss::owner);
24
auto
const
hasSeq = params.
isMember
(jss::seq);
25
26
UInt256
uNodeIndex =
beast::kZero
;
27
if
(hasVaultId && !hasOwner && !hasSeq)
28
{
29
// asString() throws on an object or an array, so the type comes first.
30
auto
const
& vaultId = params[jss::vault_id];
31
if
(!vaultId.isString() || !uNodeIndex.
parseHex
(vaultId.asString()))
32
{
33
rpc::injectError
(
34
RpcInvalidParams
,
rpc::expectedFieldMessage
(jss::vault_id,
"hex string"
), jvResult);
35
return
std::nullopt;
36
}
37
// else uNodeIndex holds the value we need
38
}
39
else
if
(!hasVaultId && hasOwner && hasSeq)
40
{
41
auto
const
& owner = params[jss::owner];
42
auto
const
id
= owner.
isString
() ?
parseBase58<AccountID>
(owner.asString())
43
:
std::optional<AccountID>
{};
44
if
(!
id
)
45
{
46
rpc::injectError
(
47
RpcActMalformed
,
rpc::expectedFieldMessage
(jss::owner,
"AccountID"
), jvResult);
48
return
std::nullopt;
49
}
50
51
// Int and UInt are both 32 bits wide, so the type check is the only upper bound needed.
52
auto
const
& seqField = params[jss::seq];
53
if
(!(seqField.isInt() || seqField.isUInt()) || seqField.asDouble() <= 0.0)
54
{
55
rpc::injectError
(
56
RpcInvalidParams
,
57
rpc::expectedFieldMessage
(jss::seq,
"a positive 32-bit integer"
),
58
jvResult);
59
return
std::nullopt;
60
}
61
62
auto
const
seq =
SeqProxy::rawSequence
(seqField.asUInt());
63
uNodeIndex =
keylet::vault
(*
id
, seq).
key
;
64
}
65
else
66
{
67
rpc::injectError
(
68
RpcInvalidParams
,
69
"Must specify either 'vault_id' or both 'owner' and 'seq'."
,
70
jvResult);
71
return
std::nullopt;
72
}
73
74
return
uNodeIndex;
75
}
76
77
json::Value
78
doVaultInfo
(
rpc::JsonContext
& context)
79
{
80
std::shared_ptr<ReadView const>
lpLedger;
81
auto
jvResult =
rpc::lookupLedger
(lpLedger, context);
82
83
if
(!lpLedger)
84
return
jvResult;
85
86
// No key means the request could not be turned into one, and parseVault has already said why.
87
auto
const
uNodeIndex =
parseVault
(context.
params
, jvResult);
88
if
(!uNodeIndex)
89
return
jvResult;
90
91
// A zero key names an entry that cannot exist, and the ledger refuses to be asked for one.
92
if
(*uNodeIndex ==
beast::kZero
)
93
{
94
rpc::injectError
(
RpcEntryNotFound
, jvResult);
95
return
jvResult;
96
}
97
98
auto
const
sleVault = lpLedger->read(
keylet::vault
(*uNodeIndex));
99
auto
const
sleIssuance = sleVault ==
nullptr
//
100
? nullptr
101
: lpLedger->read(
keylet::mptokenIssuance
(sleVault->at(sfShareMPTID)));
102
if
(!sleVault || !sleIssuance)
103
{
104
rpc::injectError
(
RpcEntryNotFound
, jvResult);
105
return
jvResult;
106
}
107
108
json::Value
& vault = jvResult[jss::vault];
109
vault = sleVault->getJson(
JsonOptions::Values::None
);
110
auto
& share = vault[jss::shares];
111
share = sleIssuance->getJson(
JsonOptions::Values::None
);
112
113
jvResult[jss::vault] = vault;
114
return
jvResult;
115
}
116
117
}
// namespace xrpl
json::Value
Represents a JSON value.
Definition
json_value.h:117
json::Value::isString
bool isString() const
Definition
json_value.cpp:1134
json::Value::isMember
bool isMember(char const *key) const
Return true if the object has a member named key.
Definition
json_value.cpp:1049
xrpl::BaseUInt::parseHex
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition
base_uint.h:525
xrpl::SeqProxy::rawSequence
static constexpr SeqProxy rawSequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
Definition
SeqProxy.h:62
memory
beast::kZero
constexpr Zero kZero
Definition
Zero.h:30
xrpl::keylet::vault
Keylet vault(AccountID const &owner, SeqProxy const &seq) noexcept
Definition
Indexes.cpp:591
xrpl::keylet::mptokenIssuance
Keylet mptokenIssuance(MPTID const &issuanceID) noexcept
Definition
Indexes.cpp:567
xrpl::rpc::injectError
void injectError(ErrorCodeI code, json::Value &json)
Add or update the json update to reflect the error code.
Definition
ErrorCodes.cpp:164
xrpl::rpc::expectedFieldMessage
std::string expectedFieldMessage(std::string const &name, std::string const &type)
Definition
ErrorCodes.h:297
xrpl::rpc::lookupLedger
Status lookupLedger(std::shared_ptr< ReadView const > &ledger, JsonContext const &context, json::Value &result)
Looks up a ledger from a request and fills a json::Value with ledger data.
Definition
RPCLedgerHelpers.cpp:390
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::RpcEntryNotFound
@ RpcEntryNotFound
Definition
ErrorCodes.h:144
xrpl::RpcActMalformed
@ RpcActMalformed
Definition
ErrorCodes.h:73
xrpl::RpcInvalidParams
@ RpcInvalidParams
Definition
ErrorCodes.h:67
xrpl::parseBase58
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
Definition
AccountID.cpp:105
xrpl::UInt256
BaseUInt< 256 > UInt256
Definition
base_uint.h:580
xrpl::doVaultInfo
json::Value doVaultInfo(rpc::JsonContext &)
Definition
VaultInfo.cpp:78
xrpl::parseVault
static std::expected< UInt256, json::Value > parseVault(json::Value const ¶ms, json::StaticString const fieldName, unsigned const apiVersion)
Definition
LedgerEntry.cpp:872
optional
std::shared_ptr
xrpl::JsonOptions::Values::None
@ None
Definition
STBase.h:27
xrpl::Keylet::key
UInt256 key
Definition
Keylet.h:21
xrpl::rpc::JsonContext
Definition
Context.h:41
xrpl::rpc::JsonContext::params
json::Value params
Definition
Context.h:51
Generated by
1.17.0