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 if (!uNodeIndex.parseHex(params[jss::vault_id].asString()))
30 {
32 return std::nullopt;
33 }
34 // else uNodeIndex holds the value we need
35 }
36 else if (!hasVaultId && hasOwner && hasSeq)
37 {
38 auto const id = parseBase58<AccountID>(params[jss::owner].asString());
39 if (!id)
40 {
42 return std::nullopt;
43 }
44 if (!(params[jss::seq].isInt() || params[jss::seq].isUInt()) ||
45 params[jss::seq].asDouble() <= 0.0 ||
46 params[jss::seq].asDouble() > double(json::Value::kMaxUInt))
47 {
49 return std::nullopt;
50 }
51
52 auto const seq = SeqProxy::rawSequence(params[jss::seq].asUInt());
53 uNodeIndex = keylet::vault(*id, seq).key;
54 }
55 else
56 {
57 // Invalid combination of fields vault_id/owner/seq
59 return std::nullopt;
60 }
61
62 return uNodeIndex;
63}
64
67{
69 auto jvResult = rpc::lookupLedger(lpLedger, context);
70
71 if (!lpLedger)
72 return jvResult;
73
74 auto const uNodeIndex = parseVault(context.params, jvResult).value_or(beast::kZero);
75 if (uNodeIndex == beast::kZero)
76 {
77 jvResult[jss::error] = "malformedRequest";
78 return jvResult;
79 }
80
81 auto const sleVault = lpLedger->read(keylet::vault(uNodeIndex));
82 auto const sleIssuance = sleVault == nullptr //
83 ? nullptr
84 : lpLedger->read(keylet::mptokenIssuance(sleVault->at(sfShareMPTID)));
85 if (!sleVault || !sleIssuance)
86 {
87 jvResult[jss::error] = "entryNotFound";
88 return jvResult;
89 }
90
91 json::Value& vault = jvResult[jss::vault];
92 vault = sleVault->getJson(JsonOptions::Values::None);
93 auto& share = vault[jss::shares];
94 share = sleIssuance->getJson(JsonOptions::Values::None);
95
96 jvResult[jss::vault] = vault;
97 return jvResult;
98}
99
100} // namespace xrpl
Represents a JSON value.
Definition json_value.h:117
std::string asString() const
Returns the unquoted string value.
static constexpr UInt kMaxUInt
Definition json_value.h:131
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:561
Keylet mptokenIssuance(MPTID const &issuanceID) noexcept
Definition Indexes.cpp:537
void injectError(ErrorCodeI code, json::Value &json)
Add or update the json update to reflect the error code.
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
@ RpcActMalformed
Definition ErrorCodes.h:73
@ RpcInvalidParams
Definition ErrorCodes.h:67
static std::expected< uint256, json::Value > parseVault(json::Value const &params, json::StaticString const fieldName, unsigned const apiVersion)
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
json::Value doVaultInfo(rpc::JsonContext &)
Definition VaultInfo.cpp:66
BaseUInt< 256 > uint256
Definition base_uint.h:580
uint256 key
Definition Keylet.h:21
json::Value params
Definition Context.h:51