mirror of
https://github.com/XRPLF/clio.git
synced 2025-12-06 17:27:58 +00:00
246
src/rpc/ngHandlers/GatewayBalances.cpp
Normal file
246
src/rpc/ngHandlers/GatewayBalances.cpp
Normal file
@@ -0,0 +1,246 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
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/GatewayBalances.h>
|
||||
|
||||
namespace RPCng {
|
||||
|
||||
GatewayBalancesHandler::Result
|
||||
GatewayBalancesHandler::process(
|
||||
GatewayBalancesHandler::Input input,
|
||||
boost::asio::yield_context& yield) const
|
||||
{
|
||||
// check ledger
|
||||
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};
|
||||
|
||||
// check account
|
||||
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"}};
|
||||
|
||||
GatewayBalancesHandler::Output output;
|
||||
|
||||
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);
|
||||
auto const lowID = lowLimit.getIssuer();
|
||||
auto const highID = highLimit.getIssuer();
|
||||
auto const viewLowest = (lowLimit.getIssuer() == accountID);
|
||||
auto const flags = sle.getFieldU32(ripple::sfFlags);
|
||||
auto const freeze = flags &
|
||||
(viewLowest ? ripple::lsfLowFreeze : ripple::lsfHighFreeze);
|
||||
if (!viewLowest)
|
||||
balance.negate();
|
||||
|
||||
auto const balSign = balance.signum();
|
||||
if (balSign == 0)
|
||||
return true;
|
||||
|
||||
auto const& peer = !viewLowest ? lowID : highID;
|
||||
|
||||
// Here, a negative balance means the cold wallet owes (normal)
|
||||
// A positive balance means the cold wallet has an asset
|
||||
// (unusual)
|
||||
|
||||
if (input.hotWallets.count(peer) > 0)
|
||||
{
|
||||
// This is a specified hot wallet
|
||||
output.hotBalances[peer].push_back(-balance);
|
||||
}
|
||||
else if (balSign > 0)
|
||||
{
|
||||
// This is a gateway asset
|
||||
output.assets[peer].push_back(balance);
|
||||
}
|
||||
else if (freeze)
|
||||
{
|
||||
// An obligation the gateway has frozen
|
||||
output.frozenBalances[peer].push_back(-balance);
|
||||
}
|
||||
else
|
||||
{
|
||||
// normal negative balance, obligation to customer
|
||||
auto& bal = output.sums[balance.getCurrency()];
|
||||
if (bal == beast::zero)
|
||||
{
|
||||
// This is needed to set the currency code correctly
|
||||
bal = -balance;
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
bal -= balance;
|
||||
}
|
||||
catch (std::runtime_error const& e)
|
||||
{
|
||||
output.overflow = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
// traverse all owned nodes, limit->max, marker->empty
|
||||
auto const ret = RPC::ngTraverseOwnedNodes(
|
||||
*sharedPtrBackend_,
|
||||
*accountID,
|
||||
lgrInfo.seq,
|
||||
std::numeric_limits<std::uint32_t>::max(),
|
||||
{},
|
||||
yield,
|
||||
addToResponse);
|
||||
|
||||
if (auto status = std::get_if<RPC::Status>(&ret))
|
||||
return Error{*status};
|
||||
|
||||
if (not std::all_of(
|
||||
input.hotWallets.begin(),
|
||||
input.hotWallets.end(),
|
||||
[&](auto const& hw) { return output.hotBalances.contains(hw); }))
|
||||
return Error{RPC::Status{
|
||||
RPC::RippledError::rpcINVALID_PARAMS, "invalidHotWallet"}};
|
||||
|
||||
output.accountID = input.account;
|
||||
output.ledgerHash = ripple::strHex(lgrInfo.hash);
|
||||
output.ledgerIndex = lgrInfo.seq;
|
||||
return output;
|
||||
}
|
||||
|
||||
void
|
||||
tag_invoke(
|
||||
boost::json::value_from_tag,
|
||||
boost::json::value& jv,
|
||||
GatewayBalancesHandler::Output const& output)
|
||||
{
|
||||
boost::json::object obj;
|
||||
if (!output.sums.empty())
|
||||
{
|
||||
boost::json::object obligations;
|
||||
for (auto const& [k, v] : output.sums)
|
||||
{
|
||||
obligations[ripple::to_string(k)] = v.getText();
|
||||
}
|
||||
obj["obligations"] = std::move(obligations);
|
||||
}
|
||||
|
||||
auto const toJson =
|
||||
[](std::map<ripple::AccountID, std::vector<ripple::STAmount>> const&
|
||||
balances) {
|
||||
boost::json::object balancesObj;
|
||||
if (!balances.empty())
|
||||
{
|
||||
for (auto const& [accId, accBalances] : balances)
|
||||
{
|
||||
boost::json::array arr;
|
||||
for (auto const& balance : accBalances)
|
||||
{
|
||||
boost::json::object entry;
|
||||
entry[JS(currency)] =
|
||||
ripple::to_string(balance.issue().currency);
|
||||
entry[JS(value)] = balance.getText();
|
||||
arr.push_back(std::move(entry));
|
||||
}
|
||||
balancesObj[ripple::to_string(accId)] = std::move(arr);
|
||||
}
|
||||
}
|
||||
return balancesObj;
|
||||
};
|
||||
|
||||
if (auto balances = toJson(output.hotBalances); balances.size())
|
||||
obj["balances"] = balances;
|
||||
|
||||
// we don't have frozen_balances field in the
|
||||
// document:https://xrpl.org/gateway_balances.html#gateway_balances
|
||||
if (auto balances = toJson(output.frozenBalances); balances.size())
|
||||
obj["frozen_balances"] = balances;
|
||||
if (auto balances = toJson(output.assets); balances.size())
|
||||
obj["assets"] = balances;
|
||||
obj["account"] = output.accountID;
|
||||
obj["ledger_index"] = output.ledgerIndex;
|
||||
obj["ledger_hash"] = output.ledgerHash;
|
||||
if (output.overflow)
|
||||
obj["overflow"] = true;
|
||||
jv = std::move(obj);
|
||||
}
|
||||
|
||||
GatewayBalancesHandler::Input
|
||||
tag_invoke(
|
||||
boost::json::value_to_tag<GatewayBalancesHandler::Input>,
|
||||
boost::json::value const& jv)
|
||||
{
|
||||
auto const& jsonObject = jv.as_object();
|
||||
GatewayBalancesHandler::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());
|
||||
}
|
||||
}
|
||||
if (jsonObject.contains("hotwallet"))
|
||||
{
|
||||
if (jsonObject.at("hotwallet").is_string())
|
||||
{
|
||||
input.hotWallets.insert(*RPC::accountFromStringStrict(
|
||||
jv.at("hotwallet").as_string().c_str()));
|
||||
}
|
||||
else
|
||||
{
|
||||
auto const& hotWallets = jv.at("hotwallet").as_array();
|
||||
std::transform(
|
||||
hotWallets.begin(),
|
||||
hotWallets.end(),
|
||||
std::inserter(input.hotWallets, input.hotWallets.begin()),
|
||||
[](auto const& hotWallet) {
|
||||
return *RPC::accountFromStringStrict(
|
||||
hotWallet.as_string().c_str());
|
||||
});
|
||||
}
|
||||
}
|
||||
return input;
|
||||
}
|
||||
} // namespace RPCng
|
||||
128
src/rpc/ngHandlers/GatewayBalances.h
Normal file
128
src/rpc/ngHandlers/GatewayBalances.h
Normal file
@@ -0,0 +1,128 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
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>
|
||||
|
||||
namespace RPCng {
|
||||
class GatewayBalancesHandler
|
||||
{
|
||||
std::shared_ptr<BackendInterface> sharedPtrBackend_;
|
||||
|
||||
public:
|
||||
struct Output
|
||||
{
|
||||
std::string ledgerHash;
|
||||
uint32_t ledgerIndex;
|
||||
std::string accountID;
|
||||
bool overflow = false;
|
||||
std::map<ripple::Currency, ripple::STAmount> sums;
|
||||
std::map<ripple::AccountID, std::vector<ripple::STAmount>> hotBalances;
|
||||
std::map<ripple::AccountID, std::vector<ripple::STAmount>> assets;
|
||||
std::map<ripple::AccountID, std::vector<ripple::STAmount>>
|
||||
frozenBalances;
|
||||
// validated should be sent via framework
|
||||
bool validated = true;
|
||||
};
|
||||
|
||||
// TODO:we did not implement the "strict" field
|
||||
struct Input
|
||||
{
|
||||
std::string account;
|
||||
std::set<ripple::AccountID> hotWallets;
|
||||
std::optional<std::string> ledgerHash;
|
||||
std::optional<uint32_t> ledgerIndex;
|
||||
};
|
||||
|
||||
using Result = RPCng::HandlerReturnType<Output>;
|
||||
|
||||
GatewayBalancesHandler(
|
||||
std::shared_ptr<BackendInterface> const& sharedPtrBackend)
|
||||
: sharedPtrBackend_(sharedPtrBackend)
|
||||
{
|
||||
}
|
||||
|
||||
RpcSpecConstRef
|
||||
spec() const
|
||||
{
|
||||
static auto const hotWalletValidator = validation::CustomValidator{
|
||||
[](boost::json::value const& value,
|
||||
std::string_view key) -> MaybeError {
|
||||
if (!value.is_string() && !value.is_array())
|
||||
{
|
||||
return Error{RPC::Status{
|
||||
RPC::RippledError::rpcINVALID_PARAMS,
|
||||
std::string(key) + "NotStringOrArray"}};
|
||||
}
|
||||
// wallet needs to be an valid accountID or public key
|
||||
auto const wallets = value.is_array()
|
||||
? value.as_array()
|
||||
: boost::json::array{value};
|
||||
auto const getAccountID =
|
||||
[](auto const& j) -> std::optional<ripple::AccountID> {
|
||||
if (j.is_string())
|
||||
{
|
||||
auto const pk = ripple::parseBase58<ripple::PublicKey>(
|
||||
ripple::TokenType::AccountPublic,
|
||||
j.as_string().c_str());
|
||||
if (pk)
|
||||
return ripple::calcAccountID(*pk);
|
||||
return ripple::parseBase58<ripple::AccountID>(
|
||||
j.as_string().c_str());
|
||||
}
|
||||
return {};
|
||||
};
|
||||
for (auto const& wallet : wallets)
|
||||
{
|
||||
if (!getAccountID(wallet))
|
||||
return Error{RPC::Status{
|
||||
RPC::RippledError::rpcINVALID_PARAMS,
|
||||
std::string(key) + "Malformed"}};
|
||||
}
|
||||
return MaybeError{};
|
||||
}};
|
||||
|
||||
static const RpcSpec rpcSpec = {
|
||||
{"account", validation::Required{}, validation::AccountValidator},
|
||||
{"ledger_hash", validation::LedgerHashValidator},
|
||||
{"ledger_index", validation::LedgerIndexValidator},
|
||||
{"hotwallet", hotWalletValidator}};
|
||||
return rpcSpec;
|
||||
}
|
||||
|
||||
Result
|
||||
process(Input input, boost::asio::yield_context& yield) const;
|
||||
};
|
||||
|
||||
void
|
||||
tag_invoke(
|
||||
boost::json::value_from_tag,
|
||||
boost::json::value& jv,
|
||||
GatewayBalancesHandler::Output const& output);
|
||||
|
||||
GatewayBalancesHandler::Input
|
||||
tag_invoke(
|
||||
boost::json::value_to_tag<GatewayBalancesHandler::Input>,
|
||||
boost::json::value const& jv);
|
||||
} // namespace RPCng
|
||||
Reference in New Issue
Block a user