2#include <test/jtx/Account.h>
3#include <test/jtx/Env.h>
4#include <test/jtx/amount.h>
5#include <test/jtx/balance.h>
6#include <test/jtx/fee.h>
7#include <test/jtx/offer.h>
8#include <test/jtx/owners.h>
9#include <test/jtx/pay.h>
10#include <test/jtx/trust.h>
11#include <test/jtx/txflags.h>
13#include <xrpl/beast/unit_test/suite.h>
14#include <xrpl/json/json_value.h>
15#include <xrpl/json/to_string.h>
16#include <xrpl/protocol/ErrorCodes.h>
17#include <xrpl/protocol/Feature.h>
18#include <xrpl/protocol/Quality.h>
19#include <xrpl/protocol/SField.h>
20#include <xrpl/protocol/STAmount.h>
21#include <xrpl/protocol/TxFlags.h>
22#include <xrpl/protocol/jss.h>
36 jvParams[jss::offer][jss::account] = acct.
human();
37 jvParams[jss::offer][jss::seq] = offerSeq;
38 return env.
rpc(
"json",
"ledger_entry",
to_string(jvParams))[jss::result];
46 ledgerOffer[jss::error].
asString() ==
"entryNotFound" &&
48 ledgerOffer[jss::error_message] ==
"Entry not found.");
57 for (
auto [acct, offerSeq] : list)
66 testcase(
"exercise partial cross new XRP/IOU offer Q change");
70 auto const gw =
Account{
"gateway"};
71 auto const alice =
Account{
"alice"};
72 auto const bob =
Account{
"bob"};
73 auto const usd = gw[
"USD"];
79 env.
fund(
XRP(10'000'000), gw, alice, bob);
82 env(
trust(alice, usd(10'000'000)));
83 env(
trust(bob, usd(10'000'000)));
86 env(
pay(gw, bob, usd(10'000'000)));
94 auto const exerciseOfferPair = [
this, &env, &alice, &bob](
96 Amounts const& newOffer) ->
unsigned int {
99 env(
offer(alice, inLedger.in, inLedger.out));
107 env(
offer(bob, newOffer.in, newOffer.out, tfSell),
Fee(bobFee));
113 if (!BEAST_EXPECT(!
offerInLedger(env, alice, aliceOfferSeq)))
121 unsigned int badRate = 1;
126 amountFromJson(sfTakerGets, bobOffer[jss::node][sfTakerGets.jsonName]);
128 amountFromJson(sfTakerPays, bobOffer[jss::node][sfTakerPays.jsonName]);
130 BEAST_EXPECT(reducedTakerPays < newOffer.in);
131 BEAST_EXPECT(reducedTakerGets < newOffer.out);
135 badRate = inLedgerRate > initialRate ? 1 : 0;
144 STAmount const tweakedTakerPays = reducedTakerPays +
drops(1);
147 BEAST_EXPECT(tweakedRate > initialRate);
163 unsigned int blockedCount = 0;
165 mantissaReduce <= 5'000'000'000ull;
166 mantissaReduce += 20'000'000ull)
173 Amounts const aliceOffer{aliceUSD, aliceXRP};
174 blockedCount += exerciseOfferPair(aliceOffer, bobOffer);
179 BEAST_EXPECT(blockedCount == 0);
186 testcase(
"exercise partial cross old XRP/IOU offer Q change");
190 auto const gw =
Account{
"gateway"};
191 auto const alice =
Account{
"alice"};
192 auto const bob =
Account{
"bob"};
193 auto const usd = gw[
"USD"];
198 env.
fund(
XRP(10'000'000), gw, alice, bob);
201 env(
trust(alice, usd(10'000'000)));
202 env(
trust(bob, usd(10'000'000)));
205 env(
pay(gw, alice, usd(10'000'000)));
212 auto const exerciseOfferPair = [
this, &env, &alice, &bob](
214 Amounts const& newOffer) ->
unsigned int {
219 env(
offer(alice, inLedger.in, inLedger.out));
225 env(
offer(bob, newOffer.in, newOffer.out));
239 unsigned int badRate = 1;
244 amountFromJson(sfTakerGets, aliceOffer[jss::node][sfTakerGets.jsonName]);
246 amountFromJson(sfTakerPays, aliceOffer[jss::node][sfTakerPays.jsonName]);
248 BEAST_EXPECT(reducedTakerPays < inLedger.in);
249 BEAST_EXPECT(reducedTakerGets < inLedger.out);
252 badRate = inLedgerRate > initialRate ? 1 : 0;
261 STAmount const tweakedTakerPays = reducedTakerPays +
drops(1);
264 BEAST_EXPECT(tweakedRate > initialRate);
280 unsigned int blockedCount = 0;
282 mantissaReduce <= 4'000'000'000ull;
283 mantissaReduce += 20'000'000ull)
290 Amounts const bobOffer{bobUSD, bobXRP};
292 blockedCount += exerciseOfferPair(aliceOffer, bobOffer);
297 BEAST_EXPECT(blockedCount == 0);
304 testcase(
"exercise underfunded XRP/IOU offer Q change");
309 auto const alice =
Account{
"alice"};
310 auto const bob =
Account{
"bob"};
312 auto const usd = gw[
"USD"];
317 env.
fund(
XRP(10000), alice, bob, gw);
319 env.
trust(usd(1000), alice, bob);
321 int blockedOrderBookCount = 0;
322 for (
STAmount initialBobUSD = usd(0.45); initialBobUSD <= usd(1);
323 initialBobUSD += usd(0.025))
326 env(
pay(gw, bob, initialBobUSD));
345 bool const bobOfferGone = !
offerInLedger(env, bob, bobOfferSeq);
349 if (aliceBalanceUSD.
signum() > 0)
351 BEAST_EXPECT(aliceBalanceUSD == initialBobUSD);
352 BEAST_EXPECT(env.
balance(bob, usd) == usd(0));
353 BEAST_EXPECT(bobOfferGone);
357 if (!bobOfferGone && aliceBalanceUSD.
signum() == 0)
359 ++blockedOrderBookCount;
368 aliceBalance.
signum() > 0)
369 env(
pay(alice, gw, aliceBalance));
372 env(
pay(bob, gw, bobBalance));
380 BEAST_EXPECT(blockedOrderBookCount == 0);
387 testcase(
"exercise underfunded IOU/IOU offer Q change");
392 using namespace std::chrono_literals;
393 auto const alice =
Account{
"alice"};
394 auto const bob =
Account{
"bob"};
397 auto const usd = gw[
"USD"];
398 auto const eur = gw[
"EUR"];
400 STAmount const tinyUSD(usd, 1, -81);
405 env.
fund(
XRP(10000), alice, bob, gw);
407 env.
trust(usd(1000), alice, bob);
408 env.
trust(eur(1000), alice, bob);
410 STAmount const eurOffer(eur, 2957, -76);
411 STAmount const usdOffer(usd, 7109, -76);
413 STAmount const endLoop(usd, 50, -81);
415 int blockedOrderBookCount = 0;
416 for (
STAmount initialBobUSD = tinyUSD; initialBobUSD <= endLoop;
417 initialBobUSD += tinyUSD)
420 env(
pay(gw, bob, initialBobUSD));
421 env(
pay(gw, alice, eur(100)));
426 env(
offer(bob, eurOffer, usdOffer));
432 env(
offer(alice, usdOffer, eurOffer));
437 bool const bobOfferGone = !
offerInLedger(env, bob, bobOfferSeq);
440 if (aliceBalanceUSD.
signum() > 0)
442 BEAST_EXPECT(aliceBalanceUSD == initialBobUSD);
443 BEAST_EXPECT(env.
balance(bob, usd) == usd(0));
444 BEAST_EXPECT(bobOfferGone);
448 if (!bobOfferGone && aliceBalanceUSD.
signum() == 0)
450 ++blockedOrderBookCount;
459 auto const zeroBalance = [&env, &gw](
Account const& acct,
IOU const& iou) {
461 env(
pay(acct, gw, balance));
464 zeroBalance(alice, eur);
465 zeroBalance(alice, usd);
466 zeroBalance(bob, eur);
467 zeroBalance(bob, usd);
473 BEAST_EXPECT(blockedOrderBookCount == 0);
492 testcase(
"exercise tfSell partial cross old XRP/IOU offer Q change");
500 auto const usd = gw[
"USD"];
507 Env env{*
this, features};
508 env.
fund(
XRP(10'000'000), gw, alice, bob, carol);
511 env(
trust(alice, usd(10'000'000)));
512 env(
trust(bob, usd(10'000'000)));
513 env(
trust(carol, usd(10'000'000)));
516 env(
pay(gw, alice, usd(10'000'000)));
517 env(
pay(gw, bob, usd(10'000'000)));
518 env(
pay(gw, carol, usd(10'000'000)));
525 auto const exerciseOfferTrio = [
this, &env, &alice, &bob, &carol, &usd](
526 Amounts const& carolOffer) ->
unsigned int {
529 static Amounts const kAliceInitialOffer(usd(2),
drops(3382562));
530 env(
offer(alice, kAliceInitialOffer.
in, kAliceInitialOffer.
out));
533 env, alice, aliceOfferSeq)[jss::node]))
538 env(
offer(bob, usd(0.97086565812384),
drops(1642020)));
544 env(
offer(carol, carolOffer.in, carolOffer.out),
Txflags(tfSell));
556 env, {{alice, aliceOfferSeq}, {bob, bobOfferSeq}, {carol, carolOfferSeq}});
561 unsigned int badRate = 1;
567 BEAST_EXPECT(aliceReducedOffer.
in < kAliceInitialOffer.
in);
568 BEAST_EXPECT(aliceReducedOffer.
out < kAliceInitialOffer.
out);
570 badRate = inLedgerRate > initialRate ? 1 : 0;
586 BEAST_EXPECT(tweakedRate > initialRate);
593 env, {{alice, aliceOfferSeq}, {bob, bobOfferSeq}, {carol, carolOfferSeq}});
597 static constexpr int kLoopCount = 100;
598 unsigned int blockedCount = 0;
602 for (
unsigned int i = 0; i < kLoopCount; ++i)
605 exerciseOfferTrio(
Amounts(
drops(1642020), usd(1) + increaseGets));
606 increaseGets += step;
615 if (features[fixReducedOffersV2])
617 BEAST_EXPECT(blockedCount == 0);
621 BEAST_EXPECT(blockedCount > 80);
TestcaseT testcase
Memberspace for declaring test cases.
std::string asString() const
Returns the unquoted string value.
Represents the logical ratio of output currency to input currency.
STAmount rate() const
Returns the quality as STAmount.
std::uint64_t mantissa() const noexcept
int signum() const noexcept
bool negative() const noexcept
Asset const & asset() const
int exponent() const noexcept
static auto ledgerEntryOffer(jtx::Env &env, jtx::Account const &acct, std::uint32_t offerSeq)
void testPartialCrossNewXrpIouQChange()
void testSellPartialCrossOldXrpIouQChange()
void testUnderFundedIouIouQChange()
void run() override
Runs the suite.
static bool offerInLedger(jtx::Env &env, jtx::Account const &acct, std::uint32_t offerSeq)
static Amounts jsonOfferToAmounts(json::Value const &json)
void testUnderFundedXrpIouQChange()
static void cleanupOldOffers(jtx::Env &env, std::initializer_list< std::pair< jtx::Account const &, std::uint32_t > > list)
void testPartialCrossOldXrpIouQChange()
Immutable cryptographic account descriptor.
std::string const & human() const
Returns the human readable public key.
A transaction testing environment.
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
std::uint32_t seq(Account const &account) const
Returns the next sequence number on account.
json::Value rpc(unsigned apiVersion, std::unordered_map< std::string, std::string > const &headers, std::string const &cmd, Args &&... args)
Execute an RPC command.
PrettyAmount balance(Account const &account) const
Returns the XRP balance on an account.
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
void require(Args const &... args)
Check a set of requirements.
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Converts to IOU Issue or STAmount.
JSON (JavaScript Object Notation).
json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
json::Value offerCancel(Account const &account, std::uint32_t offerSeq)
Cancel an offer.
XrpT const XRP
Converts to XRP Issue or STAmount.
FeatureBitset testableAmendments()
OwnerCount< ltOFFER > Offers
Match the number of offers in the account's owner directory.
json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
json::Value trust(Account const &account, STAmount const &amount, std::uint32_t flags)
Modify a trust line.
PrettyAmount drops(Integer i)
Returns an XRP PrettyAmount, which is trivially convertible to STAmount.
json::Value rate(Account const &account, double multiplier)
Set a transfer rate.
BEAST_DEFINE_TESTSUITE_PRIO(AccountDelete, app, xrpl, 2)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
TAmounts< STAmount, STAmount > Amounts
std::string to_string(BaseUInt< Bits, Tag > const &a)
STAmount amountFromJson(SField const &name, json::Value const &v)