Skip to content

Commit d4ede82

Browse files
authored
Private vars & getters (#44)
* made vars private & added getters * fixed tests * typo * cov 100%
1 parent 10a114c commit d4ede82

File tree

3 files changed

+77
-26
lines changed

3 files changed

+77
-26
lines changed

contracts/compound-rate-keeper/AbstractCompoundRateKeeper.sol

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ abstract contract AbstractCompoundRateKeeper is ICompoundRateKeeper, Initializab
2626
using Math for uint256;
2727
using DSMath for uint256;
2828

29-
uint256 public capitalizationRate;
30-
uint64 public capitalizationPeriod;
29+
uint256 private _capitalizationRate;
30+
uint64 private _capitalizationPeriod;
3131

32-
uint64 public lastUpdate;
32+
uint64 private _lastUpdate;
3333

34-
bool public isMaxRateReached;
34+
bool private _isMaxRateReached;
3535

36-
uint256 internal _currentRate;
36+
uint256 private _currentRate;
3737

3838
/**
3939
* @notice The proxy initializer function
@@ -43,7 +43,7 @@ abstract contract AbstractCompoundRateKeeper is ICompoundRateKeeper, Initializab
4343
uint64 capitalizationPeriod_
4444
) internal onlyInitializing {
4545
_currentRate = PRECISION;
46-
lastUpdate = uint64(block.timestamp);
46+
_lastUpdate = uint64(block.timestamp);
4747

4848
_changeCapitalizationRate(capitalizationRate_);
4949
_changeCapitalizationPeriod(capitalizationPeriod_);
@@ -55,10 +55,10 @@ abstract contract AbstractCompoundRateKeeper is ICompoundRateKeeper, Initializab
5555
function emergencyUpdateCompoundRate() public override {
5656
try this.getCompoundRate() returns (uint256 rate_) {
5757
if (rate_ == _getMaxRate()) {
58-
isMaxRateReached = true;
58+
_isMaxRateReached = true;
5959
}
6060
} catch {
61-
isMaxRateReached = true;
61+
_isMaxRateReached = true;
6262
}
6363
}
6464

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

83-
uint64 lastUpdate_ = lastUpdate;
83+
uint64 lastUpdate_ = _lastUpdate;
8484

8585
if (lastUpdate_ >= timestamp_) {
8686
return _currentRate;
8787
}
8888

8989
uint64 secondsPassed_ = timestamp_ - lastUpdate_;
9090

91-
uint64 capitalizationPeriod_ = capitalizationPeriod;
91+
uint64 capitalizationPeriod_ = _capitalizationPeriod;
9292
uint64 capitalizationPeriodsNum_ = secondsPassed_ / capitalizationPeriod_;
9393
uint64 secondsLeft_ = secondsPassed_ % capitalizationPeriod_;
9494

95-
uint256 capitalizationRate_ = capitalizationRate;
95+
uint256 capitalizationRate_ = _capitalizationRate;
9696
uint256 rate_ = _currentRate;
9797

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

116+
/**
117+
* @notice The function to get the current capitalization rate
118+
* @return capitalizationRate_ the current capitalization rate
119+
*/
120+
function getCapitalizationRate() public view returns (uint256 capitalizationRate_) {
121+
return _capitalizationRate;
122+
}
123+
124+
/**
125+
* @notice The function to get the current capitalization period
126+
* @return capitalizationPeriod_ the current capitalization period
127+
*/
128+
function getCapitalizationPeriod() public view returns (uint64 capitalizationPeriod_) {
129+
return _capitalizationPeriod;
130+
}
131+
132+
/**
133+
* @notice The function to get the timestamp of the last update
134+
* @return lastUpdate_ the timestamp of the last update
135+
*/
136+
function getLastUpdate() public view returns (uint64 lastUpdate_) {
137+
return _lastUpdate;
138+
}
139+
140+
/**
141+
* @notice The function to get the status of whether the max rate is reached
142+
* @return isMaxRateReached_ the boolean indicating if the max rate is reached
143+
*/
144+
function getIsMaxRateReached() public view returns (bool isMaxRateReached_) {
145+
return _isMaxRateReached;
146+
}
147+
148+
/**
149+
* @notice The function to get the current rate
150+
* @return currentRate_ the current rate
151+
*/
152+
function getCurrentRate() public view returns (uint256 currentRate_) {
153+
return _currentRate;
154+
}
155+
116156
/**
117157
* @notice The internal function to set the capitalization rate
118158
* @param capitalizationRate_ new capitalization rate
@@ -135,10 +175,10 @@ abstract contract AbstractCompoundRateKeeper is ICompoundRateKeeper, Initializab
135175
* @notice The private function to update the compound rate
136176
*/
137177
function _update() private {
138-
require(!isMaxRateReached, "CRK: max rate is reached");
178+
require(!_isMaxRateReached, "CRK: max rate is reached");
139179

140180
_currentRate = getCompoundRate();
141-
lastUpdate = uint64(block.timestamp);
181+
_lastUpdate = uint64(block.timestamp);
142182
}
143183

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

150-
capitalizationRate = capitalizationRate_;
190+
_capitalizationRate = capitalizationRate_;
151191

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

161-
capitalizationPeriod = capitalizationPeriod_;
201+
_capitalizationPeriod = capitalizationPeriod_;
162202

163203
emit CapitalizationPeriodChanged(capitalizationPeriod_);
164204
}

contracts/interfaces/compound-rate-keeper/ICompoundRateKeeper.sol

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,14 @@ interface ICompoundRateKeeper {
1313
function getCompoundRate() external view returns (uint256);
1414

1515
function getFutureCompoundRate(uint64 timestamp_) external view returns (uint256);
16+
17+
function getCapitalizationRate() external view returns (uint256);
18+
19+
function getCapitalizationPeriod() external view returns (uint64);
20+
21+
function getLastUpdate() external view returns (uint64);
22+
23+
function getIsMaxRateReached() external view returns (bool);
24+
25+
function getCurrentRate() external view returns (uint256);
1626
}

test/compound-rate-keeper/CompoundRateKeeper.test.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,18 @@ describe("CompoundRateKeeper", () => {
5555
await crk.setCapitalizationRate(precision(1.1));
5656

5757
assert.equal(fromPrecision((await crk.getCompoundRate()).toFixed()), "1");
58-
assert.equal(fromPrecision((await crk.capitalizationRate()).toFixed()), "1.1");
59-
assert.equal((await crk.lastUpdate()).toFixed(), toBN(nextBlockTime).toFixed());
58+
assert.equal(fromPrecision((await crk.getCurrentRate()).toFixed()), "1");
59+
assert.equal(fromPrecision((await crk.getCapitalizationRate()).toFixed()), "1.1");
60+
assert.equal((await crk.getLastUpdate()).toFixed(), toBN(nextBlockTime).toFixed());
6061

6162
nextBlockTime = (await getCurrentBlockTime()) + 31536000;
6263

6364
await setNextBlockTime(nextBlockTime);
6465
await crk.setCapitalizationRate(precision(1.2));
6566

6667
assert.equal(fromPrecision((await crk.getCompoundRate()).toFixed()), "1.1");
67-
assert.equal(fromPrecision((await crk.capitalizationRate()).toFixed()), "1.2");
68-
assert.equal((await crk.lastUpdate()).toFixed(), toBN(nextBlockTime).toFixed());
68+
assert.equal(fromPrecision((await crk.getCapitalizationRate()).toFixed()), "1.2");
69+
assert.equal((await crk.getLastUpdate()).toFixed(), toBN(nextBlockTime).toFixed());
6970
});
7071

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

8687
describe("setCapitalizationPeriod()", () => {
8788
it("should correctly set new capitalization period", async () => {
88-
assert.equal((await crk.capitalizationPeriod()).toFixed(), "31536000");
89+
assert.equal((await crk.getCapitalizationPeriod()).toFixed(), "31536000");
8990

9091
await crk.setCapitalizationPeriod(10);
91-
assert.equal((await crk.capitalizationPeriod()).toFixed(), "10");
92+
assert.equal((await crk.getCapitalizationPeriod()).toFixed(), "10");
9293

9394
await crk.setCapitalizationPeriod(157680000);
94-
assert.equal((await crk.capitalizationPeriod()).toFixed(), "157680000");
95+
assert.equal((await crk.getCapitalizationPeriod()).toFixed(), "157680000");
9596
});
9697

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

117118
await crk.emergencyUpdateCompoundRate();
118119

119-
assert.equal(await crk.isMaxRateReached(), false);
120+
assert.equal(await crk.getIsMaxRateReached(), false);
120121

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

124-
assert.equal(await crk.isMaxRateReached(), true);
125+
assert.equal(await crk.getIsMaxRateReached(), true);
125126
assert.equal((await crk.getCompoundRate()).toFixed(), precision(toBN(2).pow(128).minus(1)));
126127

127128
await crk.emergencyUpdateCompoundRate();
128129

129-
assert.equal(await crk.isMaxRateReached(), true);
130+
assert.equal(await crk.getIsMaxRateReached(), true);
130131
assert.equal((await crk.getCompoundRate()).toFixed(), precision(toBN(2).pow(128).minus(1)));
131132
});
132133
});

0 commit comments

Comments
 (0)