1#include <xrpld/app/ledger/OrderBookDB.h>
2#include <xrpld/app/misc/AMMHelpers.h>
3#include <xrpld/app/misc/AMMUtils.h>
4#include <xrpld/app/tx/detail/AMMCreate.h>
6#include <xrpl/ledger/Sandbox.h>
7#include <xrpl/ledger/View.h>
8#include <xrpl/protocol/AMMCore.h>
9#include <xrpl/protocol/Feature.h>
10#include <xrpl/protocol/STIssue.h>
11#include <xrpl/protocol/TxFlags.h>
24 auto const amount = ctx.
tx[sfAmount];
25 auto const amount2 = ctx.
tx[sfAmount2];
27 if (amount.issue() == amount2.issue())
30 <<
"AMM Instance: tokens can not have the same currency/issuer.";
36 JLOG(ctx.
j.
debug()) <<
"AMM Instance: invalid asset1 amount.";
42 JLOG(ctx.
j.
debug()) <<
"AMM Instance: invalid asset2 amount.";
48 JLOG(ctx.
j.
debug()) <<
"AMM Instance: invalid trading fee.";
65 auto const accountID = ctx.
tx[sfAccount];
66 auto const amount = ctx.
tx[sfAmount];
67 auto const amount2 = ctx.
tx[sfAmount2];
70 if (
auto const ammKeylet =
keylet::amm(amount.issue(), amount2.issue());
73 JLOG(ctx.
j.
debug()) <<
"AMM Instance: ltAMM already exists.";
81 <<
"AMM Instance: account is not authorized, " << amount.issue();
89 <<
"AMM Instance: account is not authorized, " << amount2.issue();
97 JLOG(ctx.
j.
debug()) <<
"AMM Instance: involves frozen asset.";
105 if (
auto const issuerAccount =
112 if (noDefaultRipple(ctx.
view, amount.issue()) ||
113 noDefaultRipple(ctx.
view, amount2.issue()))
115 JLOG(ctx.
j.
debug()) <<
"AMM Instance: DefaultRipple not set";
122 if (xrpBalance <= beast::zero)
124 JLOG(ctx.
j.
debug()) <<
"AMM Instance: insufficient reserves";
128 auto insufficientBalance = [&](
STAmount const& asset) {
130 return xrpBalance < asset;
140 if (insufficientBalance(amount) || insufficientBalance(amount2))
143 <<
"AMM Instance: insufficient funds, " << amount <<
" " << amount2;
147 auto isLPToken = [&](
STAmount const& amount) ->
bool {
150 return sle->isFieldPresent(sfAMMID);
154 if (isLPToken(amount) || isLPToken(amount2))
156 JLOG(ctx.
j.
debug()) <<
"AMM Instance: can't create with LPTokens "
157 << amount <<
" " << amount2;
165 accountId == beast::zero)
176 auto clawbackDisabled = [&](
Issue const& issue) ->
TER {
187 if (
auto const ter = clawbackDisabled(amount.issue()); ter !=
tesSUCCESS)
189 return clawbackDisabled(amount2.issue());
199 auto const amount = ctx_.
tx[sfAmount];
200 auto const amount2 = ctx_.
tx[sfAmount2];
202 auto const ammKeylet =
keylet::amm(amount.issue(), amount2.issue());
209 JLOG(j_.
error()) <<
"AMM Instance: failed to create pseudo account.";
210 return {maybeAccount.error(),
false};
212 auto& account = *maybeAccount;
213 auto const accountId = (*account)[sfAccount];
217 amount.issue().currency, amount2.issue().currency, accountId);
220 JLOG(j_.
error()) <<
"AMM Instance: LP Token already exists.";
231 auto const lpTokens =
ammLPTokens(amount, amount2, lptIss);
235 ammSle->setAccountID(sfAccount, accountId);
236 ammSle->setFieldAmount(sfLPTokenBalance, lpTokens);
237 auto const& [issue1, issue2] =
std::minmax(amount.issue(), amount2.issue());
238 ammSle->setFieldIssue(sfAsset,
STIssue{sfAsset, issue1});
239 ammSle->setFieldIssue(sfAsset2,
STIssue{sfAsset2, issue2});
242 ctx_.
view(), ammSle, account_, lptIss, ctx_.
tx[sfTradingFee]);
245 if (
auto ter =
dirLink(sb, accountId, ammSle); ter)
247 JLOG(j_.
debug()) <<
"AMM Instance: failed to insert owner dir";
256 JLOG(j_.
debug()) <<
"AMM Instance: failed to send LPT " << lpTokens;
260 auto sendAndTrustSet = [&](
STAmount const& amount) ->
TER {
278 auto const flags = sleRippleState->getFlags();
279 sleRippleState->setFieldU32(sfFlags, flags |
lsfAMMNode);
280 sb.
update(sleRippleState);
287 res = sendAndTrustSet(amount);
290 JLOG(j_.
debug()) <<
"AMM Instance: failed to send " << amount;
295 res = sendAndTrustSet(amount2);
298 JLOG(j_.
debug()) <<
"AMM Instance: failed to send " << amount2;
302 JLOG(j_.
debug()) <<
"AMM Instance: success " << accountId <<
" "
303 << ammKeylet.key <<
" " << lpTokens <<
" " << amount <<
" "
309 if (
auto const bookExisted =
static_cast<bool>(sb.
read(dir));
313 addOrderBook(amount.issue(), amount2.issue(),
getRate(amount2, amount));
314 addOrderBook(amount2.issue(), amount.issue(),
getRate(amount, amount2));
A generic endpoint for log messages.
static TER preclaim(PreclaimContext const &ctx)
static bool checkExtraFeatures(PreflightContext 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 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
Discardable, editable view to a ledger.
static XRPAmount calculateOwnerReserveFee(ReadView const &view, STTx const &tx)
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.
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.
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)
STAmount ammLPTokens(STAmount const &asset1, STAmount const &asset2, Issue const &lptIssue)
Calculate LP Tokens given AMM pool reserves.
TERSubset< CanCvtToTER > TER
std::uint16_t constexpr TRADING_FEE_THRESHOLD
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.