mirror of
https://github.com/XRPLF/clio.git
synced 2025-12-06 17:27:58 +00:00
@@ -606,8 +606,10 @@ getLedgerInfoFromHashOrSeq(
|
||||
RPC::Status{RPC::RippledError::rpcLGR_NOT_FOUND, "ledgerNotFound"};
|
||||
if (ledgerHash)
|
||||
{
|
||||
lgrInfo =
|
||||
backend.fetchLedgerByHash(ripple::uint256{*ledgerHash}, yield);
|
||||
// invoke uint256's constructor to parse the hex string , instead of
|
||||
// copying buffer
|
||||
ripple::uint256 ledgerHash256{std::string_view(*ledgerHash)};
|
||||
lgrInfo = backend.fetchLedgerByHash(ledgerHash256, yield);
|
||||
if (!lgrInfo || lgrInfo->seq > maxSeq)
|
||||
return err;
|
||||
|
||||
|
||||
@@ -75,7 +75,8 @@ public:
|
||||
|
||||
using Result = RPCng::HandlerReturnType<Output>;
|
||||
|
||||
AccountChannelsHandler(std::shared_ptr<BackendInterface>& sharedPtrBackend)
|
||||
AccountChannelsHandler(
|
||||
std::shared_ptr<BackendInterface> const& sharedPtrBackend)
|
||||
: sharedPtrBackend_(sharedPtrBackend)
|
||||
{
|
||||
}
|
||||
|
||||
129
src/rpc/ngHandlers/AccountCurrencies.cpp
Normal file
129
src/rpc/ngHandlers/AccountCurrencies.cpp
Normal file
@@ -0,0 +1,129 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of clio: https://github.com/XRPLF/clio
|
||||
Copyright (c) 2023, the clio developers.
|
||||
|
||||
Permission to use, copy, modify, and distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <rpc/RPCHelpers.h>
|
||||
#include <rpc/ngHandlers/AccountCurrencies.h>
|
||||
|
||||
namespace RPCng {
|
||||
AccountCurrenciesHandler::Result
|
||||
AccountCurrenciesHandler::process(
|
||||
AccountCurrenciesHandler::Input input,
|
||||
boost::asio::yield_context& yield) const
|
||||
{
|
||||
auto const range = sharedPtrBackend_->fetchLedgerRange();
|
||||
auto const lgrInfoOrStatus = RPC::getLedgerInfoFromHashOrSeq(
|
||||
*sharedPtrBackend_,
|
||||
yield,
|
||||
input.ledgerHash,
|
||||
input.ledgerIndex,
|
||||
range->maxSequence);
|
||||
|
||||
if (auto const status = std::get_if<RPC::Status>(&lgrInfoOrStatus))
|
||||
return Error{*status};
|
||||
|
||||
auto const lgrInfo = std::get<ripple::LedgerInfo>(lgrInfoOrStatus);
|
||||
|
||||
auto const accountID = RPC::accountFromStringStrict(input.account);
|
||||
|
||||
auto const accountLedgerObject = sharedPtrBackend_->fetchLedgerObject(
|
||||
ripple::keylet::account(*accountID).key, lgrInfo.seq, yield);
|
||||
if (!accountLedgerObject)
|
||||
return Error{RPC::Status{
|
||||
RPC::RippledError::rpcACT_NOT_FOUND, "accountNotFound"}};
|
||||
|
||||
Output response;
|
||||
auto const addToResponse = [&](ripple::SLE&& sle) {
|
||||
if (sle.getType() == ripple::ltRIPPLE_STATE)
|
||||
{
|
||||
ripple::STAmount balance = sle.getFieldAmount(ripple::sfBalance);
|
||||
auto const lowLimit = sle.getFieldAmount(ripple::sfLowLimit);
|
||||
auto const highLimit = sle.getFieldAmount(ripple::sfHighLimit);
|
||||
bool const viewLowest = (lowLimit.getIssuer() == accountID);
|
||||
auto const lineLimit = viewLowest ? lowLimit : highLimit;
|
||||
auto const lineLimitPeer = !viewLowest ? lowLimit : highLimit;
|
||||
if (!viewLowest)
|
||||
balance.negate();
|
||||
if (balance < lineLimit)
|
||||
response.receiveCurrencies.insert(
|
||||
ripple::to_string(balance.getCurrency()));
|
||||
if ((-balance) < lineLimitPeer)
|
||||
response.sendCurrencies.insert(
|
||||
ripple::to_string(balance.getCurrency()));
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
// traverse all owned nodes, limit->max, marker->empty
|
||||
RPC::ngTraverseOwnedNodes(
|
||||
*sharedPtrBackend_,
|
||||
*accountID,
|
||||
lgrInfo.seq,
|
||||
std::numeric_limits<std::uint32_t>::max(),
|
||||
{},
|
||||
yield,
|
||||
addToResponse);
|
||||
|
||||
response.ledgerHash = ripple::strHex(lgrInfo.hash);
|
||||
response.ledgerIndex = lgrInfo.seq;
|
||||
return response;
|
||||
}
|
||||
|
||||
void
|
||||
tag_invoke(
|
||||
boost::json::value_from_tag,
|
||||
boost::json::value& jv,
|
||||
AccountCurrenciesHandler::Output output)
|
||||
{
|
||||
jv = {
|
||||
{"ledger_hash", output.ledgerHash},
|
||||
{"ledger_index", output.ledgerIndex},
|
||||
{"validated", output.validated},
|
||||
{"receive_currencies",
|
||||
boost::json::value_from(output.receiveCurrencies)},
|
||||
{"send_currencies", boost::json::value_from(output.sendCurrencies)}};
|
||||
}
|
||||
|
||||
AccountCurrenciesHandler::Input
|
||||
tag_invoke(
|
||||
boost::json::value_to_tag<AccountCurrenciesHandler::Input>,
|
||||
boost::json::value const& jv)
|
||||
{
|
||||
auto const& jsonObject = jv.as_object();
|
||||
AccountCurrenciesHandler::Input input;
|
||||
input.account = jv.at("account").as_string().c_str();
|
||||
if (jsonObject.contains("ledger_hash"))
|
||||
{
|
||||
input.ledgerHash = jv.at("ledger_hash").as_string().c_str();
|
||||
}
|
||||
if (jsonObject.contains("ledger_index"))
|
||||
{
|
||||
if (!jsonObject.at("ledger_index").is_string())
|
||||
{
|
||||
input.ledgerIndex = jv.at("ledger_index").as_int64();
|
||||
}
|
||||
else if (jsonObject.at("ledger_index").as_string() != "validated")
|
||||
{
|
||||
input.ledgerIndex =
|
||||
std::stoi(jv.at("ledger_index").as_string().c_str());
|
||||
}
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
||||
} // namespace RPCng
|
||||
87
src/rpc/ngHandlers/AccountCurrencies.h
Normal file
87
src/rpc/ngHandlers/AccountCurrencies.h
Normal file
@@ -0,0 +1,87 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of clio: https://github.com/XRPLF/clio
|
||||
Copyright (c) 2023, the clio developers.
|
||||
|
||||
Permission to use, copy, modify, and distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <backend/BackendInterface.h>
|
||||
#include <rpc/common/Types.h>
|
||||
#include <rpc/common/Validators.h>
|
||||
|
||||
#include <boost/asio/spawn.hpp>
|
||||
|
||||
#include <set>
|
||||
|
||||
namespace RPCng {
|
||||
class AccountCurrenciesHandler
|
||||
{
|
||||
// dependencies
|
||||
std::shared_ptr<BackendInterface> sharedPtrBackend_;
|
||||
|
||||
public:
|
||||
struct Output
|
||||
{
|
||||
std::string ledgerHash;
|
||||
uint32_t ledgerIndex;
|
||||
std::set<std::string> receiveCurrencies;
|
||||
std::set<std::string> sendCurrencies;
|
||||
// validated should be sent via framework
|
||||
bool validated = true;
|
||||
};
|
||||
|
||||
// TODO:we did not implement the "strict" field
|
||||
struct Input
|
||||
{
|
||||
std::string account;
|
||||
std::optional<std::string> ledgerHash;
|
||||
std::optional<uint32_t> ledgerIndex;
|
||||
};
|
||||
|
||||
using Result = RPCng::HandlerReturnType<Output>;
|
||||
|
||||
AccountCurrenciesHandler(
|
||||
std::shared_ptr<BackendInterface> const& sharedPtrBackend)
|
||||
: sharedPtrBackend_(sharedPtrBackend)
|
||||
{
|
||||
}
|
||||
|
||||
RpcSpecConstRef
|
||||
spec() const
|
||||
{
|
||||
static const RpcSpec rpcSpec = {
|
||||
{"account", validation::Required{}, validation::AccountValidator},
|
||||
{"ledger_hash", validation::LedgerHashValidator},
|
||||
{"ledger_index", validation::LedgerIndexValidator}};
|
||||
return rpcSpec;
|
||||
}
|
||||
|
||||
Result
|
||||
process(Input input, boost::asio::yield_context& yield) const;
|
||||
};
|
||||
|
||||
void
|
||||
tag_invoke(
|
||||
boost::json::value_from_tag,
|
||||
boost::json::value& jv,
|
||||
AccountCurrenciesHandler::Output output);
|
||||
|
||||
AccountCurrenciesHandler::Input
|
||||
tag_invoke(
|
||||
boost::json::value_to_tag<AccountCurrenciesHandler::Input>,
|
||||
boost::json::value const& jv);
|
||||
} // namespace RPCng
|
||||
Reference in New Issue
Block a user