Skip to content

Commit

Permalink
Make PrettyAmount non-template.
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtatcam committed Jul 12, 2024
1 parent 0decffc commit fbc6cbc
Show file tree
Hide file tree
Showing 23 changed files with 154 additions and 258 deletions.
3 changes: 3 additions & 0 deletions include/xrpl/protocol/STEitherAmount.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class STEitherAmount : public STBase, public CountedObject<STEitherAmount>

Json::Value getJson(JsonOptions) const override;

void
setJson(Json::Value&) const;

void
add(Serializer& s) const override;

Expand Down
6 changes: 6 additions & 0 deletions src/libxrpl/protocol/STEitherAmount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ Json::Value STEitherAmount::getJson(JsonOptions) const
[&](auto&& a) { return a.getJson(JsonOptions::none); }, amount_);
}

void
STEitherAmount::setJson(Json::Value& jv) const
{
std::visit([&](auto&& a) { a.setJson(jv); }, amount_);
}

void
STEitherAmount::add(Serializer& s) const
{
Expand Down
7 changes: 3 additions & 4 deletions src/test/app/AMMExtended_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1547,8 +1547,7 @@ struct AMMExtended_test : public jtx::AMMTest
Env env = pathTestEnv();
fund(env, gw, {alice, bob, charlie}, {USD(11)}, Fund::All);
AMM ammCharlie(env, charlie, XRP(10), USD(11));
auto [st, sa, da] =
find_paths(env, alice, bob, USD(-1), XRP(1).value());
auto [st, sa, da] = find_paths(env, alice, bob, USD(-1), XRP(1));
BEAST_EXPECT(sa == XRP(1));
BEAST_EXPECT(equal(da, USD(1)));
if (BEAST_EXPECT(st.size() == 1 && st[0].size() == 1))
Expand All @@ -1566,7 +1565,7 @@ struct AMMExtended_test : public jtx::AMMTest
AMM ammCharlie(env, charlie, XRP(11), USD(10));
env.close();
auto [st, sa, da] =
find_paths(env, alice, bob, drops(-1), USD(1).value());
find_paths(env, alice, bob, drops(-1), USD(1));
BEAST_EXPECT(sa == USD(1));
BEAST_EXPECT(equal(da, XRP(1)));
if (BEAST_EXPECT(st.size() == 1 && st[0].size() == 1))
Expand Down Expand Up @@ -1916,7 +1915,7 @@ struct AMMExtended_test : public jtx::AMMTest
sendmax(EUR(500)),
txflags(tfNoRippleDirect | tfPartialPayment));

auto const carolUSD = env.balance(carol, USD).value();
auto const carolUSD = get<STAmount>(env.balance(carol, USD));
BEAST_EXPECT(carolUSD > USD(0) && carolUSD < USD(50));
}

Expand Down
20 changes: 10 additions & 10 deletions src/test/app/Check_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ class Check_test : public beast::unit_test::suite

Env env{*this, features};

STAmount const startBalance{XRP(1000).value()};
STAmount const startBalance{XRP(1000)};
env.fund(startBalance, gw, alice, bob);

// Note that no trust line has been set up for alice, but alice can
Expand Down Expand Up @@ -321,7 +321,7 @@ class Check_test : public beast::unit_test::suite

Env env{*this, features | disallowIncoming};

STAmount const startBalance{XRP(1000).value()};
STAmount const startBalance{XRP(1000)};
env.fund(startBalance, gw, alice, bob);

