1#include <xrpld/app/misc/AMMHelpers.h>
2#include <xrpld/app/misc/AMMUtils.h>
3#include <xrpld/app/tx/detail/AMMDeposit.h>
5#include <xrpl/ledger/Sandbox.h>
6#include <xrpl/ledger/View.h>
7#include <xrpl/protocol/AMMCore.h>
8#include <xrpl/protocol/Feature.h>
9#include <xrpl/protocol/TxFlags.h>
31 auto const amount = ctx.
tx[~sfAmount];
32 auto const amount2 = ctx.
tx[~sfAmount2];
33 auto const ePrice = ctx.
tx[~sfEPrice];
34 auto const lpTokens = ctx.
tx[~sfLPTokenOut];
35 auto const tradingFee = ctx.
tx[~sfTradingFee];
45 JLOG(ctx.
j.
debug()) <<
"AMM Deposit: invalid flags.";
51 if (!lpTokens || ePrice || (amount && !amount2) ||
52 (!amount && amount2) || tradingFee)
58 if (!amount || amount2 || ePrice || tradingFee)
64 if (!amount || !amount2 || ePrice || tradingFee)
69 if (!amount || !lpTokens || amount2 || ePrice || tradingFee)
74 if (!amount || !ePrice || lpTokens || amount2 || tradingFee)
79 if (!amount || !amount2 || ePrice || lpTokens)
83 auto const asset = ctx.
tx[sfAsset].get<
Issue>();
84 auto const asset2 = ctx.
tx[sfAsset2].get<
Issue>();
87 JLOG(ctx.
j.
debug()) <<
"AMM Deposit: invalid asset pair.";
91 if (amount && amount2 && amount->issue() == amount2->issue())
93 JLOG(ctx.
j.
debug()) <<
"AMM Deposit: invalid tokens, same issue."
94 << amount->issue() <<
" " << amount2->issue();
98 if (lpTokens && *lpTokens <= beast::zero)
100 JLOG(ctx.
j.
debug()) <<
"AMM Deposit: invalid LPTokens";
111 JLOG(ctx.
j.
debug()) <<
"AMM Deposit: invalid amount";
121 JLOG(ctx.
j.
debug()) <<
"AMM Deposit: invalid amount2";
127 if (amount && ePrice)
134 JLOG(ctx.
j.
debug()) <<
"AMM Deposit: invalid EPrice";
141 JLOG(ctx.
j.
debug()) <<
"AMM Deposit: invalid trading fee.";
151 auto const accountID = ctx.
tx[sfAccount];
157 JLOG(ctx.
j.
debug()) <<
"AMM Deposit: Invalid asset pair.";
169 return expected.error();
170 auto const [amountBalance, amount2Balance, lptAMMBalance] = *expected;
173 if (lptAMMBalance != beast::zero)
175 if (amountBalance != beast::zero || amount2Balance != beast::zero)
178 JLOG(ctx.
j.
debug()) <<
"AMM Deposit: tokens balance is not zero.";
185 if (lptAMMBalance == beast::zero)
187 if (amountBalance <= beast::zero || amount2Balance <= beast::zero ||
188 lptAMMBalance < beast::zero)
192 <<
"AMM Deposit: reserves or tokens balance is zero.";
203 auto balance = [&](
auto const&
deposit) ->
TER {
206 auto const lpIssue = (*ammSle)[sfLPTokenBalance].issue();
209 keylet::line(accountID, lpIssue.account, lpIssue.currency));
216 return (accountID ==
deposit.issue().account ||
231 auto checkAsset = [&](
Issue const& asset) ->
TER {
235 <<
"AMM Deposit: account is not authorized, " << asset;
242 <<
"AMM Deposit: account or currency is frozen, "
251 if (
auto const ter = checkAsset(ctx.
tx[sfAsset].get<
Issue>()))
254 if (
auto const ter = checkAsset(ctx.
tx[sfAsset2].get<
Issue>()))
258 auto const amount = ctx.
tx[~sfAmount];
259 auto const amount2 = ctx.
tx[~sfAmount2];
260 auto const ammAccountID = ammSle->
getAccountID(sfAccount);
263 bool checkBalance) ->
TER {
274 <<
"AMM Deposit: account is not authorized, "
283 <<
"AMM Deposit: AMM account or currency is frozen, "
290 JLOG(ctx.
j.
debug()) <<
"AMM Deposit: account is frozen, "
297 if (
auto const ter = balance(*amount))
300 <<
"AMM Deposit: account has insufficient funds, "
312 if (
auto const ter = checkAmount(amount,
true))
315 if (
auto const ter = checkAmount(amount2,
true))
320 if (
auto const ter = checkAmount(amountBalance,
false))
322 if (
auto const ter = checkAmount(amount2Balance,
false))
327 if (
auto const lpTokens = ctx.
tx[~sfLPTokenOut];
328 lpTokens && lpTokens->issue() != lptAMMBalance.issue())
330 JLOG(ctx.
j.
debug()) <<
"AMM Deposit: invalid LPTokens.";
340 if (xrpBalance <= beast::zero)
342 JLOG(ctx.
j.
debug()) <<
"AMM Instance: insufficient reserves";
353 auto const amount =
ctx_.
tx[~sfAmount];
354 auto const amount2 =
ctx_.
tx[~sfAmount2];
355 auto const ePrice =
ctx_.
tx[~sfEPrice];
356 auto const lpTokensDeposit =
ctx_.
tx[~sfLPTokenOut];
360 auto const ammAccountID = (*ammSle)[sfAccount];
370 return {expected.error(),
false};
371 auto const [amountBalance, amount2Balance, lptAMMBalance] = *expected;
372 auto const tfee = (lptAMMBalance == beast::zero)
373 ?
ctx_.
tx[~sfTradingFee].value_or(0)
378 auto const [result, newLPTokenBalance] =
380 &amountBalance = amountBalance,
381 &amount2Balance = amount2Balance,
438 lptAMMBalance.issue(),
442 JLOG(
j_.
error()) <<
"AMM Deposit: invalid options.";
450 newLPTokenBalance > beast::zero,
451 "ripple::AMMDeposit::applyGuts : valid new LP token balance");
452 ammSle->setFieldAmount(sfLPTokenBalance, newLPTokenBalance);
455 if (lptAMMBalance == beast::zero)
457 sb, ammSle,
account_, lptAMMBalance.issue(), tfee);
495 auto checkBalance = [&](
auto const& depositAmount) ->
TER {
496 if (depositAmount <= beast::zero)
498 if (
isXRP(depositAmount))
500 auto const& lpIssue = lpTokensDeposit.
issue();
508 account_ == depositAmount.issue().account ||
512 depositAmount.issue(),
520 [amountDepositActual, amount2DepositActual, lpTokensDepositActual] =
530 if (lpTokensDepositActual <= beast::zero)
536 if (amountDepositActual < depositMin ||
537 amount2DepositActual < deposit2Min ||
538 lpTokensDepositActual < lpTokensDepositMin)
541 <<
"AMM Deposit: min deposit fails " << amountDepositActual <<
" "
543 << amount2DepositActual.value_or(
STAmount{}) <<
" "
550 if (
auto const ter = checkBalance(amountDepositActual))
553 "checkBalance to deposit or is 0"
554 << amountDepositActual;
568 <<
"AMM Deposit: failed to deposit " << amountDepositActual;
573 if (amount2DepositActual)
575 if (
auto const ter = checkBalance(*amount2DepositActual))
578 <<
"AMM Deposit: account has insufficient checkBalance to "
580 << *amount2DepositActual;
588 *amount2DepositActual,
594 <<
"AMM Deposit: failed to deposit " << *amount2DepositActual;
608 return {
tesSUCCESS, lptAMMBalance + lpTokensDepositActual};
617 if (!rules.
enabled(fixAMMv1_3))
618 return lpTokensDeposit;
639 auto const tokensAdj =
644 divide(tokensAdj, lptAMMBalance, lptAMMBalance.
issue());
646 auto const amountDeposit =
648 auto const amount2Deposit =
666 JLOG(
j_.
error()) <<
"AMMDeposit::equalDepositTokens exception "
713 auto frac =
Number{amount} / amountBalance;
716 if (tokensAdj == beast::zero)
725 auto const amount2Deposit =
727 if (amount2Deposit <= amount2)
740 frac =
Number{amount2} / amount2Balance;
743 if (tokensAdj == beast::zero)
752 auto const amountDeposit =
754 if (amountDeposit <= amount)
791 lpTokensOut(amountBalance, amount, lptAMMBalance, tfee));
792 if (tokens == beast::zero)
801 view.
rules(), amountBalance, amount, lptAMMBalance, tokens, tfee);
835 auto const tokensAdj =
840 auto const amountDeposit =
841 ammAssetIn(amountBalance, lptAMMBalance, tokensAdj, tfee);
842 if (amountDeposit > amount)
893 if (amount != beast::zero)
898 lpTokensOut(amountBalance, amount, lptAMMBalance, tfee));
899 if (tokens <= beast::zero)
908 view.
rules(), amountBalance, amount, lptAMMBalance, tokens, tfee);
911 auto const ep =
Number{amountDepositAdj} / tokensAdj;
946 auto const c = f1 * amountBalance / (ePrice * lptAMMBalance);
947 auto const d = f1 + c * f2 - c;
948 auto const a1 = c * c;
949 auto const b1 = c * c * f2 * f2 + 2 * c - d * d;
950 auto const c1 = 2 * c * f2 * f2 + 1 - 2 * d * f2;
951 auto amtNoRoundCb = [&] {
957 if (amountDeposit <= beast::zero)
959 auto tokNoRoundCb = [&] {
return amountDeposit / ePrice; };
960 auto tokProdCb = [&] {
return amountDeposit / ePrice; };
994 Issue const& lptIssue,
std::pair< TER, bool > applyGuts(Sandbox &view)
std::pair< TER, STAmount > equalDepositLimit(Sandbox &view, AccountID const &ammAccount, STAmount const &amountBalance, STAmount const &amount2Balance, STAmount const &lptAMMBalance, STAmount const &amount, STAmount const &amount2, std::optional< STAmount > const &lpTokensDepositMin, std::uint16_t tfee)
Equal asset deposit (Asset1In, Asset2In) with the constraint on the maximum amount of both assets tha...
static std::uint32_t getFlagsMask(PreflightContext const &ctx)
std::pair< TER, STAmount > singleDepositEPrice(Sandbox &view, AccountID const &ammAccount, STAmount const &amountBalance, STAmount const &amount, STAmount const &lptAMMBalance, STAmount const &ePrice, std::uint16_t tfee)
Single asset deposit (Asset1In, EPrice) with two constraints.
static TER preclaim(PreclaimContext const &ctx)
std::pair< TER, STAmount > singleDeposit(Sandbox &view, AccountID const &ammAccount, STAmount const &amountBalance, STAmount const &lptAMMBalance, STAmount const &amount, std::optional< STAmount > const &lpTokensDepositMin, std::uint16_t tfee)
Single asset deposit (Asset1In) by the amount.
static NotTEC preflight(PreflightContext const &ctx)
std::pair< TER, STAmount > equalDepositTokens(Sandbox &view, AccountID const &ammAccount, STAmount const &amountBalance, STAmount const &amount2Balance, STAmount const &lptAMMBalance, STAmount const &lpTokensDeposit, std::optional< STAmount > const &depositMin, std::optional< STAmount > const &deposit2Min, std::uint16_t tfee)
Equal asset deposit (LPTokens) for the specified share of the AMM instance pools.
static bool checkExtraFeatures(PreflightContext const &ctx)
std::pair< TER, STAmount > equalDepositInEmptyState(Sandbox &view, AccountID const &ammAccount, STAmount const &amount, STAmount const &amount2, Issue const &lptIssue, std::uint16_t tfee)
Equal deposit in empty AMM state (LP tokens balance is 0)
std::pair< TER, STAmount > singleDepositTokens(Sandbox &view, AccountID const &ammAccount, STAmount const &amountBalance, STAmount const &amount, STAmount const &lptAMMBalance, STAmount const &lpTokensDeposit, std::uint16_t tfee)
Single asset deposit (Asset1In, LPTokens) by the tokens.
std::pair< TER, STAmount > deposit(Sandbox &view, AccountID const &ammAccount, STAmount const &amountBalance, STAmount const &amountDeposit, std::optional< STAmount > const &amount2Deposit, STAmount const &lptAMMBalance, STAmount const &lpTokensDeposit, std::optional< STAmount > const &depositMin, std::optional< STAmount > const &deposit2Min, std::optional< STAmount > const &lpTokensDepositMin, std::uint16_t tfee)
Deposit requested assets and token amount into LP account.
beast::Journal const journal
A currency issued by an account.
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
virtual Rules const & rules() const =0
Returns the tx processing rules.
Rules controlling protocol behavior.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Issue const & issue() const
AccountID getAccountID(SField const &field) const
std::uint32_t getFlags() const
Discardable, editable view to a ledger.
void update(std::shared_ptr< SLE > const &sle) override
Indicate changes to a peeked SLE.
std::shared_ptr< SLE > peek(Keylet const &k) override
Prepare to modify the SLE associated with key.
T make_optional(T... args)
Keylet amm(Asset const &issue1, Asset const &issue2) noexcept
AMM entry.
Keylet line(AccountID const &id0, AccountID const &id1, Currency const ¤cy) noexcept
The index of a trust line for a given currency.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
constexpr std::uint32_t tfSingleAsset
NotTEC invalidAMMAmount(STAmount const &amount, std::optional< std::pair< Issue, Issue > > const &pair=std::nullopt, bool validZero=false)
Validate the amount.
STAmount divide(STAmount const &amount, Rate const &rate)
std::pair< STAmount, STAmount > adjustAssetInByTokens(Rules const &rules, STAmount const &balance, STAmount const &amount, STAmount const &lptAMMBalance, STAmount const &tokens, std::uint16_t tfee)
bool isXRP(AccountID const &c)
bool isIndividualFrozen(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer)
std::uint16_t getTradingFee(ReadView const &view, SLE const &ammSle, AccountID const &account)
Get AMM trading fee for the given account.
Number solveQuadraticEq(Number const &a, Number const &b, Number const &c)
Positive solution for quadratic equation: x = (-b + sqrt(b**2 + 4*a*c))/(2*a)
constexpr std::uint32_t tfLimitLPToken
STAmount ammAssetIn(STAmount const &asset1Balance, STAmount const &lptAMMBalance, STAmount const &lpTokens, std::uint16_t tfee)
Calculate asset deposit given LP Tokens.
bool ammEnabled(Rules const &)
Return true if required AMM amendments are enabled.
constexpr std::uint32_t tfOneAssetLPToken
TER accountSend(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &saAmount, beast::Journal j, WaiveTransferFee waiveFee=WaiveTransferFee::No)
Calls static accountSendIOU if saAmount represents Issue.
STAmount getRoundedLPTokens(Rules const &rules, STAmount const &balance, Number const &frac, IsDeposit isDeposit)
Round AMM deposit/withdrawal LPToken amount.
bool isFrozen(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer)
TER requireAuth(ReadView const &view, Issue const &issue, AccountID const &account, AuthType authType=AuthType::Legacy)
Check if the account lacks required authorization.
STAmount adjustLPTokens(STAmount const &lptAMMBalance, STAmount const &lpTokens, IsDeposit isDeposit)
Adjust LP tokens to deposit/withdraw.
Number feeMult(std::uint16_t tfee)
Get fee multiplier (1 - tfee) @tfee trading fee in basis points.
void initializeFeeAuctionVote(ApplyView &view, std::shared_ptr< SLE > &ammSle, AccountID const &account, Issue const &lptIssue, std::uint16_t tfee)
Initialize Auction and Voting slots and set the trading/discounted fee.
constexpr std::uint32_t tfTwoAsset
STAmount ammLPHolds(ReadView const &view, Currency const &cur1, Currency const &cur2, AccountID const &ammAccount, AccountID const &lpAccount, beast::Journal const j)
Get the balance of LP tokens.
std::tuple< STAmount, std::optional< STAmount >, STAmount > adjustAmountsByLPTokens(STAmount const &amountBalance, STAmount const &amount, std::optional< STAmount > const &amount2, STAmount const &lptAMMBalance, STAmount const &lpTokens, std::uint16_t tfee, IsDeposit isDeposit)
Calls adjustLPTokens() and adjusts deposit or withdraw amounts if the adjusted LP tokens are less tha...
constexpr std::uint32_t tfDepositSubTx
static STAmount adjustLPTokensOut(Rules const &rules, STAmount const &lptAMMBalance, STAmount const &lpTokensDeposit)
constexpr std::uint32_t tfLPToken
NotTEC invalidAMMAssetPair(Issue const &issue1, Issue const &issue2, std::optional< std::pair< Issue, Issue > > const &pair=std::nullopt)
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j)
constexpr std::uint32_t tfDepositMask
Expected< std::tuple< STAmount, STAmount, STAmount >, TER > ammHolds(ReadView const &view, SLE const &ammSle, std::optional< Issue > const &optIssue1, std::optional< Issue > const &optIssue2, FreezeHandling freezeHandling, beast::Journal const j)
Get AMM pool and LP token balances.
STAmount ammLPTokens(STAmount const &asset1, STAmount const &asset2, Issue const &lptIssue)
Calculate LP Tokens given AMM pool reserves.
std::string to_string(base_uint< Bits, Tag > const &a)
constexpr std::uint32_t tfTwoAssetIfEmpty
Number feeMultHalf(std::uint16_t tfee)
Get fee multiplier (1 - tfee / 2) @tfee trading fee in basis points.
STAmount lpTokensOut(STAmount const &asset1Balance, STAmount const &asset1Deposit, STAmount const &lptAMMBalance, std::uint16_t tfee)
Calculate LP Tokens given asset's deposit amount.
TERSubset< CanCvtToTER > TER
std::uint16_t constexpr TRADING_FEE_THRESHOLD
STAmount getRoundedAsset(Rules const &rules, STAmount const &balance, A const &frac, IsDeposit isDeposit)
Round AMM equal deposit/withdrawal amount.
Number adjustFracByTokens(Rules const &rules, STAmount const &lptAMMBalance, STAmount const &tokens, Number const &frac)
Find a fraction of tokens after the tokens are adjusted.
XRPAmount xrpLiquid(ReadView const &view, AccountID const &id, std::int32_t ownerCountAdj, beast::Journal j)
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.