|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +/* |
| 3 | + This file is part of clio: https://github.com/XRPLF/clio |
| 4 | + Copyright (c) 2023, the clio developers. |
| 5 | +
|
| 6 | + Permission to use, copy, modify, and distribute this software for any |
| 7 | + purpose with or without fee is hereby granted, provided that the above |
| 8 | + copyright notice and this permission notice appear in all copies. |
| 9 | +
|
| 10 | + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 | + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 | + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 13 | + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 | + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 | + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | +*/ |
| 18 | +//============================================================================== |
| 19 | + |
| 20 | +#include "rpc/handlers/MPTHolders.h" |
| 21 | + |
| 22 | +#include "rpc/Errors.h" |
| 23 | +#include "rpc/JS.h" |
| 24 | +#include "rpc/RPCHelpers.h" |
| 25 | +#include "rpc/common/Types.h" |
| 26 | + |
| 27 | +#include <boost/json/conversion.hpp> |
| 28 | +#include <boost/json/object.hpp> |
| 29 | +#include <boost/json/value.hpp> |
| 30 | +#include <ripple/basics/base_uint.h> |
| 31 | +#include <ripple/basics/strHex.h> |
| 32 | +#include <ripple/protocol/AccountID.h> |
| 33 | +#include <ripple/protocol/ErrorCodes.h> |
| 34 | +#include <ripple/protocol/Indexes.h> |
| 35 | +#include <ripple/protocol/LedgerHeader.h> |
| 36 | +#include <ripple/protocol/jss.h> |
| 37 | +#include <ripple/protocol/nft.h> |
| 38 | + |
| 39 | +#include <optional> |
| 40 | +#include <string> |
| 41 | +#include <variant> |
| 42 | + |
| 43 | +using namespace ripple; |
| 44 | + |
| 45 | +namespace rpc { |
| 46 | + |
| 47 | +MPTHoldersHandler::Result |
| 48 | +MPTHoldersHandler::process(MPTHoldersHandler::Input input, Context const& ctx) const |
| 49 | +{ |
| 50 | + auto const range = sharedPtrBackend_->fetchLedgerRange(); |
| 51 | + auto const lgrInfoOrStatus = getLedgerInfoFromHashOrSeq( |
| 52 | + *sharedPtrBackend_, ctx.yield, input.ledgerHash, input.ledgerIndex, range->maxSequence |
| 53 | + ); |
| 54 | + if (auto const status = std::get_if<Status>(&lgrInfoOrStatus)) |
| 55 | + return Error{*status}; |
| 56 | + |
| 57 | + auto const lgrInfo = std::get<LedgerInfo>(lgrInfoOrStatus); |
| 58 | + |
| 59 | + auto const limit = input.limit.value_or(MPTHoldersHandler::LIMIT_DEFAULT); |
| 60 | + |
| 61 | + auto const mptID = ripple::uint192{input.mptID.c_str()}; |
| 62 | + |
| 63 | + std::optional<ripple::AccountID> cursor; |
| 64 | + if (input.marker) |
| 65 | + cursor = ripple::AccountID{input.marker->c_str()}; |
| 66 | + |
| 67 | + auto const dbResponse = |
| 68 | + sharedPtrBackend_->fetchMPTHolders(mptID, limit, cursor, lgrInfo.seq, ctx.yield); |
| 69 | + |
| 70 | + auto output = MPTHoldersHandler::Output{}; |
| 71 | + |
| 72 | + output.mptID = to_string(mptID); |
| 73 | + output.limit = limit; |
| 74 | + output.ledgerIndex = lgrInfo.seq; |
| 75 | + |
| 76 | + boost::json::array mpts; |
| 77 | + for (auto const& mpt : dbResponse.mptokens) { |
| 78 | + ripple::STLedgerEntry sle{ripple::SerialIter{mpt.data(), mpt.size()}, keylet::mptIssuance(mptID).key}; |
| 79 | + boost::json::object mptJson; |
| 80 | + |
| 81 | + mptJson["mpt_amount"] = std::to_string(sle[ripple::sfMPTAmount]); |
| 82 | + mptJson[JS(flags)] = sle[ripple::sfFlags]; |
| 83 | + mptJson[JS(account)] = toBase58(sle.getAccountID(ripple::sfAccount)); |
| 84 | + |
| 85 | + if(sle[~ripple::sfLockedAmount]) |
| 86 | + mptJson["locked_amount"] = std::to_string(sle[~ripple::sfLockedAmount].value_or(0)); |
| 87 | + |
| 88 | + output.mpts.push_back(mptJson); |
| 89 | + } |
| 90 | + |
| 91 | + if (dbResponse.cursor.has_value()) |
| 92 | + output.marker = strHex(*dbResponse.cursor); |
| 93 | + |
| 94 | + return output; |
| 95 | +} |
| 96 | + |
| 97 | +void |
| 98 | +tag_invoke(boost::json::value_from_tag, boost::json::value& jv, MPTHoldersHandler::Output const& output) |
| 99 | +{ |
| 100 | + jv = { |
| 101 | + {JS(mpt_issuance_id), output.mptID}, |
| 102 | + {JS(limit), output.limit}, |
| 103 | + {JS(ledger_index), output.ledgerIndex}, |
| 104 | + {"mpts", output.mpts}, |
| 105 | + {JS(validated), output.validated}, |
| 106 | + }; |
| 107 | + |
| 108 | + if (output.marker.has_value()) |
| 109 | + jv.as_object()[JS(marker)] = *(output.marker); |
| 110 | +} |
| 111 | + |
| 112 | +MPTHoldersHandler::Input |
| 113 | +tag_invoke(boost::json::value_to_tag<MPTHoldersHandler::Input>, boost::json::value const& jv) |
| 114 | +{ |
| 115 | + auto const& jsonObject = jv.as_object(); |
| 116 | + MPTHoldersHandler::Input input; |
| 117 | + |
| 118 | + input.mptID = jsonObject.at(JS(mpt_issuance_id)).as_string().c_str(); |
| 119 | + |
| 120 | + if (jsonObject.contains(JS(ledger_hash))) |
| 121 | + input.ledgerHash = jsonObject.at(JS(ledger_hash)).as_string().c_str(); |
| 122 | + |
| 123 | + if (jsonObject.contains(JS(ledger_index))) { |
| 124 | + if (!jsonObject.at(JS(ledger_index)).is_string()) { |
| 125 | + input.ledgerIndex = jsonObject.at(JS(ledger_index)).as_int64(); |
| 126 | + } else if (jsonObject.at(JS(ledger_index)).as_string() != "validated") { |
| 127 | + input.ledgerIndex = std::stoi(jsonObject.at(JS(ledger_index)).as_string().c_str()); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + if (jsonObject.contains(JS(limit))) |
| 132 | + input.limit = jsonObject.at(JS(limit)).as_int64(); |
| 133 | + |
| 134 | + if (jsonObject.contains(JS(marker))) |
| 135 | + input.marker = jsonObject.at(JS(marker)).as_string().c_str(); |
| 136 | + |
| 137 | + return input; |
| 138 | +} |
| 139 | +} // namespace rpc |
0 commit comments