xrpld
Loading...
Searching...
No Matches
AMMDeposit.cpp
1#include <xrpl/tx/transactors/dex/AMMDeposit.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/Number.h>
5#include <xrpl/beast/utility/Zero.h>
6#include <xrpl/beast/utility/instrumentation.h>
7#include <xrpl/ledger/Sandbox.h>
8#include <xrpl/ledger/helpers/AMMHelpers.h>
9#include <xrpl/ledger/helpers/AccountRootHelpers.h>
10#include <xrpl/ledger/helpers/MPTokenHelpers.h>
11#include <xrpl/ledger/helpers/TokenHelpers.h>
12#include <xrpl/protocol/AMMCore.h>
13#include <xrpl/protocol/AccountID.h>
14#include <xrpl/protocol/Asset.h>
15#include <xrpl/protocol/Feature.h>
16#include <xrpl/protocol/Indexes.h>
17#include <xrpl/protocol/MPTIssue.h>
18#include <xrpl/protocol/SField.h>
19#include <xrpl/protocol/STAmount.h>
20#include <xrpl/protocol/STLedgerEntry.h>
21#include <xrpl/protocol/STTx.h>
22#include <xrpl/protocol/TER.h>
23#include <xrpl/protocol/TxFlags.h>
24#include <xrpl/protocol/XRPAmount.h>
25#include <xrpl/tx/Transactor.h>
26
27#include <bit>
28#include <cstdint>
29#include <exception>
30#include <optional>
31#include <stdexcept>
32#include <utility>
33
34namespace xrpl {
35
36bool
38{
39 auto const amount = ctx.tx[~sfAmount];
40 auto const amount2 = ctx.tx[~sfAmount2];
41
42 return ctx.rules.enabled(featureMPTokensV2) ||
43 (!ctx.tx[sfAsset].holds<MPTIssue>() && !ctx.tx[sfAsset2].holds<MPTIssue>() &&
44 !(amount && amount->holds<MPTIssue>()) && !(amount2 && amount2->holds<MPTIssue>()));
45}
46
49{
50 return tfAMMDepositMask;
51}
52
55{
56 auto const flags = ctx.tx.getFlags();
57 auto const amount = ctx.tx[~sfAmount];
58 auto const amount2 = ctx.tx[~sfAmount2];
59 auto const ePrice = ctx.tx[~sfEPrice];
60 auto const lpTokens = ctx.tx[~sfLPTokenOut];
61 auto const tradingFee = ctx.tx[~sfTradingFee];
62 // Valid options for the flags are:
63 // tfLPTokens: LPTokenOut, [Amount, Amount2]
64 // tfSingleAsset: Amount, [LPTokenOut]
65 // tfTwoAsset: Amount, Amount2, [LPTokenOut]
66 // tfTwoAssetIfEmpty: Amount, Amount2, [sfTradingFee]
67 // tfOnAssetLPToken: Amount and LPTokenOut
68 // tfLimitLPToken: Amount and EPrice
69 if (std::popcount(flags & tfDepositSubTx) != 1)
70 {
71 JLOG(ctx.j.debug()) << "AMM Deposit: invalid flags.";
72 return temMALFORMED;
73 }
74 if (ctx.tx.isFlag(tfLPToken))
75 {
76 // if included then both amount and amount2 are deposit min
77 if (!lpTokens || ePrice || (amount && !amount2) || (!amount && amount2) || tradingFee)
78 return temMALFORMED;
79 }
80 else if (ctx.tx.isFlag(tfSingleAsset))
81 {
82 // if included then lpTokens is deposit min
83 if (!amount || amount2 || ePrice || tradingFee)
84 return temMALFORMED;
85 }
86 else if (ctx.tx.isFlag(tfTwoAsset))
87 {
88 // if included then lpTokens is deposit min
89 if (!amount || !amount2 || ePrice || tradingFee)
90 return temMALFORMED;
91 }
92 else if (ctx.tx.isFlag(tfOneAssetLPToken))
93 {
94 if (!amount || !lpTokens || amount2 || ePrice || tradingFee)
95 return temMALFORMED;
96 }
97 else if (ctx.tx.isFlag(tfLimitLPToken))
98 {
99 if (!amount || !ePrice || lpTokens || amount2 || tradingFee)
100 return temMALFORMED;
101 }
102 else if (ctx.tx.isFlag(tfTwoAssetIfEmpty))
103 {
104 if (!amount || !amount2 || ePrice || lpTokens)
105 return temMALFORMED;
106 }
107
108 auto const asset = ctx.tx[sfAsset];
109 auto const asset2 = ctx.tx[sfAsset2];
110 if (auto const res = invalidAMMAssetPair(asset, asset2))
111 {
112 JLOG(ctx.j.debug()) << "AMM Deposit: invalid asset pair.";
113 return res;
114 }
115
116 if (amount && amount2 && amount->asset() == amount2->asset())
117 {
118 JLOG(ctx.j.debug()) << "AMM Deposit: invalid tokens, same issue." << amount->asset() << " "
119 << amount2->asset();
120 return temBAD_AMM_TOKENS;
121 }
122
123 if (lpTokens && *lpTokens <= beast::kZero)
124 {
125 JLOG(ctx.j.debug()) << "AMM Deposit: invalid LPTokens";
126 return temBAD_AMM_TOKENS;
127 }
128
129 if (amount)
130 {
131 if (auto const res = invalidAMMAmount(
132 *amount, std::make_optional(std::make_pair(asset, asset2)), ePrice.has_value()))
133 {
134 JLOG(ctx.j.debug()) << "AMM Deposit: invalid amount";
135 return res;
136 }
137 }
138
139 if (amount2)
140 {
141 if (auto const res =
142 invalidAMMAmount(*amount2, std::make_optional(std::make_pair(asset, asset2))))
143 {
144 JLOG(ctx.j.debug()) << "AMM Deposit: invalid amount2";
145 return res;
146 }
147 }
148
149 if (amount && ePrice)
150 {
151 auto const assets = [&] -> std::optional<std::pair<Asset, Asset>> {
152 // don't check ePrice issue
153 if (ctx.rules.enabled(featureMPTokensV2))
154 return std::nullopt;
155 // must be amount issue
156 return std::make_optional(std::make_pair(amount->asset(), amount->asset()));
157 }();
158 if (auto const res = invalidAMMAmount(*ePrice, assets))
159 {
160 JLOG(ctx.j.debug()) << "AMM Deposit: invalid EPrice";
161 return res;
162 }
163 }
164
165 if (tradingFee > kTradingFeeThreshold)
166 {
167 JLOG(ctx.j.debug()) << "AMM Deposit: invalid trading fee.";
168 return temBAD_FEE;
169 }
170
171 return tesSUCCESS;
172}
173
174TER
176{
177 auto const accountID = ctx.tx[sfAccount];
178
179 auto const ammSle = ctx.view.read(keylet::amm(ctx.tx[sfAsset], ctx.tx[sfAsset2]));
180 if (!ammSle)
181 {
182 JLOG(ctx.j.debug()) << "AMM Deposit: Invalid asset pair.";
183 return terNO_AMM;
184 }
185
186 auto const expected = ammHolds(
187 ctx.view,
188 *ammSle,
189 std::nullopt,
190 std::nullopt,
193 ctx.j);
194 if (!expected)
195 return expected.error(); // LCOV_EXCL_LINE
196 auto const [amountBalance, amount2Balance, lptAMMBalance] = *expected;
197 if (ctx.tx.isFlag(tfTwoAssetIfEmpty))
198 {
199 if (lptAMMBalance != beast::kZero)
200 return tecAMM_NOT_EMPTY;
201 if (amountBalance != beast::kZero || amount2Balance != beast::kZero)
202 {
203 // LCOV_EXCL_START
204 JLOG(ctx.j.debug()) << "AMM Deposit: tokens balance is not zero.";
205 return tecINTERNAL;
206 // LCOV_EXCL_STOP
207 }
208 }
209 else
210 {
211 if (lptAMMBalance == beast::kZero)
212 return tecAMM_EMPTY;
213 if (amountBalance <= beast::kZero || amount2Balance <= beast::kZero ||
214 lptAMMBalance < beast::kZero)
215 {
216 // LCOV_EXCL_START
217 JLOG(ctx.j.debug()) << "AMM Deposit: reserves or tokens balance is zero.";
218 return tecINTERNAL;
219 // LCOV_EXCL_STOP
220 }
221 }
222
223 // Check account has sufficient funds.
224 // Return tesSUCCESS if it does, error otherwise.
225 // Have to check again in deposit() because
226 // amounts might be derived based on tokens or
227 // limits.
228 auto balance = [&](auto const& deposit) -> TER {
229 if (isXRP(deposit))
230 {
231 auto const lpIssue = (*ammSle)[sfLPTokenBalance].get<Issue>();
232 // Adjust the reserve if LP doesn't have LPToken trustline
233 auto const sle =
234 ctx.view.read(keylet::trustLine(accountID, lpIssue.account, lpIssue.currency));
235 if (xrpLiquid(ctx.view, accountID, !sle, ctx.j) >= deposit)
236 return TER(tesSUCCESS);
237 if (sle)
238 return tecUNFUNDED_AMM;
240 }
241 return accountFunds(
242 ctx.view,
243 accountID,
244 deposit,
247 ctx.j) >= deposit
248 ? TER(tesSUCCESS)
250 };
251
252 auto const amount = ctx.tx[~sfAmount];
253 auto const amount2 = ctx.tx[~sfAmount2];
254 auto const ammAccountID = ammSle->getAccountID(sfAccount);
255
256 if (ctx.view.rules().enabled(fixCleanup3_3_0))
257 {
258 // Unified deposit freeze check for both pool assets.
259 // AMMDeposit is not allowed if either asset is frozen.
260 auto const checkAsset = [&](Asset const& asset) -> TER {
261 if (auto const ter = requireAuth(ctx.view, asset, accountID, AuthType::WeakAuth))
262 {
263 JLOG(ctx.j.debug()) << "AMM Deposit: account is not authorized, " << asset;
264 return ter;
265 }
266 if (auto const ter = checkDepositFreeze(ctx.view, accountID, ammAccountID, asset))
267 {
268 JLOG(ctx.j.debug())
269 << "AMM Deposit: frozen, " << to_string(accountID) << " " << to_string(asset);
270 return ter;
271 }
272 return tesSUCCESS;
273 };
274
275 if (auto const ter = checkAsset(ctx.tx[sfAsset]))
276 return ter;
277
278 if (auto const ter = checkAsset(ctx.tx[sfAsset2]))
279 return ter;
280 }
281 else if (ctx.view.rules().enabled(featureAMMClawback))
282 {
283 // Check if either of the assets is frozen, AMMDeposit is not allowed
284 // if either asset is frozen
285 auto const checkAsset = [&](Asset const& asset) -> TER {
286 // WeakAuth - don't need to check if MPT object exists as might be
287 // depositing into non-MPT pool. It'll fail on send if MPT doesn't
288 // exist.
289 if (auto const ter = requireAuth(ctx.view, asset, accountID, AuthType::WeakAuth))
290 {
291 JLOG(ctx.j.debug()) << "AMM Deposit: account is not authorized, " << asset;
292 return ter;
293 }
294
295 if (auto const ter = checkFrozen(ctx.view, accountID, asset); !isTesSuccess(ter))
296 {
297 JLOG(ctx.j.debug()) << "AMM Deposit: account or currency is frozen or locked, "
298 << to_string(accountID) << " " << to_string(asset);
299
300 return ter;
301 }
302
303 return tesSUCCESS;
304 };
305
306 if (auto const ter = checkAsset(ctx.tx[sfAsset]))
307 return ter;
308
309 if (auto const ter = checkAsset(ctx.tx[sfAsset2]))
310 return ter;
311 }
312
313 auto const checkAmount = [&](std::optional<STAmount> const& amount, bool checkBalance) -> TER {
314 if (amount)
315 {
316 // This normally should not happen.
317 // Account is not authorized to hold the assets it's depositing,
318 // or it doesn't even have a trust line or MPT for them.
319 if (auto const ter = requireAuth(ctx.view, amount->asset(), accountID))
320 {
321 // LCOV_EXCL_START
322 JLOG(ctx.j.debug())
323 << "AMM Deposit: account is not authorized, " << amount->asset();
324 return ter;
325 // LCOV_EXCL_STOP
326 }
327 if (!ctx.view.rules().enabled(fixCleanup3_3_0))
328 {
329 // AMM account or currency frozen
330 if (auto const ter = checkFrozen(ctx.view, ammAccountID, amount->asset());
331 !isTesSuccess(ter))
332 {
333 JLOG(ctx.j.debug())
334 << "AMM Deposit: AMM account or currency is frozen or locked, "
335 << to_string(accountID);
336 return ter;
337 }
338 // Account frozen
339 if (auto const ter = checkIndividualFrozen(ctx.view, accountID, amount->asset());
340 !isTesSuccess(ter))
341 {
342 JLOG(ctx.j.debug())
343 << "AMM Deposit: account is frozen or locked, " << to_string(accountID)
344 << " " << to_string(amount->asset());
345 return ter;
346 }
347 }
348 if (checkBalance)
349 {
350 if (auto const ter = balance(*amount))
351 {
352 JLOG(ctx.j.debug())
353 << "AMM Deposit: account has insufficient funds, " << *amount;
354 return ter;
355 }
356 }
357 }
358 return tesSUCCESS;
359 };
360
361 // amount and amount2 are deposit min in case of tfLPToken
362 if (!ctx.tx.isFlag(tfLPToken))
363 {
364 if (auto const ter = checkAmount(amount, true))
365 return ter;
366
367 if (auto const ter = checkAmount(amount2, true))
368 return ter;
369 }
370 else
371 {
372 if (auto const ter = checkAmount(amountBalance, false))
373 return ter;
374 if (auto const ter = checkAmount(amount2Balance, false))
375 return ter;
376 }
377
378 // Equal deposit lp tokens
379 if (auto const lpTokens = ctx.tx[~sfLPTokenOut];
380 lpTokens && lpTokens->asset() != lptAMMBalance.asset())
381 {
382 JLOG(ctx.j.debug()) << "AMM Deposit: invalid LPTokens.";
383 return temBAD_AMM_TOKENS;
384 }
385
386 // Check the reserve for LPToken trustline if not LP.
387 // We checked above but need to check again if depositing IOU only.
388 if (ammLPHolds(ctx.view, *ammSle, accountID, ctx.j) == beast::kZero)
389 {
390 STAmount const xrpBalance = xrpLiquid(ctx.view, accountID, 1, ctx.j);
391 // Insufficient reserve
392 if (xrpBalance <= beast::kZero)
393 {
394 JLOG(ctx.j.debug()) << "AMM Instance: insufficient reserves";
396 }
397 }
398
399 if (auto const ter = canMPTTradeAndTransfer(ctx.view, ctx.tx[sfAsset], accountID, accountID);
400 !isTesSuccess(ter))
401 return ter;
402 if (auto const ter = canMPTTradeAndTransfer(ctx.view, ctx.tx[sfAsset2], accountID, accountID);
403 !isTesSuccess(ter))
404 return ter;
405
406 return tesSUCCESS;
407}
408
411{
412 auto const amount = ctx_.tx[~sfAmount];
413 auto const amount2 = ctx_.tx[~sfAmount2];
414 auto const ePrice = ctx_.tx[~sfEPrice];
415 auto const lpTokensDeposit = ctx_.tx[~sfLPTokenOut];
416 auto const ammSle = sb.peek(keylet::amm(ctx_.tx[sfAsset], ctx_.tx[sfAsset2]));
417 if (!ammSle)
418 return {tecINTERNAL, false}; // LCOV_EXCL_LINE
419 auto const ammAccountID = (*ammSle)[sfAccount];
420
421 auto const expected = ammHolds(
422 sb,
423 *ammSle,
424 amount ? amount->asset() : std::optional<Asset>{},
425 amount2 ? amount2->asset() : std::optional<Asset>{},
428 ctx_.journal);
429 if (!expected)
430 return {expected.error(), false}; // LCOV_EXCL_LINE
431 auto const [amountBalance, amount2Balance, lptAMMBalance] = *expected;
432 auto const tfee = (lptAMMBalance == beast::kZero)
433 ? ctx_.tx[~sfTradingFee].value_or(0)
434 : getTradingFee(ctx_.view(), *ammSle, accountID_);
435
436 auto const subTxType = ctx_.tx.getFlags() & tfDepositSubTx;
437
438 auto dispatchToDeposit = [&,
439 &amountBalance = amountBalance,
440 &amount2Balance = amount2Balance,
441 &lptAMMBalance = lptAMMBalance] -> std::pair<TER, STAmount> {
442 if (subTxType & tfTwoAsset)
443 {
444 return equalDepositLimit(
445 sb,
446 ammAccountID,
447 amountBalance,
448 amount2Balance,
449 lptAMMBalance,
450 *amount,
451 *amount2,
452 lpTokensDeposit,
453 tfee);
454 }
455 if (subTxType & tfOneAssetLPToken)
456 {
457 return singleDepositTokens(
458 sb, ammAccountID, amountBalance, *amount, lptAMMBalance, *lpTokensDeposit, tfee);
459 }
460 if (subTxType & tfLimitLPToken)
461 {
462 return singleDepositEPrice(
463 sb, ammAccountID, amountBalance, *amount, lptAMMBalance, *ePrice, tfee);
464 }
465 if (subTxType & tfSingleAsset)
466 {
467 return singleDeposit(
468 sb, ammAccountID, amountBalance, lptAMMBalance, *amount, lpTokensDeposit, tfee);
469 }
470 if (subTxType & tfLPToken)
471 {
472 return equalDepositTokens(
473 sb,
474 ammAccountID,
475 amountBalance,
476 amount2Balance,
477 lptAMMBalance,
478 *lpTokensDeposit,
479 amount,
480 amount2,
481 tfee);
482 }
483 if (subTxType & tfTwoAssetIfEmpty)
484 {
486 sb, ammAccountID, *amount, *amount2, lptAMMBalance.asset(), tfee);
487 }
488 // should not happen.
489 // LCOV_EXCL_START
490 JLOG(j_.error()) << "AMM Deposit: invalid options.";
492 // LCOV_EXCL_STOP
493 };
494
495 auto const [result, newLPTokenBalance] = [&] -> std::pair<TER, STAmount> {
496 try
497 {
498 return dispatchToDeposit();
499 }
500 catch (std::runtime_error const& e)
501 {
502 REACHABLE("xrpl::AMMDeposit::applyGuts : deposit amount out of range reached");
503 // A deposit whose solved amount exceeds the integral asset's range
504 // throws while converting to STAmount: past int64max
505 // Number::operator rep() throws std::overflow_error; above the asset
506 // maximum STAmount::canonicalize throws std::runtime_error. Fail
507 // cleanly with a tec rather than letting it escape doApply as
508 // tefEXCEPTION. Any other exception is left to propagate.
509 // Gated by fixCleanup3_4_0 to preserve the legacy result pre-amendment.
510 if (!sb.rules().enabled(fixCleanup3_4_0))
511 throw; // LCOV_EXCL_LINE - preserve legacy tefEXCEPTION
512 JLOG(j_.error()) << "AMMDeposit: deposit amount out of range " << e.what();
514 }
515 }();
516
517 if (isTesSuccess(result))
518 {
519 XRPL_ASSERT(
520 newLPTokenBalance > beast::kZero,
521 "xrpl::AMMDeposit::applyGuts : valid new LP token balance");
522 // Defensive check: deposit formulas with fixAMMv1_3 round LP tokens
523 // down and asset amounts up, so sqrt(pool1*pool2) >= newLPTokenBalance
524 // is guaranteed to hold. A precision loss failure is not expected.
525 if (sb.rules().enabled(fixCleanup3_3_0) && sb.rules().enabled(fixAMMv1_3))
526 {
527 if (auto const ter = checkAMMPrecisionLoss(
528 sb, ammAccountID, ctx_.tx[sfAsset], ctx_.tx[sfAsset2], newLPTokenBalance, j_);
529 !isTesSuccess(ter))
530 {
531 UNREACHABLE("xrpl::AMMDeposit::applyGuts : AMM precision loss");
532 return {ter, false}; // LCOV_EXCL_LINE
533 }
534 }
535 ammSle->setFieldAmount(sfLPTokenBalance, newLPTokenBalance);
536 // LP depositing into AMM empty state gets the auction slot
537 // and the voting
538 if (lptAMMBalance == beast::kZero)
539 initializeFeeAuctionVote(sb, ammSle, accountID_, lptAMMBalance.asset(), tfee);
540
541 sb.update(ammSle);
542 }
543
544 return {result, isTesSuccess(result)};
545}
546
547TER
549{
550 // This is the ledger view that we work against. Transactions are applied
551 // as we go on processing transactions.
552 Sandbox sb(&ctx_.view());
553
554 auto const result = applyGuts(sb);
555 if (result.second)
556 sb.apply(ctx_.rawView());
557
558 return result.first;
559}
560
563 Sandbox& view,
564 AccountID const& ammAccount,
565 STAmount const& amountBalance,
566 STAmount const& amountDeposit,
567 std::optional<STAmount> const& amount2Deposit,
568 STAmount const& lptAMMBalance,
569 STAmount const& lpTokensDeposit,
570 std::optional<STAmount> const& depositMin,
571 std::optional<STAmount> const& deposit2Min,
572 std::optional<STAmount> const& lpTokensDepositMin,
573 std::uint16_t tfee)
574{
575 // Check account has sufficient funds.
576 // Return true if it does, false otherwise.
577 auto const checkBalance = [&](auto const& depositAmount) -> TER {
578 if (depositAmount <= beast::kZero)
579 return temBAD_AMOUNT;
580 if (isXRP(depositAmount))
581 {
582 auto const& lpIssue = lpTokensDeposit.get<Issue>();
583 // Adjust the reserve if LP doesn't have LPToken trustline
584 auto const sle =
585 view.read(keylet::trustLine(accountID_, lpIssue.account, lpIssue.currency));
586 if (xrpLiquid(view, accountID_, !sle, j_) >= depositAmount)
587 return tesSUCCESS;
588 }
589 else if (
591 view,
593 depositAmount,
596 ctx_.journal) >= depositAmount)
597 {
598 return tesSUCCESS;
599 }
600 return tecUNFUNDED_AMM;
601 };
602
603 auto const [amountDepositActual, amount2DepositActual, lpTokensDepositActual] =
605 amountBalance,
606 amountDeposit,
607 amount2Deposit,
608 lptAMMBalance,
609 lpTokensDeposit,
610 tfee,
612
613 if (lpTokensDepositActual <= beast::kZero)
614 {
615 JLOG(ctx_.journal.debug()) << "AMM Deposit: adjusted tokens zero";
617 }
618
619 if (amountDepositActual < depositMin || amount2DepositActual < deposit2Min ||
620 lpTokensDepositActual < lpTokensDepositMin)
621 {
622 JLOG(ctx_.journal.debug())
623 << "AMM Deposit: min deposit fails " << amountDepositActual << " "
624 << depositMin.value_or(STAmount{}) << " " << amount2DepositActual.value_or(STAmount{})
625 << " " << deposit2Min.value_or(STAmount{}) << " " << lpTokensDepositActual << " "
626 << lpTokensDepositMin.value_or(STAmount{});
627 return {tecAMM_FAILED, STAmount{}};
628 }
629
630 // Deposit amountDeposit
631 if (auto const ter = checkBalance(amountDepositActual))
632 {
633 JLOG(ctx_.journal.debug()) << "AMM Deposit: account has insufficient "
634 "checkBalance to deposit or is 0"
635 << amountDepositActual;
636 return {ter, STAmount{}};
637 }
638
639 auto res = accountSend(
640 view,
642 ammAccount,
643 amountDepositActual,
644 ctx_.journal,
645 {}, // don't sponsor for AMM Trustline
647 if (!isTesSuccess(res))
648 {
649 JLOG(ctx_.journal.debug()) << "AMM Deposit: failed to deposit " << amountDepositActual;
650 return {res, STAmount{}};
651 }
652
653 // Deposit amount2Deposit
654 if (amount2DepositActual)
655 {
656 if (auto const ter = checkBalance(*amount2DepositActual))
657 {
658 JLOG(ctx_.journal.debug()) << "AMM Deposit: account has insufficient checkBalance to "
659 "deposit or is 0 "
660 << *amount2DepositActual;
661 return {ter, STAmount{}};
662 }
663
664 res = accountSend(
665 view,
667 ammAccount,
668 *amount2DepositActual,
669 ctx_.journal,
670 {}, // don't sponsor for AMM Trustline
672 if (!isTesSuccess(res))
673 {
674 JLOG(ctx_.journal.debug())
675 << "AMM Deposit: failed to deposit " << *amount2DepositActual;
676 return {res, STAmount{}};
677 }
678 }
679
680 // Deposit LP tokens
681 res = accountSend(view, ammAccount, accountID_, lpTokensDepositActual, ctx_.journal);
682 if (!isTesSuccess(res))
683 {
684 JLOG(ctx_.journal.debug()) << "AMM Deposit: failed to deposit LPTokens";
685 return {res, STAmount{}};
686 }
687
688 return {tesSUCCESS, lptAMMBalance + lpTokensDepositActual};
689}
690
691static STAmount
693 Rules const& rules,
694 STAmount const& lptAMMBalance,
695 STAmount const& lpTokensDeposit)
696{
697 if (!rules.enabled(fixAMMv1_3))
698 return lpTokensDeposit;
699 return adjustLPTokens(lptAMMBalance, lpTokensDeposit, IsDeposit::Yes);
700}
701
708 Sandbox& view,
709 AccountID const& ammAccount,
710 STAmount const& amountBalance,
711 STAmount const& amount2Balance,
712 STAmount const& lptAMMBalance,
713 STAmount const& lpTokensDeposit,
714 std::optional<STAmount> const& depositMin,
715 std::optional<STAmount> const& deposit2Min,
716 std::uint16_t tfee)
717{
718 try
719 {
720 auto const tokensAdj = adjustLPTokensOut(view.rules(), lptAMMBalance, lpTokensDeposit);
721 if (view.rules().enabled(fixAMMv1_3) && tokensAdj == beast::kZero)
723 auto const frac = divide(tokensAdj, lptAMMBalance, lptAMMBalance.asset());
724 // amounts factor in the adjusted tokens
725 auto const amountDeposit =
726 getRoundedAsset(view.rules(), amountBalance, frac, IsDeposit::Yes);
727 auto const amount2Deposit =
728 getRoundedAsset(view.rules(), amount2Balance, frac, IsDeposit::Yes);
729 return deposit(
730 view,
731 ammAccount,
732 amountBalance,
733 amountDeposit,
734 amount2Deposit,
735 lptAMMBalance,
736 tokensAdj,
737 depositMin,
738 deposit2Min,
739 std::nullopt,
740 tfee);
741 }
742 catch (std::exception const& e)
743 {
744 // LCOV_EXCL_START
745 JLOG(j_.error()) << "AMMDeposit::equalDepositTokens exception " << e.what();
746 return {tecINTERNAL, STAmount{}};
747 // LCOV_EXCL_STOP
748 }
749}
750
782 Sandbox& view,
783 AccountID const& ammAccount,
784 STAmount const& amountBalance,
785 STAmount const& amount2Balance,
786 STAmount const& lptAMMBalance,
787 STAmount const& amount,
788 STAmount const& amount2,
789 std::optional<STAmount> const& lpTokensDepositMin,
790 std::uint16_t tfee)
791{
792 auto frac = Number{amount} / amountBalance;
793 auto tokensAdj = getRoundedLPTokens(view.rules(), lptAMMBalance, frac, IsDeposit::Yes);
794 if (tokensAdj == beast::kZero)
795 {
796 if (!view.rules().enabled(fixAMMv1_3))
797 {
798 return {tecAMM_FAILED, STAmount{}}; // LCOV_EXCL_LINE
799 }
800
802 }
803 // factor in the adjusted tokens
804 frac = adjustFracByTokens(view.rules(), lptAMMBalance, tokensAdj, frac);
805 auto const amount2Deposit = getRoundedAsset(view.rules(), amount2Balance, frac, IsDeposit::Yes);
806 if (amount2Deposit <= amount2)
807 {
808 return deposit(
809 view,
810 ammAccount,
811 amountBalance,
812 amount,
813 amount2Deposit,
814 lptAMMBalance,
815 tokensAdj,
816 std::nullopt,
817 std::nullopt,
818 lpTokensDepositMin,
819 tfee);
820 }
821 frac = Number{amount2} / amount2Balance;
822 tokensAdj = getRoundedLPTokens(view.rules(), lptAMMBalance, frac, IsDeposit::Yes);
823 if (tokensAdj == beast::kZero)
824 {
825 if (!view.rules().enabled(fixAMMv1_3))
826 {
827 return {tecAMM_FAILED, STAmount{}}; // LCOV_EXCL_LINE
828 }
829
830 return {tecAMM_INVALID_TOKENS, STAmount{}}; // LCOV_EXCL_LINE
831 }
832 // factor in the adjusted tokens
833 frac = adjustFracByTokens(view.rules(), lptAMMBalance, tokensAdj, frac);
834 auto const amountDeposit = getRoundedAsset(view.rules(), amountBalance, frac, IsDeposit::Yes);
835 if (amountDeposit <= amount)
836 {
837 return deposit(
838 view,
839 ammAccount,
840 amountBalance,
841 amountDeposit,
842 amount2,
843 lptAMMBalance,
844 tokensAdj,
845 std::nullopt,
846 std::nullopt,
847 lpTokensDepositMin,
848 tfee);
849 }
850 return {tecAMM_FAILED, STAmount{}};
851}
852
864 Sandbox& view,
865 AccountID const& ammAccount,
866 STAmount const& amountBalance,
867 STAmount const& lptAMMBalance,
868 STAmount const& amount,
869 std::optional<STAmount> const& lpTokensDepositMin,
870 std::uint16_t tfee)
871{
872 auto const tokens = adjustLPTokensOut(
873 view.rules(), lptAMMBalance, lpTokensOut(amountBalance, amount, lptAMMBalance, tfee));
874 if (tokens == beast::kZero)
875 {
876 if (!view.rules().enabled(fixAMMv1_3))
877 {
878 return {tecAMM_FAILED, STAmount{}}; // LCOV_EXCL_LINE
879 }
880
882 }
883 // factor in the adjusted tokens
884 auto const [tokensAdj, amountDepositAdj] =
885 adjustAssetInByTokens(view.rules(), amountBalance, amount, lptAMMBalance, tokens, tfee);
886 if (view.rules().enabled(fixAMMv1_3) && tokensAdj == beast::kZero)
887 return {tecAMM_INVALID_TOKENS, STAmount{}}; // LCOV_EXCL_LINE
888 return deposit(
889 view,
890 ammAccount,
891 amountBalance,
892 amountDepositAdj,
893 std::nullopt,
894 lptAMMBalance,
895 tokensAdj,
896 std::nullopt,
897 std::nullopt,
898 lpTokensDepositMin,
899 tfee);
900}
901
912 Sandbox& view,
913 AccountID const& ammAccount,
914 STAmount const& amountBalance,
915 STAmount const& amount,
916 STAmount const& lptAMMBalance,
917 STAmount const& lpTokensDeposit,
918 std::uint16_t tfee)
919{
920 auto const tokensAdj = adjustLPTokensOut(view.rules(), lptAMMBalance, lpTokensDeposit);
921 if (view.rules().enabled(fixAMMv1_3) && tokensAdj == beast::kZero)
923 // the adjusted tokens are factored in
924 auto const amountDeposit = ammAssetIn(amountBalance, lptAMMBalance, tokensAdj, tfee);
925 if (amountDeposit > amount)
926 return {tecAMM_FAILED, STAmount{}};
927 return deposit(
928 view,
929 ammAccount,
930 amountBalance,
931 amountDeposit,
932 std::nullopt,
933 lptAMMBalance,
934 tokensAdj,
935 std::nullopt,
936 std::nullopt,
937 std::nullopt,
938 tfee);
939}
940
969 Sandbox& view,
970 AccountID const& ammAccount,
971 STAmount const& amountBalance,
972 STAmount const& amount,
973 STAmount const& lptAMMBalance,
974 STAmount const& ePrice,
975 std::uint16_t tfee)
976{
977 if (amount != beast::kZero)
978 {
979 auto const tokens = adjustLPTokensOut(
980 view.rules(), lptAMMBalance, lpTokensOut(amountBalance, amount, lptAMMBalance, tfee));
981 if (tokens <= beast::kZero)
982 {
983 if (!view.rules().enabled(fixAMMv1_3))
984 {
985 return {tecAMM_FAILED, STAmount{}}; // LCOV_EXCL_LINE
986 }
987
989 }
990 // factor in the adjusted tokens
991 auto const [tokensAdj, amountDepositAdj] =
992 adjustAssetInByTokens(view.rules(), amountBalance, amount, lptAMMBalance, tokens, tfee);
993 if (view.rules().enabled(fixAMMv1_3) && tokensAdj == beast::kZero)
994 return {tecAMM_INVALID_TOKENS, STAmount{}}; // LCOV_EXCL_LINE
995 auto const ep = Number{amountDepositAdj} / tokensAdj;
996 if (ep <= ePrice)
997 {
998 return deposit(
999 view,
1000 ammAccount,
1001 amountBalance,
1002 amountDepositAdj,
1003 std::nullopt,
1004 lptAMMBalance,
1005 tokensAdj,
1006 std::nullopt,
1007 std::nullopt,
1008 std::nullopt,
1009 tfee);
1010 }
1011 }
1012
1013 // LPTokens is asset out => E = b / t
1014 // substituting t in formula (3) as b/E:
1015 // b/E = T * [b/B - sqrt(t2**2 + b/(f1*B)) + t2]/
1016 // [1 + sqrt(t2**2 + b/(f1*B)) -t2] (A)
1017 // where f1 = 1 - fee, f2 = (1 - fee/2)/f1
1018 // Let R = b/(f1*B), then b/B = f1*R and b = R*f1*B
1019 // Then (A) is
1020 // R*f1*B = E*T*[R*f1 -sqrt(f2**2 + R) + f2]/[1 + sqrt(f2**2 + R) - f2] =>
1021 // Let c = f1*B/(E*T) =>
1022 // R*c*(1 + sqrt(f2**2 + R) + f2) = R*f1 - sqrt(f2**2 + R) - f2 =>
1023 // (R*c + 1)*sqrt(f2**2 + R) = R*(f1 + c*f2 - c) + f2 =>
1024 // Let d = f1 + c*f2 - c =>
1025 // (R*c + 1)*sqrt(f2**2 + R) = R*d + f2 =>
1026 // (R*c + 1)**2 * (f2**2 + R) = (R*d + f2)**2 =>
1027 // (R*c)**2 + R*((c*f2)**2 + 2*c - d**2) + 2*c*f2**2 + 1 -2*d*f2 = 0 =>
1028 // a1 = c**2, b1 = (c*f2)**2 + 2*c - d**2, c1 = 2*c*f2**2 + 1 - 2*d*f2
1029 // R = (-b1 + sqrt(b1**2 + 4*a1*c1))/(2*a1)
1030 auto const f1 = feeMult(tfee);
1031 auto const f2 = feeMultHalf(tfee) / f1;
1032 auto const c = f1 * amountBalance / (ePrice * lptAMMBalance);
1033 auto const d = f1 + c * f2 - c;
1034 auto const a1 = c * c;
1035 auto const b1 = c * c * f2 * f2 + 2 * c - d * d;
1036 auto const c1 = 2 * c * f2 * f2 + 1 - 2 * d * f2;
1037 auto const amtNoRoundCb = [&] { return f1 * amountBalance * solveQuadraticEq(a1, b1, c1); };
1038 auto const amtProdCb = [&] { return f1 * solveQuadraticEq(a1, b1, c1); };
1039 auto const amountDeposit =
1040 getRoundedAsset(view.rules(), amtNoRoundCb, amountBalance, amtProdCb, IsDeposit::Yes);
1041 if (amountDeposit <= beast::kZero)
1042 return {tecAMM_FAILED, STAmount{}};
1043 auto const tokNoRoundCb = [&] { return amountDeposit / ePrice; };
1044 auto const tokProdCb = [&] { return amountDeposit / ePrice; };
1045 auto const tokens =
1046 getRoundedLPTokens(view.rules(), tokNoRoundCb, lptAMMBalance, tokProdCb, IsDeposit::Yes);
1047 // factor in the adjusted tokens
1048 auto const [tokensAdj, amountDepositAdj] = adjustAssetInByTokens(
1049 view.rules(), amountBalance, amountDeposit, lptAMMBalance, tokens, tfee);
1050 if (view.rules().enabled(fixAMMv1_3) && tokensAdj == beast::kZero)
1051 return {tecAMM_INVALID_TOKENS, STAmount{}}; // LCOV_EXCL_LINE
1052
1053 return deposit(
1054 view,
1055 ammAccount,
1056 amountBalance,
1057 amountDepositAdj,
1058 std::nullopt,
1059 lptAMMBalance,
1060 tokensAdj,
1061 std::nullopt,
1062 std::nullopt,
1063 std::nullopt,
1064 tfee);
1065}
1066
1069 Sandbox& view,
1070 AccountID const& ammAccount,
1071 STAmount const& amount,
1072 STAmount const& amount2,
1073 Asset const& lptIssue,
1074 std::uint16_t tfee)
1075{
1076 return deposit(
1077 view,
1078 ammAccount,
1079 amount,
1080 amount,
1081 amount2,
1082 STAmount{lptIssue, 0},
1083 ammLPTokens(amount, amount2, lptIssue),
1084 std::nullopt,
1085 std::nullopt,
1086 std::nullopt,
1087 tfee);
1088}
1089
1090void
1092{
1093 // No transaction-specific invariants yet (future work).
1094}
1095
1096bool
1098{
1099 // No transaction-specific invariants yet (future work).
1100 return true;
1101}
1102
1103} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Stream debug() const
Definition Journal.h:344
static TER preclaim(PreclaimContext const &ctx)
static bool checkExtraFeatures(PreflightContext const &ctx)
static std::uint32_t getFlagsMask(PreflightContext const &ctx)
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 > equalDepositInEmptyState(Sandbox &view, AccountID const &ammAccount, STAmount const &amount, STAmount const &amount2, Asset const &lptIssue, std::uint16_t tfee)
Equal deposit in empty AMM state (LP tokens balance is 0).
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.
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.
std::pair< TER, bool > applyGuts(Sandbox &view)
void visitInvariantEntry(bool isDelete, SLE::ConstRef before, SLE::ConstRef after) override
Inspect a single ledger entry modified by this transaction.
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)
TER doApply() override
bool finalizeInvariants(STTx const &tx, TER result, XRPAmount fee, ReadView const &view, beast::Journal const &j) override
Check transaction-specific post-conditions after all entries have been visited.
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...
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.
A currency issued by an account.
Definition Issue.h:18
Number is a floating point type that can represent a wide range of values.
Definition Number.h:351
A view into a ledger.
Definition ReadView.h:41
virtual Rules const & rules() const =0
Returns the tx processing rules.
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
Rules controlling protocol behavior.
Definition Rules.h:40
bool enabled(UInt256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:182
constexpr TIss const & get() const
Asset const & asset() const
Definition STAmount.h:496
std::shared_ptr< STLedgerEntry const > const & ConstRef
bool isFlag(std::uint32_t) const
Definition STObject.cpp:511
AccountID getAccountID(SField const &field) const
Definition STObject.cpp:643
std::uint32_t getFlags() const
Definition STObject.cpp:517
Discardable, editable view to a ledger.
Definition Sandbox.h:18
void apply(RawView &to)
Definition Sandbox.h:38
beast::Journal const j_
Definition Transactor.h:164
ApplyView & view()
Definition Transactor.h:184
AccountID const accountID_
Definition Transactor.h:166
ApplyContext & ctx_
Definition Transactor.h:162
SLE::pointer peek(Keylet const &k) override
Prepare to modify the SLE associated with key.
Rules const & rules() const override
Returns the tx processing rules.
void update(SLE::Ref sle) override
Indicate changes to a peeked SLE.
T make_optional(T... args)
T make_pair(T... args)
constexpr Zero kZero
Definition Zero.h:30
Keylet amm(Asset const &issue1, Asset const &issue2) noexcept
AMM entry.
Definition Indexes.cpp:483
Keylet trustLine(AccountID const &id0, AccountID const &id1, Currency const &currency) noexcept
The index of a trust line for a given currency.
Definition Indexes.cpp:287
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
NotTEC invalidAMMAmount(STAmount const &amount, std::optional< std::pair< Asset, Asset > > const &pair=std::nullopt, bool validZero=false)
Validate the amount.
Definition AMMCore.cpp:96
STAmount divide(STAmount const &amount, Rate const &rate)
Definition Rate2.cpp:69
@ terNO_AMM
Definition TER.h:229
constexpr std::uint16_t kTradingFeeThreshold
Definition AMMCore.h:16
STAmount ammLPHolds(ReadView const &view, Asset const &asset1, Asset const &asset2, AccountID const &ammAccount, AccountID const &lpAccount, beast::Journal const j)
Get the balance of LP tokens.
XRPAmount xrpLiquid(ReadView const &view, AccountID const &id, std::int32_t ownerCountAdj, beast::Journal j)
Calculate liquid XRP balance for an account.
static STAmount adjustLPTokensOut(Rules const &rules, STAmount const &lptAMMBalance, STAmount const &lpTokensDeposit)
Number feeMultHalf(std::uint16_t tfee)
Get fee multiplier (1 - tfee / 2) @tfee trading fee in basis points.
Definition AMMCore.h:114
TER checkIndividualFrozen(ReadView const &view, AccountID const &account, Asset const &asset)
constexpr FlagValue tfDepositSubTx
Definition TxFlags.h:417
bool isXRP(AccountID const &c)
Definition AccountID.h:84
Number adjustFracByTokens(Rules const &rules, STAmount const &lptAMMBalance, STAmount const &tokens, Number const &frac)
Find a fraction of tokens after the tokens are adjusted.
STAmount ammLPTokens(STAmount const &asset1, STAmount const &asset2, Asset const &lptIssue)
Calculate LP Tokens given AMM pool reserves.
TER checkDepositFreeze(ReadView const &view, AccountID const &srcAcct, AccountID const &pseudoAcct, Asset const &asset)
Checks freeze compliance for depositing an asset into a pseudo-account (e.g.
NotTEC invalidAMMAssetPair(Asset const &asset1, Asset const &asset2, std::optional< std::pair< Asset, Asset > > const &pair=std::nullopt)
Definition AMMCore.cpp:81
TER checkFrozen(ReadView const &view, AccountID const &account, Issue const &issue)
STAmount adjustLPTokens(STAmount const &lptAMMBalance, STAmount const &lpTokens, IsDeposit isDeposit)
Adjust LP tokens to deposit/withdraw.
TER canMPTTradeAndTransfer(ReadView const &v, Asset const &asset, AccountID const &from, AccountID const &to)
Convenience to combine canTrade/Transfer.
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).
STAmount getRoundedLPTokens(Rules const &rules, STAmount const &balance, Number const &frac, IsDeposit isDeposit)
Round AMM deposit/withdrawal LPToken amount.
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
std::pair< STAmount, STAmount > adjustAssetInByTokens(Rules const &rules, STAmount const &balance, STAmount const &amount, STAmount const &lptAMMBalance, STAmount const &tokens, std::uint16_t tfee)
STAmount accountFunds(ReadView const &view, AccountID const &id, STAmount const &saDefault, FreezeHandling freezeHandling, beast::Journal j)
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:617
TER accountSend(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &saAmount, beast::Journal j, SLE::Ref sponsorSle={}, WaiveTransferFee waiveFee=WaiveTransferFee::No, AllowMPTOverflow allowOverflow=AllowMPTOverflow::No)
Calls static accountSendIOU if saAmount represents Issue.
STAmount getRoundedAsset(Rules const &rules, STAmount const &balance, A const &frac, IsDeposit isDeposit)
Round AMM equal deposit/withdrawal amount.
Definition AMMHelpers.h:586
Number feeMult(std::uint16_t tfee)
Get fee multiplier (1 - tfee) @tfee trading fee in basis points.
Definition AMMCore.h:104
TER checkAMMPrecisionLoss(Number const &poolProductMean, STAmount const &newLPTokenBalance)
Check AMM pool product invariant after an AMM operation that changes LP tokens (deposit/withdraw/claw...
STAmount ammAssetIn(STAmount const &asset1Balance, STAmount const &lptAMMBalance, STAmount const &lpTokens, std::uint16_t tfee)
Calculate asset deposit given LP Tokens.
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
void initializeFeeAuctionVote(ApplyView const &view, SLE::pointer const &ammSle, AccountID const &account, Asset const &lptAsset, std::uint16_t tfee)
Initialize Auction and Voting slots and set the trading/discounted fee.
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...
@ temBAD_FEE
Definition TER.h:80
@ temBAD_AMM_TOKENS
Definition TER.h:117
@ temMALFORMED
Definition TER.h:75
@ temBAD_AMOUNT
Definition TER.h:77
bool isTesSuccess(TER x) noexcept
Definition TER.h:686
TERSubset< CanCvtToTER > TER
Definition TER.h:657
TER requireAuth(ReadView const &view, MPTIssue const &mptIssue, AccountID const &account, AuthType authType=AuthType::Legacy, std::uint8_t depth=0)
Check if the account lacks required authorization for MPT.
@ tecAMM_EMPTY
Definition TER.h:343
@ tecAMM_INVALID_TOKENS
Definition TER.h:342
@ tecINSUF_RESERVE_LINE
Definition TER.h:299
@ tecAMM_FAILED
Definition TER.h:341
@ tecAMM_NOT_EMPTY
Definition TER.h:344
@ tecUNFUNDED_AMM
Definition TER.h:339
@ tecINTERNAL
Definition TER.h:321
std::expected< std::tuple< STAmount, STAmount, STAmount >, TER > ammHolds(ReadView const &view, SLE const &ammSle, std::optional< Asset > const &optAsset1, std::optional< Asset > const &optAsset2, FreezeHandling freezeHandling, AuthHandling authHandling, beast::Journal const j)
Get AMM pool and LP token balances.
std::uint16_t getTradingFee(ReadView const &view, SLE const &ammSle, AccountID const &account)
Get AMM trading fee for the given account.
@ tesSUCCESS
Definition TER.h:253
STAmount lpTokensOut(STAmount const &asset1Balance, STAmount const &asset1Deposit, STAmount const &lptAMMBalance, std::uint16_t tfee)
Calculate LP Tokens given asset's deposit amount.
T popcount(T... args)
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:92
ReadView const & view
Definition Transactor.h:95
beast::Journal const j
Definition Transactor.h:100
State information when preflighting a tx.
Definition Transactor.h:39
beast::Journal const j
Definition Transactor.h:46
T value_or(T... args)
T what(T... args)