xrpld
Loading...
Searching...
No Matches
VaultScale_test.cpp
1#include <test/app/vault/VaultTestBase.h>
2#include <test/jtx/Account.h>
3#include <test/jtx/Env.h>
4#include <test/jtx/TestHelpers.h>
5#include <test/jtx/amount.h>
6#include <test/jtx/fee.h>
7#include <test/jtx/flags.h>
8#include <test/jtx/mpt.h>
9#include <test/jtx/pay.h>
10#include <test/jtx/sig.h>
11#include <test/jtx/ter.h>
12#include <test/jtx/utility.h>
13#include <test/jtx/vault.h>
14
15#include <xrpl/basics/Number.h>
16#include <xrpl/beast/unit_test/suite.h>
17#include <xrpl/beast/utility/Journal.h>
18#include <xrpl/core/ServiceRegistry.h>
19#include <xrpl/json/json_forwards.h>
20#include <xrpl/json/json_value.h>
21#include <xrpl/ledger/ApplyView.h>
22#include <xrpl/ledger/OpenView.h>
23#include <xrpl/ledger/Sandbox.h>
24#include <xrpl/protocol/Asset.h>
25#include <xrpl/protocol/Feature.h>
26#include <xrpl/protocol/Indexes.h>
27#include <xrpl/protocol/Issue.h>
28#include <xrpl/protocol/Keylet.h>
29#include <xrpl/protocol/MPTIssue.h>
30#include <xrpl/protocol/SField.h>
31#include <xrpl/protocol/STAmount.h>
32#include <xrpl/protocol/SeqProxy.h>
33#include <xrpl/protocol/SystemParameters.h>
34#include <xrpl/protocol/TER.h>
35#include <xrpl/protocol/TxFlags.h>
36#include <xrpl/protocol/UintTypes.h>
37#include <xrpl/protocol/Units.h>
38#include <xrpl/protocol/XRPAmount.h>
39
40#include <cstdint>
41#include <exception>
42#include <functional>
43#include <limits>
44#include <string>
45#include <tuple>
46#include <utility>
47
48namespace xrpl {
49
51{
52 // The IOU precision-boundary bugs in this suite probe the STAmount
53 // 16-digit mantissa cliff (~1e16). FixedPrecision's Open-zone cap
54 // (9e(15-Scale)) makes that value unreachable at any Scale, so these
55 // scenarios cannot be reproduced under V1.2 by construction.
56 FeatureBitset const cashBasisAll_{all_ - featureLendingProtocolV1_2};
57
58private:
59 void
61 {
62 using namespace test::jtx;
63
64 struct Data
65 {
66 Account const& owner;
67 Account const& issuer;
68 Account const& depositor;
69 Account const& vaultAccount;
70 MPTIssue shares;
71 PrettyAsset const& share;
72 Vault& vault;
74 Issue assets;
75 PrettyAsset const& asset;
76 std::function<bool(std::function<bool(SLE&, SLE&)>)> peek;
77 };
78
79 auto const testCase =
80 [&, this](std::uint8_t scale, std::function<void(Env & env, Data data)> test) {
81 // These scale-focused tests build an open-ended vault and
82 // exercise deposit/withdraw/clawback (with one test also
83 // attaching a loan broker). featureLendingProtocolV1_1 adds a
84 // closed-ended vault gate on LoanBrokerSet::preclaim and is
85 // orthogonal to what this suite asserts, so strip it here.
86 Env env{*this, cashBasisAll_ - featureLendingProtocolV1_1};
87 Account const owner{"owner"};
88 Account const issuer{"issuer"};
89 Account const depositor{"depositor"};
90 Vault vault{env};
91 env.fund(XRP(1000), issuer, owner, depositor);
92 env(fset(issuer, asfAllowTrustLineClawback));
93 env.close();
94
95 PrettyAsset const asset = issuer["IOU"];
96 env.trust(asset(1000), owner);
97 env.trust(asset(1000), depositor);
98 env(pay(issuer, owner, asset(200)));
99 env(pay(issuer, depositor, asset(200)));
100 env.close();
101
102 auto [tx, keylet] = vault.create({.owner = owner, .asset = asset});
103 tx[sfScale] = scale;
104 env(tx);
105
106 auto const [vaultAccount, issuanceId] =
108 auto const vault = env.le(keylet);
109 return {Account("vault", vault->at(sfAccount)), vault->at(sfShareMPTID)};
110 }(keylet);
111 MPTIssue const shares(issuanceId);
112 env.memoize(vaultAccount);
113
114 auto const peek = [keylet, &env, this](std::function<bool(SLE&, SLE&)> fn) -> bool {
115 return env.app().getOpenLedger().modify(
116 [&](OpenView& view, beast::Journal j) -> bool {
117 Sandbox sb(&view, TapNone);
118 auto const vault = sb.peek(keylet::vault(keylet.key));
119 if (!BEAST_EXPECT(vault))
120 return false;
121 auto const shares =
122 sb.peek(keylet::mptokenIssuance(vault->at(sfShareMPTID)));
123 if (!BEAST_EXPECT(shares))
124 return false;
125 if (fn(*vault, *shares))
126 {
127 sb.update(vault);
128 sb.update(shares);
129 sb.apply(view);
130 return true;
131 }
132 return false;
133 });
134 };
135
136 test(
137 env,
138 {
139 .owner = owner,
140 .issuer = issuer,
141 .depositor = depositor,
142 .vaultAccount = vaultAccount,
143 .shares = shares,
144 .share = PrettyAsset(shares),
145 .vault = vault,
146 .keylet = keylet,
147 .assets = asset.raw().get<Issue>(),
148 .asset = asset,
149 .peek = peek,
150 });
151 };
152
153 testCase(18, [&, this](Env& env, Data d) {
154 testcase("Scale deposit overflow on first deposit");
155 auto const tx = d.vault.deposit(
156 {.depositor = d.depositor, .id = d.keylet.key, .amount = d.asset(10)});
157 env(tx, Ter{tecPATH_DRY});
158 env.close();
159 });
160
161 testCase(18, [&, this](Env& env, Data d) {
162 testcase("Scale deposit overflow on second deposit");
163
164 {
165 auto const tx = d.vault.deposit(
166 {.depositor = d.depositor, .id = d.keylet.key, .amount = d.asset(5)});
167 env(tx);
168 env.close();
169 }
170
171 {
172 auto const tx = d.vault.deposit(
173 {.depositor = d.depositor, .id = d.keylet.key, .amount = d.asset(10)});
174 env(tx, Ter{tecPATH_DRY});
175 env.close();
176 }
177 });
178
179 testCase(18, [&, this](Env& env, Data d) {
180 testcase("Scale deposit overflow on total shares");
181
182 {
183 auto const tx = d.vault.deposit(
184 {.depositor = d.depositor, .id = d.keylet.key, .amount = d.asset(5)});
185 env(tx);
186 env.close();
187 }
188
189 {
190 auto const tx = d.vault.deposit(
191 {.depositor = d.depositor, .id = d.keylet.key, .amount = d.asset(5)});
192 env(tx, Ter{tecPATH_DRY});
193 env.close();
194 }
195 });
196
197 testCase(1, [&, this](Env& env, Data d) {
198 testcase("Scale deposit exact");
199
200 auto const start = env.balance(d.depositor, d.assets).number();
201 auto const tx = d.vault.deposit(
202 {.depositor = d.depositor, .id = d.keylet.key, .amount = d.asset(1)});
203 env(tx);
204 env.close();
205 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(10));
206 BEAST_EXPECT(env.balance(d.depositor, d.assets) == STAmount(d.asset, start - 1));
207 });
208
209 testCase(1, [&, this](Env& env, Data d) {
210 testcase("Scale deposit insignificant amount");
211
212 auto const tx = d.vault.deposit({
213 .depositor = d.depositor,
214 .id = d.keylet.key,
215 .amount = STAmount(d.asset, Number(9, -2)),
216 });
217 env(tx, Ter{tecPRECISION_LOSS});
218 });
219
220 testCase(1, [&, this](Env& env, Data d) {
221 testcase("Scale deposit exact, using full precision");
222
223 auto const start = env.balance(d.depositor, d.assets).number();
224 auto const tx = d.vault.deposit({
225 .depositor = d.depositor,
226 .id = d.keylet.key,
227 .amount = STAmount(d.asset, Number(15, -1)),
228 });
229 env(tx);
230 env.close();
231 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(15));
232 BEAST_EXPECT(
233 env.balance(d.depositor, d.assets) == STAmount(d.asset, start - Number(15, -1)));
234 });
235
236 testCase(1, [&, this](Env& env, Data d) {
237 testcase("Scale deposit exact, truncating from .5");
238
239 auto const start = env.balance(d.depositor, d.assets).number();
240 // Each of the cases below will transfer exactly 1.2 IOU to the
241 // vault and receive 12 shares in exchange
242 {
243 auto const tx = d.vault.deposit({
244 .depositor = d.depositor,
245 .id = d.keylet.key,
246 .amount = STAmount(d.asset, Number(125, -2)),
247 });
248 env(tx);
249 env.close();
250 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(12));
251 BEAST_EXPECT(
252 env.balance(d.depositor, d.assets) ==
253 STAmount(d.asset, start - Number(12, -1)));
254 }
255
256 {
257 auto const tx = d.vault.deposit({
258 .depositor = d.depositor,
259 .id = d.keylet.key,
260 .amount = STAmount(d.asset, Number(1201, -3)),
261 });
262 env(tx);
263 env.close();
264 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(24));
265 BEAST_EXPECT(
266 env.balance(d.depositor, d.assets) ==
267 STAmount(d.asset, start - Number(24, -1)));
268 }
269
270 {
271 auto const tx = d.vault.deposit({
272 .depositor = d.depositor,
273 .id = d.keylet.key,
274 .amount = STAmount(d.asset, Number(1299, -3)),
275 });
276 env(tx);
277 env.close();
278 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(36));
279 BEAST_EXPECT(
280 env.balance(d.depositor, d.assets) ==
281 STAmount(d.asset, start - Number(36, -1)));
282 }
283 });
284
285 testCase(1, [&, this](Env& env, Data d) {
286 testcase("Scale deposit exact, truncating from .01");
287
288 auto const start = env.balance(d.depositor, d.assets).number();
289 // round to 12
290 auto const tx = d.vault.deposit({
291 .depositor = d.depositor,
292 .id = d.keylet.key,
293 .amount = STAmount(d.asset, Number(1201, -3)),
294 });
295 env(tx);
296 env.close();
297 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(12));
298 BEAST_EXPECT(
299 env.balance(d.depositor, d.assets) == STAmount(d.asset, start - Number(12, -1)));
300
301 {
302 // round to 6
303 auto const tx = d.vault.deposit({
304 .depositor = d.depositor,
305 .id = d.keylet.key,
306 .amount = STAmount(d.asset, Number(69, -2)),
307 });
308 env(tx);
309 env.close();
310 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(18));
311 BEAST_EXPECT(
312 env.balance(d.depositor, d.assets) ==
313 STAmount(d.asset, start - Number(18, -1)));
314 }
315 });
316
317 testCase(1, [&, this](Env& env, Data d) {
318 testcase("Scale deposit exact, truncating from .99");
319
320 auto const start = env.balance(d.depositor, d.assets).number();
321 // round to 12
322 auto const tx = d.vault.deposit({
323 .depositor = d.depositor,
324 .id = d.keylet.key,
325 .amount = STAmount(d.asset, Number(1299, -3)),
326 });
327 env(tx);
328 env.close();
329 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(12));
330 BEAST_EXPECT(
331 env.balance(d.depositor, d.assets) == STAmount(d.asset, start - Number(12, -1)));
332
333 {
334 // round to 6
335 auto const tx = d.vault.deposit({
336 .depositor = d.depositor,
337 .id = d.keylet.key,
338 .amount = STAmount(d.asset, Number(62, -2)),
339 });
340 env(tx);
341 env.close();
342 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(18));
343 BEAST_EXPECT(
344 env.balance(d.depositor, d.assets) ==
345 STAmount(d.asset, start - Number(18, -1)));
346 }
347 });
348
349 testCase(1, [&, this](Env& env, Data d) {
350 // initial setup: deposit 100 IOU, receive 1000 shares
351 auto const start = env.balance(d.depositor, d.assets).number();
352 auto tx = d.vault.deposit({
353 .depositor = d.depositor,
354 .id = d.keylet.key,
355 .amount = STAmount(d.asset, Number(100, 0)),
356 });
357 env(tx);
358 env.close();
359 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(1000));
360 BEAST_EXPECT(
361 env.balance(d.depositor, d.assets) == STAmount(d.asset, start - Number(100, 0)));
362 BEAST_EXPECT(
363 env.balance(d.vaultAccount, d.assets) == STAmount(d.asset, Number(100, 0)));
364 BEAST_EXPECT(
365 env.balance(d.vaultAccount, d.shares) == STAmount(d.share, Number(-1000, 0)));
366
367 {
368 testcase("Scale redeem exact");
369 // sharesToAssetsWithdraw:
370 // assets = assetsTotal * (shares / sharesTotal)
371 // assets = 100 * 100 / 1000 = 100 * 0.1 = 10
372
373 auto const start = env.balance(d.depositor, d.assets).number();
374 auto const tx = d.vault.withdraw({
375 .depositor = d.depositor,
376 .id = d.keylet.key,
377 .amount = STAmount(d.share, Number(100, 0)),
378 });
379 env(tx);
380 env.close();
381 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(900));
382 BEAST_EXPECT(
383 env.balance(d.depositor, d.assets) == STAmount(d.asset, start + Number(10, 0)));
384 BEAST_EXPECT(
385 env.balance(d.vaultAccount, d.assets) == STAmount(d.asset, Number(90, 0)));
386 BEAST_EXPECT(
387 env.balance(d.vaultAccount, d.shares) == STAmount(d.share, Number(-900, 0)));
388 }
389
390 {
391 testcase("Scale redeem with rounding");
392 // sharesToAssetsWithdraw:
393 // assets = assetsTotal * (shares / sharesTotal)
394 // assets = 90 * 25 / 900 = 90 * 0.02777... = 2.5
395
396 auto const start = env.balance(d.depositor, d.assets).number();
397 d.peek([](SLE& vault, auto&) -> bool {
398 vault[sfAssetsAvailable] = Number(1);
399 return true;
400 });
401
402 // Note, this transaction fails first (because of above change
403 // in the open ledger) but then succeeds when the ledger is
404 // closed (because a modification like above is not persistent),
405 // which is why the checks below are expected to pass.
406 auto const tx = d.vault.withdraw({
407 .depositor = d.depositor,
408 .id = d.keylet.key,
409 .amount = STAmount(d.share, Number(25, 0)),
410 });
411 env(tx, Ter{tecINSUFFICIENT_FUNDS});
412 env.close();
413 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(900 - 25));
414 BEAST_EXPECT(
415 env.balance(d.depositor, d.assets) ==
416 STAmount(d.asset, start + Number(25, -1)));
417 BEAST_EXPECT(
418 env.balance(d.vaultAccount, d.assets) ==
419 STAmount(d.asset, Number(900 - 25, -1)));
420 BEAST_EXPECT(
421 env.balance(d.vaultAccount, d.shares) ==
422 STAmount(d.share, -Number(900 - 25, 0)));
423 }
424
425 {
426 testcase("Scale redeem exact");
427 // sharesToAssetsWithdraw:
428 // assets = assetsTotal * (shares / sharesTotal)
429 // assets = 87.5 * 21 / 875 = 87.5 * 0.024 = 2.1
430
431 auto const start = env.balance(d.depositor, d.assets).number();
432
433 tx = d.vault.withdraw({
434 .depositor = d.depositor,
435 .id = d.keylet.key,
436 .amount = STAmount(d.share, Number(21, 0)),
437 });
438 env(tx);
439 env.close();
440 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(875 - 21));
441 BEAST_EXPECT(
442 env.balance(d.depositor, d.assets) ==
443 STAmount(d.asset, start + Number(21, -1)));
444 BEAST_EXPECT(
445 env.balance(d.vaultAccount, d.assets) ==
446 STAmount(d.asset, Number(875 - 21, -1)));
447 BEAST_EXPECT(
448 env.balance(d.vaultAccount, d.shares) ==
449 STAmount(d.share, -Number(875 - 21, 0)));
450 }
451
452 {
453 testcase("Scale redeem rest");
454 auto const rest = env.balance(d.depositor, d.shares).number();
455
456 tx = d.vault.withdraw({
457 .depositor = d.depositor,
458 .id = d.keylet.key,
459 .amount = STAmount(d.share, rest),
460 });
461 env(tx);
462 env.close();
463 BEAST_EXPECT(env.balance(d.depositor, d.shares).number() == 0);
464 BEAST_EXPECT(env.balance(d.vaultAccount, d.assets).number() == 0);
465 BEAST_EXPECT(env.balance(d.vaultAccount, d.shares).number() == 0);
466 }
467 });
468
469 testCase(18, [&, this](Env& env, Data d) {
470 testcase("Scale withdraw overflow");
471
472 {
473 auto const tx = d.vault.deposit(
474 {.depositor = d.depositor, .id = d.keylet.key, .amount = d.asset(5)});
475 env(tx);
476 env.close();
477 }
478
479 {
480 auto const tx = d.vault.withdraw({
481 .depositor = d.depositor,
482 .id = d.keylet.key,
483 .amount = STAmount(d.asset, Number(10, 0)),
484 });
485 env(tx, Ter{tecPATH_DRY});
486 env.close();
487 }
488 });
489
490 testCase(1, [&, this](Env& env, Data d) {
491 // initial setup: deposit 100 IOU, receive 1000 shares
492 auto const start = env.balance(d.depositor, d.assets).number();
493 auto tx = d.vault.deposit({
494 .depositor = d.depositor,
495 .id = d.keylet.key,
496 .amount = STAmount(d.asset, Number(100, 0)),
497 });
498 env(tx);
499 env.close();
500 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(1000));
501 BEAST_EXPECT(
502 env.balance(d.depositor, d.assets) == STAmount(d.asset, start - Number(100, 0)));
503 BEAST_EXPECT(
504 env.balance(d.vaultAccount, d.assets) == STAmount(d.asset, Number(100, 0)));
505 BEAST_EXPECT(
506 env.balance(d.vaultAccount, d.shares) == STAmount(d.share, Number(-1000, 0)));
507
508 {
509 testcase("Scale withdraw exact");
510 // assetsToSharesWithdraw:
511 // shares = sharesTotal * (assets / assetsTotal)
512 // shares = 1000 * 10 / 100 = 1000 * 0.1 = 100
513 // sharesToAssetsWithdraw:
514 // assets = assetsTotal * (shares / sharesTotal)
515 // assets = 100 * 100 / 1000 = 100 * 0.1 = 10
516
517 auto const start = env.balance(d.depositor, d.assets).number();
518 auto const tx = d.vault.withdraw({
519 .depositor = d.depositor,
520 .id = d.keylet.key,
521 .amount = STAmount(d.asset, Number(10, 0)),
522 });
523 env(tx);
524 env.close();
525 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(900));
526 BEAST_EXPECT(
527 env.balance(d.depositor, d.assets) == STAmount(d.asset, start + Number(10, 0)));
528 BEAST_EXPECT(
529 env.balance(d.vaultAccount, d.assets) == STAmount(d.asset, Number(90, 0)));
530 BEAST_EXPECT(
531 env.balance(d.vaultAccount, d.shares) == STAmount(d.share, Number(-900, 0)));
532 }
533
534 {
535 testcase("Scale withdraw insignificant amount");
536 auto const tx = d.vault.withdraw({
537 .depositor = d.depositor,
538 .id = d.keylet.key,
539 .amount = STAmount(d.asset, Number(4, -2)),
540 });
541 env(tx, Ter{tecPRECISION_LOSS});
542 }
543
544 {
545 testcase("Scale withdraw with rounding assets");
546 // assetsToSharesWithdraw:
547 // shares = sharesTotal * (assets / assetsTotal)
548 // shares = 900 * 2.5 / 90 = 900 * 0.02777... = 25
549 // sharesToAssetsWithdraw:
550 // assets = assetsTotal * (shares / sharesTotal)
551 // assets = 90 * 25 / 900 = 90 * 0.02777... = 2.5
552
553 auto const start = env.balance(d.depositor, d.assets).number();
554 d.peek([](SLE& vault, auto&) -> bool {
555 vault[sfAssetsAvailable] = Number(1);
556 return true;
557 });
558
559 // Note, this transaction fails first (because of above change
560 // in the open ledger) but then succeeds when the ledger is
561 // closed (because a modification like above is not persistent),
562 // which is why the checks below are expected to pass.
563 auto const tx = d.vault.withdraw({
564 .depositor = d.depositor,
565 .id = d.keylet.key,
566 .amount = STAmount(d.asset, Number(25, -1)),
567 });
568 env(tx, Ter{tecINSUFFICIENT_FUNDS});
569 env.close();
570 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(900 - 25));
571 BEAST_EXPECT(
572 env.balance(d.depositor, d.assets) ==
573 STAmount(d.asset, start + Number(25, -1)));
574 BEAST_EXPECT(
575 env.balance(d.vaultAccount, d.assets) ==
576 STAmount(d.asset, Number(900 - 25, -1)));
577 BEAST_EXPECT(
578 env.balance(d.vaultAccount, d.shares) ==
579 STAmount(d.share, -Number(900 - 25, 0)));
580 }
581
582 {
583 testcase("Scale withdraw with rounding shares up (truncated post-fixCleanup3_4_0)");
584 // Pre-fixCleanup3_4_0:
585 // shares = round(875 * 3.75 / 87.5) = 38
586 // assets = 87.5 * 38 / 875 = 3.8 > 3.75 requested.
587 // Post-fixCleanup3_4_0:
588 // shares = floor(37.5) = 37
589 // assets = 87.5 * 37 / 875 = 3.7 <= 3.75 requested.
590
591 auto const start = env.balance(d.depositor, d.assets).number();
592 auto const tx = d.vault.withdraw({
593 .depositor = d.depositor,
594 .id = d.keylet.key,
595 .amount = STAmount(d.asset, Number(375, -2)),
596 });
597 env(tx);
598 env.close();
599 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(875 - 37));
600 BEAST_EXPECT(
601 env.balance(d.depositor, d.assets) ==
602 STAmount(d.asset, start + Number(37, -1)));
603 BEAST_EXPECT(
604 env.balance(d.vaultAccount, d.assets) ==
605 STAmount(d.asset, Number(875 - 37, -1)));
606 BEAST_EXPECT(
607 env.balance(d.vaultAccount, d.shares) ==
608 STAmount(d.share, -Number(875 - 37, 0)));
609 }
610
611 {
612 testcase("Scale withdraw with rounding shares down");
613 // Chained state: 838 shares outstanding, 83.8 assets.
614 // shares = floor(838 * 3.72 / 83.8) = floor(37.199...) = 37
615 // assets = 83.8 * 37 / 838 = 3.7 <= 3.72 requested.
616
617 auto const start = env.balance(d.depositor, d.assets).number();
618 auto const tx = d.vault.withdraw({
619 .depositor = d.depositor,
620 .id = d.keylet.key,
621 .amount = STAmount(d.asset, Number(372, -2)),
622 });
623 env(tx);
624 env.close();
625 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(838 - 37));
626 BEAST_EXPECT(
627 env.balance(d.depositor, d.assets) ==
628 STAmount(d.asset, start + Number(37, -1)));
629 BEAST_EXPECT(
630 env.balance(d.vaultAccount, d.assets) ==
631 STAmount(d.asset, Number(838 - 37, -1)));
632 BEAST_EXPECT(
633 env.balance(d.vaultAccount, d.shares) ==
634 STAmount(d.share, -Number(838 - 37, 0)));
635 }
636
637 {
638 testcase("Scale withdraw tiny amount rejected post-fixCleanup3_4_0");
639 // Chained state: 801 shares outstanding, 80.1 assets.
640 // shares = floor(801 * 0.09 / 80.1) = floor(0.9) = 0
641 // Zero shares => tecPRECISION_LOSS. State is unchanged.
642
643 auto const start = env.balance(d.depositor, d.assets).number();
644 auto const tx = d.vault.withdraw({
645 .depositor = d.depositor,
646 .id = d.keylet.key,
647 .amount = STAmount(d.asset, Number(9, -2)),
648 });
649 env(tx, Ter{tecPRECISION_LOSS});
650 env.close();
651 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(801));
652 BEAST_EXPECT(env.balance(d.depositor, d.assets) == STAmount(d.asset, start));
653 BEAST_EXPECT(
654 env.balance(d.vaultAccount, d.assets) == STAmount(d.asset, Number(801, -1)));
655 BEAST_EXPECT(
656 env.balance(d.vaultAccount, d.shares) == STAmount(d.share, -Number(801, 0)));
657 }
658
659 {
660 testcase("Scale withdraw rest");
661 auto const rest = env.balance(d.vaultAccount, d.assets).number();
662
663 tx = d.vault.withdraw({
664 .depositor = d.depositor,
665 .id = d.keylet.key,
666 .amount = STAmount(d.asset, rest),
667 });
668 env(tx);
669 env.close();
670 BEAST_EXPECT(env.balance(d.depositor, d.shares).number() == 0);
671 BEAST_EXPECT(env.balance(d.vaultAccount, d.assets).number() == 0);
672 BEAST_EXPECT(env.balance(d.vaultAccount, d.shares).number() == 0);
673 }
674 });
675
676 testCase(18, [&, this](Env& env, Data d) {
677 testcase("Scale clawback overflow");
678
679 {
680 auto const tx = d.vault.deposit(
681 {.depositor = d.depositor, .id = d.keylet.key, .amount = d.asset(5)});
682 env(tx);
683 env.close();
684 }
685
686 {
687 auto const tx = d.vault.clawback({
688 .issuer = d.issuer,
689 .id = d.keylet.key,
690 .holder = d.depositor,
691 .amount = STAmount(d.asset, Number(10, 0)),
692 });
693 env(tx, Ter{tecPATH_DRY});
694 env.close();
695 }
696 });
697
698 testCase(1, [&, this](Env& env, Data d) {
699 // initial setup: deposit 100 IOU, receive 1000 shares
700 auto const start = env.balance(d.depositor, d.assets).number();
701 auto tx = d.vault.deposit({
702 .depositor = d.depositor,
703 .id = d.keylet.key,
704 .amount = STAmount(d.asset, Number(100, 0)),
705 });
706 env(tx);
707 env.close();
708 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(1000));
709 BEAST_EXPECT(
710 env.balance(d.depositor, d.assets) == STAmount(d.asset, start - Number(100, 0)));
711 BEAST_EXPECT(
712 env.balance(d.vaultAccount, d.assets) == STAmount(d.asset, Number(100, 0)));
713 BEAST_EXPECT(
714 env.balance(d.vaultAccount, d.shares) == STAmount(d.share, -Number(1000, 0)));
715 {
716 testcase("Scale clawback exact");
717 // assetsToSharesWithdraw:
718 // shares = sharesTotal * (assets / assetsTotal)
719 // shares = 1000 * 10 / 100 = 1000 * 0.1 = 100
720 // sharesToAssetsWithdraw:
721 // assets = assetsTotal * (shares / sharesTotal)
722 // assets = 100 * 100 / 1000 = 100 * 0.1 = 10
723
724 auto const start = env.balance(d.depositor, d.assets).number();
725 auto const tx = d.vault.clawback({
726 .issuer = d.issuer,
727 .id = d.keylet.key,
728 .holder = d.depositor,
729 .amount = STAmount(d.asset, Number(10, 0)),
730 });
731 env(tx);
732 env.close();
733 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(900));
734 BEAST_EXPECT(env.balance(d.depositor, d.assets) == STAmount(d.asset, start));
735 BEAST_EXPECT(
736 env.balance(d.vaultAccount, d.assets) == STAmount(d.asset, Number(90, 0)));
737 BEAST_EXPECT(
738 env.balance(d.vaultAccount, d.shares) == STAmount(d.share, -Number(900, 0)));
739 }
740
741 {
742 testcase("Scale clawback insignificant amount");
743 auto const tx = d.vault.clawback({
744 .issuer = d.issuer,
745 .id = d.keylet.key,
746 .holder = d.depositor,
747 .amount = STAmount(d.asset, Number(4, -2)),
748 });
749 env(tx, Ter{tecPRECISION_LOSS});
750 }
751
752 {
753 testcase("Scale clawback with rounding assets");
754 // assetsToSharesWithdraw:
755 // shares = sharesTotal * (assets / assetsTotal)
756 // shares = 900 * 2.5 / 90 = 900 * 0.02777... = 25
757 // sharesToAssetsWithdraw:
758 // assets = assetsTotal * (shares / sharesTotal)
759 // assets = 90 * 25 / 900 = 90 * 0.02777... = 2.5
760
761 auto const start = env.balance(d.depositor, d.assets).number();
762 auto const tx = d.vault.clawback({
763 .issuer = d.issuer,
764 .id = d.keylet.key,
765 .holder = d.depositor,
766 .amount = STAmount(d.asset, Number(25, -1)),
767 });
768 env(tx);
769 env.close();
770 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(900 - 25));
771 BEAST_EXPECT(env.balance(d.depositor, d.assets) == STAmount(d.asset, start));
772 BEAST_EXPECT(
773 env.balance(d.vaultAccount, d.assets) ==
774 STAmount(d.asset, Number(900 - 25, -1)));
775 BEAST_EXPECT(
776 env.balance(d.vaultAccount, d.shares) ==
777 STAmount(d.share, -Number(900 - 25, 0)));
778 }
779
780 {
781 testcase("Scale clawback with rounding shares up (truncated post-fixCleanup3_4_0)");
782 // Pre-fixCleanup3_4_0:
783 // shares = round(875 * 3.75 / 87.5) = 38
784 // assets = 87.5 * 38 / 875 = 3.8 > 3.75 requested.
785 // Post-fixCleanup3_4_0:
786 // shares = floor(37.5) = 37
787 // assets = 87.5 * 37 / 875 = 3.7 <= 3.75 requested.
788
789 auto const start = env.balance(d.depositor, d.assets).number();
790 auto const tx = d.vault.clawback({
791 .issuer = d.issuer,
792 .id = d.keylet.key,
793 .holder = d.depositor,
794 .amount = STAmount(d.asset, Number(375, -2)),
795 });
796 env(tx);
797 env.close();
798 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(875 - 37));
799 BEAST_EXPECT(env.balance(d.depositor, d.assets) == STAmount(d.asset, start));
800 BEAST_EXPECT(
801 env.balance(d.vaultAccount, d.assets) ==
802 STAmount(d.asset, Number(875 - 37, -1)));
803 BEAST_EXPECT(
804 env.balance(d.vaultAccount, d.shares) ==
805 STAmount(d.share, -Number(875 - 37, 0)));
806 }
807
808 {
809 testcase("Scale clawback with rounding shares down");
810 // Chained state: 838 shares outstanding, 83.8 assets.
811 // shares = floor(838 * 3.72 / 83.8) = floor(37.199...) = 37
812 // assets = 83.8 * 37 / 838 = 3.7 <= 3.72 requested.
813
814 auto const start = env.balance(d.depositor, d.assets).number();
815 auto const tx = d.vault.clawback({
816 .issuer = d.issuer,
817 .id = d.keylet.key,
818 .holder = d.depositor,
819 .amount = STAmount(d.asset, Number(372, -2)),
820 });
821 env(tx);
822 env.close();
823 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(838 - 37));
824 BEAST_EXPECT(env.balance(d.depositor, d.assets) == STAmount(d.asset, start));
825 BEAST_EXPECT(
826 env.balance(d.vaultAccount, d.assets) ==
827 STAmount(d.asset, Number(838 - 37, -1)));
828 BEAST_EXPECT(
829 env.balance(d.vaultAccount, d.shares) ==
830 STAmount(d.share, -Number(838 - 37, 0)));
831 }
832
833 {
834 testcase("Scale clawback tiny amount rejected post-fixCleanup3_4_0");
835 // Chained state: 801 shares outstanding, 80.1 assets.
836 // shares = floor(801 * 0.09 / 80.1) = floor(0.9) = 0
837 // Zero shares => tecPRECISION_LOSS. State is unchanged.
838
839 auto const start = env.balance(d.depositor, d.assets).number();
840 auto const tx = d.vault.clawback({
841 .issuer = d.issuer,
842 .id = d.keylet.key,
843 .holder = d.depositor,
844 .amount = STAmount(d.asset, Number(9, -2)),
845 });
846 env(tx, Ter{tecPRECISION_LOSS});
847 env.close();
848 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(801));
849 BEAST_EXPECT(env.balance(d.depositor, d.assets) == STAmount(d.asset, start));
850 BEAST_EXPECT(
851 env.balance(d.vaultAccount, d.assets) == STAmount(d.asset, Number(801, -1)));
852 BEAST_EXPECT(
853 env.balance(d.vaultAccount, d.shares) == STAmount(d.share, -Number(801, 0)));
854 }
855
856 {
857 testcase("Scale clawback rest");
858 auto const rest = env.balance(d.vaultAccount, d.assets).number();
859 d.peek([](SLE& vault, auto&) -> bool {
860 vault[sfAssetsAvailable] = Number(5);
861 return true;
862 });
863
864 // Note, this transaction yields two different results:
865 // * in the open ledger, with AssetsAvailable = 5
866 // * when the ledger is closed with unmodified AssetsAvailable
867 // because a modification like above is not persistent.
868 tx = d.vault.clawback({
869 .issuer = d.issuer,
870 .id = d.keylet.key,
871 .holder = d.depositor,
872 .amount = STAmount(d.asset, rest),
873 });
874 env(tx);
875 env.close();
876 BEAST_EXPECT(env.balance(d.depositor, d.shares).number() == 0);
877 BEAST_EXPECT(env.balance(d.vaultAccount, d.assets).number() == 0);
878 BEAST_EXPECT(env.balance(d.vaultAccount, d.shares).number() == 0);
879 }
880 });
881
882 // Non-1:1 ratio (scale=1, 10:1 shares:assets) with an outstanding loan.
883 // Deposit 100 IOU → 1000 shares. Borrow 40 → assetsAvailable=60.
884 // Clawback 80 IOU → clamped to 60, then share math uses truncation.
885 testCase(1, [&, this](Env& env, Data d) {
886 using namespace loan_broker;
887 using namespace loan;
888
889 testcase("Scale clawback clamped with outstanding loan");
890
891 auto tx = d.vault.deposit({
892 .depositor = d.depositor,
893 .id = d.keylet.key,
894 .amount = STAmount(d.asset, Number(100, 0)),
895 });
896 env(tx);
897 env.close();
898 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(1000));
899
900 // Create a loan broker backed by this vault
901 auto const brokerKeylet =
902 keylet::loanBroker(d.owner.id(), SeqProxy::rawSequence(env.seq(d.owner)));
903 env(set(d.owner, d.keylet.key));
904 env.close();
905
906 // Borrow 40: assetsAvailable=60, assetsTotal=100
907 env(set(d.depositor, brokerKeylet.key, STAmount(d.asset, Number(40, 0))),
908 loan::kInterestRate(TenthBips32(0)),
909 kGracePeriod(60),
910 kPaymentInterval(120),
911 kPaymentTotal(10),
912 Sig(sfCounterpartySignature, d.owner),
913 Fee(env.current()->fees().base * 2),
914 Ter(tesSUCCESS));
915 env.close();
916
917 {
918 auto const sle = env.le(d.keylet);
919 BEAST_EXPECT(sle->at(sfAssetsAvailable) == STAmount(d.asset, Number(60, 0)));
920 BEAST_EXPECT(sle->at(sfAssetsTotal) == STAmount(d.asset, Number(100, 0)));
921 }
922
923 // Request 80 IOU clawback — clamped to assetsAvailable (60)
924 // With scale=1 (10:1), 60 assets = 600 shares destroyed
925 tx = d.vault.clawback({
926 .issuer = d.issuer,
927 .id = d.keylet.key,
928 .holder = d.depositor,
929 .amount = STAmount(d.asset, Number(80, 0)),
930 });
931 env(tx, Ter(tesSUCCESS));
932 env.close();
933
934 {
935 auto const sle = env.le(d.keylet);
936 BEAST_EXPECT(sle != nullptr);
937 BEAST_EXPECT(sle->at(sfAssetsAvailable) == STAmount(d.asset, Number(0, 0)));
938 BEAST_EXPECT(sle->at(sfAssetsTotal) == STAmount(d.asset, Number(40, 0)));
939
940 // 600 of 1000 shares destroyed, 400 remain
941 BEAST_EXPECT(env.balance(d.depositor, d.shares) == d.share(400));
942 }
943 });
944
945 // peek() writes the open ledger only; do not close() before le().
946 auto seedLargeTotal = [](Env& env,
947 Data& d,
948 Number const& total,
949 Number const& available,
950 std::uint64_t outstanding) {
951 auto const tx = d.vault.deposit({
952 .depositor = d.depositor,
953 .id = d.keylet.key,
954 .amount = STAmount(d.asset, Number(100, 0)),
955 });
956 env(tx);
957 env.close();
958 d.peek([&](SLE& vault, SLE& shares) -> bool {
959 vault[sfAssetsTotal] = total;
960 vault[sfAssetsAvailable] = available;
961 shares[sfOutstandingAmount] = outstanding;
962 return true;
963 });
964 };
965
966 auto expectVault = [this](
967 Env& env,
968 Data const& d,
969 Number const& total,
970 Number const& available,
971 STAmount const& shareBalance) {
972 auto const sle = env.le(d.keylet);
973 BEAST_EXPECT(sle != nullptr);
974 BEAST_EXPECT(sle->at(sfAssetsTotal) == total);
975 BEAST_EXPECT(sle->at(sfAssetsAvailable) == available);
976 BEAST_EXPECT(env.balance(d.depositor, d.shares) == shareBalance);
977 };
978
979 // T-6 is exact after the decade; recover 6.
980 testCase(0, [&, this](Env& env, Data d) {
981 testcase("Scale clawback uses posterior scale across decade boundary");
982
983 Number const midGridTotal{10000000000000005ll};
984 Number const available{6};
985 seedLargeTotal(env, d, midGridTotal, available, 10000000000000005ull);
986
987 auto const tx =
988 d.vault.clawback({.issuer = d.issuer, .id = d.keylet.key, .holder = d.depositor});
989 env(tx, Ter(tesSUCCESS));
990 expectVault(env, d, midGridTotal - available, Number(0), d.share(94));
991 });
992
993 // T stays on the 10-asset grid; 6 is unrepresentable.
994 testCase(0, [&, this](Env& env, Data d) {
995 testcase("Scale clawback rejects amount below posterior scale");
996
997 Number const midGridTotal{12345678901234567ll};
998 Number const available{6};
999 seedLargeTotal(env, d, midGridTotal, available, 12345678901234567ull);
1000
1001 auto const tx =
1002 d.vault.clawback({.issuer = d.issuer, .id = d.keylet.key, .holder = d.depositor});
1003 env(tx, Ter(tecPRECISION_LOSS));
1004 expectVault(env, d, midGridTotal, available, d.share(100));
1005 });
1006
1007 // A recovery larger than the anterior ULP also lands exactly on the finer posterior grid.
1008 testCase(0, [&, this](Env& env, Data d) {
1009 testcase("Scale clawback preserves exact posterior amount");
1010
1011 Number const midGridTotal{10000000000000005ll};
1012 Number const available{15};
1013 seedLargeTotal(env, d, midGridTotal, available, 10000000000000005ull);
1014
1015 auto const tx =
1016 d.vault.clawback({.issuer = d.issuer, .id = d.keylet.key, .holder = d.depositor});
1017 env(tx, Ter(tesSUCCESS));
1018 expectVault(env, d, midGridTotal - available, Number(0), d.share(85));
1019 });
1020
1021 testCase(0, [&, this](Env& env, Data d) {
1022 testcase("Scale deposit rejects amount below posterior scale");
1023
1024 Number const midGridTotal{10000000000000005ll};
1025 Number const available{100};
1026 seedLargeTotal(env, d, midGridTotal, available, 10000000000000005ull);
1027
1028 auto const assetsBefore = env.balance(d.depositor, d.assets);
1029 auto const tx = d.vault.deposit({
1030 .depositor = d.depositor,
1031 .id = d.keylet.key,
1032 .amount = STAmount(d.asset, Number(6)),
1033 });
1034 env(tx, Ter(tecPRECISION_LOSS));
1035 expectVault(env, d, midGridTotal, available, d.share(100));
1036 BEAST_EXPECT(env.balance(d.depositor, d.assets) == assetsBefore);
1037 });
1038
1039 testCase(0, [&, this](Env& env, Data d) {
1040 testcase("Scale withdraw uses posterior scale across decade boundary");
1041
1042 Number const midGridTotal{10000000000000005ll};
1043 Number const available{100};
1044 seedLargeTotal(env, d, midGridTotal, available, 10000000000000005ull);
1045
1046 auto const assetsBefore = env.balance(d.depositor, d.assets);
1047 auto const tx = d.vault.withdraw({
1048 .depositor = d.depositor,
1049 .id = d.keylet.key,
1050 .amount = STAmount(d.share, Number(15)),
1051 });
1052 env(tx, Ter(tesSUCCESS));
1053 expectVault(env, d, midGridTotal - Number(15), Number(85), d.share(85));
1054 BEAST_EXPECT(
1055 env.balance(d.depositor, d.assets) ==
1056 STAmount(d.asset, assetsBefore.number() + Number(15)));
1057 });
1058 }
1059
1060 void
1062 {
1063 bool const withFix = features[fixCleanup3_5_0];
1064 testcase(
1065 std::string("Assets Maximum (") + (withFix ? "post" : "pre") + "-fixCleanup3_5_0)");
1066
1067 using namespace test::jtx;
1068
1069 Env env{*this, features};
1070 Account const owner{"owner"};
1071 Account const issuer{"issuer"};
1072
1073 Vault const vault{env};
1074 env.fund(XRP(1'000'000), issuer, owner);
1075 env.close();
1076
1078 BEAST_EXPECT(maxInt64 == "9223372036854775807");
1079
1080 auto const maxInt64Plus1 = std::to_string(
1082 BEAST_EXPECT(maxInt64Plus1 == "9223372036854775808");
1083
1084 // Naming things is hard
1085 auto const maxInt64Plus2 = std::to_string(
1087 BEAST_EXPECT(maxInt64Plus2 == "9223372036854775809");
1088
1089 auto const initialXRP = to_string(kInitialXrp);
1090 BEAST_EXPECT(initialXRP == "100000000000000000");
1091
1092 auto const initialXRPPlus1 = to_string(kInitialXrp + 1);
1093 BEAST_EXPECT(initialXRPPlus1 == "100000000000000001");
1094
1095 // Without the fix, rounding an out-of-range AssetsMaximum to the vault
1096 // asset throws.
1097 TER const outOfRangeErr = withFix ? TER{temMALFORMED} : TER{tefEXCEPTION};
1098
1099 {
1100 testcase("Assets Maximum: XRP");
1101
1102 PrettyAsset const xrpAsset = xrpIssue();
1103
1104 auto [tx, keylet] = vault.create({.owner = owner, .asset = xrpAsset});
1105 tx[sfData] = "4D65746144617461";
1106
1107 tx[sfAssetsMaximum] = maxInt64;
1108 env(tx, Ter(outOfRangeErr));
1109 env.close();
1110
1111 tx[sfAssetsMaximum] = initialXRPPlus1;
1112 env(tx, Ter(outOfRangeErr));
1113 env.close();
1114
1115 tx[sfAssetsMaximum] = initialXRP;
1116 env(tx);
1117 env.close();
1118
1119 // There are several parse failures expected in this function, so just disable it once.
1120 env.setParseFailureExpected(true);
1121 try
1122 {
1123 tx[sfAssetsMaximum] = maxInt64Plus1;
1124 env(tx, Ter(tefEXCEPTION));
1125 env.close();
1126 // should throw in parser
1127 fail();
1128 }
1129 catch (std::exception const& e)
1130 {
1131 BEAST_EXPECT(
1132 std::string(e.what()) ==
1133 "invalidParamsField 'tx_json.AssetsMaximum' has invalid data.");
1134 }
1135
1136 try
1137 {
1138 tx[sfAssetsMaximum] = maxInt64Plus2;
1139 env(tx, Ter(tefEXCEPTION));
1140 // should throw in parser
1141 fail();
1142 }
1143 catch (std::exception const& e)
1144 {
1145 BEAST_EXPECT(
1146 std::string(e.what()) ==
1147 "invalidParamsField 'tx_json.AssetsMaximum' has invalid data.");
1148 }
1149
1150 auto const newKeylet = keylet::vault(owner.id(), SeqProxy::rawSequence(env.seq(owner)));
1151 try
1152 {
1153 auto const insertAt = maxInt64Plus2.size() - 3;
1154 auto const decimalTest = maxInt64Plus2.substr(0, insertAt) + "." +
1155 maxInt64Plus2.substr(insertAt); // (max int64+2) / 1000
1156 BEAST_EXPECT(decimalTest == "9223372036854775.809");
1157 tx[sfAssetsMaximum] = decimalTest;
1158 env(tx);
1159 // should throw in parser
1160 fail();
1161 }
1162 catch (std::exception const& e)
1163 {
1164 BEAST_EXPECT(
1165 std::string(e.what()) ==
1166 "invalidParamsField 'tx_json.AssetsMaximum' has invalid data.");
1167 }
1168
1169 auto const vaultSle = env.le(newKeylet);
1170 BEAST_EXPECT(!vaultSle);
1171 }
1172
1173 {
1174 testcase("Assets Maximum: MPT");
1175
1176 PrettyAsset const mptAsset = [&] {
1177 MPTTester mptt{env, issuer, kMptInitNoFund};
1178 mptt.create({.flags = tfMPTCanClawback | tfMPTCanTransfer | tfMPTCanLock});
1179 env.close();
1180 PrettyAsset const mptAsset = mptt["MPT"];
1181 mptt.authorize({.account = owner});
1182 env.close();
1183 return mptAsset;
1184 }();
1185
1186 env(pay(issuer, owner, mptAsset(100'000)));
1187 env.close();
1188
1189 auto [tx, keylet] = vault.create({.owner = owner, .asset = mptAsset});
1190 tx[sfData] = "4D65746144617461";
1191
1192 tx[sfAssetsMaximum] = maxInt64;
1193 env(tx);
1194 env.close();
1195
1196 tx[sfAssetsMaximum] = initialXRPPlus1;
1197 env(tx);
1198 env.close();
1199
1200 tx[sfAssetsMaximum] = initialXRP;
1201 env(tx);
1202 env.close();
1203
1204 try
1205 {
1206 tx[sfAssetsMaximum] = maxInt64Plus2;
1207 env(tx, Ter(tefEXCEPTION));
1208 // should throw in parser
1209 fail();
1210 }
1211 catch (std::exception const& e)
1212 {
1213 BEAST_EXPECT(
1214 std::string(e.what()) ==
1215 "invalidParamsField 'tx_json.AssetsMaximum' has invalid data.");
1216 }
1217
1218 auto const newKeylet = keylet::vault(owner.id(), SeqProxy::rawSequence(env.seq(owner)));
1219 try
1220 {
1221 auto const insertAt = maxInt64Plus2.size() - 1;
1222 auto const decimalTest = maxInt64Plus2.substr(0, insertAt) + "." +
1223 maxInt64Plus2.substr(insertAt); // (max int64+2) / 10
1224 BEAST_EXPECT(decimalTest == "922337203685477580.9");
1225 tx[sfAssetsMaximum] = decimalTest;
1226 env(tx);
1227 // should throw in parser
1228 fail();
1229 }
1230 catch (std::exception const& e)
1231 {
1232 BEAST_EXPECT(
1233 std::string(e.what()) ==
1234 "invalidParamsField 'tx_json.AssetsMaximum' has invalid data.");
1235 }
1236
1237 auto const vaultSle = env.le(newKeylet);
1238 BEAST_EXPECT(!vaultSle);
1239 }
1240
1241 {
1242 testcase("Assets Maximum: IOU");
1243
1244 // Almost anything goes with IOUs
1245 PrettyAsset const iouAsset = issuer["IOU"];
1246 env.trust(iouAsset(1000), owner);
1247 env(pay(issuer, owner, iouAsset(200)));
1248 env.close();
1249
1250 auto [tx, keylet] = vault.create({.owner = owner, .asset = iouAsset});
1251 tx[sfData] = "4D65746144617461";
1252
1253 tx[sfAssetsMaximum] = maxInt64;
1254 env(tx);
1255 env.close();
1256
1257 tx[sfAssetsMaximum] = initialXRPPlus1;
1258 env(tx);
1259 env.close();
1260
1261 tx[sfAssetsMaximum] = initialXRP;
1262 env(tx);
1263 env.close();
1264
1265 // Since several tests are expected to have parser failures, leave this flag set for the
1266 // remainder of this function.
1267 env.setParseFailureExpected(true);
1268 try
1269 {
1270 tx[sfAssetsMaximum] = maxInt64Plus2;
1271 env(tx);
1272 // should throw in parser
1273 fail();
1274 }
1275 catch (std::exception const& e)
1276 {
1277 BEAST_EXPECT(
1278 std::string(e.what()) ==
1279 "invalidParamsField 'tx_json.AssetsMaximum' has invalid data.");
1280 }
1281
1282 tx[sfAssetsMaximum] = "1000000000000000e80";
1283 env.close();
1284
1285 tx[sfAssetsMaximum] = "1000000000000000e-96";
1286 env.close();
1287
1288 // These values will be rounded to 15 significant digits
1289 {
1290 auto const newKeylet =
1291 keylet::vault(owner.id(), SeqProxy::rawSequence(env.seq(owner)));
1292 try
1293 {
1294 auto const insertAt = maxInt64Plus2.size() - 1;
1295 auto const decimalTest = maxInt64Plus2.substr(0, insertAt) + "." +
1296 maxInt64Plus2.substr(insertAt); // (max int64+2) / 10
1297 BEAST_EXPECT(decimalTest == "922337203685477580.9");
1298 tx[sfAssetsMaximum] = decimalTest;
1299 env(tx);
1300 // should throw in parser
1301 fail();
1302 }
1303 catch (std::exception const& e)
1304 {
1305 BEAST_EXPECT(
1306 std::string(e.what()) ==
1307 "invalidParamsField 'tx_json.AssetsMaximum' has invalid data.");
1308 }
1309
1310 auto const vaultSle = env.le(newKeylet);
1311 BEAST_EXPECT(!vaultSle);
1312 }
1313 {
1314 tx[sfAssetsMaximum] = "9223372036854775807e40"; // max int64 * 10^40
1315 auto const newKeylet =
1316 keylet::vault(owner.id(), SeqProxy::rawSequence(env.seq(owner)));
1317 env(tx);
1318 env.close();
1319
1320 auto const vaultSle = env.le(newKeylet);
1321 if (!BEAST_EXPECT(vaultSle))
1322 return;
1323
1324 BEAST_EXPECT(
1325 (vaultSle->at(sfAssetsMaximum) ==
1326 Number{9223372036854776, 43, Number::Normalized{}}));
1327 }
1328 {
1329 tx[sfAssetsMaximum] = "9223372036854775807e-40"; // max int64 * 10^-40
1330 auto const newKeylet =
1331 keylet::vault(owner.id(), SeqProxy::rawSequence(env.seq(owner)));
1332 env(tx);
1333 env.close();
1334
1335 auto const vaultSle = env.le(newKeylet);
1336 if (!BEAST_EXPECT(vaultSle))
1337 return;
1338
1339 BEAST_EXPECT(
1340 (vaultSle->at(sfAssetsMaximum) ==
1341 Number{9223372036854776, -37, Number::Normalized{}}));
1342 }
1343 {
1344 tx[sfAssetsMaximum] = "9223372036854775807e-100"; // max int64 * 10^-100
1345 auto const newKeylet =
1346 keylet::vault(owner.id(), SeqProxy::rawSequence(env.seq(owner)));
1347 env(tx);
1348 env.close();
1349
1350 // Field 'AssetsMaximum' may not be explicitly set to default.
1351 auto const vaultSle = env.le(newKeylet);
1352 if (!BEAST_EXPECT(vaultSle))
1353 return;
1354
1355 BEAST_EXPECT(vaultSle->at(sfAssetsMaximum) == kNumZero);
1356 }
1357
1358 // What _can't_ IOUs do?
1359 // 1. Exceed maximum exponent / offset
1360 tx[sfAssetsMaximum] = "1000000000000000e81";
1361 env(tx, Ter(outOfRangeErr));
1362 env.close();
1363
1364 // 2. Mantissa larger than uint64 max
1365 try
1366 {
1367 auto const g = env.getParseFailureGuard(true);
1368 tx[sfAssetsMaximum] = "18446744073709551617e5"; // uint64 max + 1
1369 env(tx);
1370 BEAST_EXPECTS(false, "Expected parse_error for mantissa larger than uint64 max");
1371 }
1372 catch (ParseError const& e)
1373 {
1374 using namespace std::string_literals;
1375 BEAST_EXPECT(
1376 e.what() == "invalidParamsField 'tx_json.AssetsMaximum' has invalid data."s);
1377 }
1378 }
1379 }
1380
1381public:
1382 void
1383 run() override
1384 {
1385 testScaleIOU();
1386 testAssetsMaximum(cashBasisAll_ - fixCleanup3_5_0);
1388 }
1389};
1390
1392
1393} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition suite.h:554
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
constexpr TIss const & get() const
A currency issued by an account.
Definition Issue.h:18
Number is a floating point type that can represent a wide range of values.
Definition Number.h:351
Writable ledger view that accumulates state and tx changes.
Definition OpenView.h:59
Discardable, editable view to a ledger.
Definition Sandbox.h:18
void apply(RawView &to)
Definition Sandbox.h:38
static constexpr SeqProxy rawSequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
Definition SeqProxy.h:62
void run() override
Runs the suite.
void testAssetsMaximum(FeatureBitset features)
FeatureBitset const cashBasisAll_
Shared base for the Vault*_test family under src/test/app/vault/.
FeatureBitset const all_
test::jtx::PrettyAsset PrettyAsset
SLE::pointer peek(Keylet const &k) override
Prepare to modify the SLE associated with key.
void update(SLE::Ref sle) override
Indicate changes to a peeked SLE.
T max(T... args)
Keylet computation functions.
Definition Indexes.h:39
Keylet vault(AccountID const &owner, SeqProxy const &seq) noexcept
Definition Indexes.cpp:565
Keylet loanBroker(AccountID const &owner, SeqProxy const &seq) noexcept
Definition Indexes.cpp:571
Keylet mptokenIssuance(MPTID const &issuanceID) noexcept
Definition Indexes.cpp:541
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
static constexpr Number kNumZero
Definition Number.h:667
bool set(T &target, std::string const &name, Section const &section)
Set a value from a configuration Section If the named value is not found or doesn't parse as a T,...
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:108
int scale(Number const &number, Asset const &asset)
Get the scale of a Number for a given asset.
Definition STAmount.h:819
@ tefEXCEPTION
Definition TER.h:166
TenthBips< std::uint32_t > TenthBips32
Definition Units.h:454
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
STLedgerEntry SLE
BEAST_DEFINE_TESTSUITE_PRIO(NFTokenBaseUtil, app, xrpl, 2)
@ TapNone
Definition ApplyView.h:28
@ temMALFORMED
Definition TER.h:75
TERSubset< CanCvtToTER > TER
Definition TER.h:657
@ tecPATH_DRY
Definition TER.h:305
@ tecINSUFFICIENT_FUNDS
Definition TER.h:336
@ tecPRECISION_LOSS
Definition TER.h:374
constexpr XRPAmount kInitialXrp
Configure the native currency.
@ tesSUCCESS
Definition TER.h:253
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
T to_string(T... args)
T what(T... args)