xrpld
Loading...
Searching...
No Matches
AMMClawbackMPT_test.cpp
1#include <test/jtx/AMM.h>
2#include <test/jtx/Account.h>
3#include <test/jtx/CaptureLogs.h>
4#include <test/jtx/Env.h>
5#include <test/jtx/TestHelpers.h>
6#include <test/jtx/amount.h>
7#include <test/jtx/balance.h>
8#include <test/jtx/flags.h>
9#include <test/jtx/mpt.h>
10#include <test/jtx/pay.h>
11#include <test/jtx/ter.h>
12#include <test/jtx/trust.h>
13#include <test/jtx/txflags.h>
14
15#include <xrpl/beast/unit_test/suite.h>
16#include <xrpl/ledger/helpers/AMMHelpers.h>
17#include <xrpl/protocol/Feature.h>
18#include <xrpl/protocol/Indexes.h>
19#include <xrpl/protocol/LedgerFormats.h>
20#include <xrpl/protocol/SField.h>
21#include <xrpl/protocol/STAmount.h>
22#include <xrpl/protocol/TER.h>
23#include <xrpl/protocol/TxFlags.h>
24#include <xrpl/protocol/XRPAmount.h>
25#include <xrpl/protocol/jss.h>
26
27#include <cstdint>
28#include <memory>
29#include <optional>
30
31namespace xrpl::test {
33{
34 // SingleAssetVault, LendingProtocol and MPTokensV2 enable the large
35 // Number mantissa.
36 static bool
38 {
39 return features[featureSingleAssetVault] || features[featureLendingProtocol] ||
40 features[featureMPTokensV2];
41 }
42
43 void
45 {
46 testcase("test invalid request");
47 using namespace jtx;
48
49 for (auto const& feature : {features, features - featureSingleAssetVault})
50 {
51 Env env(*this, feature);
52 Account const gw{"gateway"};
53 Account const alice{"alice"};
54 Account const bob{"bob"};
55 env.fund(XRP(100000), gw, alice, bob);
56 env.close();
57
58 env(fset(gw, asfAllowTrustLineClawback));
59 env.close();
60
61 MPT const btc = MPTTester({
62 .env = env,
63 .issuer = gw,
64 .holders = {alice},
65 .pay = 40'000,
66 .flags = tfMPTCanClawback | kMptDexFlags,
67 });
68
69 auto const usd = gw["USD"];
70 env.trust(usd(10000), alice);
71 env(pay(gw, alice, usd(100)));
72 env.close();
73
74 AMM amm(env, gw, btc(100), usd(100));
75
76 // holder does not exist
77 env(amm::ammClawback(gw, Account("unknown"), usd, btc, std::nullopt),
79
80 // can not clawback from self.
81 env(amm::ammClawback(gw, gw, usd, btc, std::nullopt), Ter(temMALFORMED));
82
83 // provided Asset does not match issuer gw
84 {
86 gw, alice, Issue{gw["USD"].currency, alice.id()}, btc, std::nullopt),
88 env(amm::ammClawback(gw, alice, MPTIssue{makeMptID(1, alice)}, usd, std::nullopt),
90 }
91
92 // Amount does not match asset
93 {
95 gw, alice, usd, btc, STAmount{Issue{gw["USD"].currency, alice.id()}, 1}),
98 gw, alice, btc, usd, STAmount{MPTIssue{makeMptID(1, alice)}, 10}),
100 }
101
102 // Amount is not greater than 0
103 {
104 env(amm::ammClawback(gw, alice, btc, usd, btc(-1)), Ter(temBAD_AMOUNT));
105 env(amm::ammClawback(gw, alice, btc, usd, btc(0)), Ter(temBAD_AMOUNT));
106 }
107
108 // clawback from account not holding lptoken
109 env(amm::ammClawback(gw, bob, btc, usd, btc(1000)), Ter(tecAMM_BALANCE));
110
111 // can not perform regular claw from amm pool
112 {
113 Issue const ammUsd(usd.currency, amm.ammAccount());
114 auto const amount = amountFromString(ammUsd, "10");
115 auto const err =
116 feature[featureSingleAssetVault] ? tecPSEUDO_ACCOUNT : tecAMM_ACCOUNT;
117 env(claw(gw, amount), Ter(err));
118 }
119
120 // AMM does not exist
121 {
122 // withdraw all tokens will delete the AMM
123 amm.withdrawAll(gw);
124 BEAST_EXPECT(!amm.ammExists());
125 env.close();
126 env(amm::ammClawback(gw, alice, usd, btc, std::nullopt), Ter(terNO_AMM));
127 }
128 }
129
130 // tfMPTCanClawback is not enabled
131 {
132 Env env(*this, features);
133 Account const gw{"gateway"};
134 Account const alice{"alice"};
135 env.fund(XRP(100000), gw, alice);
136 env.close();
137
138 env(fset(gw, asfAllowTrustLineClawback));
139 env.close();
140
141 MPT const btc =
142 MPTTester({.env = env, .issuer = gw, .holders = {alice}, .pay = 40'000});
143
144 auto const usd = gw["USD"];
145 env.trust(usd(10000), alice);
146 env(pay(gw, alice, usd(10000)));
147 env.close();
148
149 AMM amm(env, gw, btc(100), usd(100));
150 env.close();
151 amm.deposit(alice, 1'000);
152
153 // can not clawback when tfMPTCanClawback is not enabled
154 env(amm::ammClawback(gw, alice, btc, usd, std::nullopt), Ter(tecNO_PERMISSION));
155 }
156
157 // can not claw with tfClawTwoAssets if the assets are not issued by the
158 // same issuer
159 {
160 Env env(*this, features);
161 Account const gw{"gateway"};
162 Account const gw2{"gateway2"};
163 Account const alice{"alice"};
164 env.fund(XRP(100000), gw, gw2, alice);
165 env.close();
166
167 env(fset(gw, asfAllowTrustLineClawback));
168 env.close();
169
170 auto const usd = gw["USD"];
171 env.trust(usd(10000), alice);
172 env(pay(gw, alice, usd(10000)));
173 env.close();
174
175 // todo: check tfMPTCanTransfer in xrpl.org
176 MPT const btc = MPTTester({
177 .env = env,
178 .issuer = gw2,
179 .holders = {alice},
180 .pay = 40'000,
181 .flags = tfMPTCanClawback | kMptDexFlags,
182 });
183
184 AMM const amm(env, alice, btc(100), usd(100));
185 env.close();
186
187 {
188 // Return temINVALID_FLAG because the issuer set
189 // tfClawTwoAssets, but the issuer only issues USD in the pool.
190 // The issuer is not allowed to set tfClawTwoAssets flag if he
191 // did not issue both assets in the pool.
192 env(amm::ammClawback(gw, alice, usd, btc, std::nullopt),
193 Txflags(tfClawTwoAssets),
195 }
196 }
197
198 // Test if the issuer did not set asfAllowTrustLineClawback, but the MPT
199 // is set tfMPTCanClawback, the issuer can claw MPT.
200 {
201 Env env(*this, features);
202 Account const gw{"gateway"};
203 Account const alice{"alice"};
204 env.fund(XRP(10000), gw, alice);
205 env.close();
206
207 MPT const btc = MPTTester({
208 .env = env,
209 .issuer = gw,
210 .holders = {alice},
211 .pay = 40'000,
212 .flags = tfMPTCanClawback | kMptDexFlags,
213 });
214
215 AMM const amm(env, alice, btc(100), XRP(100));
216 env.close();
217
218 // If asfAllowTrustLineClawback is not set, the issuer can
219 // still claw MPT because the MPT's tfMPTCanClawback is set.
220 env(amm::ammClawback(gw, alice, btc, XRP, std::nullopt));
221 }
222 }
223
224 void
226 {
227 testcase("test feature disabled.");
228 using namespace jtx;
229 Env env{*this, features};
230 Account const gw("gateway"), alice("alice");
231 env.fund(XRP(30'000), gw, alice);
232 env.close();
233 env(fset(gw, asfAllowTrustLineClawback));
234 env.close();
235
236 MPT const btc = MPTTester({
237 .env = env,
238 .issuer = gw,
239 .holders = {alice},
240 .pay = 10'000,
241 .flags = tfMPTCanClawback | kMptDexFlags,
242 });
243
244 AMM const amm(env, alice, XRP(1'000), btc(1'000));
245
246 // disable featureAMMClawback
247 env.disableFeature(featureAMMClawback);
248 env(amm::ammClawback(gw, alice, btc, XRP, std::nullopt), Ter(temDISABLED));
249
250 // enable featureAMMClawback and disable featureMPTokensV2
251 env.enableFeature(featureAMMClawback);
252 env.disableFeature(featureMPTokensV2);
253 env(amm::ammClawback(gw, alice, btc, XRP, btc(100)), Ter(temDISABLED));
254
255 // enable featureMPTokensV2
256 env.enableFeature(featureMPTokensV2);
257 env(amm::ammClawback(gw, alice, btc, XRP, btc(200)));
258 }
259
260 void
262 {
263 testcase("test AMMClawback specific amount");
264 using namespace jtx;
265
266 // AMMClawback from MPT/IOU issued by different issuers
267 {
268 Env env(*this, features);
269 Account const gw{"gateway"};
270 Account const gw2{"gateway2"};
271 Account const alice{"alice"};
272 env.fund(XRP(100000), gw, gw2, alice);
273 env.close();
274
275 env(fset(gw, asfAllowTrustLineClawback));
276 env(fset(gw2, asfAllowTrustLineClawback));
277 env.close();
278
279 auto const usd = gw["USD"];
280 env.trust(usd(100000), alice);
281 env(pay(gw, alice, usd(50000)));
282 env.close();
283
284 MPT const btc = MPTTester({
285 .env = env,
286 .issuer = gw2,
287 .holders = {alice},
288 .pay = 40'000'000000,
289 .flags = tfMPTCanClawback | kMptDexFlags,
290 });
291
292 AMM const amm(env, alice, btc(1000000000), usd(2000));
293 env.close();
294 BEAST_EXPECT(amm.expectBalances(
295 btc(1'000'000000), usd(2000), IOUAmount{1414'213'562373095, -9}));
296
297 // can not set tfClawTwoAssets because the assets are not issued by
298 // the same issuer.
299 env(amm::ammClawback(gw2, alice, btc, usd, btc(1000)),
300 Txflags(tfClawTwoAssets),
302
303 auto aliceUSD = env.balance(alice, usd);
304 auto aliceBTC = env.balance(alice, btc);
305 // gw clawback 1000 USD from alice
306 env(amm::ammClawback(gw, alice, usd, btc, usd(1000)));
307 env.close();
308
309 BEAST_EXPECT(
310 amm.expectBalances(btc(500'000000), usd(1000), IOUAmount{707'106'7811865475, -10}));
311 // USD is clawed back,
312 env.require(Balance(alice, aliceUSD));
313 // a proportional amount of BTC is returned to alice
314 env.require(Balance(alice, aliceBTC + btc(500'000000)));
315 aliceBTC = env.balance(alice, btc);
316
317 // gw2 clawback 250'000000 BTC from alice
318 env(amm::ammClawback(gw2, alice, btc, usd, btc(250'000000)));
319 env.close();
320 BEAST_EXPECT(
321 amm.expectBalances(btc(250'000000), usd(500), IOUAmount{353'553'3905932737, -10}));
322 env.require(Balance(alice, aliceUSD + usd(500)));
323 env.require(Balance(alice, aliceBTC));
324 aliceUSD = env.balance(alice, usd);
325
326 // gw2 clawback 500'000000 BTC which exceeds the balance,
327 // this will clawback all and the amm will be deleted.
328 env(amm::ammClawback(gw2, alice, btc, usd, btc(500'000000)));
329 env.close();
330 BEAST_EXPECT(!amm.ammExists());
331 env.require(Balance(alice, aliceUSD + usd(500)));
332 env.require(Balance(alice, aliceBTC));
333 }
334
335 // AMMClawback from MPT/XRP pool
336 {
337 Env env(*this, features);
338 Account const gw{"gateway"};
339 Account const alice{"alice"};
340 Account const bob{"bob"};
341 env.fund(XRP(100000), gw, alice, bob);
342 env.close();
343
344 env(fset(gw, asfAllowTrustLineClawback));
345 env.close();
346
347 MPT const btc = MPTTester({
348 .env = env,
349 .issuer = gw,
350 .holders = {alice, bob},
351 .pay = 40'000'000000,
352 .flags = tfMPTCanClawback | kMptDexFlags,
353 });
354
355 AMM amm(env, alice, btc(1000000000), XRP(2000));
356 env.close();
357 BEAST_EXPECT(amm.expectBalances(
358 btc(1'000'000000), XRP(2000), IOUAmount{1'414'213'562'373095, -6}));
359
360 amm.deposit(bob, btc(2'000'000000), XRP(4000));
361 BEAST_EXPECT(amm.expectBalances(
362 btc(3'000'000000), XRP(6000), IOUAmount{4'242'640'687'119285, -6}));
363
364 auto aliceXRP = env.balance(alice, XRP);
365 auto const aliceBTC = env.balance(alice, btc);
366 auto bobXRP = env.balance(bob, XRP);
367 auto const bobBTC = env.balance(bob, btc);
368
369 // can not claw XRP
370 env(amm::ammClawback(gw, alice, XRP, btc, XRP(1000)), Ter(temMALFORMED));
371 // can not set tfClawTwoAssets
372 env(amm::ammClawback(gw, alice, btc, XRP, btc(1000)),
373 Txflags(tfClawTwoAssets),
375
376 // gw clawback 500 BTC from alice
377 env(amm::ammClawback(gw, alice, btc, XRP, btc(500)));
378 env.close();
379 BEAST_EXPECT(amm.expectBalances(
380 btc(2'999'999501),
381 STAmount{XRP, UINT64_C(5'999'999001)},
382 IOUAmount{4'242'639'980'012504, -6}));
383 env.require(Balance(alice, aliceXRP + drops(999)));
384 env.require(Balance(alice, aliceBTC));
385 env.require(Balance(bob, bobXRP));
386 env.require(Balance(bob, bobBTC));
387 BEAST_EXPECT(amm.expectLPTokens(alice, IOUAmount{1'414'212'855'266314, -6}));
388 BEAST_EXPECT(amm.expectLPTokens(bob, IOUAmount{2'828'427'124'74619, -5}));
389 aliceXRP = env.balance(alice, XRP);
390
391 // gw clawback 1000'000000 BTC from bob
392 env(amm::ammClawback(gw, bob, btc, XRP, btc(1'000'000000)));
393 env.close();
394 BEAST_EXPECT(amm.expectBalances(
395 btc(1'999'999501),
396 STAmount{XRP, UINT64_C(3'999'999002)},
397 IOUAmount{2828426418'110813, -6}));
398 env.require(Balance(alice, aliceXRP));
399 env.require(Balance(alice, aliceBTC));
400 env.require(Balance(bob, bobXRP + XRPAmount(1999999999)));
401 env.require(Balance(bob, bobBTC));
402 BEAST_EXPECT(amm.expectLPTokens(alice, IOUAmount{1'414'212'855'266314, -6}));
403 BEAST_EXPECT(amm.expectLPTokens(bob, IOUAmount{1'414'213'562'844499, -6}));
404 bobXRP = env.balance(bob, XRP);
405
406 // gw clawback 1000'000000 BTC from alice, which exceeds her balance
407 // will clawback all her balance
408 env(amm::ammClawback(gw, alice, btc, XRP, btc(1'000'000000)));
409 env.close();
410 BEAST_EXPECT(amm.expectBalances(
411 btc(1'000'000001), XRPAmount(2'000'000002), IOUAmount{1'414'213'562'844499, -6}));
412 env.require(Balance(alice, aliceXRP + STAmount{XRP, UINT64_C(1'999'999000)}));
413 env.require(Balance(alice, aliceBTC));
414 env.require(Balance(bob, bobXRP));
415 env.require(Balance(bob, bobBTC));
416 BEAST_EXPECT(amm.expectLPTokens(alice, IOUAmount(0)));
417 BEAST_EXPECT(amm.expectLPTokens(bob, IOUAmount{1'414'213'562'844499, -6}));
418 aliceXRP = env.balance(alice, XRP);
419
420 // gw clawback from bob, which exceeds his balance
421 env(amm::ammClawback(gw, bob, btc, XRP, btc(2'000'000000)));
422 env.close();
423 // amm is empty and deleted
424 BEAST_EXPECT(!amm.ammExists());
425 env.require(Balance(alice, aliceXRP));
426 env.require(Balance(alice, aliceBTC));
427 env.require(Balance(bob, bobXRP + XRPAmount(2000000002)));
428 env.require(Balance(bob, bobBTC));
429 }
430
431 // AMMClawback from MPT/MPT pool, different issuers
432 {
433 Env env(*this, features);
434 Account const gw{"gateway"};
435 Account const gw2{"gateway2"};
436 Account const alice{"alice"};
437 Account const bob{"bob"};
438 env.fund(XRP(100000), gw, gw2, alice, bob);
439 env.close();
440
441 env(fset(gw, asfAllowTrustLineClawback));
442 env(fset(gw2, asfAllowTrustLineClawback));
443 env.close();
444
445 MPT const btc = MPTTester({
446 .env = env,
447 .issuer = gw,
448 .holders = {alice, bob},
449 .pay = 40'000'000000,
450 .flags = tfMPTCanClawback | kMptDexFlags,
451 });
452
453 MPT const eth = MPTTester({
454 .env = env,
455 .issuer = gw2,
456 .holders = {alice, bob},
457 .pay = 30'000'000000,
458 .flags = tfMPTCanClawback | kMptDexFlags,
459 });
460
461 AMM amm(env, alice, btc(2'000'000000), eth(3'000'000000));
462 env.close();
463 BEAST_EXPECT(amm.expectBalances(
464 btc(2'000'000000), eth(3'000'000000), IOUAmount{2'449'489'742'783178, -6}));
465
466 amm.deposit(bob, btc(4'000'000000), eth(6'000'000000));
467 BEAST_EXPECT(amm.expectBalances(
468 btc(6'000'000000), eth(9'000'000000), IOUAmount{7'348'469'228'349534, -6}));
469
470 auto aliceBTC = env.balance(alice, btc);
471 auto aliceETH = env.balance(alice, eth);
472 auto bobBTC = env.balance(bob, btc);
473 auto const bobETH = env.balance(bob, eth);
474
475 // gw clawback BTC from alice
476 env(amm::ammClawback(gw, alice, btc, eth, btc(1'000'000000)));
477 env.close();
478 BEAST_EXPECT(amm.expectBalances(
479 btc(5'000'000000), eth(7'500'000000), IOUAmount{6'123'724'356'957944, -6}));
480 env.require(Balance(alice, aliceBTC));
481 env.require(Balance(alice, aliceETH + eth(1'500'000000)));
482 env.require(Balance(bob, bobBTC));
483 env.require(Balance(bob, bobETH));
484 BEAST_EXPECT(amm.expectLPTokens(alice, IOUAmount{1'224'744'871'391588, -6}));
485 BEAST_EXPECT(amm.expectLPTokens(bob, IOUAmount{4'898'979'485'566356, -6}));
486 aliceETH = env.balance(alice, eth);
487
488 // gw2 clawback ETH from bob
489 env(amm::ammClawback(gw2, bob, eth, btc, eth(3'000'000000)));
490 env.close();
491 BEAST_EXPECT(amm.expectBalances(
492 btc(3'000'000000), eth(4'500'000000), IOUAmount{3'674'234'614'174766, -6}));
493 env.require(Balance(alice, aliceBTC));
494 env.require(Balance(alice, aliceETH));
495 env.require(Balance(bob, bobBTC + btc(2'000'000000)));
496 env.require(Balance(bob, bobETH));
497 BEAST_EXPECT(amm.expectLPTokens(alice, IOUAmount{1'224'744'871'391588, -6}));
498 BEAST_EXPECT(amm.expectLPTokens(bob, IOUAmount{2'449'489'742'783178, -6}));
499 bobBTC = env.balance(bob, btc);
500
501 // gw2 clawback ETH from alice, which exceeds her balance
502 env(amm::ammClawback(gw2, alice, eth, btc, eth(4'000'000000)));
503 env.close();
504 BEAST_EXPECT(amm.expectBalances(
505 btc(2'000'000001), eth(3'000'000001), IOUAmount{2'449'489'742'783178, -6}));
506 env.require(Balance(alice, aliceBTC + btc(999'999999)));
507 env.require(Balance(alice, aliceETH));
508 env.require(Balance(bob, bobBTC));
509 env.require(Balance(bob, bobETH));
510 aliceBTC = env.balance(alice, btc);
511
512 // gw clawback BTC from bob, which exceeds his balance
513 env(amm::ammClawback(gw, bob, btc, eth, btc(3'000'000000)));
514 env.close();
515 // amm is empty and deleted
516 BEAST_EXPECT(!amm.ammExists());
517 env.require(Balance(alice, aliceBTC));
518 env.require(Balance(alice, aliceETH));
519 env.require(Balance(bob, bobBTC));
520 env.require(Balance(bob, bobETH + eth(3'000'000001)));
521 }
522 }
523
524 void
526 {
527 // Ensure a clawback that rounds down to zero MPT fails with
528 // tecAMM_FAILED instead of silently burning the holder's LP.
529 testcase("test AMMClawback amount that rounds down to zero");
530 using namespace jtx;
531
532 Env env(*this, features);
533 Account const gw{"gateway"};
534 Account const alice{"alice"};
535 Account const bob{"bob"};
536 env.fund(XRP(10'000'000), gw, alice, bob);
537 env.close();
538
539 env(fset(gw, asfAllowTrustLineClawback));
540 env.close();
541
542 // The clawed asset (amountRounded) rounds to zero while its XRP
543 // counterpart is always large.
544 {
545 MPTTester const mptBtc({
546 .env = env,
547 .issuer = gw,
548 .holders = {alice, bob},
549 .pay = 1'000,
550 .flags = tfMPTCanClawback | kMptDexFlags,
551 });
552 MPT const btc = mptBtc;
553
554 AMM amm(env, alice, btc(3), XRP(333'000));
555 amm.deposit(bob, btc(3), XRP(333'000));
556
557 [[maybe_unused]] auto const [poolBtcBefore, poolXrpBefore, lptBefore] = amm.balances();
558 BEAST_EXPECT(poolBtcBefore == btc(6));
559
560 auto const issuerOABefore = mptBtc.getBalance(gw);
561 auto const aliceLpBefore = amm.getLPTokensBalance(alice.id());
562 auto const bobLpBefore = amm.getLPTokensBalance(bob.id());
563
564 // Attempt to clawback 1/6th of the BTC pool. When the zero-rounding
565 // guard is active (gated by fixCleanup3_4_0) the rounded amount
566 // drops to 0 and should trigger tecAMM_FAILED.
567 env(amm::ammClawback(gw, alice, btc, XRP, btc(1)),
568 Ter(features[fixCleanup3_4_0] ? TER{tecAMM_FAILED} : TER{tesSUCCESS}));
569 env.close();
570
571 [[maybe_unused]] auto const [poolBtcAfter, poolXrpAfter, lptAfter] = amm.balances();
572 auto const issuerOAAfter = mptBtc.getBalance(gw);
573 auto const aliceLpAfter = amm.getLPTokensBalance(alice.id());
574 auto const bobLpAfter = amm.getLPTokensBalance(bob.id());
575
576 if (features[fixCleanup3_4_0])
577 {
578 // Post-fixCleanup3_4_0: Clawback fails because the BTC balance
579 // would round to zero. All balances must remain untouched.
580 BEAST_EXPECT(poolBtcAfter == poolBtcBefore);
581 BEAST_EXPECT(poolXrpAfter == poolXrpBefore);
582 BEAST_EXPECT(issuerOAAfter == issuerOABefore);
583 BEAST_EXPECT(aliceLpAfter == aliceLpBefore);
584 BEAST_EXPECT(bobLpAfter == bobLpBefore);
585 }
586 else
587 {
588 // Pre-fixCleanup3_4_0: BTC rounds to zero and the clawback
589 // silently burns alice's LP without clawing back any BTC.
590 BEAST_EXPECT(poolBtcAfter == poolBtcBefore);
591 BEAST_EXPECT(poolXrpAfter < poolXrpBefore);
592 BEAST_EXPECT(issuerOAAfter == issuerOABefore);
593 BEAST_EXPECT(aliceLpAfter < aliceLpBefore);
594 BEAST_EXPECT(bobLpAfter == bobLpBefore);
595 }
596 }
597
598 // The pool above only ever rounds the clawed asset (amountRounded) to
599 // zero; its XRP counterpart is always large. Exercise the other operand
600 // of the guard (amount2Rounded == 0) with an MPT/MPT pool where the
601 // *paired* asset is the tiny integer that floors to zero while the
602 // clawed asset still rounds non-zero.
603 {
604 Account const carol{"carol"};
605 Account const dan{"dan"};
606 env.fund(XRP(10'000'000), carol, dan);
607 env.close();
608
609 MPTTester const mptBtc({
610 .env = env,
611 .issuer = gw,
612 .holders = {carol, dan},
613 .pay = 100'000,
614 .flags = tfMPTCanClawback | kMptDexFlags,
615 });
616 MPT const btc = mptBtc;
617
618 MPTTester const mptEth({
619 .env = env,
620 .issuer = gw,
621 .holders = {carol, dan},
622 .pay = 1'000,
623 .flags = tfMPTCanClawback | kMptDexFlags,
624 });
625 MPT const eth = mptEth;
626
627 // btc pool dwarfs the eth pool, so a ~1/12th claw withdraws a
628 // non-zero btc amount while the eth counterpart rounds to zero.
629 AMM amm(env, carol, btc(3'000), eth(3));
630 amm.deposit(dan, btc(3'000), eth(3));
631
632 [[maybe_unused]] auto const [poolBtcBefore, poolEthBefore, lptBefore] = amm.balances();
633 BEAST_EXPECT(poolBtcBefore == btc(6'000));
634 BEAST_EXPECT(poolEthBefore == eth(6));
635
636 auto const carolLpBefore = amm.getLPTokensBalance(carol.id());
637 auto const danLpBefore = amm.getLPTokensBalance(dan.id());
638
639 env(amm::ammClawback(gw, carol, btc, eth, btc(500)),
640 Ter(features[fixCleanup3_4_0] ? TER{tecAMM_FAILED} : TER{tesSUCCESS}));
641 env.close();
642
643 [[maybe_unused]] auto const [poolBtcAfter, poolEthAfter, lptAfter] = amm.balances();
644 auto const carolLpAfter = amm.getLPTokensBalance(carol.id());
645 auto const danLpAfter = amm.getLPTokensBalance(dan.id());
646
647 if (features[fixCleanup3_4_0])
648 {
649 // Post-fixCleanup3_4_0: clawback fails because the ETH (Asset2)
650 // balance would round to zero (guard fires via
651 // amount2Rounded == 0). All balances must remain untouched.
652 BEAST_EXPECT(poolBtcAfter == poolBtcBefore);
653 BEAST_EXPECT(poolEthAfter == poolEthBefore);
654 BEAST_EXPECT(carolLpAfter == carolLpBefore);
655 BEAST_EXPECT(danLpAfter == danLpBefore);
656 }
657 else
658 {
659 // Pre-fixCleanup3_4_0: the asymmetric round-off goes through.
660 // btc is clawed (non-zero) but eth rounds to zero, so the eth
661 // pool is untouched while carol's LP is burned. This asymmetry
662 // proves amount2Rounded == 0 is the trigger.
663 BEAST_EXPECT(poolBtcAfter < poolBtcBefore);
664 BEAST_EXPECT(poolEthAfter == poolEthBefore);
665 BEAST_EXPECT(carolLpAfter < carolLpBefore);
666 BEAST_EXPECT(danLpAfter == danLpBefore);
667 }
668 }
669 }
670
671 void
673 {
674 testcase("test AMMClawback all");
675 using namespace jtx;
676
677 // AMMClawback all from MPT/IOU issued by different issuers
678 {
679 Env env(*this, features);
680 Account const gw{"gateway"};
681 Account const gw2{"gateway2"};
682 Account const alice{"alice"};
683 Account const bob{"bob"};
684 env.fund(XRP(100000), gw, gw2, alice, bob);
685 env.close();
686
687 env(fset(gw, asfAllowTrustLineClawback));
688 env(fset(gw2, asfAllowTrustLineClawback));
689 env.close();
690
691 auto const usd = gw["USD"];
692 env.trust(usd(100000), alice);
693 env(pay(gw, alice, usd(50000)));
694 env.trust(usd(200000), bob);
695 env(pay(gw, bob, usd(60000)));
696 env.close();
697
698 MPT const btc = MPTTester({
699 .env = env,
700 .issuer = gw2,
701 .holders = {alice, bob},
702 .pay = 40'000'000000,
703 .flags = tfMPTCanClawback | kMptDexFlags,
704 });
705
706 AMM amm(env, alice, btc(2000000000), usd(2000));
707 env.close();
708 BEAST_EXPECT(amm.expectBalances(btc(2'000'000000), usd(2000), IOUAmount(2000000)));
709
710 // gw clawback all BTC from alice
711 amm.deposit(bob, btc(1'000'000000), usd(2000));
712 BEAST_EXPECT(amm.expectBalances(btc(3'000'000000), usd(3000), IOUAmount(3000000)));
713
714 auto const aliceBTC = env.balance(alice, btc);
715 auto aliceUSD = env.balance(alice, usd);
716 auto const bobBTC = env.balance(bob, btc);
717 auto const bobUSD = env.balance(bob, usd);
718
719 // gw2 clawback all BTC from alice
720 env(amm::ammClawback(gw2, alice, btc, usd, std::nullopt));
721 env.close();
722 BEAST_EXPECT(amm.expectBalances(btc(1'000'000000), usd(1000), IOUAmount(1000000)));
723 env.require(Balance(alice, aliceBTC));
724 env.require(Balance(alice, aliceUSD + usd(2000)));
725 env.require(Balance(bob, bobBTC));
726 env.require(Balance(bob, bobUSD));
727 aliceUSD = env.balance(alice, usd);
728
729 // gw clawback all USD from bob
730 env(amm::ammClawback(gw, bob, usd, btc, std::nullopt));
731 env.close();
732 // amm is empty and deleted
733 BEAST_EXPECT(!amm.ammExists());
734 env.require(Balance(alice, aliceBTC));
735 env.require(Balance(alice, aliceUSD));
736 env.require(Balance(bob, bobBTC + btc(1'000'000000)));
737 env.require(Balance(bob, bobUSD));
738 }
739
740 // AMMClawback all from MPT/XRP pool
741 {
742 Env env(*this, features);
743 Account const gw{"gateway"};
744 Account const alice{"alice"};
745 Account const bob{"bob"};
746 env.fund(XRP(100000), gw, alice, bob);
747 env.close();
748
749 env(fset(gw, asfAllowTrustLineClawback));
750 env.close();
751
752 MPT const btc = MPTTester({
753 .env = env,
754 .issuer = gw,
755 .holders = {alice, bob},
756 .pay = 40'000'000000,
757 .flags = tfMPTCanClawback | kMptDexFlags,
758 });
759
760 AMM amm(env, alice, btc(5000), XRP(10'000));
761 env.close();
762 BEAST_EXPECT(
763 amm.expectBalances(btc(5'000), XRP(10'000), IOUAmount{7'071'067'811865475, -9}));
764
765 amm.deposit(bob, btc(10'000), XRP(20'000));
766 BEAST_EXPECT(
767 amm.expectBalances(btc(15'000), XRP(30'000), IOUAmount{21'213'203'43559642, -8}));
768
769 auto aliceXRP = env.balance(alice, XRP);
770 auto const aliceBTC = env.balance(alice, btc);
771 auto const bobXRP = env.balance(bob, XRP);
772 auto const bobBTC = env.balance(bob, btc);
773
774 // gw clawback all BTC from alice
775 env(amm::ammClawback(gw, alice, btc, XRP, std::nullopt));
776 env.close();
777 BEAST_EXPECT(
778 amm.expectBalances(btc(10'000), XRP(20'000), IOUAmount{14'142'135'62373094, -8}));
779 env.require(Balance(alice, aliceBTC));
780 env.require(Balance(alice, aliceXRP + XRP(10'000)));
781 env.require(Balance(bob, bobBTC));
782 env.require(Balance(bob, bobXRP));
783 aliceXRP = env.balance(alice, XRP);
784
785 // gw clawback all BTC from bob
786 env(amm::ammClawback(gw, bob, btc, XRP, std::nullopt));
787 env.close();
788 // amm is empty and deleted
789 BEAST_EXPECT(!amm.ammExists());
790 env.require(Balance(alice, aliceBTC));
791 env.require(Balance(alice, aliceXRP));
792 env.require(Balance(bob, bobBTC));
793 env.require(Balance(bob, bobXRP + XRP(20'000)));
794 }
795
796 // AMMClawback all from MPT/MPT pool, different issuers
797 {
798 Env env(*this, features);
799 Account const gw{"gateway"};
800 Account const gw2{"gateway2"};
801 Account const alice{"alice"};
802 Account const bob{"bob"};
803 env.fund(XRP(100000), gw, gw2, alice, bob);
804 env.close();
805
806 env(fset(gw, asfAllowTrustLineClawback));
807 env(fset(gw2, asfAllowTrustLineClawback));
808 env.close();
809
810 MPT const btc = MPTTester({
811 .env = env,
812 .issuer = gw,
813 .holders = {alice, bob},
814 .pay = 40'000'000000,
815 .flags = tfMPTCanClawback | kMptDexFlags,
816 });
817
818 MPT const eth = MPTTester({
819 .env = env,
820 .issuer = gw2,
821 .holders = {alice, bob},
822 .pay = 30'000'000000,
823 .flags = tfMPTCanClawback | kMptDexFlags,
824 });
825
826 AMM amm(env, alice, btc(20'000), eth(50'000));
827 env.close();
828 BEAST_EXPECT(
829 amm.expectBalances(btc(20'000), eth(50'000), IOUAmount{31'622'77660168379, -11}));
830
831 amm.deposit(bob, btc(40'000), eth(100'000));
832 BEAST_EXPECT(
833 amm.expectBalances(btc(60'000), eth(150'000), IOUAmount{94'868'32980505137, -11}));
834
835 auto const aliceBTC = env.balance(alice, btc);
836 auto const aliceETH = env.balance(alice, eth);
837 auto const bobBTC = env.balance(bob, btc);
838 auto bobETH = env.balance(bob, eth);
839
840 // gw clawback all BTC from bob
841 env(amm::ammClawback(gw, bob, btc, eth, std::nullopt));
842 env.close();
843 BEAST_EXPECT(
844 amm.expectBalances(btc(20'000), eth(50'000), IOUAmount{31'622'77660168379, -11}));
845 env.require(Balance(alice, aliceBTC));
846 env.require(Balance(alice, aliceETH));
847 env.require(Balance(bob, bobBTC));
848 env.require(Balance(bob, bobETH + eth(100'000)));
849 bobETH = env.balance(bob, eth);
850
851 // gw2 clawback all ETH from alice
852 env(amm::ammClawback(gw2, alice, eth, btc, std::nullopt));
853 env.close();
854 // amm is empty and deleted
855 BEAST_EXPECT(!amm.ammExists());
856 env.require(Balance(alice, aliceBTC + btc(20'000)));
857 env.require(Balance(alice, aliceETH));
858 env.require(Balance(bob, bobBTC));
859 env.require(Balance(bob, bobETH));
860 }
861 }
862
863 void
865 {
866 testcase("test AMMClawback specific amount, assets have the same issuer");
867 using namespace jtx;
868
869 // AMMClawback from MPT/IOU issued by the same issuer
870 {
871 Env env(*this, features);
872 Account const gw{"gateway"};
873 Account const alice{"alice"};
874 Account const bob{"bob"};
875 env.fund(XRP(100000), gw, alice, bob);
876 env.close();
877
878 env(fset(gw, asfAllowTrustLineClawback));
879 env.close();
880
881 auto const usd = gw["USD"];
882 env.trust(usd(100000), alice);
883 env(pay(gw, alice, usd(50000)));
884 env.trust(usd(100000), bob);
885 env(pay(gw, bob, usd(40000)));
886 env.close();
887
888 MPT const btc = MPTTester({
889 .env = env,
890 .issuer = gw,
891 .holders = {alice, bob},
892 .pay = 40'000'000000,
893 .flags = tfMPTCanClawback | kMptDexFlags,
894 });
895
896 AMM amm(env, alice, btc(1'000'000000), usd(2000));
897 env.close();
898 BEAST_EXPECT(amm.expectBalances(
899 btc(1'000'000000), usd(2000), IOUAmount{1414'213'562373095, -9}));
900
901 amm.deposit(bob, btc(500'000000), usd(1000));
902 BEAST_EXPECT(amm.expectBalances(
903 btc(1'500'000000),
904 STAmount{usd, UINT64_C(2'999'999999999999), -12},
905 IOUAmount{2'121'320'343559642, -9}));
906
907 auto const aliceUSD = env.balance(alice, usd);
908 auto aliceBTC = env.balance(alice, btc);
909 auto const bobUSD = env.balance(bob, usd);
910 auto const bobBTC = env.balance(bob, btc);
911
912 // gw clawback 500 USD from alice.
913 env(amm::ammClawback(gw, alice, usd, btc, usd(500)));
914 env.close();
915 BEAST_EXPECT(amm.expectBalances(
916 btc(1250'000001), usd(2500), IOUAmount{1'767'766'952966369, -9}));
917 env.require(Balance(alice, aliceUSD));
918 env.require(Balance(alice, aliceBTC + btc(249'999999)));
919 env.require(Balance(bob, bobUSD));
920 env.require(Balance(bob, bobBTC));
921 aliceBTC = env.balance(alice, btc);
922 // gw clawback 250'000000 BTC and 500 USD from bob
923 // with tfClawTwoAssets
924 env(amm::ammClawback(gw, bob, btc, usd, btc(250'000000)), Txflags(tfClawTwoAssets));
925 env.close();
926 BEAST_EXPECT(amm.expectBalances(
927 btc(1'000'000002),
928 STAmount{usd, UINT64_C(2000'0000004), -7},
929 IOUAmount{1'414'213'562655938, -9}));
930 env.require(Balance(alice, aliceUSD));
931 env.require(Balance(alice, aliceBTC));
932 env.require(Balance(bob, bobUSD));
933 env.require(Balance(bob, bobBTC));
934 BEAST_EXPECT(amm.expectLPTokens(alice, IOUAmount{1'060'660'171779822, -9}));
935 BEAST_EXPECT(amm.expectLPTokens(bob, IOUAmount{353'553'390876116, -9}));
936
937 // gw clawback USD from alice exceeding her balance
938 env(amm::ammClawback(gw, alice, usd, btc, usd(5'000)));
939 env.close();
940 BEAST_EXPECT(amm.expectBalances(
941 btc(250'000001),
942 STAmount{usd, UINT64_C(500'0000004), -7},
943 IOUAmount{353'553'390876116, -9}));
944 env.require(Balance(alice, aliceUSD));
945 env.require(Balance(alice, aliceBTC + btc(750'000001)));
946 env.require(Balance(bob, bobUSD));
947 env.require(Balance(bob, bobBTC));
948 BEAST_EXPECT(amm.expectLPTokens(alice, IOUAmount(0)));
949 BEAST_EXPECT(amm.expectLPTokens(bob, IOUAmount{353'553'390876116, -9}));
950 aliceBTC = env.balance(alice, btc);
951
952 // gw clawback BTC from bob which exceeds his balance with
953 // tfClawTwoAssets
954 env(amm::ammClawback(gw, bob, btc, usd, btc(300'000000)), Txflags(tfClawTwoAssets));
955 env.close();
956 // amm is empty and deleted
957 BEAST_EXPECT(!amm.ammExists());
958 env.require(Balance(alice, aliceUSD));
959 env.require(Balance(alice, aliceBTC));
960 // USD is also clawed back from bob because of tfClawTwoAssets,
961 // bob's USD balance will not change
962 env.require(Balance(bob, bobUSD));
963 env.require(Balance(bob, bobBTC));
964 }
965
966 // AMMClawback from MPT/MPT issued by the same issuer
967 {
968 Env env(*this, features);
969 Account const gw{"gateway"};
970 Account const alice{"alice"};
971 Account const bob{"bob"};
972 env.fund(XRP(100000), gw, alice, bob);
973 env.close();
974
975 env(fset(gw, asfAllowTrustLineClawback));
976 env.close();
977
978 MPT const btc = MPTTester({
979 .env = env,
980 .issuer = gw,
981 .holders = {alice, bob},
982 .pay = 40'000'000000,
983 .flags = tfMPTCanClawback | kMptDexFlags,
984 });
985
986 MPT const eth = MPTTester({
987 .env = env,
988 .issuer = gw,
989 .holders = {alice, bob},
990 .pay = 30'000'000000,
991 .flags = tfMPTCanClawback | kMptDexFlags,
992 });
993
994 AMM amm(env, alice, btc(2'000'000000), eth(3'000'000000));
995 env.close();
996 BEAST_EXPECT(amm.expectBalances(
997 btc(2'000'000000), eth(3'000'000000), IOUAmount{2'449'489'742'783178, -6}));
998
999 amm.deposit(bob, btc(4'000'000000), eth(6'000'000000));
1000 BEAST_EXPECT(amm.expectBalances(
1001 btc(6'000'000000), eth(9'000'000000), IOUAmount{7'348'469'228'349534, -6}));
1002
1003 auto const aliceBTC = env.balance(alice, btc);
1004 auto aliceETH = env.balance(alice, eth);
1005 auto const bobBTC = env.balance(bob, btc);
1006 auto const bobETH = env.balance(bob, eth);
1007
1008 // gw clawback BTC from alice
1009 env(amm::ammClawback(gw, alice, btc, eth, btc(1'000'000000)));
1010 env.close();
1011 BEAST_EXPECT(amm.expectBalances(
1012 btc(5'000'000000), eth(7'500'000000), IOUAmount{6'123'724'356'957944, -6}));
1013 env.require(Balance(alice, aliceBTC));
1014 env.require(Balance(alice, aliceETH + eth(1'500'000000)));
1015 env.require(Balance(bob, bobBTC));
1016 env.require(Balance(bob, bobETH));
1017 BEAST_EXPECT(amm.expectLPTokens(alice, IOUAmount{1'224'744'871'391588, -6}));
1018 BEAST_EXPECT(amm.expectLPTokens(bob, IOUAmount{4'898'979'485'566356, -6}));
1019 aliceETH = env.balance(alice, eth);
1020
1021 // gw clawback ETH and BTC from bob with tfClawTwoAssets
1022 env(amm::ammClawback(gw, bob, eth, btc, eth(3'000'000000)), Txflags(tfClawTwoAssets));
1023 env.close();
1024 BEAST_EXPECT(amm.expectBalances(
1025 btc(3'000'000000), eth(4'500'000000), IOUAmount{3'674'234'614'174766, -6}));
1026 env.require(Balance(alice, aliceBTC));
1027 env.require(Balance(alice, aliceETH));
1028 env.require(Balance(bob, bobBTC));
1029 env.require(Balance(bob, bobETH));
1030 BEAST_EXPECT(amm.expectLPTokens(alice, IOUAmount{1'224'744'871'391588, -6}));
1031 BEAST_EXPECT(amm.expectLPTokens(bob, IOUAmount{2'449'489'742'783178, -6}));
1032
1033 // gw clawback BTC from alice, which exceeds her balance with
1034 // tfClawTwoAssets
1035 env(amm::ammClawback(gw, alice, btc, eth, btc(3'000'000000)), Txflags(tfClawTwoAssets));
1036 env.close();
1037 BEAST_EXPECT(amm.expectBalances(
1038 btc(2'000'000001), eth(3'000'000001), IOUAmount{2'449'489'742'783178, -6}));
1039 env.require(Balance(alice, aliceBTC));
1040 env.require(Balance(alice, aliceETH));
1041 env.require(Balance(bob, bobBTC));
1042 env.require(Balance(bob, bobETH));
1043 BEAST_EXPECT(amm.expectLPTokens(alice, IOUAmount(0)));
1044 BEAST_EXPECT(amm.expectLPTokens(bob, IOUAmount{2'449'489'742'783178, -6}));
1045
1046 // gw clawback ETH from bob, which is the same as his balance
1047 env(amm::ammClawback(gw, bob, eth, btc, eth(3'000'000001)));
1048 env.close();
1049 // amm is empty and deleted
1050 BEAST_EXPECT(!amm.ammExists());
1051 env.require(Balance(alice, aliceBTC));
1052 env.require(Balance(alice, aliceETH));
1053 env.require(Balance(bob, bobBTC + btc(2'000'000001)));
1054 env.require(Balance(bob, bobETH));
1055 }
1056 }
1057
1058 void
1060 {
1061 testcase("test AMMClawback all, assets have the same issuer");
1062 using namespace jtx;
1063
1064 // AMMClawback all from MPT/IOU issued by the same issuer
1065 {
1066 Env env(*this, features);
1067 Account const gw{"gateway"};
1068 Account const alice{"alice"};
1069 Account const bob{"bob"};
1070 env.fund(XRP(100000), gw, alice, bob);
1071 env.close();
1072
1073 env(fset(gw, asfAllowTrustLineClawback));
1074 env.close();
1075
1076 auto const usd = gw["USD"];
1077 env.trust(usd(100000), alice);
1078 env(pay(gw, alice, usd(50000)));
1079 env.trust(usd(200000), bob);
1080 env(pay(gw, bob, usd(60000)));
1081 env.close();
1082
1083 MPT const btc = MPTTester({
1084 .env = env,
1085 .issuer = gw,
1086 .holders = {alice, bob},
1087 .pay = 40'000'000000,
1088 .flags = tfMPTCanClawback | kMptDexFlags,
1089 });
1090
1091 AMM amm(env, alice, btc(2'000'000000), usd(8'000));
1092 env.close();
1093 BEAST_EXPECT(amm.expectBalances(btc(2'000'000000), usd(8'000), IOUAmount(4'000'000)));
1094
1095 amm.deposit(bob, btc(1'000'000000), usd(4'000));
1096 BEAST_EXPECT(amm.expectBalances(btc(3'000'000000), usd(12'000), IOUAmount(6'000'000)));
1097
1098 auto const aliceBTC = env.balance(alice, btc);
1099 auto const aliceUSD = env.balance(alice, usd);
1100 auto const bobBTC = env.balance(bob, btc);
1101 auto const bobUSD = env.balance(bob, usd);
1102
1103 // gw clawback all BTC and USD from alice
1104 env(amm::ammClawback(gw, alice, btc, usd, std::nullopt), Txflags(tfClawTwoAssets));
1105 env.close();
1106
1107 BEAST_EXPECT(amm.expectBalances(btc(1'000'000000), usd(4'000), IOUAmount(2'000'000)));
1108 BEAST_EXPECT(amm.expectLPTokens(bob, IOUAmount(2'000'000)));
1109 BEAST_EXPECT(amm.expectLPTokens(alice, IOUAmount(0)));
1110 env.require(Balance(alice, aliceBTC));
1111 env.require(Balance(alice, aliceUSD));
1112 env.require(Balance(bob, bobBTC));
1113 env.require(Balance(bob, bobUSD));
1114
1115 // gw clawback all USD from bob
1116 env(amm::ammClawback(gw, bob, usd, btc, std::nullopt));
1117 env.close();
1118 // amm is empty and deleted
1119 BEAST_EXPECT(!amm.ammExists());
1120 env.require(Balance(alice, aliceBTC));
1121 env.require(Balance(alice, aliceUSD));
1122 env.require(Balance(bob, bobBTC + btc(1'000'000000)));
1123 env.require(Balance(bob, bobUSD));
1124 }
1125
1126 // AMMClawback all from MPT/MPT issued by the same issuer
1127 {
1128 Env env(*this, features);
1129 Account const gw{"gateway"};
1130 Account const alice{"alice"};
1131 Account const bob{"bob"};
1132 env.fund(XRP(100000), gw, alice, bob);
1133 env.close();
1134
1135 env(fset(gw, asfAllowTrustLineClawback));
1136 env.close();
1137
1138 MPT const btc = MPTTester({
1139 .env = env,
1140 .issuer = gw,
1141 .holders = {alice, bob},
1142 .pay = 40'000'000000,
1143 .flags = tfMPTCanClawback | kMptDexFlags,
1144 });
1145
1146 MPT const eth = MPTTester({
1147 .env = env,
1148 .issuer = gw,
1149 .holders = {alice, bob},
1150 .pay = 30'000'000000,
1151 .flags = tfMPTCanClawback | kMptDexFlags,
1152 });
1153
1154 AMM amm(env, alice, btc(20'000), eth(10'000));
1155 env.close();
1156 BEAST_EXPECT(
1157 amm.expectBalances(btc(20'000), eth(10'000), IOUAmount{14'142'13562373095, -11}));
1158
1159 amm.deposit(bob, btc(40'000), eth(20'000));
1160 BEAST_EXPECT(
1161 amm.expectBalances(btc(60'000), eth(30'000), IOUAmount{42'426'40687119285, -11}));
1162
1163 auto const aliceBTC = env.balance(alice, btc);
1164 auto const aliceETH = env.balance(alice, eth);
1165 auto bobBTC = env.balance(bob, btc);
1166 auto const bobETH = env.balance(bob, eth);
1167
1168 // gw clawback all ETH from bob
1169 env(amm::ammClawback(gw, bob, eth, btc, std::nullopt));
1170 env.close();
1171 BEAST_EXPECT(
1172 amm.expectBalances(btc(20'000), eth(10'000), IOUAmount{14'142'13562373095, -11}));
1173 env.require(Balance(alice, aliceBTC));
1174 env.require(Balance(alice, aliceETH));
1175 env.require(Balance(bob, bobBTC + btc(40'000)));
1176 env.require(Balance(bob, bobETH));
1177 BEAST_EXPECT(amm.expectLPTokens(alice, IOUAmount{14'142'13562373095, -11}));
1178 BEAST_EXPECT(amm.expectLPTokens(bob, IOUAmount(0)));
1179 bobBTC = env.balance(bob, btc);
1180
1181 // gw clawback all ETH and BTC from alice with tfClawTwoAssets
1182 env(amm::ammClawback(gw, alice, eth, btc, std::nullopt), Txflags(tfClawTwoAssets));
1183 env.close();
1184
1185 // amm is empty and deleted
1186 BEAST_EXPECT(!amm.ammExists());
1187 env.require(Balance(alice, aliceBTC));
1188 env.require(Balance(alice, aliceETH));
1189 env.require(Balance(bob, bobBTC));
1190 env.require(Balance(bob, bobETH));
1191 }
1192 }
1193
1194 void
1196 {
1197 testcase("test AMMClawback when issuing token for each other");
1198 using namespace jtx;
1199
1200 // AMMClawback from MPT/IOU issued by each other
1201 {
1202 Env env(*this, features);
1203 Account const gw{"gateway"};
1204 Account const gw2{"gateway2"};
1205 Account const alice{"alice"};
1206 env.fund(XRP(1000000), gw, gw2, alice);
1207 env.close();
1208
1209 env(fset(gw, asfAllowTrustLineClawback));
1210 env(fset(gw2, asfAllowTrustLineClawback));
1211 env.close();
1212
1213 auto const usd = gw["USD"];
1214 env.trust(usd(100000), gw2);
1215 env(pay(gw, gw2, usd(5000)));
1216 env.trust(usd(100000), alice);
1217 env(pay(gw, alice, usd(5000)));
1218
1219 MPT const btc = MPTTester({
1220 .env = env,
1221 .issuer = gw2,
1222 .holders = {alice, gw},
1223 .pay = 40'000'000000,
1224 .flags = tfMPTCanClawback | kMptDexFlags,
1225 });
1226
1227 AMM amm(env, gw, usd(1000), btc(2000));
1228 env.close();
1229 BEAST_EXPECT(
1230 amm.expectBalances(usd(1000), btc(2000), IOUAmount{1414'213562373095, -12}));
1231
1232 amm.deposit(gw2, usd(2000), btc(4000));
1233 BEAST_EXPECT(
1234 amm.expectBalances(usd(3000), btc(6000), IOUAmount{4242'640687119285, -12}));
1235
1236 amm.deposit(alice, usd(3000), btc(6000));
1237 BEAST_EXPECT(
1238 amm.expectBalances(usd(6000), btc(12000), IOUAmount{8485'281374238570, -12}));
1239
1240 BEAST_EXPECT(amm.expectLPTokens(gw, IOUAmount{1414'213562373095, -12}));
1241 BEAST_EXPECT(amm.expectLPTokens(gw2, IOUAmount{2828'427124746190, -12}));
1242 BEAST_EXPECT(amm.expectLPTokens(alice, IOUAmount{4242'640687119285, -12}));
1243
1244 auto const aliceBTC = env.balance(alice, btc);
1245 auto const aliceUSD = env.balance(alice, usd);
1246 auto const gwBTC = env.balance(gw, btc);
1247 auto const gw2USD = env.balance(gw2, usd);
1248
1249 // gw claws back 1000 USD from gw2.
1250 env(amm::ammClawback(gw, gw2, usd, btc, usd(1000)));
1251 env.close();
1252 BEAST_EXPECT(
1253 amm.expectBalances(usd(5000), btc(10000), IOUAmount{7071'067811865474, -12}));
1254 BEAST_EXPECT(amm.expectLPTokens(gw, IOUAmount{1414'213562373095, -12}));
1255 BEAST_EXPECT(amm.expectLPTokens(gw2, IOUAmount{1414'213562373094, -12}));
1256 BEAST_EXPECT(amm.expectLPTokens(alice, IOUAmount{4242'640687119285, -12}));
1257 env.require(Balance(alice, aliceBTC));
1258 env.require(Balance(alice, aliceUSD));
1259 env.require(Balance(gw, gwBTC));
1260 env.require(Balance(gw2, gw2USD));
1261
1262 // gw2 claws back 1000 BTC from gw.
1263 env(amm::ammClawback(gw2, gw, btc, usd, btc(1000)), Ter(tesSUCCESS));
1264 env.close();
1265 BEAST_EXPECT(
1266 amm.expectBalances(usd(4500), btc(9001), IOUAmount{6363'961030678927, -12}));
1267
1268 BEAST_EXPECT(amm.expectLPTokens(gw, IOUAmount{707'1067811865480, -13}));
1269 BEAST_EXPECT(amm.expectLPTokens(gw2, IOUAmount{1414'213562373094, -12}));
1270 BEAST_EXPECT(amm.expectLPTokens(alice, IOUAmount{4242'640687119285, -12}));
1271 env.require(Balance(alice, aliceBTC));
1272 env.require(Balance(alice, aliceUSD));
1273 env.require(Balance(gw, gwBTC));
1274 env.require(Balance(gw2, gw2USD));
1275
1276 // gw2 claws back 4000 BTC from alice
1277 env(amm::ammClawback(gw2, alice, btc, usd, btc(4000)));
1278 env.close();
1279 BEAST_EXPECT(amm.expectBalances(
1280 STAmount{usd, UINT64_C(2500'222197533607), -12},
1281 btc(5001),
1282 IOUAmount{3535'84814069829, -11}));
1283
1284 BEAST_EXPECT(amm.expectLPTokens(gw, IOUAmount{707'1067811865480, -13}));
1285 BEAST_EXPECT(amm.expectLPTokens(gw2, IOUAmount{1414'213562373094, -12}));
1286 BEAST_EXPECT(amm.expectLPTokens(alice, IOUAmount{1414'527797138648, -12}));
1287 env.require(Balance(alice, aliceBTC));
1288 env.require(Balance(alice, aliceUSD + STAmount{usd, UINT64_C(1999'777802466393), -12}));
1289 env.require(Balance(gw, gwBTC));
1290 env.require(Balance(gw2, gw2USD));
1291 }
1292
1293 // AMMClawback from MPT/MPT issued by each other
1294 {
1295 Env env(*this, features);
1296 Account const gw{"gateway"};
1297 Account const gw2{"gateway2"};
1298 Account const alice{"alice"};
1299 env.fund(XRP(100000), gw, gw2, alice);
1300 env.close();
1301
1302 env(fset(gw, asfAllowTrustLineClawback));
1303 env(fset(gw2, asfAllowTrustLineClawback));
1304 env.close();
1305
1306 MPT const btc = MPTTester({
1307 .env = env,
1308 .issuer = gw,
1309 .holders = {gw2, alice},
1310 .pay = 40'000'000000,
1311 .flags = tfMPTCanClawback | kMptDexFlags,
1312 });
1313
1314 MPT const eth = MPTTester({
1315 .env = env,
1316 .issuer = gw2,
1317 .holders = {gw, alice},
1318 .pay = 30'000'000000,
1319 .flags = tfMPTCanClawback | kMptDexFlags,
1320 });
1321
1322 AMM amm(env, gw, btc(10'000), eth(50'000));
1323 env.close();
1324 BEAST_EXPECT(
1325 amm.expectBalances(btc(10'000), eth(50'000), IOUAmount{22'360'67977499789, -11}));
1326
1327 amm.deposit(gw2, btc(20'000), eth(100'000));
1328 BEAST_EXPECT(
1329 amm.expectBalances(btc(30'000), eth(150'000), IOUAmount{67'082'03932499367, -11}));
1330
1331 amm.deposit(alice, btc(40'000), eth(200'000));
1332 BEAST_EXPECT(
1333 amm.expectBalances(btc(70'000), eth(350'000), IOUAmount{156'524'7584249852, -10}));
1334
1335 auto const aliceBTC = env.balance(alice, btc);
1336 auto const aliceETH = env.balance(alice, eth);
1337 auto const gw2BTC = env.balance(gw2, btc);
1338 auto const gwETH = env.balance(gw, eth);
1339
1340 // gw claws back 1000 BTC from gw2.
1341 env(amm::ammClawback(gw, gw2, btc, eth, btc(1000)));
1342 env.close();
1343 BEAST_EXPECT(
1344 amm.expectBalances(btc(69'001), eth(345'001), IOUAmount{154'288'6904474855, -10}));
1345 env.require(Balance(alice, aliceBTC));
1346 env.require(Balance(alice, aliceETH));
1347 env.require(Balance(gw, gwETH));
1348 env.require(Balance(gw2, gw2BTC));
1349
1350 // gw2 claws back all ETH from gw
1351 env(amm::ammClawback(gw2, gw, eth, btc, std::nullopt));
1352 env.close();
1353 BEAST_EXPECT(
1354 amm.expectBalances(btc(59'001), eth(295'001), IOUAmount{131'928'0106724876, -10}));
1355 env.require(Balance(alice, aliceBTC));
1356 env.require(Balance(alice, aliceETH));
1357 env.require(Balance(gw, gwETH));
1358 env.require(Balance(gw2, gw2BTC));
1359
1360 // gw claws back all BTC from alice
1361 env(amm::ammClawback(gw, alice, btc, eth, std::nullopt));
1362 env.close();
1363 BEAST_EXPECT(
1364 amm.expectBalances(btc(19'001), eth(95'001), IOUAmount{42'485'29157249607, -11}));
1365 env.require(Balance(alice, aliceBTC));
1366 env.require(Balance(alice, aliceETH + eth(200'000)));
1367 env.require(Balance(gw, gwETH));
1368 env.require(Balance(gw2, gw2BTC));
1369 }
1370 }
1371
1372 void
1374 {
1375 testcase("test AMMClawback when asset is frozen or locked");
1376 using namespace jtx;
1377
1378 // test AMMClawback when MPT globally locked or IOU globally frozen
1379 {
1380 Env env{*this, features};
1381 Account const gw{"gateway"};
1382 Account const alice{"alice"};
1383 env.fund(XRP(1'000'000), gw, alice);
1384
1385 env(fset(gw, asfAllowTrustLineClawback));
1386 env.close();
1387 auto const usd = gw["USD"];
1388 env.trust(usd(1'000'000), alice);
1389 env(pay(gw, alice, usd(500'000)));
1390
1391 MPTTester btc({
1392 .env = env,
1393 .issuer = gw,
1394 .holders = {alice},
1395 .pay = 30'000,
1396 .flags = tfMPTCanClawback | tfMPTCanLock | kMptDexFlags,
1397 });
1398 AMM const ammAlice(env, alice, usd(10'000), btc(10'000));
1399 BEAST_EXPECT(ammAlice.expectBalances(usd(10'000), btc(10'000), IOUAmount(10'000)));
1400 env.close();
1401
1402 auto aliceBTC = env.balance(alice, MPT(btc));
1403 auto aliceUSD = env.balance(alice, usd);
1404
1405 // globally locked and claw back 1000 BTC.
1406 // this should be successful
1407 btc.set({.flags = tfMPTLock});
1408 env(amm::ammClawback(gw, alice, MPT(btc), usd, btc(1'000)));
1409 BEAST_EXPECT(ammAlice.expectBalances(usd(9'000), btc(9'000), IOUAmount(9'000)));
1410 env.require(Balance(alice, aliceBTC));
1411 env.require(Balance(alice, aliceUSD + usd(1'000)));
1412 aliceUSD = env.balance(alice, usd);
1413
1414 // unlock and claw back 2000 BTC
1415 btc.set({.flags = tfMPTUnlock});
1416 env(amm::ammClawback(gw, alice, MPT(btc), usd, btc(2'000)));
1417 BEAST_EXPECT(ammAlice.expectBalances(
1418 STAmount(usd, UINT64_C(7'000'000000000001), -12), btc(7'001), IOUAmount(7'000)));
1419 env.require(Balance(alice, aliceBTC));
1420 env.require(Balance(alice, aliceUSD + usd(2'000)));
1421 aliceUSD = env.balance(alice, usd);
1422
1423 // globally freeze trustline and claw back 1000 USD.
1424 // this should be successful
1425 env(trust(gw, alice["USD"](0), tfSetFreeze));
1426 env.close();
1427 env(amm::ammClawback(gw, alice, usd, MPT(btc), usd(1'000)));
1428 BEAST_EXPECT(ammAlice.expectBalances(
1429 STAmount(usd, UINT64_C(6000'000000000002), -12),
1430 btc(6'001),
1431 IOUAmount(6'000'000000000001, -12)));
1432 env.require(Balance(alice, aliceBTC + btc(1'000)));
1433 env.require(Balance(alice, aliceUSD));
1434 aliceBTC = env.balance(alice, MPT(btc));
1435
1436 // globally unfreeze trustline and claw back 2000 USD
1437 // and 2000 BTC with tfClawTwoAssets
1438 env(fset(gw, asfGlobalFreeze));
1439 env.close();
1440 env(amm::ammClawback(gw, alice, usd, MPT(btc), usd(2'000)), Txflags(tfClawTwoAssets));
1441 BEAST_EXPECT(ammAlice.expectBalances(
1442 STAmount(usd, UINT64_C(4'000'000000000002), -12),
1443 btc(4'001),
1444 IOUAmount(4'000'000000000001, -12)));
1445 env.require(Balance(alice, aliceBTC));
1446 env.require(Balance(alice, aliceUSD));
1447 }
1448
1449 // test AMMClawback when MPT individually locked or IOU individually
1450 // frozen
1451 {
1452 Env env{*this, features};
1453 Account const gw{"gateway"};
1454 Account const alice{"alice"};
1455 env.fund(XRP(1'000'000), gw, alice);
1456
1457 env(fset(gw, asfAllowTrustLineClawback));
1458 env.close();
1459 auto const usd = gw["USD"];
1460 env.trust(usd(1'000'000), alice);
1461 env(pay(gw, alice, usd(500'000)));
1462
1463 MPTTester btc({
1464 .env = env,
1465 .issuer = gw,
1466 .holders = {alice},
1467 .pay = 30'000,
1468 .flags = tfMPTCanClawback | tfMPTCanLock | kMptDexFlags,
1469 });
1470 AMM const ammAlice(env, alice, usd(10'000), btc(10'000));
1471 BEAST_EXPECT(ammAlice.expectBalances(usd(10'000), btc(10'000), IOUAmount(10'000)));
1472 env.close();
1473
1474 auto aliceBTC = env.balance(alice, MPT(btc));
1475 auto aliceUSD = env.balance(alice, usd);
1476
1477 // individually locked and claw back 2000 BTC from alice
1478 btc.set({.holder = alice, .flags = tfMPTLock});
1479 env(amm::ammClawback(gw, alice, MPT(btc), usd, btc(2'000)));
1480 BEAST_EXPECT(ammAlice.expectBalances(usd(8'000), btc(8'000), IOUAmount(8'000)));
1481 env.require(Balance(alice, aliceBTC));
1482 env.require(Balance(alice, aliceUSD + usd(2'000)));
1483 aliceUSD = env.balance(alice, usd);
1484
1485 // individually freeze trustline and claw back 1000 USD from alice
1486 env(trust(gw, alice["USD"](0), tfSetFreeze));
1487 env.close();
1488 env(amm::ammClawback(gw, alice, usd, MPT(btc), usd(1'000)));
1489 BEAST_EXPECT(ammAlice.expectBalances(usd(7'000), btc(7'000), IOUAmount(7'000)));
1490 env.require(Balance(alice, aliceBTC + btc(1'000)));
1491 env.require(Balance(alice, aliceUSD));
1492 aliceBTC = env.balance(alice, MPT(btc));
1493
1494 // unlock MPT and claw back 3000 BTC from alice
1495 btc.set({.holder = alice, .flags = tfMPTUnlock});
1496 env(amm::ammClawback(gw, alice, MPT(btc), usd, btc(3'000)));
1497 BEAST_EXPECT(ammAlice.expectBalances(
1498 STAmount{usd, UINT64_C(4000'000000000001), -12}, btc(4'001), IOUAmount(4'000)));
1499 env.require(Balance(alice, aliceBTC));
1500 env.require(Balance(alice, aliceUSD + usd(3'000)));
1501 aliceUSD = env.balance(alice, usd);
1502
1503 // unlock trustline and claw back 1000 USD from alice
1504 env(trust(gw, alice["USD"](0), tfClearFreeze));
1505 env.close();
1506 env(amm::ammClawback(gw, alice, usd, MPT(btc), usd(1'000)));
1507 BEAST_EXPECT(ammAlice.expectBalances(
1508 STAmount(usd, UINT64_C(3'000'000000000002), -12),
1509 btc(3'001),
1510 IOUAmount(3000'000000000001, -12)));
1511 env.require(Balance(alice, aliceBTC + btc(1'000)));
1512 env.require(Balance(alice, aliceUSD));
1513 }
1514 }
1515
1516 void
1518 {
1519 testcase("test AMMClawback creates missing MPToken");
1520 using namespace jtx;
1521
1522 auto const test = [&](std::optional<std::uint64_t> const clawAmount) {
1523 Env env{*this, features};
1524 Account const gw{"gateway"};
1525 Account const alice{"alice"};
1526 env.fund(XRP(1'000'000), gw, alice);
1527 env.close();
1528
1530 .env = env,
1531 .issuer = gw,
1532 .holders = {alice},
1533 .pay = 1'000,
1534 .flags = tfMPTCanClawback | tfMPTRequireAuth | kMptDexFlags,
1535 .authHolder = true,
1536 });
1537
1538 AMM ammAlice(env, alice, token(1'000), XRP(1'000));
1539 env.close();
1540 BEAST_EXPECT(env.balance(alice, token) == token(0));
1541
1542 // The holder can delete the zero-balance MPToken while still
1543 // holding LP tokens. A regular AMMWithdraw remains subject to
1544 // RequireAuth and cannot recreate the missing token.
1545 token.authorize({.account = alice, .flags = tfMPTUnauthorize});
1546 env.close();
1547 BEAST_EXPECT(!env.le(keylet::mptoken(token.issuanceID(), alice.id())));
1548 ammAlice.withdrawAll(alice, std::nullopt, Ter(tecNO_AUTH));
1549 env.close();
1550 BEAST_EXPECT(!env.le(keylet::mptoken(token.issuanceID(), alice.id())));
1551
1552 // AMMClawback ignores authorization and must be able to recreate
1553 // the holder MPToken so the issuer can recover MPT from the pool.
1555 if (clawAmount)
1556 amount = token(*clawAmount);
1557 env(amm::ammClawback(gw, alice, token, XRP, amount));
1558 env.close();
1559
1560 auto const sleMpt = env.le(keylet::mptoken(token.issuanceID(), alice.id()));
1561 BEAST_EXPECT(sleMpt && sleMpt->isFlag(lsfMPTAuthorized));
1562 env.require(Balance(alice, token(0)));
1563
1564 BEAST_EXPECT(clawAmount ? ammAlice.ammExists() : !ammAlice.ammExists());
1565 };
1566
1567 test(std::nullopt);
1568 test(400);
1569 }
1570
1571 void
1573 {
1574 testcase("test single depoit and clawback");
1575 using namespace jtx;
1576
1577 // MPT/XRP
1578 {
1579 Env env(*this, features);
1580 Account const gw{"gateway"};
1581 Account const alice{"alice"};
1582 env.fund(XRP(1000000000), gw, alice);
1583 env.close();
1584
1585 MPT const btc = MPTTester({
1586 .env = env,
1587 .issuer = gw,
1588 .holders = {alice},
1589 .pay = 40'000'000000,
1590 .flags = tfMPTCanClawback | kMptDexFlags,
1591 });
1592
1593 // gw creates AMM pool of BTC/XRP.
1594 AMM amm(env, gw, XRP(100), btc(400), Ter(tesSUCCESS));
1595 env.close();
1596 BEAST_EXPECT(amm.expectBalances(XRP(100), btc(400), IOUAmount(200000)));
1597 amm.deposit(alice, btc(400));
1598 BEAST_EXPECT(amm.expectBalances(XRP(100), btc(800), IOUAmount{282842'712474619, -9}));
1599
1600 auto const aliceBTC = env.balance(alice, MPT(btc));
1601 auto const aliceXRP = env.balance(alice, XRP);
1602
1603 // gw clawback 100 BTC from alice
1604 env(amm::ammClawback(gw, alice, MPT(btc), XRP, btc(100)));
1605 BEAST_EXPECT(amm.expectBalances(
1606 XRPAmount(87500001), btc(701), IOUAmount{247'487'3734152917, -10}));
1607
1608 env.require(Balance(alice, aliceBTC));
1609 env.require(Balance(alice, aliceXRP + XRPAmount(12'499999)));
1610 }
1611
1612 // MPT/IOU
1613 {
1614 Env env(*this, features);
1615 Account const gw{"gateway"};
1616 Account const alice{"alice"};
1617 env.fund(XRP(1000000000), gw, alice);
1618 env.close();
1619
1620 // gw sets asfAllowTrustLineClawback.
1621 env(fset(gw, asfAllowTrustLineClawback));
1622 env.close();
1623 env.require(Flags(gw, asfAllowTrustLineClawback));
1624
1625 // gw issues 1000 USD to Alice.
1626 auto const usd = gw["USD"];
1627 env.trust(usd(100000), alice);
1628 env(pay(gw, alice, usd(1000)));
1629 env.close();
1630
1631 MPT const btc = MPTTester({
1632 .env = env,
1633 .issuer = gw,
1634 .holders = {alice},
1635 .pay = 40'000'000000,
1636 .flags = tfMPTCanClawback | kMptDexFlags,
1637 });
1638
1639 // gw creates AMM pool of BTC/USD.
1640 AMM amm(env, gw, usd(100), btc(400), Ter(tesSUCCESS));
1641 env.close();
1642 BEAST_EXPECT(amm.expectBalances(usd(100), btc(400), IOUAmount(200)));
1643 amm.deposit(alice, btc(400));
1644 BEAST_EXPECT(amm.expectBalances(usd(100), btc(800), IOUAmount{282'842712474619, -12}));
1645
1646 auto const aliceBTC = env.balance(alice, MPT(btc));
1647 auto aliceUSD = env.balance(alice, usd);
1648
1649 // gw clawback 100 BTC from alice
1650 env(amm::ammClawback(gw, alice, MPT(btc), usd, btc(100)));
1651 BEAST_EXPECT(amm.expectBalances(
1652 STAmount{usd, UINT64_C(87'50000000000003), -14},
1653 btc(701),
1654 IOUAmount{247'4873734152917, -13}));
1655
1656 env.require(Balance(alice, aliceBTC));
1657 env.require(Balance(alice, aliceUSD + usd(12.5)));
1658 aliceUSD = env.balance(alice, usd);
1659
1660 // gw clawback 30 USD from alice with tfClawTwoAssets, which exceeds
1661 // her balance
1662 env(amm::ammClawback(gw, alice, usd, MPT(btc), usd(30)), Txflags(tfClawTwoAssets));
1663 BEAST_EXPECT(amm.expectBalances(
1664 STAmount{usd, UINT64_C(70'71067811865476), -14}, btc(567), IOUAmount(200)));
1665 env.require(Balance(alice, aliceBTC));
1666 env.require(Balance(alice, aliceUSD));
1667 BEAST_EXPECT(amm.expectLPTokens(alice, IOUAmount(0)));
1668 BEAST_EXPECT(amm.expectLPTokens(gw, IOUAmount(200)));
1669 }
1670
1671 // MPT/MPT
1672 {
1673 Env env(*this, features);
1674 Account const gw{"gateway"};
1675 Account const alice{"alice"};
1676 env.fund(XRP(1000000000), gw, alice);
1677 env.close();
1678
1679 MPT const usd = MPTTester({
1680 .env = env,
1681 .issuer = gw,
1682 .holders = {alice},
1683 .pay = 40'000'000000,
1684 .flags = tfMPTCanClawback | kMptDexFlags,
1685 });
1686
1687 MPT const btc = MPTTester({
1688 .env = env,
1689 .issuer = gw,
1690 .holders = {alice},
1691 .pay = 40'000'000000,
1692 .flags = tfMPTCanClawback | kMptDexFlags,
1693 });
1694
1695 // gw creates AMM pool of BTC/USD.
1696 AMM amm(env, gw, usd(100), btc(400), Ter(tesSUCCESS));
1697 env.close();
1698 BEAST_EXPECT(amm.expectBalances(usd(100), btc(400), IOUAmount(200)));
1699 amm.deposit(alice, btc(400));
1700 BEAST_EXPECT(amm.expectBalances(usd(100), btc(800), IOUAmount{282'842712474619, -12}));
1701
1702 auto const aliceBTC = env.balance(alice, MPT(btc));
1703 auto aliceUSD = env.balance(alice, usd);
1704
1705 // gw clawback 100 BTC from alice
1706 env(amm::ammClawback(gw, alice, MPT(btc), usd, btc(100)));
1707 BEAST_EXPECT(amm.expectBalances(usd(88), btc(701), IOUAmount{247'4873734152917, -13}));
1708
1709 env.require(Balance(alice, aliceBTC));
1710 env.require(Balance(alice, aliceUSD + usd(12)));
1711 aliceUSD = env.balance(alice, usd);
1712
1713 // gw clawback 30 USD from alice with tfClawTwoAssets, which exceeds
1714 // her balance
1715 env(amm::ammClawback(gw, alice, usd, MPT(btc), usd(30)), Txflags(tfClawTwoAssets));
1716 BEAST_EXPECT(amm.expectBalances(usd(72), btc(567), IOUAmount(200)));
1717 env.require(Balance(alice, aliceBTC));
1718 env.require(Balance(alice, aliceUSD));
1719 BEAST_EXPECT(amm.expectLPTokens(alice, IOUAmount(0)));
1720 BEAST_EXPECT(amm.expectLPTokens(gw, IOUAmount(200)));
1721 }
1722 }
1723
1724 void
1726 {
1727 testcase(
1728 "test last holder's lptoken balance not equal to AMM's lptoken "
1729 "balance before clawback");
1730 using namespace jtx;
1731 std::string logs;
1732
1733 // MPT/IOU
1734 {
1735 Env env(*this, features, std::make_unique<CaptureLogs>(&logs));
1736 Account const gw{"gateway"}, alice{"alice"}, bob{"bob"};
1737 env.fund(XRP(100000), gw, alice, bob);
1738 env.close();
1739 env(fset(gw, asfAllowTrustLineClawback));
1740 env.close();
1741
1742 auto const usd = gw["USD"];
1743 env.trust(usd(100000), alice);
1744 env(pay(gw, alice, usd(50000)));
1745 env.trust(usd(100000), bob);
1746 env(pay(gw, bob, usd(40000)));
1747 env.close();
1748
1749 MPT const eur = MPTTester({
1750 .env = env,
1751 .issuer = gw,
1752 .holders = {alice, bob},
1753 .pay = 40'000'000000,
1754 .flags = tfMPTCanClawback | kMptDexFlags,
1755 });
1756
1757 AMM amm(env, alice, usd(2), eur(1));
1758 amm.deposit(alice, IOUAmount{1'576123487565916, -15});
1759 amm.deposit(bob, IOUAmount{1'000});
1760 amm.withdraw(alice, IOUAmount{1'576123487565916, -15});
1761 amm.withdrawAll(bob);
1762
1763 auto const lpToken =
1764 getAccountLines(env, alice, amm.lptIssue())[jss::lines][0u][jss::balance]
1765 .asString();
1766 auto const lpTokenBalance =
1767 amm.ammRpcInfo()[jss::amm][jss::lp_token][jss::value].asString();
1768 if (largeMantissa(features))
1769 {
1770 BEAST_EXPECT(lpToken == "1.414213562374011" && lpTokenBalance == "1.4142135623741");
1771 }
1772 else
1773 {
1774 BEAST_EXPECT(lpToken == "1.414213562374011" && lpTokenBalance == "1.414213562374");
1775 }
1776
1777 auto res = isOnlyLiquidityProvider(*env.current(), amm.lptIssue(), alice);
1778 BEAST_EXPECT(res && res.value());
1779
1780 if (features[fixAMMv1_3] && features[fixAMMClawbackRounding])
1781 {
1782 env(amm::ammClawback(gw, alice, usd, eur, std::nullopt));
1783 BEAST_EXPECT(!amm.ammExists());
1784 }
1785 else if (features[fixAMMv1_3] && largeMantissa(features))
1786 {
1787 env(amm::ammClawback(gw, alice, usd, eur, std::nullopt));
1788 // Without the Rounding feature and with new Number a dust pool
1789 // amount remains
1790 BEAST_EXPECT(amm.ammExists());
1791 }
1792 else if (!largeMantissa(features))
1793 {
1794 env(amm::ammClawback(gw, alice, usd, eur, std::nullopt), Ter(tecINTERNAL));
1795 BEAST_EXPECT(amm.ammExists());
1796 }
1797 else
1798 {
1799 env(amm::ammClawback(gw, alice, usd, eur, std::nullopt), Ter(tecAMM_BALANCE));
1800 BEAST_EXPECT(amm.ammExists());
1801 }
1802 }
1803
1804 // MPT/MPT
1805 {
1806 Env env(*this, features, std::make_unique<CaptureLogs>(&logs));
1807 Account const gw{"gateway"}, alice{"alice"}, bob{"bob"};
1808 env.fund(XRP(100000), gw, alice, bob);
1809 env.close();
1810
1811 MPT const usd = MPTTester({
1812 .env = env,
1813 .issuer = gw,
1814 .holders = {alice, bob},
1815 .pay = 40'000'000000,
1816 .flags = tfMPTCanClawback | kMptDexFlags,
1817 });
1818
1819 MPT const eur = MPTTester({
1820 .env = env,
1821 .issuer = gw,
1822 .holders = {alice, bob},
1823 .pay = 40'000'000000,
1824 .flags = tfMPTCanClawback | kMptDexFlags,
1825 });
1826
1827 AMM amm(env, alice, usd(2), eur(1));
1828 amm.deposit(alice, IOUAmount{1'576123487565916, -15});
1829 amm.deposit(bob, IOUAmount{1'000});
1830 amm.withdraw(alice, IOUAmount{1'576123487565916, -15});
1831 amm.withdrawAll(bob);
1832
1833 auto const lpToken =
1834 getAccountLines(env, alice, amm.lptIssue())[jss::lines][0u][jss::balance]
1835 .asString();
1836 auto const lpTokenBalance =
1837 amm.ammRpcInfo()[jss::amm][jss::lp_token][jss::value].asString();
1838 if (!largeMantissa(features))
1839 {
1840 BEAST_EXPECT(lpToken == "1.414213562374011" && lpTokenBalance == "1.414213562374");
1841 }
1842 else
1843 {
1844 BEAST_EXPECT(lpToken == "1.414213562374011" && lpTokenBalance == "1.4142135623741");
1845 }
1846
1847 auto res = isOnlyLiquidityProvider(*env.current(), amm.lptIssue(), alice);
1848 BEAST_EXPECT(res && res.value());
1849
1850 if (features[fixAMMv1_3] && features[fixAMMClawbackRounding])
1851 {
1852 env(amm::ammClawback(gw, alice, usd, eur, std::nullopt));
1853 BEAST_EXPECT(!amm.ammExists());
1854 }
1855 else if (features[fixAMMv1_3] && largeMantissa(features))
1856 {
1857 // Without the Rounding feature and with new Number a dust pool
1858 // amount remains
1859 env(amm::ammClawback(gw, alice, usd, eur, std::nullopt));
1860 BEAST_EXPECT(amm.ammExists());
1861 }
1862 else if (!largeMantissa(features))
1863 {
1864 env(amm::ammClawback(gw, alice, usd, eur, std::nullopt), Ter(tecINTERNAL));
1865 BEAST_EXPECT(amm.ammExists());
1866 }
1867 else if (features[featureMPTokensV2])
1868 {
1869 env(amm::ammClawback(gw, alice, usd, eur, std::nullopt), Ter(tecAMM_BALANCE));
1870 BEAST_EXPECT(amm.ammExists());
1871 }
1872 }
1873 }
1874
1875 void
1877 {
1878 testcase("claw asset check for MPT and IOU");
1879 using namespace jtx;
1880
1881 // IOU/MPT, MPT not clawable
1882 {
1883 Env env(*this, features);
1884 Account const gw{"gateway"};
1885 Account const alice{"alice"};
1886 env.fund(XRP(100000), gw, alice);
1887 env.close();
1888
1889 env(fset(gw, asfAllowTrustLineClawback));
1890 env.close();
1891
1892 auto const usd = gw["USD"];
1893 env.trust(usd(100000), alice);
1894 env(pay(gw, alice, usd(1000)));
1895 env.close();
1896
1897 MPT const btc =
1898 MPTTester({.env = env, .issuer = gw, .holders = {alice}, .pay = 40'000});
1899
1900 AMM const amm(env, alice, usd(200), btc(100));
1901 // Asset BTC is not clawable without tfMPTCanClawback.
1902 env(amm::ammClawback(gw, alice, btc, usd, std::nullopt), Ter(tecNO_PERMISSION));
1903
1904 // Although USD is clawable with asfAllowTrustLineClawback.
1905 // When tfClawTwoAssets is set, we will claw Asset2 as well.
1906 // But Asset2 is not clawable. tfMPTCanClawback was not set for BTC.
1907 env(amm::ammClawback(gw, alice, usd, btc, std::nullopt),
1908 Txflags(tfClawTwoAssets),
1910
1911 // Can only claw the other asset
1912 env(amm::ammClawback(gw, alice, usd, btc, std::nullopt));
1913 }
1914
1915 // IOU/MPT, IOU not clawable
1916 {
1917 Env env(*this, features);
1918 Account const gw{"gateway"};
1919 Account const alice{"alice"};
1920 env.fund(XRP(100000), gw, alice);
1921 env.close();
1922
1923 auto const usd = gw["USD"];
1924 env.trust(usd(100000), alice);
1925 env(pay(gw, alice, usd(1000)));
1926 env.close();
1927
1928 MPT const btc = MPTTester({
1929 .env = env,
1930 .issuer = gw,
1931 .holders = {alice},
1932 .pay = 40'000,
1933 .flags = tfMPTCanClawback | kMptDexFlags,
1934 });
1935
1936 // Asset USD is not clawable without asfAllowTrustLineClawback.
1937 AMM const amm(env, alice, usd(200), btc(100));
1938 env(amm::ammClawback(gw, alice, usd, btc, std::nullopt), Ter(tecNO_PERMISSION));
1939
1940 // Although BTC is clawable with tfMPTCanClawback.
1941 // When tfClawTwoAssets is set, we will claw Asset2 as well.
1942 // But Asset2 is not clawable. asfAllowTrustLineClawback was not set
1943 // by the issuer.
1944 env(amm::ammClawback(gw, alice, btc, usd, std::nullopt),
1945 Txflags(tfClawTwoAssets),
1947
1948 // Can only claw the other asset
1949 env(amm::ammClawback(gw, alice, btc, usd, std::nullopt));
1950 }
1951
1952 // IOU/MPT both clawable
1953 {
1954 Env env(*this, features);
1955 Account const gw{"gateway"};
1956 Account const gw2{"gateway2"};
1957 Account const alice{"alice"};
1958 env.fund(XRP(100000), gw, gw2, alice);
1959 env.close();
1960
1961 env(fset(gw, asfAllowTrustLineClawback));
1962 env.close();
1963
1964 auto const usd = gw["USD"];
1965 env.trust(usd(100000), alice);
1966 env(pay(gw, alice, usd(1000)));
1967 env.close();
1968
1969 MPT const btc = MPTTester({
1970 .env = env,
1971 .issuer = gw2,
1972 .holders = {alice},
1973 .pay = 40'000,
1974 .flags = tfMPTCanClawback | kMptDexFlags,
1975 });
1976
1977 AMM const amm(env, alice, usd(200), btc(100));
1978
1979 // the account trying to claw MPT is not its issuer
1980 // will return temMALFORMED in preflight.
1981 env(amm::ammClawback(gw, alice, btc, usd, std::nullopt), Ter(temMALFORMED));
1982 }
1983
1984 // only issuer can claw. IOU/MPT mix
1985 {
1986 auto const test = [&](auto&& issue1, auto&& issue2) {
1987 Env env(*this);
1988 Account const gw("gateway"), alice("alice"), bob("bob");
1989 env.fund(XRP(30'000), alice, bob, gw);
1990 env.close();
1991 auto const usd = issue1({
1992 .env = env,
1993 .token = "USD",
1994 .issuer = gw,
1995 .holders = {alice},
1996 .limit = 1'000'000,
1997 });
1998 auto const btc = issue2({
1999 .env = env,
2000 .token = "BTC",
2001 .issuer = bob,
2002 .holders = {alice},
2003 .limit = 1'000'000,
2004 });
2005 env(pay(gw, alice, usd(50000)));
2006 env(pay(bob, alice, btc(50000)));
2007 env.close();
2008
2009 auto const ammAlice = AMM(env, alice, usd(10000), btc(10100));
2010 // BTC's issuer is bob, alice can not clawback
2011 env(amm::ammClawback(gw, alice, btc, usd, std::nullopt), Ter(temMALFORMED));
2012 };
2014 }
2015
2016 // set tfClawTwoAssets, but the two assets are from different issuer.
2017 {
2018 auto const test = [&](auto&& issue1, auto&& issue2) {
2019 Env env(*this);
2020 Account const gw("gateway"), alice("alice"), bob("bob");
2021 env.fund(XRP(30'000), alice, bob, gw);
2022 env.close();
2023 auto const usd = issue1({
2024 .env = env,
2025 .token = "USD",
2026 .issuer = gw,
2027 .holders = {alice},
2028 .limit = 1'000'000,
2029 });
2030 auto const btc = issue2({
2031 .env = env,
2032 .token = "BTC",
2033 .issuer = bob,
2034 .holders = {alice},
2035 .limit = 1'000'000,
2036 });
2037 env(pay(gw, alice, usd(50000)));
2038 env(pay(bob, alice, btc(50000)));
2039 env.close();
2040
2041 auto const ammAlice = AMM(env, alice, usd(10000), btc(10100));
2042 // BTC's issuer is bob. But with tfClawTwoAssets, we will claw
2043 // both. It will fail because the other asset USD's issuer is
2044 // gw.
2045 env(amm::ammClawback(bob, alice, btc, usd, std::nullopt),
2046 Txflags(tfClawTwoAssets),
2048 };
2050 }
2051 }
2052
2053 // Test that AMMClawback succeeds when the LP has previously deleted both
2054 // zero-balance MPToken objects in an MPT/MPT pool. The fix changes the
2055 // ValidMPTIssuance invariant threshold from > 1 to > 2 so that the two
2056 // MPToken creations triggered by the internal AMMWithdraw are permitted.
2057 void
2059 {
2060 testcase("test AMMClawback after holder deletes zero-balance MPTokens");
2061 using namespace jtx;
2062
2063 // Partial clawback (one asset): verify both MPTokens are recreated and
2064 // the non-claw asset is returned to alice.
2065 {
2066 Env env(*this, features);
2067 Account const gw{"gateway"};
2068 Account const alice{"alice"};
2069 env.fund(XRP(100'000), gw, alice);
2070 env.close();
2071
2072 MPTTester btc({
2073 .env = env,
2074 .issuer = gw,
2075 .holders = {alice},
2076 .pay = 10'000,
2077 .flags = tfMPTCanClawback | kMptDexFlags,
2078 });
2079
2080 MPTTester eth({
2081 .env = env,
2082 .issuer = gw,
2083 .holders = {alice},
2084 .pay = 10'000,
2085 .flags = tfMPTCanClawback | kMptDexFlags,
2086 });
2087
2088 // Alice deposits everything into the MPT/MPT pool; her MPT
2089 // balances drop to zero.
2090 AMM const amm(env, alice, btc(10'000), eth(10'000));
2091 env.close();
2092 BEAST_EXPECT(amm.expectBalances(btc(10'000), eth(10'000), IOUAmount{10'000}));
2093
2094 auto const aliceBTC = env.balance(alice, btc);
2095 auto const aliceETH = env.balance(alice, eth);
2096 BEAST_EXPECT(aliceBTC == btc(0));
2097 BEAST_EXPECT(aliceETH == eth(0));
2098
2099 // Alice deletes both zero-balance MPTokens to reclaim reserves.
2100 btc.authorize({.account = alice, .flags = tfMPTUnauthorize});
2101 eth.authorize({.account = alice, .flags = tfMPTUnauthorize});
2102 BEAST_EXPECT(!env.le(keylet::mptoken(btc.issuanceID(), alice.id())));
2103 BEAST_EXPECT(!env.le(keylet::mptoken(eth.issuanceID(), alice.id())));
2104
2105 // gw claws back some BTC from alice's share in the pool.
2106 // AMMWithdraw internally creates both missing MPTokens
2107 // (mptokensCreated_ == 2); the invariant (> 2) allows this.
2108 env(amm::ammClawback(gw, alice, btc, eth, btc(1'000)));
2109 env.close();
2110
2111 // Both MPToken objects must have been recreated.
2112 BEAST_EXPECT(env.le(keylet::mptoken(btc.issuanceID(), alice.id())));
2113 BEAST_EXPECT(env.le(keylet::mptoken(eth.issuanceID(), alice.id())));
2114
2115 // The non-claw asset (eth) was returned to alice.
2116 BEAST_EXPECT(env.balance(alice, eth) > aliceETH);
2117 // The claw asset (btc) was burned; alice's btc balance stays 0.
2118 env.require(Balance(alice, aliceBTC));
2119 BEAST_EXPECT(amm.ammExists());
2120 }
2121
2122 // Full clawback (two assets, tfClawTwoAssets): verify both MPTokens
2123 // are recreated and the AMM is deleted when fully drained.
2124 {
2125 Env env(*this, features);
2126 Account const gw{"gateway"};
2127 Account const alice{"alice"};
2128 env.fund(XRP(100'000), gw, alice);
2129 env.close();
2130
2131 MPTTester btc({
2132 .env = env,
2133 .issuer = gw,
2134 .holders = {alice},
2135 .pay = 10'000,
2136 .flags = tfMPTCanClawback | kMptDexFlags,
2137 });
2138
2139 MPTTester eth({
2140 .env = env,
2141 .issuer = gw,
2142 .holders = {alice},
2143 .pay = 10'000,
2144 .flags = tfMPTCanClawback | kMptDexFlags,
2145 });
2146
2147 AMM const amm(env, alice, btc(10'000), eth(10'000));
2148 env.close();
2149
2150 auto const aliceBTC = env.balance(alice, btc);
2151 auto const aliceETH = env.balance(alice, eth);
2152
2153 btc.authorize({.account = alice, .flags = tfMPTUnauthorize});
2154 eth.authorize({.account = alice, .flags = tfMPTUnauthorize});
2155 BEAST_EXPECT(!env.le(keylet::mptoken(btc.issuanceID(), alice.id())));
2156 BEAST_EXPECT(!env.le(keylet::mptoken(eth.issuanceID(), alice.id())));
2157
2158 // Full two-asset clawback: both assets are clawed and alice
2159 // receives nothing back. The AMM should be empty and deleted.
2160 env(amm::ammClawback(gw, alice, btc, eth, std::nullopt), Txflags(tfClawTwoAssets));
2161 env.close();
2162
2163 BEAST_EXPECT(!amm.ammExists());
2164 // Both assets were clawed; alice's balances remain at zero.
2165 env.require(Balance(alice, aliceBTC));
2166 env.require(Balance(alice, aliceETH));
2167 }
2168 }
2169
2170 void
2172 {
2173 testcase("test AMMClawback recreates paired-issuer MPToken unauthorized");
2174 using namespace jtx;
2175
2176 // Cross-issuer MPT/MPT pool: btc is issued by gw, eth by gw2, and both
2177 // require authorization. Alice deposits her entire balance of both and
2178 // deletes the resulting zero-balance MPTokens. When gw claws back its
2179 // own asset (btc), the two-asset withdrawal must recreate both of
2180 // Alice's MPTokens so the pool can pay her the paired asset. The
2181 // recreated MPToken may only be auto-authorized for the clawback
2182 // issuer's own asset (btc); the paired asset's issuer (gw2) never
2183 // consented, so eth must be recreated *unauthorized*, leaving gw2 in
2184 // control of its own token and preserving its RequireAuth guarantee.
2185 Env env(*this, features);
2186 Account const gw{"gateway"};
2187 Account const gw2{"gateway2"};
2188 Account const alice{"alice"};
2189 env.fund(XRP(100'000), gw, gw2, alice);
2190 env.close();
2191
2192 MPTTester btc({
2193 .env = env,
2194 .issuer = gw,
2195 .holders = {alice},
2196 .pay = 10'000,
2197 .flags = tfMPTCanClawback | tfMPTRequireAuth | kMptDexFlags,
2198 .authHolder = true,
2199 });
2200
2201 MPTTester eth({
2202 .env = env,
2203 .issuer = gw2,
2204 .holders = {alice},
2205 .pay = 10'000,
2206 .flags = tfMPTCanClawback | tfMPTRequireAuth | kMptDexFlags,
2207 .authHolder = true,
2208 });
2209
2210 // Alice deposits everything into the pool; her MPT balances drop to 0.
2211 AMM const amm(env, alice, btc(10'000), eth(10'000));
2212 env.close();
2213 BEAST_EXPECT(amm.expectBalances(btc(10'000), eth(10'000), IOUAmount{10'000}));
2214 BEAST_EXPECT(env.balance(alice, btc) == btc(0));
2215 BEAST_EXPECT(env.balance(alice, eth) == eth(0));
2216
2217 // Alice deletes both zero-balance MPTokens to reclaim reserves.
2218 btc.authorize({.account = alice, .flags = tfMPTUnauthorize});
2219 eth.authorize({.account = alice, .flags = tfMPTUnauthorize});
2220 BEAST_EXPECT(!env.le(keylet::mptoken(btc.issuanceID(), alice.id())));
2221 BEAST_EXPECT(!env.le(keylet::mptoken(eth.issuanceID(), alice.id())));
2222
2223 // gw (issuer of btc) claws back part of Alice's btc. This is a
2224 // cross-issuer pool, so tfClawTwoAssets is not permitted: only btc is
2225 // clawed back, while the paired eth is returned to Alice.
2226 env(amm::ammClawback(gw, alice, btc, eth, btc(1'000)));
2227 env.close();
2228
2229 // Both MPTokens were recreated so the withdrawal could pay Alice.
2230 auto const sleBtc = env.le(keylet::mptoken(btc.issuanceID(), alice.id()));
2231 auto const sleEth = env.le(keylet::mptoken(eth.issuanceID(), alice.id()));
2232 BEAST_EXPECT(sleBtc);
2233 BEAST_EXPECT(sleEth);
2234
2235 // The clawback issuer's own asset (btc) may be recreated authorized:
2236 // gw has authority over its own token.
2237 BEAST_EXPECT(sleBtc && sleBtc->isFlag(lsfMPTAuthorized));
2238
2239 // The paired asset (eth) is issued by gw2, who did not sign this
2240 // transaction. It must be recreated *unauthorized* so gw2's RequireAuth
2241 // is not bypassed. This is the core assertion for the cross-issuer fix.
2242 BEAST_EXPECT(sleEth && !sleEth->isFlag(lsfMPTAuthorized));
2243
2244 // The clawback still completed: btc was clawed back (Alice keeps a zero
2245 // btc balance) and the paired eth was delivered into Alice's now
2246 // unauthorized, gw2-gated MPToken (non-zero raw balance).
2247 BEAST_EXPECT(sleBtc && sleBtc->getFieldU64(sfMPTAmount) == 0);
2248 BEAST_EXPECT(sleEth && sleEth->getFieldU64(sfMPTAmount) > 0);
2249 BEAST_EXPECT(amm.ammExists());
2250 }
2251
2252 void
2254 {
2255 // Same as the IOU case, but the paired asset is an MPT alice does not
2256 // hold yet. The reserve check is skipped on the clawback path while
2257 // createMPToken() still runs, so alice's MPToken is created even though
2258 // neither she nor the low-XRP issuer can cover the owner reserve.
2259 testcase("test clawback bypasses recipient reserve (MPT)");
2260 using namespace jtx;
2261
2262 Env env(*this, features);
2263 Account const gw{"gateway"}; // IOU issuer + claw authority, low XRP
2264 Account const gw2{"gateway2"}; // MPT issuer of the paired asset
2265 Account const carol{"carol"};
2266 Account const alice{"alice"};
2267
2268 auto const usd = gw["USD"];
2269 auto const baseFee = env.current()->fees().base;
2270
2271 env.fund(XRP(1'000'000), gw2, carol);
2272 // Low XRP so the legacy issuer-balance check cannot pass.
2273 env.fund(env.current()->fees().accountReserve(0, 1) + baseFee * 10, gw);
2274 // Reserve for the USD trustline and LP token trustline.
2275 env.fund(env.current()->fees().accountReserve(2, 1) + baseFee * 5, alice);
2276 env.close();
2277
2278 env(fset(gw, asfAllowTrustLineClawback));
2279 env.close();
2280
2281 // The paired MPT: transferable so an AMM can hold it, and no
2282 // RequireAuth so createMPToken()'s WeakAuth check passes.
2283 MPT const btc = MPTTester({
2284 .env = env,
2285 .issuer = gw2,
2286 .holders = {carol},
2287 .pay = 1'000'000,
2288 .flags = kMptDexFlags,
2289 });
2290
2291 env.trust(usd(1'000'000), carol);
2292 env(pay(gw, carol, usd(100'000)));
2293 env.close();
2294 AMM amm(env, carol, usd(1'000), btc(1'000), Ter(tesSUCCESS));
2295 env.close();
2296
2297 // alice holds a USD trustline and LP tokens, but no BTC MPToken.
2298 env.trust(usd(100'000), alice);
2299 env(pay(gw, alice, usd(1'000)));
2300 env.close();
2301 amm.deposit(alice, usd(100));
2302
2303 BEAST_EXPECT(env.ownerCount(alice) == 2);
2304 BEAST_EXPECT(!env.le(keylet::mptoken(btc.issuanceID, alice.id())));
2305
2306 // AMMWithdraw still enforces the reserve check.
2307 amm.withdrawAll(alice, std::nullopt, Ter(tecINSUFFICIENT_RESERVE));
2308 BEAST_EXPECT(!env.le(keylet::mptoken(btc.issuanceID, alice.id())));
2309 BEAST_EXPECT(env.ownerCount(alice) == 2);
2310 // alice cannot afford a third owner object.
2311 BEAST_EXPECT(env.balance(alice) < STAmount(env.current()->fees().accountReserve(3, 1)));
2312
2313 if (features[fixCleanup3_4_0])
2314 {
2315 // Reserve check skipped; the paired BTC returns to alice on a
2316 // newly created MPToken.
2317 env(amm::ammClawback(gw, alice, usd, btc, usd(10)), Ter(tesSUCCESS));
2318 env.close();
2319
2320 BEAST_EXPECT(env.le(keylet::mptoken(btc.issuanceID, alice.id())));
2321 BEAST_EXPECT(env.balance(alice, btc) > btc(0));
2322 BEAST_EXPECT(env.ownerCount(alice) == 3);
2323 }
2324 else
2325 {
2326 // Legacy path: the check runs against max(issuer, holder) XRP,
2327 // neither of which covers a third owner object.
2328 env(amm::ammClawback(gw, alice, usd, btc, usd(10)), Ter(tecINSUFFICIENT_RESERVE));
2329 env.close();
2330
2331 BEAST_EXPECT(!env.le(keylet::mptoken(btc.issuanceID, alice.id())));
2332 BEAST_EXPECT(env.ownerCount(alice) == 2);
2333 }
2334 }
2335
2336 void
2337 run() override
2338 {
2339 FeatureBitset const all{jtx::testableAmendments() | fixAMMClawbackRounding};
2340
2341 testInvalidRequest(all);
2344 testAMMClawbackAmount(all - fixCleanup3_4_0);
2346 testAMMClawbackAmountRoundsToZero(all - fixCleanup3_4_0);
2347 testAMMClawbackAll(all);
2357 testLastHolderLPTokenBalance(all - fixAMMv1_3 - fixAMMClawbackRounding);
2359 all - fixAMMv1_3 - fixAMMClawbackRounding - featureSingleAssetVault -
2360 featureLendingProtocol);
2361 testLastHolderLPTokenBalance(all - fixAMMClawbackRounding);
2362 testClawAssetCheck(all);
2364 testClawbackBypassesReserve(all - fixCleanup3_4_0);
2365 }
2366};
2367
2368BEAST_DEFINE_TESTSUITE(AMMClawbackMPT, app, xrpl);
2369
2370} // namespace xrpl::test
A testsuite class.
Definition suite.h:52
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
std::string asString() const
Returns the unquoted string value.
Floating point representation of amounts with high dynamic range.
Definition IOUAmount.h:26
A currency issued by an account.
Definition Issue.h:18
void testAMMClawbackIssuesEachOther(FeatureBitset features)
void testAMMClawbackAmount(FeatureBitset features)
void testAMMClawbackAmountRoundsToZero(FeatureBitset features)
void testClawbackAfterDeletingMPTokens(FeatureBitset features)
void testClawbackBypassesReserve(FeatureBitset features)
void testClawbackCreatesMissingMPToken(FeatureBitset features)
void testInvalidRequest(FeatureBitset features)
void testAssetFrozenOrLocked(FeatureBitset features)
void testAMMClawbackAllSameIssuer(FeatureBitset features)
void testClawbackCrossIssuerPairedAssetAuth(FeatureBitset features)
void testAMMClawbackAmountSameIssuer(FeatureBitset features)
void testLastHolderLPTokenBalance(FeatureBitset features)
void testSingleDepositAndClawback(FeatureBitset features)
static bool largeMantissa(FeatureBitset const &features)
void testClawAssetCheck(FeatureBitset features)
void run() override
Runs the suite.
void testAMMClawbackAll(FeatureBitset features)
void testFeatureDisabled(FeatureBitset features)
Convenience class to test AMM functionality.
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
AccountID id() const
Returns the Account ID.
A balance matches.
Definition balance.h:24
A transaction testing environment.
Definition Env.h:161
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:133
SLE::const_pointer le(Account const &account) const
Return an account root.
Definition Env.cpp:311
std::uint32_t ownerCount(Account const &account) const
Return the number of objects owned by an account.
Definition Env.cpp:266
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:323
PrettyAmount limit(Account const &account, Issue const &issue) const
Returns the IOU limit on an account.
Definition Env.cpp:254
PrettyAmount balance(Account const &account) const
Returns the XRP balance on an account.
Definition Env.cpp:201
void disableFeature(UInt256 const feature)
Definition Env.cpp:717
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
Definition Env.cpp:354
void enableFeature(UInt256 const feature)
Definition Env.cpp:709
void require(Args const &... args)
Check a set of requirements.
Definition Env.h:766
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:377
Match set account flags.
Definition flags.h:119
Test helper for creating, mutating, and asserting MPT and confidential MPT ledger state.
Definition mpt.h:512
Converts to MPT Issue or STAmount.
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition ter.h:18
Set the flags on a JTx.
Definition txflags.h:14
T make_unique(T... args)
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition Indexes.cpp:547
json::Value ammClawback(Account const &issuer, Account const &holder, Asset const &asset, Asset const &asset2, std::optional< STAmount > const &amount)
Definition AMM.cpp:912
auto const kMptDexFlags
Definition mpt.h:47
json::Value claw(Account const &account, STAmount const &amount, std::optional< Account > const &mptHolder)
Definition trust.cpp:51
json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
Definition pay.cpp:14
void testHelper2TokensMix(TTester &&tester)
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
FeatureBitset testableAmendments()
Definition Env.h:92
json::Value trust(Account const &account, STAmount const &amount, std::uint32_t flags)
Modify a trust line.
Definition trust.cpp:18
PrettyAmount drops(Integer i)
Returns an XRP PrettyAmount, which is trivially convertible to STAmount.
json::Value getAccountLines(Env &env, AccountID const &acctId)
json::Value fset(Account const &account, std::uint32_t on, std::uint32_t off=0)
Add and/or remove flag.
Definition flags.cpp:15
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
constexpr XRPAmount
Convert XRP to drops (integral types).
Definition TxTest.h:53
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ terNO_AMM
Definition TER.h:229
@ terNO_ACCOUNT
Definition TER.h:219
std::expected< bool, TER > isOnlyLiquidityProvider(ReadView const &view, Issue const &ammIssue, AccountID const &lpAccount)
Return true if the Liquidity Provider is the only AMM provider, false otherwise.
STAmount amountFromString(Asset const &asset, std::string const &amount)
MPTID makeMptID(std::uint32_t const sequence, AccountID const &account)
Definition Indexes.cpp:219
@ temINVALID_FLAG
Definition TER.h:99
@ temMALFORMED
Definition TER.h:75
@ temDISABLED
Definition TER.h:102
@ temBAD_AMOUNT
Definition TER.h:77
TERSubset< CanCvtToTER > TER
Definition TER.h:657
@ tecPSEUDO_ACCOUNT
Definition TER.h:373
@ tecAMM_FAILED
Definition TER.h:341
@ tecNO_AUTH
Definition TER.h:311
@ tecINTERNAL
Definition TER.h:321
@ tecAMM_BALANCE
Definition TER.h:340
@ tecAMM_ACCOUNT
Definition TER.h:345
@ tecINSUFFICIENT_RESERVE
Definition TER.h:318
@ tecNO_PERMISSION
Definition TER.h:316
@ tesSUCCESS
Definition TER.h:253