Skip to content

Commit

Permalink
(Demurrage): improve storage reads and writes for caching the demurra…
Browse files Browse the repository at this point in the history
…ge factor table
  • Loading branch information
benjaminbollen committed Aug 7, 2024
1 parent df2d292 commit 2f80080
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/circles/Demurrage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -242,22 +242,27 @@ contract Demurrage is ICirclesDemurrageErrors {
}

function _calculateDemurrageFactor(uint256 _dayDifference) internal view returns (int128) {
if (_dayDifference <= R_TABLE_LOOKUP && R[_dayDifference] != 0) {
return R[_dayDifference];
} else {
return Math64x64.pow(GAMMA_64x64, _dayDifference);
if (_dayDifference <= R_TABLE_LOOKUP) {
// if the day difference is in the lookup table, return the value from the table
int128 demurrageFactor = R[_dayDifference];
if (demurrageFactor != 0) {
return demurrageFactor;
}
}
// if the day difference is not in the lookup table, calculate the value
return Math64x64.pow(GAMMA_64x64, _dayDifference);
}

function _calculateDemurrageFactorAndCache(uint256 _dayDifference) internal returns (int128) {
if (_dayDifference <= R_TABLE_LOOKUP) {
if (R[_dayDifference] == 0) {
int128 demurrageFactor = R[_dayDifference];
if (demurrageFactor == 0) {
// for proxy ERC20 contracts, the storage does not contain the R table yet
// so compute it lazily and store it in the table
int128 r = Math64x64.pow(GAMMA_64x64, _dayDifference);
R[_dayDifference] = r;
demurrageFactor = Math64x64.pow(GAMMA_64x64, _dayDifference);
R[_dayDifference] = demurrageFactor;
}
return R[_dayDifference];
return demurrageFactor;
} else {
return Math64x64.pow(GAMMA_64x64, _dayDifference);
}
Expand Down

0 comments on commit 2f80080

Please sign in to comment.