Skip to content

Commit 8bdd904

Browse files
aritkulovaArvolear
andauthored
Feat/rpow function (#74)
* Delete DSMath lib Added private function _rpow to AbstractCompoundRateKeeper * update rpow Make func internal for test reasons * Update AbstractCompoundRateKeeper.sol * rpow changed to private Check if base or exponent equal to 0 removed * Update AbstractCompoundRateKeeper.sol * Optimization rpow is renamed to raiseToPower precision taken not as func parameter but from the global scope upd to version 2.6.6 * minor style --------- Co-authored-by: Artem Chystiakov <artem.ch31@gmail.com>
1 parent dc6dff0 commit 8bdd904

File tree

7 files changed

+26
-121
lines changed

7 files changed

+26
-121
lines changed

contracts/compound-rate-keeper/AbstractCompoundRateKeeper.sol

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";
66

77
import {ICompoundRateKeeper} from "../interfaces/compound-rate-keeper/ICompoundRateKeeper.sol";
88

9-
import {DSMath} from "../libs/math/DSMath.sol";
10-
119
import {PRECISION} from "../utils/Globals.sol";
1210

1311
/**
@@ -24,7 +22,6 @@ import {PRECISION} from "../utils/Globals.sol";
2422
*/
2523
abstract contract AbstractCompoundRateKeeper is ICompoundRateKeeper, Initializable {
2624
using Math for uint256;
27-
using DSMath for uint256;
2825

2926
uint256 private _capitalizationRate;
3027
uint64 private _capitalizationPeriod;
@@ -96,9 +93,9 @@ abstract contract AbstractCompoundRateKeeper is ICompoundRateKeeper, Initializab
9693
uint256 rate_ = _currentRate;
9794

9895
if (capitalizationPeriodsNum_ != 0) {
99-
uint256 capitalizationPeriodRate_ = capitalizationRate_.rpow(
100-
capitalizationPeriodsNum_,
101-
PRECISION
96+
uint256 capitalizationPeriodRate_ = _raiseToPower(
97+
capitalizationRate_,
98+
capitalizationPeriodsNum_
10299
);
103100
rate_ = (rate_ * capitalizationPeriodRate_) / PRECISION;
104101
}
@@ -203,6 +200,25 @@ abstract contract AbstractCompoundRateKeeper is ICompoundRateKeeper, Initializab
203200
emit CapitalizationPeriodChanged(capitalizationPeriod_);
204201
}
205202

203+
/**
204+
* @notice Implementation of exponentiation by squaring with fixed precision
205+
* @dev Checks if base or exponent equal to 0 done before
206+
*/
207+
function _raiseToPower(
208+
uint256 base_,
209+
uint256 exponent_
210+
) private pure returns (uint256 result_) {
211+
result_ = exponent_ & 1 == 0 ? PRECISION : base_;
212+
213+
while ((exponent_ >>= 1) > 0) {
214+
base_ = (base_ * base_) / PRECISION;
215+
216+
if (exponent_ & 1 == 1) {
217+
result_ = (result_ * base_) / PRECISION;
218+
}
219+
}
220+
}
221+
206222
/**
207223
* @notice The private function to get the maximal possible compound rate
208224
*/

contracts/libs/math/DSMath.sol

Lines changed: 0 additions & 60 deletions
This file was deleted.

contracts/mock/libs/math/DSMathMock.sol

Lines changed: 0 additions & 10 deletions
This file was deleted.

contracts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@solarity/solidity-lib",
3-
"version": "2.6.5",
3+
"version": "2.6.6",
44
"license": "MIT",
55
"author": "Distributed Lab",
66
"readme": "README.md",

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@solarity/solidity-lib",
3-
"version": "2.6.5",
3+
"version": "2.6.6",
44
"license": "MIT",
55
"author": "Distributed Lab",
66
"description": "Solidity Library by Distributed Lab",

test/libs/math/DSMath.test.ts

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)