From d4ede824c887728e2263395f5d29fe74b90e97ef Mon Sep 17 00:00:00 2001 From: dovgopoly <69435717+dovgopoly@users.noreply.github.com> Date: Thu, 25 May 2023 11:55:39 +0300 Subject: [PATCH] Private vars & getters (#44) * made vars private & added getters * fixed tests * typo * cov 100% --- .../AbstractCompoundRateKeeper.sol | 72 ++++++++++++++----- .../ICompoundRateKeeper.sol | 10 +++ .../CompoundRateKeeper.test.js | 21 +++--- 3 files changed, 77 insertions(+), 26 deletions(-) diff --git a/contracts/compound-rate-keeper/AbstractCompoundRateKeeper.sol b/contracts/compound-rate-keeper/AbstractCompoundRateKeeper.sol index 7d993487..b14adfa6 100644 --- a/contracts/compound-rate-keeper/AbstractCompoundRateKeeper.sol +++ b/contracts/compound-rate-keeper/AbstractCompoundRateKeeper.sol @@ -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 @@ -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_); @@ -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; } } @@ -76,11 +76,11 @@ 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; @@ -88,11 +88,11 @@ abstract contract AbstractCompoundRateKeeper is ICompoundRateKeeper, Initializab 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) { @@ -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 @@ -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); } /** @@ -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_); } @@ -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_); } diff --git a/contracts/interfaces/compound-rate-keeper/ICompoundRateKeeper.sol b/contracts/interfaces/compound-rate-keeper/ICompoundRateKeeper.sol index 7d54ecb5..1484b802 100644 --- a/contracts/interfaces/compound-rate-keeper/ICompoundRateKeeper.sol +++ b/contracts/interfaces/compound-rate-keeper/ICompoundRateKeeper.sol @@ -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); } diff --git a/test/compound-rate-keeper/CompoundRateKeeper.test.js b/test/compound-rate-keeper/CompoundRateKeeper.test.js index 5bf45a9c..f5aa7b72 100644 --- a/test/compound-rate-keeper/CompoundRateKeeper.test.js +++ b/test/compound-rate-keeper/CompoundRateKeeper.test.js @@ -55,8 +55,9 @@ 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; @@ -64,8 +65,8 @@ describe("CompoundRateKeeper", () => { 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 () => { @@ -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 () => { @@ -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))); }); });