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/jss.h>
12
13#include <memory>
14#include <optional>
15
16namespace xrpl {
17
18static std::optional<uint256>
19parseVault(json::Value const& params, json::Value& jvResult)
20{
21 auto const hasVaultId = params.isMember(jss::vault_id);
22 auto const hasOwner = params.isMember(jss::owner);
23 auto const hasSeq = params.isMember(jss::seq);
24
25 uint256 uNodeIndex = beast::kZero;
26 if (hasVaultId && !hasOwner && !hasSeq)
27 {
28 if (!uNodeIndex.parseHex(params[jss::vault_id].asString()))
29 {
31 return std::nullopt;
32 }
33 // else uNodeIndex holds the value we need
34 }
35 else if (!hasVaultId && hasOwner && hasSeq)
36 {
37 auto const id = parseBase58<AccountID>(params[jss::owner].asString());
38 if (!id)
39 {
41 return std::nullopt;
42 }
43 if (!(params[jss::seq].isInt() || params[jss::seq].isUInt()) ||
44 params[jss::seq].asDouble() <= 0.0 ||
45 params[jss::seq].asDouble() > double(json::Value::kMaxUInt))
46 {
48 return std::nullopt;
49 }
50
51 uNodeIndex = keylet::vault(*id, params[jss::seq].asUInt()).key;
52 }
53 else
54 {
55 // Invalid combination of fields vault_id/owner/seq
57 return std::nullopt;
58 }
59
60 return uNodeIndex;
61}
62
65{
67 auto jvResult = RPC::lookupLedger(lpLedger, context);
68
69 if (!lpLedger)
70 return jvResult;
71
72 auto const uNodeIndex = parseVault(context.params, jvResult).value_or(beast::kZero);
73 if (uNodeIndex == beast::kZero)
74 {
75 jvResult[jss::error] = "malformedRequest";
76 return jvResult;
77 }
78
79 auto const sleVault = lpLedger->read(keylet::vault(uNodeIndex));
80 auto const sleIssuance = sleVault == nullptr //
81 ? nullptr
82 : lpLedger->read(keylet::mptokenIssuance(sleVault->at(sfShareMPTID)));
83 if (!sleVault || !sleIssuance)
84 {
85 jvResult[jss::error] = "entryNotFound";
86 return jvResult;
87 }
88
89 json::Value& vault = jvResult[jss::vault];
90 vault = sleVault->getJson(JsonOptions::Values::None);
91 auto& share = vault[jss::shares];
92 share = sleIssuance->getJson(JsonOptions::Values::None);
93
94 jvResult[jss::vault] = vault;
95 return jvResult;
96}
97
98} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
std::string asString() const
Returns the unquoted string value.
static constexpr UInt kMaxUInt
Definition json_value.h:144
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:507
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.
void injectError(ErrorCodeI code, json::Value &json)
Add or update the json update to reflect the error code.
Keylet mptokenIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Definition Indexes.cpp:521
Keylet vault(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:551
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ RpcActMalformed
Definition ErrorCodes.h:72
@ RpcInvalidParams
Definition ErrorCodes.h:66
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:64
BaseUInt< 256 > uint256
Definition base_uint.h:562
uint256 key
Definition Keylet.h:20
json::Value params
Definition Context.h:43