Skip to content

Commit

Permalink
Implement amm_info handler (#1060)
Browse files Browse the repository at this point in the history
Fixes #283
  • Loading branch information
godexsoft authored Jan 11, 2024
1 parent 93d5c12 commit f4df5c2
Show file tree
Hide file tree
Showing 16 changed files with 1,878 additions and 40 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ target_sources (clio PRIVATE
## RPC
src/rpc/Errors.cpp
src/rpc/Factories.cpp
src/rpc/AMMHelpers.cpp
src/rpc/RPCHelpers.cpp
src/rpc/Counters.cpp
src/rpc/WorkQueue.cpp
Expand All @@ -133,6 +134,7 @@ target_sources (clio PRIVATE
src/rpc/handlers/AccountObjects.cpp
src/rpc/handlers/AccountOffers.cpp
src/rpc/handlers/AccountTx.cpp
src/rpc/handlers/AMMInfo.cpp
src/rpc/handlers/BookChanges.cpp
src/rpc/handlers/BookOffers.cpp
src/rpc/handlers/DepositAuthorized.cpp
Expand Down Expand Up @@ -248,6 +250,7 @@ if (tests)
unittests/rpc/handlers/BookChangesTests.cpp
unittests/rpc/handlers/LedgerTests.cpp
unittests/rpc/handlers/VersionHandlerTests.cpp
unittests/rpc/handlers/AMMInfoTests.cpp
# Backend
unittests/data/BackendFactoryTests.cpp
unittests/data/BackendCountersTests.cpp
Expand Down
1 change: 1 addition & 0 deletions src/data/BackendInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ BackendInterface::fetchLedgerObjects(

return results;
}

// Fetches the successor to key/index
std::optional<ripple::uint256>
BackendInterface::fetchSuccessorKey(
Expand Down
82 changes: 82 additions & 0 deletions src/rpc/AMMHelpers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2024, 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/AMMHelpers.h"

#include "data/BackendInterface.h"
#include "util/log/Logger.h"

#include <ripple/protocol/AMMCore.h>

namespace rpc {

std::pair<ripple::STAmount, ripple::STAmount>
getAmmPoolHolds(
BackendInterface const& backend,
std::uint32_t sequence,
ripple::AccountID const& ammAccountID,
ripple::Issue const& issue1,
ripple::Issue const& issue2,
bool freezeHandling,
boost::asio::yield_context yield
)
{
auto const assetInBalance =
accountHolds(backend, sequence, ammAccountID, issue1.currency, issue1.account, freezeHandling, yield);
auto const assetOutBalance =
accountHolds(backend, sequence, ammAccountID, issue2.currency, issue2.account, freezeHandling, yield);
return std::make_pair(assetInBalance, assetOutBalance);
}

ripple::STAmount
getAmmLpHolds(
BackendInterface const& backend,
std::uint32_t sequence,
ripple::Currency const& cur1,
ripple::Currency const& cur2,
ripple::AccountID const& ammAccount,
ripple::AccountID const& lpAccount,
boost::asio::yield_context yield
)
{
auto const lptCurrency = ammLPTCurrency(cur1, cur2);
return accountHolds(backend, sequence, lpAccount, lptCurrency, ammAccount, true, yield);
}

ripple::STAmount
getAmmLpHolds(
BackendInterface const& backend,
std::uint32_t sequence,
ripple::SLE const& ammSle,
ripple::AccountID const& lpAccount,
boost::asio::yield_context yield
)
{
return getAmmLpHolds(
backend,
sequence,
ammSle[ripple::sfAsset].currency,
ammSle[ripple::sfAsset2].currency,
ammSle[ripple::sfAccount],
lpAccount,
yield
);
}

} // namespace rpc
67 changes: 67 additions & 0 deletions src/rpc/AMMHelpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2024, 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 "data/BackendInterface.h"
#include "rpc/RPCHelpers.h"

namespace rpc {

/**
* @brief getAmmPoolHolds returns the balances of the amm asset pair.
*/
std::pair<ripple::STAmount, ripple::STAmount>
getAmmPoolHolds(
BackendInterface const& backend,
std::uint32_t sequence,
ripple::AccountID const& ammAccountID,
ripple::Issue const& issue1,
ripple::Issue const& issue2,
bool freezeHandling,
boost::asio::yield_context yield
);

/**
* @brief getAmmLpHolds returns the lp token balance.
*/
ripple::STAmount
getAmmLpHolds(
BackendInterface const& backend,
std::uint32_t sequence,
ripple::Currency const& cur1,
ripple::Currency const& cur2,
ripple::AccountID const& ammAccount,
ripple::AccountID const& lpAccount,
boost::asio::yield_context yield
);

/**
* @brief getAmmLpHolds returns the lp token balance.
*/
ripple::STAmount
getAmmLpHolds(
BackendInterface const& backend,
std::uint32_t sequence,
ripple::SLE const& ammSle,
ripple::AccountID const& lpAccount,
boost::asio::yield_context yield
);

} // namespace rpc
24 changes: 14 additions & 10 deletions src/rpc/RPCHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ xrpLiquid(
boost::asio::yield_context yield
)
{
auto key = ripple::keylet::account(id).key;
auto const key = ripple::keylet::account(id).key;
auto blob = backend.fetchLedgerObject(key, sequence, yield);

if (!blob)
Expand All @@ -999,13 +999,18 @@ xrpLiquid(

std::uint32_t const ownerCount = sle.getFieldU32(ripple::sfOwnerCount);

auto const reserve = backend.fetchFees(sequence, yield)->accountReserve(ownerCount);
auto balance = sle.getFieldAmount(ripple::sfBalance);

auto const balance = sle.getFieldAmount(ripple::sfBalance);

ripple::STAmount amount = balance - reserve;
if (balance < reserve)
amount.clear();
ripple::STAmount const amount = [&]() {
// AMM doesn't require the reserves
if ((sle.getFlags() & ripple::lsfAMMNode) != 0u)
return balance;
auto const reserve = backend.fetchFees(sequence, yield)->accountReserve(ownerCount);
ripple::STAmount amount = balance - reserve;
if (balance < reserve)
amount.clear();
return amount;
}();

return amount.xrp();
}
Expand Down Expand Up @@ -1038,11 +1043,10 @@ accountHolds(
)
{
ripple::STAmount amount;
if (ripple::isXRP(currency)) {
if (ripple::isXRP(currency))
return {xrpLiquid(backend, sequence, account, yield)};
}
auto key = ripple::keylet::line(account, issuer, currency).key;

auto const key = ripple::keylet::line(account, issuer, currency).key;
auto const blob = backend.fetchLedgerObject(key, sequence, yield);

if (!blob) {
Expand Down
20 changes: 20 additions & 0 deletions src/rpc/common/Validators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,24 @@ CustomValidator SubscribeAccountsValidator =
return MaybeError{};
}};

CustomValidator AMMAssetValidator =
CustomValidator{[](boost::json::value const& value, std::string_view key) -> MaybeError {
if (not value.is_object())
return Error{Status{RippledError::rpcINVALID_PARAMS, std::string(key) + "NotObject"}};

Json::Value jvAsset;
if (value.as_object().contains(JS(issuer)))
jvAsset["issuer"] = value.at(JS(issuer)).as_string().c_str();
if (value.as_object().contains(JS(currency)))
jvAsset["currency"] = value.at(JS(currency)).as_string().c_str();
// same as rippled
try {
ripple::issueFromJson(jvAsset);
} catch (std::runtime_error const&) {
return Error{Status{ClioError::rpcMALFORMED_REQUEST}};
}

return MaybeError{};
}};

} // namespace rpc::validation
7 changes: 7 additions & 0 deletions src/rpc/common/Validators.h
Original file line number Diff line number Diff line change
Expand Up @@ -508,4 +508,11 @@ extern CustomValidator SubscribeStreamValidator;
*/
extern CustomValidator SubscribeAccountsValidator;

/**
* @brief Validates an asset (ripple::Issue).
*
* Used by amm_info.
*/
extern CustomValidator AMMAssetValidator;

} // namespace rpc::validation
2 changes: 2 additions & 0 deletions src/rpc/common/impl/HandlerProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "feed/SubscriptionManager.h"
#include "rpc/Counters.h"
#include "rpc/common/AnyHandler.h"
#include "rpc/handlers/AMMInfo.h"
#include "rpc/handlers/AccountChannels.h"
#include "rpc/handlers/AccountCurrencies.h"
#include "rpc/handlers/AccountInfo.h"
Expand Down Expand Up @@ -79,6 +80,7 @@ ProductionHandlerProvider::ProductionHandlerProvider(
{"account_objects", {AccountObjectsHandler{backend}}},
{"account_offers", {AccountOffersHandler{backend}}},
{"account_tx", {AccountTxHandler{backend}}},
{"amm_info", {AMMInfoHandler{backend}}},
{"book_changes", {BookChangesHandler{backend}}},
{"book_offers", {BookOffersHandler{backend}}},
{"deposit_authorized", {DepositAuthorizedHandler{backend}}},
Expand Down
Loading

0 comments on commit f4df5c2

Please sign in to comment.