/*
Expand Down Expand Up @@ -405,7 +405,7 @@ class Check_test : public beast::unit_test::suite

Env env{*this, features};

STAmount const startBalance{XRP(1000).value()};
STAmount const startBalance{XRP(1000)};
env.fund(startBalance, gw1, gwF, alice, bob);

// Bad fee.
Expand Down Expand Up @@ -587,7 +587,7 @@ class Check_test : public beast::unit_test::suite
Env env{*this, features};

XRPAmount const baseFeeDrops{env.current()->fees().base};
STAmount const startBalance{XRP(300).value()};
STAmount const startBalance{XRP(300)};
env.fund(startBalance, alice, bob);
{
// Basic XRP check.
Expand Down Expand Up @@ -1192,8 +1192,8 @@ class Check_test : public beast::unit_test::suite
double pct,
double amount) {
// Capture bob's and alice's balances so we can test at the end.
STAmount const aliceStart{env.balance(alice, USD.issue()).value()};
STAmount const bobStart{env.balance(bob, USD.issue()).value()};
STAmount const aliceStart{env.balance(alice, USD.issue())};
STAmount const bobStart{env.balance(bob, USD.issue())};

// Set the modified quality.
env(trust(truster, iou(1000)), inOrOut(pct));
Expand All @@ -1217,8 +1217,8 @@ class Check_test : public beast::unit_test::suite
double pct,
double amount) {
// Capture bob's and alice's balances so we can test at the end.
STAmount const aliceStart{env.balance(alice, USD.issue()).value()};
STAmount const bobStart{env.balance(bob, USD.issue()).value()};
STAmount const aliceStart{env.balance(alice, USD.issue())};
STAmount const bobStart{env.balance(bob, USD.issue())};

// Set the modified quality.
env(trust(truster, iou(1000)), inOrOut(pct));
Expand Down Expand Up @@ -1281,7 +1281,7 @@ class Check_test : public beast::unit_test::suite
double max2) {
// Capture alice's balance so we can test at the end. It doesn't
// make any sense to look at the balance of a gateway.
STAmount const aliceStart{env.balance(alice, USD.issue()).value()};
STAmount const aliceStart{env.balance(alice, USD.issue())};

// Set the modified quality.
env(trust(truster, iou(1000)), inOrOut(pct));
Expand Down Expand Up @@ -1314,7 +1314,7 @@ class Check_test : public beast::unit_test::suite
double max2) {
// Capture alice's balance so we can test at the end. It doesn't
// make any sense to look at the balance of the issuer.
STAmount const aliceStart{env.balance(alice, USD.issue()).value()};
STAmount const aliceStart{env.balance(alice, USD.issue())};

// Set the modified quality.
env(trust(truster, iou(1000)), inOrOut(pct));
Expand Down
2 changes: 1 addition & 1 deletion src/test/app/Flow_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ struct Flow_test : public beast::unit_test::suite
sendmax(EUR(500)),
txflags(tfNoRippleDirect | tfPartialPayment));

auto const carolUSD = env.balance(carol, USD).value();
auto const carolUSD = get<STAmount>(env.balance(carol, USD));
BEAST_EXPECT(carolUSD > USD(0) && carolUSD < USD(50));
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/app/MPToken_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ class MPToken_test : public beast::unit_test::suite
env,
alice,
{.holders = {&bob},
.xrpHolders = acctReserve + XRP(1).value().xrp()});
.xrpHolders = acctReserve + get<STAmount>(XRP(1)).xrp()});
mptAlice1.create();

MPTTester mptAlice2(env, alice, {.fund = false});
Expand Down
81 changes: 40 additions & 41 deletions src/test/app/Offer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,12 +485,12 @@ class OfferBaseUtil_test : public beast::unit_test::suite
auto const USD = gw["USD"];
auto const EUR = gw["EUR"];

auto tinyAmount = [&](IOU const& iou) -> PrettyAmount<STAmount> {
auto tinyAmount = [&](IOU const& iou) -> PrettyAmount {
STAmount amt(
iou.issue(),
/*mantissa*/ 1,
/*exponent*/ -81);
return PrettyAmount<STAmount>(amt, iou.account.name());
return PrettyAmount(amt, iou.account.name());
};

// Test offer crossing
Expand Down Expand Up @@ -1876,8 +1876,7 @@ class OfferBaseUtil_test : public beast::unit_test::suite
jrr = ledgerEntryRoot(env, bob);
BEAST_EXPECT(
jrr[jss::node][sfBalance.fieldName] ==
std::to_string(
XRP(10000).value().mantissa() + XRP(250).value().mantissa()));
std::to_string((XRP(10000) + XRP(250)).value().mantissa()));

