Skip to content

Commit

Permalink
Private vars & getters (#44)
Browse files Browse the repository at this point in the history
* made vars private & added getters

* fixed tests

* typo

* cov 100%
  • Loading branch information
dovgopoly authored May 25, 2023
1 parent 10a114c commit d4ede82
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 26 deletions.
72 changes: 56 additions & 16 deletions contracts/compound-rate-keeper/AbstractCompoundRateKeeper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ abstract contract AbstractCompoundRateKeeper is ICompoundRateKeeper, Initializab
using Math for uint256;
using DSMath for uint256;

uint256 public capitalizationRate;
uint64 public capitalizationPeriod;
uint256 private _capitalizationRate;
uint64 private _capitalizationPeriod;

uint64 public lastUpdate;
uint64 private _lastUpdate;

bool public isMaxRateReached;
bool private _isMaxRateReached;

uint256 internal _currentRate;
uint256 private _currentRate;

/**
* @notice The proxy initializer function
Expand All @@ -43,7 +43,7 @@ abstract contract AbstractCompoundRateKeeper is ICompoundRateKeeper, Initializab
uint64 capitalizationPeriod_
) internal onlyInitializing {
_currentRate = PRECISION;
lastUpdate = uint64(block.timestamp);
_lastUpdate = uint64(block.timestamp);

_changeCapitalizationRate(capitalizationRate_);
_changeCapitalizationPeriod(capitalizationPeriod_);
Expand All @@ -55,10 +55,10 @@ abstract contract AbstractCompoundRateKeeper is ICompoundRateKeeper, Initializab
function emergencyUpdateCompoundRate() public override {
try this.getCompoundRate() returns (uint256 rate_) {
if (rate_ == _getMaxRate()) {
isMaxRateReached = true;
_isMaxRateReached = true;
}
} catch {
isMaxRateReached = true;
_isMaxRateReached = true;
}
}

Expand All @@ -76,23 +76,23 @@ abstract contract AbstractCompoundRateKeeper is ICompoundRateKeeper, Initializab
* @return the compound rate for the provided timestamp
*/
function getFutureCompoundRate(uint64 timestamp_) public view override returns (uint256) {
if (isMaxRateReached) {
if (_isMaxRateReached) {
return _getMaxRate();
}

uint64 lastUpdate_ = lastUpdate;
uint64 lastUpdate_ = _lastUpdate;

if (lastUpdate_ >= timestamp_) {
return _currentRate;
}

uint64 secondsPassed_ = timestamp_ - lastUpdate_;

uint64 capitalizationPeriod_ = capitalizationPeriod;
uint64 capitalizationPeriod_ = _capitalizationPeriod;
uint64 capitalizationPeriodsNum_ = secondsPassed_ / capitalizationPeriod_;
uint64 secondsLeft_ = secondsPassed_ % capitalizationPeriod_;

uint256 capitalizationRate_ = capitalizationRate;
uint256 capitalizationRate_ = _capitalizationRate;
uint256 rate_ = _currentRate;

if (capitalizationPeriodsNum_ != 0) {
Expand All @@ -113,6 +113,46 @@ abstract contract AbstractCompoundRateKeeper is ICompoundRateKeeper, Initializab
return rate_.min(_getMaxRate());
}

/**
* @notice The function to get the current capitalization rate
* @return capitalizationRate_ the current capitalization rate
*/
function getCapitalizationRate() public view returns (uint256 capitalizationRate_) {
return _capitalizationRate;
}

/**
* @notice The function to get the current capitalization period
* @return capitalizationPeriod_ the current capitalization period
*/
function getCapitalizationPeriod() public view returns (uint64 capitalizationPeriod_) {
return _capitalizationPeriod;
}

/**
* @notice The function to get the timestamp of the last update
* @return lastUpdate_ the timestamp of the last update
*/
function getLastUpdate() public view returns (uint64 lastUpdate_) {
return _lastUpdate;
}

/**
* @notice The function to get the status of whether the max rate is reached
* @return isMaxRateReached_ the boolean indicating if the max rate is reached
*/
function getIsMaxRateReached() public view returns (bool isMaxRateReached_) {
return _isMaxRateReached;
}

/**
* @notice The function to get the current rate
* @return currentRate_ the current rate
*/
function getCurrentRate() public view returns (uint256 currentRate_) {
return _currentRate;
}

/**
* @notice The internal function to set the capitalization rate
* @param capitalizationRate_ new capitalization rate
Expand All @@ -135,10 +175,10 @@ abstract contract AbstractCompoundRateKeeper is ICompoundRateKeeper, Initializab
* @notice The private function to update the compound rate
*/
function _update() private {
require(!isMaxRateReached, "CRK: max rate is reached");
require(!_isMaxRateReached, "CRK: max rate is reached");

_currentRate = getCompoundRate();
lastUpdate = uint64(block.timestamp);
_lastUpdate = uint64(block.timestamp);
}

/**
Expand All @@ -147,7 +187,7 @@ abstract contract AbstractCompoundRateKeeper is ICompoundRateKeeper, Initializab
function _changeCapitalizationRate(uint256 capitalizationRate_) private {
require(capitalizationRate_ >= PRECISION, "CRK: rate is less than 1");

capitalizationRate = capitalizationRate_;
_capitalizationRate = capitalizationRate_;

emit CapitalizationRateChanged(capitalizationRate_);
}
Expand All @@ -158,7 +198,7 @@ abstract contract AbstractCompoundRateKeeper is ICompoundRateKeeper, Initializab
function _changeCapitalizationPeriod(uint64 capitalizationPeriod_) private {
require(capitalizationPeriod_ > 0, "CRK: invalid period");

capitalizationPeriod = capitalizationPeriod_;
_capitalizationPeriod = capitalizationPeriod_;

emit CapitalizationPeriodChanged(capitalizationPeriod_);
}
Expand Down
10 changes: 10 additions & 0 deletions contracts/interfaces/compound-rate-keeper/ICompoundRateKeeper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,14 @@ interface ICompoundRateKeeper {
function getCompoundRate() external view returns (uint256);

function getFutureCompoundRate(uint64 timestamp_) external view returns (uint256);

function getCapitalizationRate() external view returns (uint256);

function getCapitalizationPeriod() external view returns (uint64);

function getLastUpdate() external view returns (uint64);

function getIsMaxRateReached() external view returns (bool);

function getCurrentRate() external view returns (uint256);
}
21 changes: 11 additions & 10 deletions test/compound-rate-keeper/CompoundRateKeeper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,18 @@ describe("CompoundRateKeeper", () => {
await crk.setCapitalizationRate(precision(1.1));

assert.equal(fromPrecision((await crk.getCompoundRate()).toFixed()), "1");
assert.equal(fromPrecision((await crk.capitalizationRate()).toFixed()), "1.1");
assert.equal((await crk.lastUpdate()).toFixed(), toBN(nextBlockTime).toFixed());
assert.equal(fromPrecision((await crk.getCurrentRate()).toFixed()), "1");
assert.equal(fromPrecision((await crk.getCapitalizationRate()).toFixed()), "1.1");
assert.equal((await crk.getLastUpdate()).toFixed(), toBN(nextBlockTime).toFixed());

nextBlockTime = (await getCurrentBlockTime()) + 31536000;

await setNextBlockTime(nextBlockTime);
await crk.setCapitalizationRate(precision(1.2));

assert.equal(fromPrecision((await crk.getCompoundRate()).toFixed()), "1.1");
assert.equal(fromPrecision((await crk.capitalizationRate()).toFixed()), "1.2");
assert.equal((await crk.lastUpdate()).toFixed(), toBN(nextBlockTime).toFixed());
assert.equal(fromPrecision((await crk.getCapitalizationRate()).toFixed()), "1.2");
assert.equal((await crk.getLastUpdate()).toFixed(), toBN(nextBlockTime).toFixed());
});

it("should revert if rate is less than zero", async () => {
Expand All @@ -85,13 +86,13 @@ describe("CompoundRateKeeper", () => {

describe("setCapitalizationPeriod()", () => {
it("should correctly set new capitalization period", async () => {
assert.equal((await crk.capitalizationPeriod()).toFixed(), "31536000");
assert.equal((await crk.getCapitalizationPeriod()).toFixed(), "31536000");

await crk.setCapitalizationPeriod(10);
assert.equal((await crk.capitalizationPeriod()).toFixed(), "10");
assert.equal((await crk.getCapitalizationPeriod()).toFixed(), "10");

await crk.setCapitalizationPeriod(157680000);
assert.equal((await crk.capitalizationPeriod()).toFixed(), "157680000");
assert.equal((await crk.getCapitalizationPeriod()).toFixed(), "157680000");
});

it("should revert if capitalization period is zero", async () => {
Expand All @@ -116,17 +117,17 @@ describe("CompoundRateKeeper", () => {

await crk.emergencyUpdateCompoundRate();

assert.equal(await crk.isMaxRateReached(), false);
assert.equal(await crk.getIsMaxRateReached(), false);

await setNextBlockTime((await getCurrentBlockTime()) + 100 * 31536000);
await crk.emergencyUpdateCompoundRate();

assert.equal(await crk.isMaxRateReached(), true);
assert.equal(await crk.getIsMaxRateReached(), true);
assert.equal((await crk.getCompoundRate()).toFixed(), precision(toBN(2).pow(128).minus(1)));

await crk.emergencyUpdateCompoundRate();

assert.equal(await crk.isMaxRateReached(), true);
assert.equal(await crk.getIsMaxRateReached(), true);
assert.equal((await crk.getCompoundRate()).toFixed(), precision(toBN(2).pow(128).minus(1)));
});
});
Expand Down

0 comments on commit d4ede82

Please sign in to comment.