Skip to content

Commit

Permalink
Merge pull request #71 from aboutcircles/v0.3.7-patch22-tests-on-demu…
Browse files Browse the repository at this point in the history
…rrage-stable-point

(test/demurrage): quick test on stable point of issuance
  • Loading branch information
benjaminbollen authored Oct 11, 2024
2 parents 1337093 + d4bc123 commit f15d953
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
48 changes: 48 additions & 0 deletions test/circles/Demurrage.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/circles/MockDemurrage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit f15d953

Please sign in to comment.