add transfer rate

This commit is contained in:
Denis Angell
2023-01-24 05:43:35 -05:00
committed by Richard Holland
parent 4438ebb5b7
commit 0133a4136e
6 changed files with 469 additions and 147 deletions

View File

@@ -202,6 +202,15 @@ struct Escrow_test : public beast::unit_test::suite
return STAmount(iou, 0);
}
static Rate
escrowRate(jtx::Env const& env, jtx::Account const& account, uint32_t const& seq)
{
auto const sle = env.le(keylet::escrow(account.id(), seq));
if (sle->isFieldPresent(sfTransferRate))
return ripple::Rate((*sle)[sfTransferRate]);
return Rate{0};
}
void
testEnablement(FeatureBitset features)
{
@@ -210,7 +219,7 @@ struct Escrow_test : public beast::unit_test::suite
using namespace jtx;
using namespace std::chrono;
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), "alice", "bob");
env(escrow("alice", "bob", XRP(1000)), finish_time(env.now() + 1s));
env.close();
@@ -246,7 +255,7 @@ struct Escrow_test : public beast::unit_test::suite
{
testcase("Timing: Finish Only");
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), "alice", "bob");
env.close();
@@ -268,7 +277,7 @@ struct Escrow_test : public beast::unit_test::suite
{
testcase("Timing: Cancel Only");
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), "alice", "bob");
env.close();
@@ -300,7 +309,7 @@ struct Escrow_test : public beast::unit_test::suite
{
testcase("Timing: Finish and Cancel -> Finish");
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), "alice", "bob");
env.close();
@@ -334,7 +343,7 @@ struct Escrow_test : public beast::unit_test::suite
{
testcase("Timing: Finish and Cancel -> Cancel");
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), "alice", "bob");
env.close();
@@ -383,7 +392,7 @@ struct Escrow_test : public beast::unit_test::suite
using namespace jtx;
using namespace std::chrono;
Env env(*this, features);
Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
@@ -432,7 +441,7 @@ struct Escrow_test : public beast::unit_test::suite
{
// Ignore the "asfDisallowXRP" account flag, which we should
// have been doing before.
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), "bob", "george");
env(fset("george", asfDisallowXRP));
@@ -483,7 +492,7 @@ struct Escrow_test : public beast::unit_test::suite
{
testcase("Implied Finish Time (with fix1571)");
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), "alice", "bob", "carol");
env.close();
@@ -517,7 +526,7 @@ struct Escrow_test : public beast::unit_test::suite
using namespace jtx;
using namespace std::chrono;
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), "alice", "bob");
env.close();
@@ -651,7 +660,7 @@ struct Escrow_test : public beast::unit_test::suite
{
// Unconditional
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), "alice", "bob");
auto const seq = env.seq("alice");
env(escrow("alice", "alice", XRP(1000)),
@@ -675,7 +684,7 @@ struct Escrow_test : public beast::unit_test::suite
// Unconditionally pay from Alice to Bob. Zelda (neither source nor
// destination) signs all cancels and finishes. This shows that
// Escrow will make a payment to Bob with no intervention from Bob.
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), "alice", "bob", "zelda");
auto const seq = env.seq("alice");
env(escrow("alice", "bob", XRP(1000)), finish_time(env.now() + 5s));
@@ -700,7 +709,7 @@ struct Escrow_test : public beast::unit_test::suite
}
{
// Bob sets DepositAuth so only Bob can finish the escrow.
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), "alice", "bob", "zelda");
env(fset("bob", asfDepositAuth));
@@ -738,7 +747,7 @@ struct Escrow_test : public beast::unit_test::suite
{
// Bob sets DepositAuth but preauthorizes Zelda, so Zelda can
// finish the escrow.
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), "alice", "bob", "zelda");
env(fset("bob", asfDepositAuth));
@@ -765,7 +774,7 @@ struct Escrow_test : public beast::unit_test::suite
}
{
// Conditional
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), "alice", "bob");
auto const seq = env.seq("alice");
env(escrow("alice", "alice", XRP(1000)),
@@ -806,7 +815,7 @@ struct Escrow_test : public beast::unit_test::suite
}
{
// Self-escrowed conditional with DepositAuth.
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), "alice", "bob");
auto const seq = env.seq("alice");
@@ -842,7 +851,7 @@ struct Escrow_test : public beast::unit_test::suite
}
{
// Self-escrowed conditional with DepositAuth and DepositPreauth.
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), "alice", "bob", "zelda");
auto const seq = env.seq("alice");
@@ -893,7 +902,7 @@ struct Escrow_test : public beast::unit_test::suite
using namespace std::chrono;
{ // Test cryptoconditions
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), "alice", "bob", "carol");
auto const seq = env.seq("alice");
BEAST_EXPECT((*env.le("alice"))[sfOwnerCount] == 0);
@@ -966,7 +975,7 @@ struct Escrow_test : public beast::unit_test::suite
env(cancel("bob", "carol", 1), ter(tecNO_TARGET));
}
{ // Test cancel when condition is present
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), "alice", "bob", "carol");
auto const seq = env.seq("alice");
BEAST_EXPECT((*env.le("alice"))[sfOwnerCount] == 0);
@@ -982,7 +991,7 @@ struct Escrow_test : public beast::unit_test::suite
BEAST_EXPECT(!env.le(keylet::escrow(Account("alice").id(), seq)));
}
{
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), "alice", "bob", "carol");
env.close();
auto const seq = env.seq("alice");
@@ -1004,7 +1013,7 @@ struct Escrow_test : public beast::unit_test::suite
env.require(balance("carol", XRP(5000)));
}
{ // Test long & short conditions during creation
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), "alice", "bob", "carol");
std::vector<std::uint8_t> v;
@@ -1061,7 +1070,7 @@ struct Escrow_test : public beast::unit_test::suite
env.require(balance("carol", XRP(6000)));
}
{ // Test long and short conditions & fulfillments during finish
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), "alice", "bob", "carol");
std::vector<std::uint8_t> cv;
@@ -1207,7 +1216,7 @@ struct Escrow_test : public beast::unit_test::suite
}
{ // Test empty condition during creation and
// empty condition & fulfillment during finish
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), "alice", "bob", "carol");
env(escrow("alice", "carol", XRP(1000)),
@@ -1253,7 +1262,7 @@ struct Escrow_test : public beast::unit_test::suite
}
{ // Test a condition other than PreimageSha256, which
// would require a separate amendment
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), "alice", "bob");
std::array<std::uint8_t, 45> cb = {
@@ -1285,7 +1294,7 @@ struct Escrow_test : public beast::unit_test::suite
{
testcase("Metadata to self");
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), alice, bruce, carol);
auto const aseq = env.seq(alice);
auto const bseq = env.seq(bruce);
@@ -1360,7 +1369,7 @@ struct Escrow_test : public beast::unit_test::suite
{
testcase("Metadata to other");
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), alice, bruce, carol);
auto const aseq = env.seq(alice);
auto const bseq = env.seq(bruce);
@@ -1456,7 +1465,7 @@ struct Escrow_test : public beast::unit_test::suite
using namespace jtx;
using namespace std::chrono;
Env env(*this, features);
Env env{*this, features};
env.memoize("alice");
env.memoize("bob");
@@ -1521,7 +1530,7 @@ struct Escrow_test : public beast::unit_test::suite
{
// Create escrow and finish using tickets.
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), alice, bob);
env.close();
@@ -1582,7 +1591,7 @@ struct Escrow_test : public beast::unit_test::suite
{
// Create escrow and cancel using tickets.
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), alice, bob);
env.close();
@@ -1656,7 +1665,7 @@ struct Escrow_test : public beast::unit_test::suite
using namespace jtx;
using namespace std::chrono;
Env env(*this, features);
Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const gw = Account{"gateway"};
@@ -1703,7 +1712,7 @@ struct Escrow_test : public beast::unit_test::suite
{
testcase("Timing: IC Finish Only");
Env env(*this, features);
Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const gw = Account{"gateway"};
@@ -1732,7 +1741,7 @@ struct Escrow_test : public beast::unit_test::suite
{
testcase("Timing: IC Cancel Only");
Env env(*this, features);
Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const gw = Account{"gateway"};
@@ -1773,7 +1782,7 @@ struct Escrow_test : public beast::unit_test::suite
{
testcase("Timing: IC Finish and Cancel -> Finish");
Env env(*this, features);
Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const gw = Account{"gateway"};
@@ -1816,7 +1825,7 @@ struct Escrow_test : public beast::unit_test::suite
{
testcase("Timing: IC Finish and Cancel -> Cancel");
Env env(*this, features);
Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const gw = Account{"gateway"};
@@ -1874,7 +1883,7 @@ struct Escrow_test : public beast::unit_test::suite
using namespace jtx;
using namespace std::chrono;
Env env(*this, features);
Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const gw = Account{"gateway"};
@@ -1937,7 +1946,7 @@ struct Escrow_test : public beast::unit_test::suite
{
// Ignore the "asfDisallowXRP" account flag, which we should
// have been doing before.
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), bob, george, gw);
env.close();
env.trust(USD(10000), bob, george);
@@ -2003,7 +2012,7 @@ struct Escrow_test : public beast::unit_test::suite
{
testcase("IC Implied Finish Time (with fix1571)");
Env env(*this, features);
Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const carol = Account("carol");
@@ -2048,7 +2057,7 @@ struct Escrow_test : public beast::unit_test::suite
using namespace jtx;
using namespace std::chrono;
Env env(*this, features);
Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const gw = Account{"gateway"};
@@ -2206,7 +2215,7 @@ struct Escrow_test : public beast::unit_test::suite
{
// Unconditional
Env env(*this, features);
Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const gw = Account{"gateway"};
@@ -2245,7 +2254,7 @@ struct Escrow_test : public beast::unit_test::suite
// Unconditionally pay from Alice to Bob. Zelda (neither source nor
// destination) signs all cancels and finishes. This shows that
// Escrow will make a payment to Bob with no intervention from Bob.
Env env(*this, features);
Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const carol = Account("carol");
@@ -2291,7 +2300,7 @@ struct Escrow_test : public beast::unit_test::suite
}
{
// Bob sets DepositAuth so only Bob can finish the escrow.
Env env(*this, features);
Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const carol = Account("carol");
@@ -2352,7 +2361,7 @@ struct Escrow_test : public beast::unit_test::suite
{
// Bob sets DepositAuth but preauthorizes Zelda, so Zelda can
// finish the escrow.
Env env(*this, features);
Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const carol = Account("carol");
@@ -2400,7 +2409,7 @@ struct Escrow_test : public beast::unit_test::suite
}
{
// Conditional
Env env(*this, features);
Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const gw = Account{"gateway"};
@@ -2456,7 +2465,7 @@ struct Escrow_test : public beast::unit_test::suite
}
{
// Self-escrowed conditional with DepositAuth.
Env env(*this, features);
Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const gw = Account{"gateway"};
@@ -2507,7 +2516,7 @@ struct Escrow_test : public beast::unit_test::suite
}
{
// Self-escrowed conditional with DepositAuth and DepositPreauth.
Env env(*this, features);
Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const carol = Account("carol");
@@ -2576,7 +2585,7 @@ struct Escrow_test : public beast::unit_test::suite
using namespace std::chrono;
{ // Test cryptoconditions
Env env(*this, features);
Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const carol = Account("carol");
@@ -2666,7 +2675,7 @@ struct Escrow_test : public beast::unit_test::suite
env(cancel(bob, carol, 1), ter(tecNO_TARGET));
}
{ // Test cancel when condition is present
Env env(*this, features);
Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const carol = Account("carol");
@@ -2702,7 +2711,7 @@ struct Escrow_test : public beast::unit_test::suite
BEAST_EXPECT(!env.le(keylet::escrow(Account(alice).id(), seq)));
}
{
Env env(*this, features);
Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const carol = Account("carol");
@@ -2735,7 +2744,7 @@ struct Escrow_test : public beast::unit_test::suite
env.require(balance(carol, USD(5000)));
}
{ // Test long & short conditions during creation
Env env(*this, features);
Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const carol = Account("carol");
@@ -2810,7 +2819,7 @@ struct Escrow_test : public beast::unit_test::suite
env.require(balance(carol, USD(6000)));
}
{ // Test long and short conditions & fulfillments during finish
Env env(*this, features);
Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const carol = Account("carol");
@@ -2973,7 +2982,7 @@ struct Escrow_test : public beast::unit_test::suite
}
{ // Test empty condition during creation and
// empty condition & fulfillment during finish
Env env(*this, features);
Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const carol = Account("carol");
@@ -3034,7 +3043,7 @@ struct Escrow_test : public beast::unit_test::suite
}
{ // Test a condition other than PreimageSha256, which
// would require a separate amendment
Env env(*this, features);
Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const gw = Account{"gateway"};
@@ -3077,7 +3086,7 @@ struct Escrow_test : public beast::unit_test::suite
{
testcase("IC Metadata to self");
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), alice, bob, carol, gw);
env.close();
env.trust(USD(10000), alice, bob, carol);
@@ -3158,7 +3167,7 @@ struct Escrow_test : public beast::unit_test::suite
{
testcase("IC Metadata to other");
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), alice, bob, carol, gw);
env.close();
env.trust(USD(10000), alice, bob, carol);
@@ -3261,7 +3270,7 @@ struct Escrow_test : public beast::unit_test::suite
using namespace jtx;
using namespace std::chrono;
Env env(*this, features);
Env env{*this, features};
auto const alice = Account("alice");
auto const bob = Account("bob");
@@ -3342,7 +3351,7 @@ struct Escrow_test : public beast::unit_test::suite
{
// Create escrow and finish using tickets.
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), alice, bob, gw);
env.close();
env.trust(USD(10000), alice, bob);
@@ -3407,7 +3416,7 @@ struct Escrow_test : public beast::unit_test::suite
{
// Create escrow and cancel using tickets.
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), alice, bob, gw);
env.close();
env.trust(USD(10000), alice, bob);
@@ -3492,7 +3501,7 @@ struct Escrow_test : public beast::unit_test::suite
// test create escrow from issuer with ic
// test with dest tl
// test finish from destination account
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), alice, gw);
env.close();
env.trust(USD(10000), alice);
@@ -3517,7 +3526,7 @@ struct Escrow_test : public beast::unit_test::suite
}
{
// setup env
Env env(*this, features);
Env env{*this, features};
env.fund(XRP(5000), alice, gw);
env.close();
@@ -3541,6 +3550,126 @@ struct Escrow_test : public beast::unit_test::suite
}
}
void
testICLockedRate(FeatureBitset features)
{
testcase("IC Locked Rate");
using namespace test::jtx;
using namespace std::literals;
auto const alice = Account("alice");
auto const bob = Account("bob");
auto const carol = Account("carol");
auto const gw = Account{"gateway"};
auto const USD = gw["USD"];
auto const aliceUSD = alice["USD"];
auto const bobUSD = bob["USD"];
// test TransferRate
{
Env env{*this, features};
env.fund(XRP(10000), alice, bob, gw);
env(rate(gw, 1.25));
env.close();
env.trust(USD(100000), alice);
env.trust(USD(100000), bob);
env.close();
env(pay(gw, alice, USD(10000)));
env(pay(gw, bob, USD(10000)));
env.close();
auto const preAlice = env.balance(alice, USD.issue());
auto const seq1 = env.seq(alice);
auto const delta = USD(125);
env(escrow(alice, bob, delta),
condition(cb1),
finish_time(env.now() + 1s),
fee(1500));
env.close();
auto const transferRate = escrowRate(env, alice, seq1);
BEAST_EXPECT(transferRate.value == std::uint32_t(1000000000 * 1.25));
env(finish(bob, alice, seq1),
condition(cb1),
fulfillment(fb1),
fee(1500));
env.close();
auto const postLocked = lockedAmount(env, alice, gw, USD);
BEAST_EXPECT(postLocked == USD(0));
BEAST_EXPECT(env.balance(alice, USD.issue()) == preAlice - delta);
BEAST_EXPECT(env.balance(bob, USD.issue()) == USD(10100));
}
// issuer changes to higher rate
{
Env env{*this, features};
env.fund(XRP(10000), alice, bob, gw);
env(rate(gw, 1.25));
env.close();
env.trust(USD(100000), alice);
env.trust(USD(100000), bob);
env.close();
env(pay(gw, alice, USD(10000)));
env(pay(gw, bob, USD(10000)));
env.close();
auto const preAlice = env.balance(alice, USD.issue());
auto const seq1 = env.seq(alice);
auto const delta = USD(125);
env(escrow(alice, bob, delta),
condition(cb1),
finish_time(env.now() + 1s),
fee(1500));
env.close();
auto transferRate = escrowRate(env, alice, seq1);
BEAST_EXPECT(transferRate.value == std::uint32_t(1000000000 * 1.25));
env(rate(gw, 1.26));
env.close();
env(finish(bob, alice, seq1),
condition(cb1),
fulfillment(fb1),
fee(1500));
env.close();
auto const postLocked = lockedAmount(env, alice, gw, USD);
BEAST_EXPECT(postLocked == USD(0));
BEAST_EXPECT(env.balance(alice, USD.issue()) == preAlice - delta);
BEAST_EXPECT(env.balance(bob, USD.issue()) == USD(10100));
}
// issuer changes to lower rate
{
Env env{*this, features};
env.fund(XRP(10000), alice, bob, gw);
env(rate(gw, 1.25));
env.close();
env.trust(USD(100000), alice);
env.trust(USD(100000), bob);
env.close();
env(pay(gw, alice, USD(10000)));
env(pay(gw, bob, USD(10000)));
env.close();
auto const preAlice = env.balance(alice, USD.issue());
auto const seq1 = env.seq(alice);
auto const delta = USD(125);
env(escrow(alice, bob, delta),
condition(cb1),
finish_time(env.now() + 1s),
fee(1500));
env.close();
auto transferRate = escrowRate(env, alice, seq1);
BEAST_EXPECT(transferRate.value == std::uint32_t(1000000000 * 1.25));
env(rate(gw, 1.00));
env.close();
env(finish(bob, alice, seq1),
condition(cb1),
fulfillment(fb1),
fee(1500));
env.close();
auto const postLocked = lockedAmount(env, alice, gw, USD);
BEAST_EXPECT(postLocked == USD(0));
BEAST_EXPECT(env.balance(alice, USD.issue()) == preAlice - delta);
std::cout << "BOB BAL: " << env.balance(bob, USD.issue()) << "\n";
BEAST_EXPECT(env.balance(bob, USD.issue()) == USD(10125));
}
}
void
testWithFeats(FeatureBitset features)
{
@@ -3567,6 +3696,7 @@ struct Escrow_test : public beast::unit_test::suite
testICConsequences(features);
testICEscrowWithTickets(features);
testICGateway(features);
testICLockedRate(features);
}
public: