Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/xrpl/ledger/entries/AMMEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ class AMMEntry : public SLEBase<ViewT, ltAMM>
void
initializeFeeAuctionVote(AccountID const& account, Asset const& lptAsset, std::uint16_t tfee)
requires Base::kIsWritable;

// Due to rounding, the LPTokenBalance of the last LP might not match the
// LP's trustline balance. If account is the only LP and lpTokens is within
// the tolerance, set LPTokenBalance to lpTokens.
[[nodiscard]] std::expected<bool, TER>
verifyAndAdjustLPTokenBalance(STAmount const& lpTokens, AccountID const& account)
requires Base::kIsWritable;
};

using AMMEntryR = AMMEntry<ReadView>;
Expand Down
13 changes: 0 additions & 13 deletions include/xrpl/ledger/helpers/AMMHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <xrpl/beast/utility/Zero.h>
#include <xrpl/ledger/ReadView.h>
#include <xrpl/ledger/Sandbox.h>
#include <xrpl/ledger/entries/AMMEntry.h>
#include <xrpl/ledger/helpers/TokenHelpers.h>
#include <xrpl/protocol/AMMCore.h>
#include <xrpl/protocol/AccountID.h>
Expand Down Expand Up @@ -757,16 +756,4 @@ deleteAMMAccount(Sandbox& view, Asset const& asset, Asset const& asset2, beast::
std::expected<bool, TER>
isOnlyLiquidityProvider(ReadView const& view, Issue const& ammIssue, AccountID const& lpAccount);

/**
* Due to rounding, the LPTokenBalance of the last LP might
* not match the LP's trustline balance. If it's within the tolerance,
* update LPTokenBalance to match the LP's trustline balance.
*/
std::expected<bool, TER>
verifyAndAdjustLPTokenBalance(
Sandbox const& sb,
STAmount const& lpTokens,
AMMEntryW& ammSle,
AccountID const& account);

} // namespace xrpl
28 changes: 28 additions & 0 deletions src/libxrpl/ledger/entries/AMMEntry.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <xrpl/ledger/entries/AMMEntry.h>

#include <xrpl/basics/Log.h>
#include <xrpl/basics/Number.h>
#include <xrpl/basics/safe_cast.h>
#include <xrpl/beast/utility/instrumentation.h>
#include <xrpl/ledger/ApplyView.h>
Expand Down Expand Up @@ -232,6 +233,33 @@
auctionSlot.makeFieldAbsent(sfAuthAccounts);
}

template <typename ViewT>
std::expected<bool, TER>
AMMEntry<ViewT>::verifyAndAdjustLPTokenBalance(STAmount const& lpTokens, AccountID const& account)
requires Base::kIsWritable
{
auto const res = isOnlyLiquidityProvider(this->applyView(), lpTokens.get<Issue>(), account);
if (!res.has_value())
{
return std::unexpected<TER>(res.error());

Check warning on line 244 in src/libxrpl/ledger/entries/AMMEntry.cpp

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/ledger/entries/AMMEntry.cpp#L244

Added line #L244 was not covered by tests
}

if (res.value())
{
if (withinRelativeDistance(
lpTokens, Base::operator->()->getFieldAmount(sfLPTokenBalance), Number{1, -3}))
{
Base::operator->()->setFieldAmount(sfLPTokenBalance, lpTokens);
this->updateInView();
}
else
{
return std::unexpected<TER>(tecAMM_INVALID_TOKENS); // LCOV_EXCL_LINE
}
}
return true;
}

template class AMMEntry<ReadView>;
template class AMMEntry<ApplyView>;

Expand Down
29 changes: 0 additions & 29 deletions src/libxrpl/ledger/helpers/AMMHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,33 +711,4 @@ isOnlyLiquidityProvider(ReadView const& view, Issue const& ammIssue, AccountID c
return std::unexpected<TER>(tecINTERNAL); // LCOV_EXCL_LINE
}

std::expected<bool, TER>
verifyAndAdjustLPTokenBalance(
Sandbox const& sb,
STAmount const& lpTokens,
AMMEntryW& ammSle,
AccountID const& account)
{
auto const res = isOnlyLiquidityProvider(sb, lpTokens.get<Issue>(), account);
if (!res.has_value())
{
return std::unexpected<TER>(res.error());
}

if (res.value())
{
if (withinRelativeDistance(
lpTokens, ammSle->getFieldAmount(sfLPTokenBalance), Number{1, -3}))
{
ammSle->setFieldAmount(sfLPTokenBalance, lpTokens);
ammSle.updateInView();
}
else
{
return std::unexpected<TER>(tecAMM_INVALID_TOKENS); // LCOV_EXCL_LINE
}
}
return true;
}

} // namespace xrpl
3 changes: 1 addition & 2 deletions src/libxrpl/tx/transactors/dex/AMMClawback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,7 @@ AMMClawback::applyGuts(Sandbox& sb)
if (lpTokenBalance == beast::kZero)
return tecAMM_BALANCE;

if (auto const res = verifyAndAdjustLPTokenBalance(sb, lpTokenBalance, ammSle, holder);
!res)
if (auto const res = ammSle.verifyAndAdjustLPTokenBalance(lpTokenBalance, holder); !res)
return res.error(); // LCOV_EXCL_LINE
}

Expand Down
2 changes: 1 addition & 1 deletion src/libxrpl/tx/transactors/dex/AMMWithdraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ AMMWithdraw::applyGuts(Sandbox& sb)

// Due to rounding, the LPTokenBalance of the last LP
// might not match the LP's trustline balance
if (auto const res = verifyAndAdjustLPTokenBalance(sb, lpTokens, ammSle, accountID_); !res)
if (auto const res = ammSle.verifyAndAdjustLPTokenBalance(lpTokens, accountID_); !res)
return {res.error(), false}; // LCOV_EXCL_LINE

auto const tfee = ammSle.tradingFee(accountID_);
Expand Down
Loading