xrpld
Loading...
Searching...
No Matches
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
17namespace xrpl {
18
19static std::optional<UInt256>
20parseVault(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 {
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())
44 if (!id)
45 {
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 {
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 {
69 "Must specify either 'vault_id' or both 'owner' and 'seq'.",
70 jvResult);
71 return std::nullopt;
72 }
73
74 return uNodeIndex;
75}
76
79{
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 {
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 {
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
Represents a JSON value.
Definition json_value.h:117
bool isString() const
bool isMember(char const *key) const
Return true if the object has a member named key.
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:525
static constexpr SeqProxy rawSequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
Definition SeqProxy.h:62
constexpr Zero kZero
Definition Zero.h:30
Keylet vault(AccountID const &owner, SeqProxy const &seq) noexcept
Definition Indexes.cpp:591
Keylet mptokenIssuance(MPTID const &issuanceID) noexcept
Definition Indexes.cpp:567
void injectError(ErrorCodeI code, json::Value &json)
Add or update the json update to reflect the error code.
std::string expectedFieldMessage(std::string const &name, std::string const &type)
Definition ErrorCodes.h:297
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.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ RpcEntryNotFound
Definition ErrorCodes.h:144
@ RpcActMalformed
Definition ErrorCodes.h:73
@ RpcInvalidParams
Definition ErrorCodes.h:67
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
BaseUInt< 256 > UInt256
Definition base_uint.h:580
json::Value doVaultInfo(rpc::JsonContext &)
Definition VaultInfo.cpp:78
static std::expected< UInt256, json::Value > parseVault(json::Value const &params, json::StaticString const fieldName, unsigned const apiVersion)
UInt256 key
Definition Keylet.h:21
json::Value params
Definition Context.h:51