20#include <xrpld/app/ledger/OrderBookDB.h>
21#include <xrpld/app/misc/AMMHelpers.h>
22#include <xrpld/app/misc/AMMUtils.h>
23#include <xrpld/app/tx/detail/AMMCreate.h>
25#include <xrpl/ledger/Sandbox.h>
26#include <xrpl/ledger/View.h>
27#include <xrpl/protocol/AMMCore.h>
28#include <xrpl/protocol/Feature.h>
29#include <xrpl/protocol/STIssue.h>
30#include <xrpl/protocol/TxFlags.h>
45 JLOG(ctx.
j.
debug()) <<
"AMM Instance: invalid flags.";
49 auto const amount = ctx.
tx[sfAmount];
50 auto const amount2 = ctx.
tx[sfAmount2];
52 if (amount.issue() == amount2.issue())
55 <<
"AMM Instance: tokens can not have the same currency/issuer.";
61 JLOG(ctx.
j.
debug()) <<
"AMM Instance: invalid asset1 amount.";
67 JLOG(ctx.
j.
debug()) <<
"AMM Instance: invalid asset2 amount.";
73 JLOG(ctx.
j.
debug()) <<
"AMM Instance: invalid trading fee.";
90 auto const accountID = ctx.
tx[sfAccount];
91 auto const amount = ctx.
tx[sfAmount];
92 auto const amount2 = ctx.
tx[sfAmount2];
95 if (
auto const ammKeylet =
keylet::amm(amount.issue(), amount2.issue());
98 JLOG(ctx.
j.
debug()) <<
"AMM Instance: ltAMM already exists.";
106 <<
"AMM Instance: account is not authorized, " << amount.issue();
110 if (
auto const ter =
requireAuth(ctx.
view, amount2.issue(), accountID);
114 <<
"AMM Instance: account is not authorized, " << amount2.issue();
122 JLOG(ctx.
j.
debug()) <<
"AMM Instance: involves frozen asset.";
130 if (
auto const issuerAccount =
137 if (noDefaultRipple(ctx.
view, amount.issue()) ||
138 noDefaultRipple(ctx.
view, amount2.issue()))
140 JLOG(ctx.
j.
debug()) <<
"AMM Instance: DefaultRipple not set";
147 if (xrpBalance <= beast::zero)
149 JLOG(ctx.
j.
debug()) <<
"AMM Instance: insufficient reserves";
153 auto insufficientBalance = [&](
STAmount const& asset) {
155 return xrpBalance < asset;
165 if (insufficientBalance(amount) || insufficientBalance(amount2))
168 <<
"AMM Instance: insufficient funds, " << amount <<
" " << amount2;
172 auto isLPToken = [&](
STAmount const& amount) ->
bool {
175 return sle->isFieldPresent(sfAMMID);
179 if (isLPToken(amount) || isLPToken(amount2))
181 JLOG(ctx.
j.
debug()) <<
"AMM Instance: can't create with LPTokens "
182 << amount <<
" " << amount2;
190 accountId == beast::zero)
201 auto clawbackDisabled = [&](
Issue const& issue) ->
TER {
212 if (
auto const ter = clawbackDisabled(amount.issue()); ter !=
tesSUCCESS)
214 return clawbackDisabled(amount2.issue());
224 auto const amount = ctx_.
tx[sfAmount];
225 auto const amount2 = ctx_.
tx[sfAmount2];
227 auto const ammKeylet =
keylet::amm(amount.issue(), amount2.issue());
234 JLOG(j_.
error()) <<
"AMM Instance: failed to create pseudo account.";
235 return {maybeAccount.error(),
false};
237 auto& account = *maybeAccount;
238 auto const accountId = (*account)[sfAccount];
242 amount.issue().currency, amount2.issue().currency, accountId);
245 JLOG(j_.
error()) <<
"AMM Instance: LP Token already exists.";
256 auto const lpTokens =
ammLPTokens(amount, amount2, lptIss);
260 ammSle->setAccountID(sfAccount, accountId);
261 ammSle->setFieldAmount(sfLPTokenBalance, lpTokens);
262 auto const& [issue1, issue2] =
std::minmax(amount.issue(), amount2.issue());
263 ammSle->setFieldIssue(sfAsset,
STIssue{sfAsset, issue1});
264 ammSle->setFieldIssue(sfAsset2,
STIssue{sfAsset2, issue2});
267 ctx_.
view(), ammSle, account_, lptIss, ctx_.
tx[sfTradingFee]);
270 if (
auto ter =
dirLink(sb, accountId, ammSle); ter)
272 JLOG(j_.
debug()) <<
"AMM Instance: failed to insert owner dir";
281 JLOG(j_.
debug()) <<
"AMM Instance: failed to send LPT " << lpTokens;
285 auto sendAndTrustSet = [&](
STAmount const& amount) ->
TER {
303 auto const flags = sleRippleState->getFlags();
304 sleRippleState->setFieldU32(sfFlags, flags |
lsfAMMNode);
305 sb.
update(sleRippleState);
312 res = sendAndTrustSet(amount);
315 JLOG(j_.
debug()) <<
"AMM Instance: failed to send " << amount;
320 res = sendAndTrustSet(amount2);
323 JLOG(j_.
debug()) <<
"AMM Instance: failed to send " << amount2;
327 JLOG(j_.
debug()) <<
"AMM Instance: success " << accountId <<
" "
328 << ammKeylet.key <<
" " << lpTokens <<
" " << amount <<
" "
334 if (
auto const bookExisted =
static_cast<bool>(sb.
read(dir));
338 addOrderBook(amount.issue(), amount2.issue(),
getRate(amount2, amount));
339 addOrderBook(amount2.issue(), amount.issue(),
getRate(amount, amount2));
A generic endpoint for log messages.
static TER preclaim(PreclaimContext const &ctx)
TER doApply() override
Attempt to create the AMM instance.
static NotTEC preflight(PreflightContext const &ctx)
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
virtual OrderBookDB & getOrderBookDB()=0
State information when applying a tx.
beast::Journal const journal
A currency issued by an account.
void addOrderBook(Book const &)
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
virtual Rules const & rules() const =0
Returns the tx processing rules.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Issue const & issue() 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.
void insert(std::shared_ptr< SLE > const &sle) override
Insert a new state SLE.
std::shared_ptr< SLE const > read(Keylet const &k) const override
Return the state item associated with a key.
std::shared_ptr< SLE > peek(Keylet const &k) override
Prepare to modify the SLE associated with key.
Keylet quality(Keylet const &k, std::uint64_t q) noexcept
The initial directory page for a specific quality.
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.
Keylet account(AccountID const &id) noexcept
AccountID root.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
NotTEC invalidAMMAmount(STAmount const &amount, std::optional< std::pair< Issue, Issue > > const &pair=std::nullopt, bool validZero=false)
Validate the amount.
bool isXRP(AccountID const &c)
@ lsfAllowTrustLineClawback
bool ammEnabled(Rules const &)
Return true if required AMM amendments are enabled.
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
Issue ammLPTIssue(Currency const &cur1, Currency const &cur2, AccountID const &ammAccountID)
Calculate LPT Issue from AMM asset pair.
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.
bool isFrozen(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer)
std::uint64_t getRate(STAmount const &offerOut, STAmount const &offerIn)
TER requireAuth(ReadView const &view, Issue const &issue, AccountID const &account, AuthType authType=AuthType::Legacy)
Check if the account lacks required authorization.
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.
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
static std::pair< TER, bool > applyCreate(ApplyContext &ctx_, Sandbox &sb, AccountID const &account_, beast::Journal j_)
Expected< std::shared_ptr< SLE >, TER > createPseudoAccount(ApplyView &view, uint256 const &pseudoOwnerKey, SField const &ownerField)
Create pseudo-account, storing pseudoOwnerKey into ownerField.
AccountID pseudoAccountAddress(ReadView const &view, uint256 const &pseudoOwnerKey)
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j)
bool isTesSuccess(TER x) noexcept
STAmount ammLPTokens(STAmount const &asset1, STAmount const &asset2, Issue const &lptIssue)
Calculate LP Tokens given AMM pool reserves.
constexpr std::uint32_t tfUniversalMask
TERSubset< CanCvtToTER > TER
std::uint16_t constexpr TRADING_FEE_THRESHOLD
TERSubset< CanCvtToNotTEC > NotTEC
TER dirLink(ApplyView &view, AccountID const &owner, std::shared_ptr< SLE > &object)
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.