diff --git a/test/circles/Demurrage.t.sol b/test/circles/Demurrage.t.sol index 15f0303..6570620 100644 --- a/test/circles/Demurrage.t.sol +++ b/test/circles/Demurrage.t.sol @@ -58,6 +58,54 @@ contract DemurrageTest is Test, TimeCirclesSetup, Approximation { } } + // Test on stable point of issuance + + function testFuzzStablePointIssuance(int192 x) public { + vm.assume(x >= -100000000); + vm.assume(x <= 100000000); + + // Stable point of issuance 120804.56 Circles + int192 stable = int192(120804563587458981173795); + + // overshoot stable amount 120804563587458981178828 attoCRC + // undershoot stable amount 120804563587458981173795 attoCRC + // difference 0.000000000000005033 CRC + + int192 dailyIssuance = int192(24 * int256(int192(CRC))); + // start with a balance offset by x + int192 balance = stable + x; + + for (uint256 i = 0; i < 20; i++) { + 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)); + } + + // 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)); + } + // Test the inversion accuracy of the gamma and beta exponentiation over 20 and 100 years // with and without the extension // conclusion: we can just drop the extension as the 64x64 fixed point is accurate, and unsure if the extension diff --git a/test/circles/MockDemurrage.sol b/test/circles/MockDemurrage.sol index 130f65e..766afdc 100644 --- a/test/circles/MockDemurrage.sol +++ b/test/circles/MockDemurrage.sol @@ -25,4 +25,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); + } }