rippled
Loading...
Searching...
No Matches
AMMCreate.cpp
1#include <xrpld/app/misc/AMMHelpers.h>
2#include <xrpld/app/misc/AMMUtils.h>
3#include <xrpld/app/tx/detail/AMMCreate.h>
4
5#include <xrpl/ledger/OrderBookDB.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>
12
13namespace xrpl {
14
15bool
20
23{
24 auto const amount = ctx.tx[sfAmount];
25 auto const amount2 = ctx.tx[sfAmount2];
26
27 if (amount.issue() == amount2.issue())
28 {
29 JLOG(ctx.j.debug()) << "AMM Instance: tokens can not have the same currency/issuer.";
30 return temBAD_AMM_TOKENS;
31 }
32
33 if (auto const err = invalidAMMAmount(amount))
34 {
35 JLOG(ctx.j.debug()) << "AMM Instance: invalid asset1 amount.";
36 return err;
37 }
38
39 if (auto const err = invalidAMMAmount(amount2))
40 {
41 JLOG(ctx.j.debug()) << "AMM Instance: invalid asset2 amount.";
42 return err;
43 }
44
45 if (ctx.tx[sfTradingFee] > TRADING_FEE_THRESHOLD)
46 {
47 JLOG(ctx.j.debug()) << "AMM Instance: invalid trading fee.";
48 return temBAD_FEE;
49 }
50
51 return tesSUCCESS;
52}
53
56{
57 // The fee required for AMMCreate is one owner reserve.
59}
60
61TER
63{
64 auto const accountID = ctx.tx[sfAccount];
65 auto const amount = ctx.tx[sfAmount];
66 auto const amount2 = ctx.tx[sfAmount2];
67
68 // Check if AMM already exists for the token pair
69 if (auto const ammKeylet = keylet::amm(amount.issue(), amount2.issue()); ctx.view.read(ammKeylet))
70 {
71 JLOG(ctx.j.debug()) << "AMM Instance: ltAMM already exists.";
72 return tecDUPLICATE;
73 }
74
75 if (auto const ter = requireAuth(ctx.view, amount.issue(), accountID); ter != tesSUCCESS)
76 {
77 JLOG(ctx.j.debug()) << "AMM Instance: account is not authorized, " << amount.issue();
78 return ter;
79 }
80
81 if (auto const ter = requireAuth(ctx.view, amount2.issue(), accountID); ter != tesSUCCESS)
82 {
83 JLOG(ctx.j.debug()) << "AMM Instance: account is not authorized, " << amount2.issue();
84 return ter;
85 }
86
87 // Globally or individually frozen
88 if (isFrozen(ctx.view, accountID, amount.issue()) || isFrozen(ctx.view, accountID, amount2.issue()))
89 {
90 JLOG(ctx.j.debug()) << "AMM Instance: involves frozen asset.";
91 return tecFROZEN;
92 }
93
94 auto noDefaultRipple = [](ReadView const& view, Issue const& issue) {
95 if (isXRP(issue))
96 return false;
97
98 if (auto const issuerAccount = view.read(keylet::account(issue.account)))
99 return (issuerAccount->getFlags() & lsfDefaultRipple) == 0;
100
101 return false;
102 };
103
104 if (noDefaultRipple(ctx.view, amount.issue()) || noDefaultRipple(ctx.view, amount2.issue()))
105 {
106 JLOG(ctx.j.debug()) << "AMM Instance: DefaultRipple not set";
107 return terNO_RIPPLE;
108 }
109
110 // Check the reserve for LPToken trustline
111 STAmount const xrpBalance = xrpLiquid(ctx.view, accountID, 1, ctx.j);
112 // Insufficient reserve
113 if (xrpBalance <= beast::zero)
114 {
115 JLOG(ctx.j.debug()) << "AMM Instance: insufficient reserves";
117 }
118
119 auto insufficientBalance = [&](STAmount const& asset) {
120 if (isXRP(asset))
121 return xrpBalance < asset;
122 return accountID != asset.issue().account &&
123 accountHolds(ctx.view, accountID, asset.issue(), FreezeHandling::fhZERO_IF_FROZEN, ctx.j) < asset;
124 };
125
126 if (insufficientBalance(amount) || insufficientBalance(amount2))
127 {
128 JLOG(ctx.j.debug()) << "AMM Instance: insufficient funds, " << amount << " " << amount2;
129 return tecUNFUNDED_AMM;
130 }
131
132 auto isLPToken = [&](STAmount const& amount) -> bool {
133 if (auto const sle = ctx.view.read(keylet::account(amount.issue().account)))
134 return sle->isFieldPresent(sfAMMID);
135 return false;
136 };
137
138 if (isLPToken(amount) || isLPToken(amount2))
139 {
140 JLOG(ctx.j.debug()) << "AMM Instance: can't create with LPTokens " << amount << " " << amount2;
142 }
143
144 if (ctx.view.rules().enabled(featureSingleAssetVault))
145 {
146 if (auto const accountId = pseudoAccountAddress(ctx.view, keylet::amm(amount.issue(), amount2.issue()).key);
147 accountId == beast::zero)
149 }
150
151 // If featureAMMClawback is enabled, allow AMMCreate without checking
152 // if the issuer has clawback enabled
153 if (ctx.view.rules().enabled(featureAMMClawback))
154 return tesSUCCESS;
155
156 // Disallow AMM if the issuer has clawback enabled when featureAMMClawback
157 // is not enabled
158 auto clawbackDisabled = [&](Issue const& issue) -> TER {
159 if (isXRP(issue))
160 return tesSUCCESS;
161 if (auto const sle = ctx.view.read(keylet::account(issue.account)); !sle)
162 return tecINTERNAL; // LCOV_EXCL_LINE
163 else if (sle->getFlags() & lsfAllowTrustLineClawback)
164 return tecNO_PERMISSION;
165 return tesSUCCESS;
166 };
167
168 if (auto const ter = clawbackDisabled(amount.issue()); ter != tesSUCCESS)
169 return ter;
170 return clawbackDisabled(amount2.issue());
171}
172
175{
176 auto const amount = ctx_.tx[sfAmount];
177 auto const amount2 = ctx_.tx[sfAmount2];
178
179 auto const ammKeylet = keylet::amm(amount.issue(), amount2.issue());
180
181 // Mitigate same account exists possibility
182 auto const maybeAccount = createPseudoAccount(sb, ammKeylet.key, sfAMMID);
183 // AMM account already exists (should not happen)
184 if (!maybeAccount)
185 {
186 JLOG(j_.error()) << "AMM Instance: failed to create pseudo account.";
187 return {maybeAccount.error(), false};
188 }
189 auto& account = *maybeAccount;
190 auto const accountId = (*account)[sfAccount];
191
192 // LP Token already exists. (should not happen)
193 auto const lptIss = ammLPTIssue(amount.issue().currency, amount2.issue().currency, accountId);
194 if (sb.read(keylet::line(accountId, lptIss)))
195 {
196 JLOG(j_.error()) << "AMM Instance: LP Token already exists.";
197 return {tecDUPLICATE, false};
198 }
199
200 // Note, that the trustlines created by AMM have 0 credit limit.
201 // This prevents shifting the balance between accounts via AMM,
202 // or sending unsolicited LPTokens. This is a desired behavior.
203 // A user can only receive LPTokens through affirmative action -
204 // either an AMMDeposit, TrustSet, crossing an offer, etc.
205
206 // Calculate initial LPT balance.
207 auto const lpTokens = ammLPTokens(amount, amount2, lptIss);
208
209 // Create ltAMM
210 auto ammSle = std::make_shared<SLE>(ammKeylet);
211 ammSle->setAccountID(sfAccount, accountId);
212 ammSle->setFieldAmount(sfLPTokenBalance, lpTokens);
213 auto const& [issue1, issue2] = std::minmax(amount.issue(), amount2.issue());
214 ammSle->setFieldIssue(sfAsset, STIssue{sfAsset, issue1});
215 ammSle->setFieldIssue(sfAsset2, STIssue{sfAsset2, issue2});
216 // AMM creator gets the auction slot and the voting slot.
217 initializeFeeAuctionVote(ctx_.view(), ammSle, account_, lptIss, ctx_.tx[sfTradingFee]);
218
219 // Add owner directory to link the root account and AMM object.
220 if (auto ter = dirLink(sb, accountId, ammSle); ter)
221 {
222 JLOG(j_.debug()) << "AMM Instance: failed to insert owner dir";
223 return {ter, false};
224 }
225 sb.insert(ammSle);
226
227 // Send LPT to LP.
228 auto res = accountSend(sb, accountId, account_, lpTokens, ctx_.journal);
229 if (res != tesSUCCESS)
230 {
231 JLOG(j_.debug()) << "AMM Instance: failed to send LPT " << lpTokens;
232 return {res, false};
233 }
234
235 auto sendAndTrustSet = [&](STAmount const& amount) -> TER {
236 if (auto const res = accountSend(sb, account_, accountId, amount, ctx_.journal, WaiveTransferFee::Yes))
237 return res;
238 // Set AMM flag on AMM trustline
239 if (!isXRP(amount))
240 {
241 if (SLE::pointer sleRippleState = sb.peek(keylet::line(accountId, amount.issue())); !sleRippleState)
242 return tecINTERNAL; // LCOV_EXCL_LINE
243 else
244 {
245 auto const flags = sleRippleState->getFlags();
246 sleRippleState->setFieldU32(sfFlags, flags | lsfAMMNode);
247 sb.update(sleRippleState);
248 }
249 }
250 return tesSUCCESS;
251 };
252
253 // Send asset1.
254 res = sendAndTrustSet(amount);
255 if (res != tesSUCCESS)
256 {
257 JLOG(j_.debug()) << "AMM Instance: failed to send " << amount;
258 return {res, false};
259 }
260
261 // Send asset2.
262 res = sendAndTrustSet(amount2);
263 if (res != tesSUCCESS)
264 {
265 JLOG(j_.debug()) << "AMM Instance: failed to send " << amount2;
266 return {res, false};
267 }
268
269 JLOG(j_.debug()) << "AMM Instance: success " << accountId << " " << ammKeylet.key << " " << lpTokens << " "
270 << amount << " " << amount2;
271 auto addOrderBook = [&](Issue const& issueIn, Issue const& issueOut, std::uint64_t uRate) {
272 Book const book{issueIn, issueOut, std::nullopt};
273 auto const dir = keylet::quality(keylet::book(book), uRate);
274 if (auto const bookExisted = static_cast<bool>(sb.read(dir)); !bookExisted)
275 ctx_.app.getOrderBookDB().addOrderBook(book);
276 };
277 addOrderBook(amount.issue(), amount2.issue(), getRate(amount2, amount));
278 addOrderBook(amount2.issue(), amount.issue(), getRate(amount, amount2));
279
280 return {res, res == tesSUCCESS};
281}
282
283TER
285{
286 // This is the ledger view that we work against. Transactions are applied
287 // as we go on processing transactions.
288 Sandbox sb(&ctx_.view());
289
290 auto const result = applyCreate(ctx_, sb, account_, j_);
291 if (result.second)
292 sb.apply(ctx_.rawView());
293
294 return result.first;
295}
296
297} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:40
Stream error() const
Definition Journal.h:318
Stream debug() const
Definition Journal.h:300
TER doApply() override
Attempt to create the AMM instance.
static bool checkExtraFeatures(PreflightContext const &ctx)
Definition AMMCreate.cpp:16
static TER preclaim(PreclaimContext const &ctx)
Definition AMMCreate.cpp:62
static NotTEC preflight(PreflightContext const &ctx)
Definition AMMCreate.cpp:22
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
Definition AMMCreate.cpp:55
State information when applying a tx.
STTx const & tx
beast::Journal const journal
RawView & rawView()
ApplyView & view()
Application & app
Specifies an order book.
Definition Book.h:16
A currency issued by an account.
Definition Issue.h:13
AccountID account
Definition Issue.h:16
virtual void addOrderBook(Book const &book)=0
Add an order book to track.
A view into a ledger.
Definition ReadView.h:31
virtual Rules const & rules() const =0
Returns the tx processing rules.
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:118
Issue const & issue() const
Definition STAmount.h:454
Discardable, editable view to a ledger.
Definition Sandbox.h:15
void apply(RawView &to)
Definition Sandbox.h:35
virtual OrderBookDB & getOrderBookDB()=0
AccountID const account_
Definition Transactor.h:112
static XRPAmount calculateOwnerReserveFee(ReadView const &view, STTx const &tx)
beast::Journal const j_
Definition Transactor.h:110
ApplyView & view()
Definition Transactor.h:128
ApplyContext & ctx_
Definition Transactor.h:108
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.
std::shared_ptr< SLE const > read(Keylet const &k) const override
Return the state item associated with a key.
void insert(std::shared_ptr< SLE > const &sle) override
Insert a new state SLE.
T is_same_v
T minmax(T... args)
Keylet quality(Keylet const &k, std::uint64_t q) noexcept
The initial directory page for a specific quality.
Definition Indexes.cpp:241
static book_t const book
Definition Indexes.h:85
Keylet amm(Asset const &issue1, Asset const &issue2) noexcept
AMM entry.
Definition Indexes.cpp:393
Keylet line(AccountID const &id0, AccountID const &id1, Currency const &currency) noexcept
The index of a trust line for a given currency.
Definition Indexes.cpp:214
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:160
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ terNO_RIPPLE
Definition TER.h:204
@ terADDRESS_COLLISION
Definition TER.h:208
std::uint16_t constexpr TRADING_FEE_THRESHOLD
Definition AMMCore.h:11
XRPAmount xrpLiquid(ReadView const &view, AccountID const &id, std::int32_t ownerCountAdj, beast::Journal j)
Definition View.cpp:571
@ fhZERO_IF_FROZEN
Definition View.h:58
AccountID pseudoAccountAddress(ReadView const &view, uint256 const &pseudoOwnerKey)
Definition View.cpp:971
bool ammEnabled(Rules const &)
Return true if required AMM amendments are enabled.
Definition AMMCore.cpp:95
bool isXRP(AccountID const &c)
Definition AccountID.h:70
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j, SpendableHandling includeFullBalance=shSIMPLE_BALANCE)
Definition View.cpp:392
STAmount ammLPTokens(STAmount const &asset1, STAmount const &asset2, Issue const &lptIssue)
Calculate LP Tokens given AMM pool reserves.
Definition AMMHelpers.cpp:6
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.
Definition View.cpp:2446
TERSubset< CanCvtToTER > TER
Definition TER.h:620
std::uint64_t getRate(STAmount const &offerOut, STAmount const &offerIn)
Definition STAmount.cpp:434
TER requireAuth(ReadView const &view, Issue const &issue, AccountID const &account, AuthType authType=AuthType::Legacy)
Check if the account lacks required authorization.
Definition View.cpp:2710
bool isFrozen(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer)
Definition View.cpp:194
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.
Definition AMMUtils.cpp:269
TER dirLink(ApplyView &view, AccountID const &owner, std::shared_ptr< SLE > &object, SF_UINT64 const &node=sfOwnerNode)
Definition View.cpp:961
NotTEC invalidAMMAmount(STAmount const &amount, std::optional< std::pair< Issue, Issue > > const &pair=std::nullopt, bool validZero=false)
Validate the amount.
Definition AMMCore.cpp:67
@ temBAD_FEE
Definition TER.h:72
@ temBAD_AMM_TOKENS
Definition TER.h:109
@ tecAMM_INVALID_TOKENS
Definition TER.h:312
@ tecINSUF_RESERVE_LINE
Definition TER.h:269
@ tecUNFUNDED_AMM
Definition TER.h:309
@ tecINTERNAL
Definition TER.h:291
@ tecFROZEN
Definition TER.h:284
@ tecNO_PERMISSION
Definition TER.h:286
@ tecDUPLICATE
Definition TER.h:296
@ lsfAllowTrustLineClawback
@ lsfDefaultRipple
Expected< std::shared_ptr< SLE >, TER > createPseudoAccount(ApplyView &view, uint256 const &pseudoOwnerKey, SField const &ownerField)
Create pseudo-account, storing pseudoOwnerKey into ownerField.
Definition View.cpp:1033
Issue ammLPTIssue(Currency const &cur1, Currency const &cur2, AccountID const &ammAccountID)
Calculate LPT Issue from AMM asset pair.
Definition AMMCore.cpp:37
@ tesSUCCESS
Definition TER.h:225
static std::pair< TER, bool > applyCreate(ApplyContext &ctx_, Sandbox &sb, AccountID const &account_, beast::Journal j_)
uint256 key
Definition Keylet.h:20
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:53
ReadView const & view
Definition Transactor.h:56
beast::Journal const j
Definition Transactor.h:61
State information when preflighting a tx.
Definition Transactor.h:15
beast::Journal const j
Definition Transactor.h:22