mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-05 16:57:53 +00:00
add compound interest test (#124)
* add compound interest test * clang-format
This commit is contained in:
@@ -1516,7 +1516,7 @@ struct XahauGenesis_test : public beast::unit_test::suite
|
|||||||
void testGovernanceL2(FeatureBitset features)
|
void testGovernanceL2(FeatureBitset features)
|
||||||
{
|
{
|
||||||
using namespace jtx;
|
using namespace jtx;
|
||||||
testcase("Test governance membership voting L1");
|
testcase("Test governance membership voting L2");
|
||||||
|
|
||||||
Env env{*this, envconfig(), features - featureXahauGenesis};
|
Env env{*this, envconfig(), features - featureXahauGenesis};
|
||||||
|
|
||||||
@@ -3846,6 +3846,99 @@ struct XahauGenesis_test : public beast::unit_test::suite
|
|||||||
env.close();
|
env.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
testCompoundInterest(FeatureBitset features)
|
||||||
|
{
|
||||||
|
using namespace jtx;
|
||||||
|
using namespace std::chrono_literals;
|
||||||
|
testcase("test compound interest over 12 claims");
|
||||||
|
|
||||||
|
Env env{
|
||||||
|
*this, envconfig(), supported_amendments() - featureXahauGenesis};
|
||||||
|
|
||||||
|
double const rateDrops = 0.00333333333 * 1'000'000;
|
||||||
|
STAmount const feesXRP = XRP(1);
|
||||||
|
|
||||||
|
auto const user = Account("user");
|
||||||
|
env.fund(XRP(10000), user);
|
||||||
|
env.close();
|
||||||
|
|
||||||
|
// setup governance
|
||||||
|
auto const alice = Account("alice");
|
||||||
|
auto const bob = Account("bob");
|
||||||
|
auto const carol = Account("carol");
|
||||||
|
auto const david = Account("david");
|
||||||
|
auto const edward = Account("edward");
|
||||||
|
|
||||||
|
env.fund(XRP(10000), alice, bob, carol, david, edward);
|
||||||
|
env.close();
|
||||||
|
|
||||||
|
std::vector<AccountID> initial_members_ids{
|
||||||
|
alice.id(), bob.id(), carol.id(), david.id(), edward.id()};
|
||||||
|
|
||||||
|
setupGov(env, initial_members_ids);
|
||||||
|
|
||||||
|
// update reward delay
|
||||||
|
{
|
||||||
|
// this will be the new reward delay
|
||||||
|
// 100
|
||||||
|
std::vector<uint8_t> vote_data{
|
||||||
|
0x00U, 0x80U, 0xC6U, 0xA4U, 0x7EU, 0x8DU, 0x03U, 0x55U};
|
||||||
|
|
||||||
|
updateTopic(
|
||||||
|
env, alice, bob, carol, david, edward, 'R', 'D', vote_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
// validate no unl report
|
||||||
|
BEAST_EXPECT(hasUNLReport(env) == false);
|
||||||
|
|
||||||
|
// opt in claim reward
|
||||||
|
env(claimReward(user, env.master), fee(XRP(1)), ter(tesSUCCESS));
|
||||||
|
env.close();
|
||||||
|
|
||||||
|
STAmount const begBal = env.balance(user);
|
||||||
|
|
||||||
|
for (int i = 0; i < 12; ++i)
|
||||||
|
{
|
||||||
|
// close ledgers
|
||||||
|
std::uint64_t ledgerDiff = 90 + (i * 30) + (i * 90);
|
||||||
|
validateTime(lastClose(env), ledgerDiff);
|
||||||
|
for (int i = 0; i < 5; ++i)
|
||||||
|
{
|
||||||
|
env.close(10s);
|
||||||
|
}
|
||||||
|
validateTime(lastClose(env), ledgerDiff + 90);
|
||||||
|
|
||||||
|
// define pre close values
|
||||||
|
STAmount const preUser = env.balance(user);
|
||||||
|
NetClock::time_point const preTime = lastClose(env);
|
||||||
|
std::uint32_t const preLedger = env.current()->seq();
|
||||||
|
auto const [acct, acctSle] = accountKeyAndSle(*env.current(), user);
|
||||||
|
|
||||||
|
// claim reward
|
||||||
|
env(claimReward(user, env.master), fee(feesXRP), ter(tesSUCCESS));
|
||||||
|
env.close();
|
||||||
|
|
||||||
|
// trigger emitted txn
|
||||||
|
env.close();
|
||||||
|
|
||||||
|
// calculate rewards
|
||||||
|
auto const netReward =
|
||||||
|
rewardUserAmount(*acctSle, preLedger, rateDrops);
|
||||||
|
|
||||||
|
// validate account fields
|
||||||
|
STAmount const postUser = preUser + netReward;
|
||||||
|
BEAST_EXPECT(expectAccountFields(
|
||||||
|
env, user, preLedger, preLedger + 1, postUser, preTime));
|
||||||
|
}
|
||||||
|
|
||||||
|
STAmount const endBal = env.balance(user);
|
||||||
|
auto const gainloss = ((endBal - begBal) / begBal);
|
||||||
|
std::uint64_t const asPercent =
|
||||||
|
static_cast<std::int64_t>(gainloss * 100);
|
||||||
|
BEAST_EXPECT(asPercent == 4);
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
testRewardHookWithFeats(FeatureBitset features)
|
testRewardHookWithFeats(FeatureBitset features)
|
||||||
{
|
{
|
||||||
@@ -3862,6 +3955,7 @@ struct XahauGenesis_test : public beast::unit_test::suite
|
|||||||
testValidElapsed1(features);
|
testValidElapsed1(features);
|
||||||
testInvalidElapsed0(features);
|
testInvalidElapsed0(features);
|
||||||
testInvalidElapsedNegative(features);
|
testInvalidElapsedNegative(features);
|
||||||
|
testCompoundInterest(features);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|||||||
Reference in New Issue
Block a user