1#include <test/app/vault/VaultPrecisionFixture.h>
2#include <test/jtx/Env.h>
3#include <test/jtx/amount.h>
4#include <test/jtx/envconfig.h>
5#include <test/jtx/ter.h>
6#include <test/jtx/vault.h>
8#include <xrpl/basics/Number.h>
9#include <xrpl/beast/unit_test/suite.h>
10#include <xrpl/beast/utility/Journal.h>
11#include <xrpl/protocol/Feature.h>
12#include <xrpl/protocol/MPTIssue.h>
13#include <xrpl/protocol/STAmount.h>
14#include <xrpl/protocol/TER.h>
41 bool const fixEnabled = features[fixCleanup3_4_0];
44 (fixEnabled ?
" (fixCleanup3_4_0)" :
" (pre-fix)"));
48 for (
auto const amount : kAmounts)
52 if (!f.asset || !f.broker)
54 BEAST_EXPECT(f.asset && f.broker);
57 auto const& asset = *f.asset;
59 auto const before =
read(env, f);
63 .depositor = f.depositor,
64 .id = f.vaultKeylet.key,
65 .amount = asset(amount).value(),
70 TER const actual = env.
ter();
76 "amount=" +
std::to_string(amount) +
" expected tesSUCCESS, got " +
80 Number const tDelta =
after.assetsTotal - before.assetsTotal;
81 Number const aDelta =
after.assetsAvailable - before.assetsAvailable;
82 Number const requested = asset(amount).number();
84 BEAST_EXPECT(tDelta <= requested);
86 Number const gap = tDelta > aDelta ? tDelta - aDelta : aDelta - tDelta;
93 "amount=" +
std::to_string(amount) +
" expected tecINVARIANT_FAILED, got " +
106 bool const fixEnabled = features[fixCleanup3_4_0];
109 (fixEnabled ?
" (fixCleanup3_4_0)" :
" (pre-fix)"));
124 if (!f.asset || !f.broker)
126 BEAST_EXPECT(f.asset && f.broker);
129 auto const& asset = *f.asset;
134 .depositor = f.depositor,
135 .id = f.vaultKeylet.key,
136 .amount = asset(1'000'000).value(),
141 for (
auto const count : kShareCounts)
143 auto const before =
read(env, f);
144 if (before.sharesTotal < count)
149 {.depositor = f.depositor, .id = f.vaultKeylet.key, .amount = shareAmount}),
153 TER const actual = env.
ter();
159 "shares=" +
std::to_string(count) +
" unexpected invariant failure");
164 Number const tDelta = before.assetsTotal -
after.assetsTotal;
165 Number const pDelta = before.pseudo -
after.pseudo;
166 Number const gap = tDelta > pDelta ? tDelta - pDelta : pDelta - tDelta;
169 BEAST_EXPECT(gap <=
oneUnit(asset, before.assetsTotal));
188 bool const fixEnabled = features[fixCleanup3_4_0];
191 (fixEnabled ?
" (fixCleanup3_4_0)" :
" (pre-fix)"));
197 if (!f.asset || !f.broker)
199 BEAST_EXPECT(f.asset && f.broker);
202 auto const& asset = *f.asset;
209 .depositor = f.depositor,
210 .id = f.vaultKeylet.key,
211 .amount = asset(2'000).value(),
216 for (
auto const amount : kAmounts)
218 auto const before =
read(env, f);
219 if (before.sharesTotal == 0)
224 .id = f.vaultKeylet.key,
225 .holder = f.depositor,
226 .amount = asset(amount).value(),
231 TER const actual = env.
ter();
237 "amount=" +
std::to_string(amount) +
" unexpected invariant failure");
248 env(v.
clawback({.issuer = f.lender, .id = f.vaultKeylet.key, .holder = f.depositor}),
261 bool const fixEnabled = features[fixCleanup3_4_0];
264 (fixEnabled ?
" (fixCleanup3_4_0)" :
" (pre-fix)"));
268 for (
auto const amount : kAmounts)
272 if (!f.asset || !f.broker)
274 BEAST_EXPECT(f.asset && f.broker);
277 auto const& asset = *f.asset;
281 .depositor = f.depositor,
282 .id = f.vaultKeylet.key,
283 .amount = asset(amount).value(),
288 TER const actual = env.
ter();
294 "amount=" +
std::to_string(amount) +
" expected tesSUCCESS, got " +
306 "amount=" +
std::to_string(amount) +
" expected tecINVARIANT_FAILED, got " +
319 bool const fixEnabled = features[fixCleanup3_4_0];
322 (fixEnabled ?
" (fixCleanup3_4_0)" :
" (pre-fix)"));
345 for (
auto const amount : kAmounts)
349 if (!f.asset || !f.broker)
351 BEAST_EXPECT(f.asset && f.broker);
354 auto const& asset = *f.asset;
358 .depositor = f.depositor,
359 .id = f.vaultKeylet.key,
360 .amount = asset(amount).value(),
365 TER const actual = env.
ter();
371 "amount=" +
std::to_string(amount) +
" expected tesSUCCESS, got " +
376 bool const shouldFail =
383 " expected tecINVARIANT_FAILED, got " +
transToken(actual));
400 bool const fixEnabled = features[fixCleanup3_4_0];
403 (fixEnabled ?
" (fixCleanup3_4_0)" :
" (pre-fix)"));
419 for (
auto const amount : kAmounts)
423 if (!f.asset || !f.broker)
425 BEAST_EXPECT(f.asset && f.broker);
428 auto const& asset = *f.asset;
432 .depositor = f.depositor,
433 .id = f.vaultKeylet.key,
434 .amount = asset(amount).value(),
439 TER const actual = env.
ter();
445 "amount=" +
std::to_string(amount) +
" expected tesSUCCESS, got " +
450 bool const shouldFail =
457 " expected tecINVARIANT_FAILED, got " +
transToken(actual));
467 for (
auto const& features : {
all_ - fixCleanup3_4_0,
all_})
TestcaseT testcase
Memberspace for declaring test cases.
Number is a floating point type that can represent a wide range of values.
void testWithdrawBoundaryInvariant(FeatureBitset features)
void testA1DepositMagnitudes(FeatureBitset features)
void testDepositBoundaryInvariant(FeatureBitset features)
void testA3DepositMagnitudes(FeatureBitset features)
void testClawbackBoundaryInvariant(FeatureBitset features)
void testLossInvariantA3(FeatureBitset features)
void run() override
Runs the suite.
static Number oneUnit(Asset const &asset, Number const &assetsTotalAfter)
static Numbers read(jtx::Env const &env, Fixture const &f)
static Fixture setupSingleLoanVault(jtx::Env &env, bool impairAndPaySibling, bool allowClawback=false)
A transaction testing environment.
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
TER ter() const
Return the TER for the last JTx.
Set the expected result code for a JTx The test will fail if the code doesn't match.
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::string transToken(TER code)
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
TERSubset< CanCvtToTER > TER
static json::Value withdraw(WithdrawArgs const &args)
static json::Value clawback(ClawbackArgs const &args)
static json::Value deposit(DepositArgs const &args)