auto jro = ledgerEntryOffer(env, carol, carolOfferSeq);
BEAST_EXPECT(
Expand Down Expand Up @@ -2300,14 +2299,14 @@ class OfferBaseUtil_test : public beast::unit_test::suite
verifyDefaultTrustline(
jtx::Env& env,
jtx::Account const& account,
jtx::PrettyAmount<STAmount> const& expectBalance)
jtx::PrettyAmount const& expectBalance)
{
auto const sleTrust =
env.le(keylet::line(account.id(), expectBalance.value().issue()));
auto const sleTrust = env.le(
keylet::line(account.id(), get<STAmount>(expectBalance).issue()));
BEAST_EXPECT(sleTrust);
if (sleTrust)
{
Issue const issue = expectBalance.value().issue();
Issue const issue = get<STAmount>(expectBalance).issue();
bool const accountLow = account.id() < issue.account;

STAmount low{issue};
Expand Down Expand Up @@ -2354,16 +2353,16 @@ class OfferBaseUtil_test : public beast::unit_test::suite
enum preTrustType { noPreTrust, gwPreTrust, acctPreTrust };
struct TestData
{
std::string account; // Account operated on
STAmount fundXrp; // Account funded with
int bookAmount; // USD -> XRP offer on the books
preTrustType preTrust; // If true, pre-establish trust line
int offerAmount; // Account offers this much XRP -> USD
TER tec; // Returned tec code
STAmount spentXrp; // Amount removed from fundXrp
PrettyAmount<STAmount> balanceUsd; // Balance on account end
int offers; // Offers on account
int owners; // Owners on account
std::string account; // Account operated on
STAmount fundXrp; // Account funded with
int bookAmount; // USD -> XRP offer on the books
preTrustType preTrust; // If true, pre-establish trust line
int offerAmount; // Account offers this much XRP -> USD
TER tec; // Returned tec code
STAmount spentXrp; // Amount removed from fundXrp
PrettyAmount balanceUsd; // Balance on account end
int offers; // Offers on account
int owners; // Owners on account
};

// clang-format off
Expand Down Expand Up @@ -4034,9 +4033,9 @@ class OfferBaseUtil_test : public beast::unit_test::suite
{
Account acct;
int offers; // offers on account after crossing
PrettyAmount<STAmount> xrp; // final expected after crossing
PrettyAmount<STAmount> btc; // final expected after crossing
PrettyAmount<STAmount> usd; // final expected after crossing
PrettyAmount xrp; // final expected after crossing
PrettyAmount btc; // final expected after crossing
PrettyAmount usd; // final expected after crossing
};
struct TestData
{
Expand All @@ -4047,7 +4046,7 @@ class OfferBaseUtil_test : public beast::unit_test::suite
std::size_t self;
std::size_t leg0;
std::size_t leg1;
PrettyAmount<STAmount> btcStart;
PrettyAmount btcStart;
std::vector<Actor> actors;
};

Expand Down Expand Up @@ -4184,10 +4183,10 @@ class OfferBaseUtil_test : public beast::unit_test::suite
struct Actor
{
Account acct;
int offers; // offers on account after crossing
PrettyAmount<STAmount> xrp; // final expected after crossing
PrettyAmount<STAmount> btc; // final expected after crossing
PrettyAmount<STAmount> usd; // final expected after crossing
int offers; // offers on account after crossing
PrettyAmount xrp; // final expected after crossing
PrettyAmount btc; // final expected after crossing
PrettyAmount usd; // final expected after crossing
};
struct TestData
{
Expand All @@ -4198,7 +4197,7 @@ class OfferBaseUtil_test : public beast::unit_test::suite
std::size_t self;
std::size_t leg0;
std::size_t leg1;
PrettyAmount<STAmount> btcStart;
PrettyAmount btcStart;
std::vector<Actor> actors;
};

Expand Down Expand Up @@ -5142,12 +5141,12 @@ class OfferBaseUtil_test : public beast::unit_test::suite
env(pay(issuer, maker, EUR(1'000)));
env.close();

auto makerUSDBalance = env.balance(maker, USD).value();
auto takerUSDBalance = env.balance(taker, USD).value();
auto makerEURBalance = env.balance(maker, EUR).value();
auto takerEURBalance = env.balance(taker, EUR).value();
auto makerXRPBalance = env.balance(maker, XRP).value();
auto takerXRPBalance = env.balance(taker, XRP).value();
auto makerUSDBalance = get<STAmount>(env.balance(maker, USD));
auto takerUSDBalance = get<STAmount>(env.balance(taker, USD));
auto makerEURBalance = get<STAmount>(env.balance(maker, EUR));
auto takerEURBalance = get<STAmount>(env.balance(taker, EUR));
auto makerXRPBalance = get<STAmount>(env.balance(maker, XRP));
auto takerXRPBalance = get<STAmount>(env.balance(taker, XRP));

// tfFillOrKill, TakerPays must be filled
{
Expand All @@ -5170,8 +5169,8 @@ class OfferBaseUtil_test : public beast::unit_test::suite
{
makerUSDBalance -= USD(100);
takerUSDBalance += USD(100);
makerXRPBalance += XRP(100).value();
takerXRPBalance -= XRP(100).value();
makerXRPBalance += XRP(100);
takerXRPBalance -= XRP(100);
}
BEAST_EXPECT(expectOffers(env, taker, 0));

Expand All @@ -5189,8 +5188,8 @@ class OfferBaseUtil_test : public beast::unit_test::suite
{
makerUSDBalance += USD(100);
takerUSDBalance -= USD(100);
makerXRPBalance -= XRP(100).value();
takerXRPBalance += XRP(100).value();
makerXRPBalance -= XRP(100);
takerXRPBalance += XRP(100);
}
BEAST_EXPECT(expectOffers(env, taker, 0));

Expand Down Expand Up @@ -5225,8 +5224,8 @@ class OfferBaseUtil_test : public beast::unit_test::suite

makerUSDBalance -= USD(101);
takerUSDBalance += USD(101);
makerXRPBalance += XRP(101).value() - txfee(env, 1);
takerXRPBalance -= XRP(101).value() + txfee(env, 1);
makerXRPBalance += XRP(101) - txfee(env, 1);
takerXRPBalance -= XRP(101) + txfee(env, 1);
BEAST_EXPECT(expectOffers(env, taker, 0));

env(offer(maker, USD(101), XRP(101)));
Expand All @@ -5238,8 +5237,8 @@ class OfferBaseUtil_test : public beast::unit_test::suite

makerUSDBalance += USD(101);
takerUSDBalance -= USD(101);
makerXRPBalance -= XRP(101).value() + txfee(env, 1);
takerXRPBalance += XRP(101).value() - txfee(env, 1);
makerXRPBalance -= XRP(101) + txfee(env, 1);
takerXRPBalance += XRP(101) - txfee(env, 1);
BEAST_EXPECT(expectOffers(env, taker, 0));

env(offer(maker, USD(101), EUR(101)));
Expand Down
5 changes: 2 additions & 3 deletions src/test/app/Path_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1246,8 +1246,7 @@ class Path_test : public beast::unit_test::suite
env.close();
env(offer(charlie, XRP(10), USD(10)));
env.close();
auto [st, sa, da] =
find_paths(env, alice, bob, USD(-1), XRP(100).value());
auto [st, sa, da] = find_paths(env, alice, bob, USD(-1), XRP(100));
BEAST_EXPECT(sa == XRP(10));
BEAST_EXPECT(equal(da, USD(10)));
if (BEAST_EXPECT(st.size() == 1 && st[0].size() == 1))
Expand All @@ -1270,7 +1269,7 @@ class Path_test : public beast::unit_test::suite
env(offer(charlie, USD(10), XRP(10)));
env.close();
auto [st, sa, da] =
find_paths(env, alice, bob, drops(-1), USD(100).value());
find_paths(env, alice, bob, drops(-1), USD(100));
BEAST_EXPECT(sa == USD(10));
BEAST_EXPECT(equal(da, XRP(10)));
if (BEAST_EXPECT(st.size() == 1 && st[0].size() == 1))
Expand Down
Loading

0 comments on commit fbc6cbc

Please sign in to comment.