From 28784f9fd71b69188c77b4280a7ec0db543c55d9 Mon Sep 17 00:00:00 2001 From: Benjamin Bollen Date: Fri, 11 Oct 2024 13:29:13 +0100 Subject: [PATCH 1/2] (test/demurrage): quick test on stable point of issuance --- test/circles/Demurrage.t.sol | 25 +++++++++++++++++++++++++ test/circles/MockDemurrage.sol | 4 ++++ 2 files changed, 29 insertions(+) diff --git a/test/circles/Demurrage.t.sol b/test/circles/Demurrage.t.sol index 0586f75..5436b46 100644 --- a/test/circles/Demurrage.t.sol +++ b/test/circles/Demurrage.t.sol @@ -55,4 +55,29 @@ contract DemurrageTest is Test, TimeCirclesSetup, Approximation { ); } } + + // Test on stable point of issuance + + function testStablePointIssuance() public view { + // Stable point of issuance 120804.56 Circles + uint192 stable = uint192(120804563587458981173795 + 2343); + console.log("stable: ", stable); + + // overshoot stable amount 120804563587458981178828 attoCRC + // undershoot stable amount 120804563587458981173795 attoCRC + // difference 0.000000000000005033 CRC + + uint192 dailyIssuance = uint192(24 * CRC); + + uint192 balance = stable; + + for (uint256 i = 0; i < 20; i++) { + uint192 demurragedBalance = uint192(demurrage.calculateDiscountedBalance(balance, 1)); + uint192 newBalance = demurragedBalance + dailyIssuance; + console.log("balance: ", newBalance); + balance = newBalance; + } + + console.log("stable: ", stable); + } } diff --git a/test/circles/MockDemurrage.sol b/test/circles/MockDemurrage.sol index 04faeb2..04fa4af 100644 --- a/test/circles/MockDemurrage.sol +++ b/test/circles/MockDemurrage.sol @@ -21,4 +21,8 @@ contract MockDemurrage is Demurrage { function rLength() external pure returns (uint256) { return R_TABLE_LOOKUP; } + + function calculateDiscountedBalance(uint256 _balance, uint256 _daysDifference) external view returns (uint256) { + return _calculateDiscountedBalance(_balance, _daysDifference); + } } From 3c4124e12b6746cdc4bca92dec7e6b65a004029e Mon Sep 17 00:00:00 2001 From: Benjamin Bollen Date: Fri, 11 Oct 2024 16:20:19 +0100 Subject: [PATCH 2/2] (tes/demurrage): fuzz test for stable point, and accuracy on repeated demurrage vs exponentiation --- test/circles/Demurrage.t.sol | 43 +++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/test/circles/Demurrage.t.sol b/test/circles/Demurrage.t.sol index 5436b46..da38ca7 100644 --- a/test/circles/Demurrage.t.sol +++ b/test/circles/Demurrage.t.sol @@ -58,26 +58,49 @@ contract DemurrageTest is Test, TimeCirclesSetup, Approximation { // Test on stable point of issuance - function testStablePointIssuance() public view { + function testFuzzStablePointIssuance(int192 x) public { + vm.assume(x >= -100000000); + vm.assume(x <= 100000000); + // Stable point of issuance 120804.56 Circles - uint192 stable = uint192(120804563587458981173795 + 2343); - console.log("stable: ", stable); + int192 stable = int192(120804563587458981173795); // overshoot stable amount 120804563587458981178828 attoCRC // undershoot stable amount 120804563587458981173795 attoCRC // difference 0.000000000000005033 CRC - uint192 dailyIssuance = uint192(24 * CRC); - - uint192 balance = stable; + int192 dailyIssuance = int192(24 * int256(CRC)); + // start with a balance offset by x + int192 balance = stable + x; for (uint256 i = 0; i < 20; i++) { - uint192 demurragedBalance = uint192(demurrage.calculateDiscountedBalance(balance, 1)); - uint192 newBalance = demurragedBalance + dailyIssuance; - console.log("balance: ", newBalance); + int192 demurragedBalance = + int192(int256(demurrage.calculateDiscountedBalance(uint256(uint192(balance)), 1))); + int192 newBalance = demurragedBalance + dailyIssuance; + console.log("balance: ", uint256(int256(newBalance))); balance = newBalance; } + console.log("stable: ", uint256(int256(stable))); + + // to get to the exact stable point, you may need to run this loop ~100.000 times + // but once we're close it converges quickly (loop of 20) + assertTrue(relativeApproximatelyEqual(uint256(int256(balance)), uint256(int256(stable)), 1000 * DUST)); + } - console.log("stable: ", stable); + // Test repeated multiplication versus exponentiation + + function testRepeatedDemurrage() public { + uint256 numberDays = 365 * 10; + uint192 balance = uint192(1000 * CRC); + uint192 demurragedBalance = uint192(demurrage.calculateDiscountedBalance(balance, numberDays)); + + // Calculate the demurraged balance by repeated multiplication + for (uint256 i = 1; i < numberDays + 1; i++) { + uint192 newBalance = uint192(demurrage.calculateDiscountedBalance(balance, 1)); + console.log("day ", i, ": ", newBalance); + balance = newBalance; + } + console.log("demurragedBalance: ", demurragedBalance); + assertTrue(relativeApproximatelyEqual(demurragedBalance, balance, 1000 * DUST)); } }