rippled
Loading...
Searching...
No Matches
VaultInfo.cpp
1#include <xrpld/rpc/Context.h>
2#include <xrpld/rpc/detail/RPCLedgerHelpers.h>
3
4#include <xrpl/beast/utility/Zero.h>
5#include <xrpl/json/json_value.h>
6#include <xrpl/protocol/ErrorCodes.h>
7#include <xrpl/protocol/Indexes.h>
8#include <xrpl/protocol/jss.h>
9#include <xrpl/protocol/serialize.h>
10
11namespace xrpl {
12
14parseVault(Json::Value const& params, Json::Value& jvResult)
15{
16 auto const hasVaultId = params.isMember(jss::vault_id);
17 auto const hasOwner = params.isMember(jss::owner);
18 auto const hasSeq = params.isMember(jss::seq);
19
20 uint256 uNodeIndex = beast::zero;
21 if (hasVaultId && !hasOwner && !hasSeq)
22 {
23 if (!uNodeIndex.parseHex(params[jss::vault_id].asString()))
24 {
26 return std::nullopt;
27 }
28 // else uNodeIndex holds the value we need
29 }
30 else if (!hasVaultId && hasOwner && hasSeq)
31 {
32 auto const id = parseBase58<AccountID>(params[jss::owner].asString());
33 if (!id)
34 {
36 return std::nullopt;
37 }
38 else if (
39 !(params[jss::seq].isInt() || params[jss::seq].isUInt()) || params[jss::seq].asDouble() <= 0.0 ||
40 params[jss::seq].asDouble() > double(Json::Value::maxUInt))
41 {
43 return std::nullopt;
44 }
45
46 uNodeIndex = keylet::vault(*id, params[jss::seq].asUInt()).key;
47 }
48 else
49 {
50 // Invalid combination of fields vault_id/owner/seq
52 return std::nullopt;
53 }
54
55 return uNodeIndex;
56}
57
60{
62 auto jvResult = RPC::lookupLedger(lpLedger, context);
63
64 if (!lpLedger)
65 return jvResult;
66
67 auto const uNodeIndex = parseVault(context.params, jvResult).value_or(beast::zero);
68 if (uNodeIndex == beast::zero)
69 {
70 jvResult[jss::error] = "malformedRequest";
71 return jvResult;
72 }
73
74 auto const sleVault = lpLedger->read(keylet::vault(uNodeIndex));
75 auto const sleIssuance = sleVault == nullptr //
76 ? nullptr
77 : lpLedger->read(keylet::mptIssuance(sleVault->at(sfShareMPTID)));
78 if (!sleVault || !sleIssuance)
79 {
80 jvResult[jss::error] = "entryNotFound";
81 return jvResult;
82 }
83
84 Json::Value& vault = jvResult[jss::vault];
85 vault = sleVault->getJson(JsonOptions::none);
86 auto& share = vault[jss::shares];
87 share = sleIssuance->getJson(JsonOptions::none);
88
89 jvResult[jss::vault] = vault;
90 return jvResult;
91}
92
93} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
std::string asString() const
Returns the unquoted string value.
bool isMember(char const *key) const
Return true if the object has a member named key.
static constexpr UInt maxUInt
Definition json_value.h:144
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:471
T is_same_v
void inject_error(error_code_i 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.
Keylet mptIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Definition Indexes.cpp:462
Keylet vault(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:492
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
Json::Value doVaultInfo(RPC::JsonContext &)
Definition VaultInfo.cpp:59
static Expected< uint256, Json::Value > parseVault(Json::Value const &params, Json::StaticString const fieldName, unsigned const apiVersion)
@ rpcINVALID_PARAMS
Definition ErrorCodes.h:64
@ rpcACT_MALFORMED
Definition ErrorCodes.h:70
uint256 key
Definition Keylet.h:20
Json::Value params
Definition Context.h:43