From 68a882d3d297d0f5172e8ed4371a5d2817ca9fb2 Mon Sep 17 00:00:00 2001 From: aritkulova Date: Mon, 23 Oct 2023 20:03:10 +0300 Subject: [PATCH] Delete DSMath lib Added private function _rpow to AbstractCompoundRateKeeper --- .../AbstractCompoundRateKeeper.sol | 30 ++++++++-- contracts/libs/math/DSMath.sol | 60 ------------------- contracts/mock/libs/math/DSMathMock.sol | 10 ---- test/libs/math/DSMath.test.ts | 41 ------------- 4 files changed, 26 insertions(+), 115 deletions(-) delete mode 100644 contracts/libs/math/DSMath.sol delete mode 100644 contracts/mock/libs/math/DSMathMock.sol delete mode 100644 test/libs/math/DSMath.test.ts diff --git a/contracts/compound-rate-keeper/AbstractCompoundRateKeeper.sol b/contracts/compound-rate-keeper/AbstractCompoundRateKeeper.sol index 16332624..d2917d1d 100644 --- a/contracts/compound-rate-keeper/AbstractCompoundRateKeeper.sol +++ b/contracts/compound-rate-keeper/AbstractCompoundRateKeeper.sol @@ -6,8 +6,6 @@ import {Math} from "@openzeppelin/contracts/utils/math/Math.sol"; import {ICompoundRateKeeper} from "../interfaces/compound-rate-keeper/ICompoundRateKeeper.sol"; -import {DSMath} from "../libs/math/DSMath.sol"; - import {PRECISION} from "../utils/Globals.sol"; /** @@ -24,7 +22,6 @@ import {PRECISION} from "../utils/Globals.sol"; */ abstract contract AbstractCompoundRateKeeper is ICompoundRateKeeper, Initializable { using Math for uint256; - using DSMath for uint256; uint256 private _capitalizationRate; uint64 private _capitalizationPeriod; @@ -96,7 +93,8 @@ abstract contract AbstractCompoundRateKeeper is ICompoundRateKeeper, Initializab uint256 rate_ = _currentRate; if (capitalizationPeriodsNum_ != 0) { - uint256 capitalizationPeriodRate_ = capitalizationRate_.rpow( + uint256 capitalizationPeriodRate_ = _rpow( + capitalizationRate_, capitalizationPeriodsNum_, PRECISION ); @@ -209,4 +207,28 @@ abstract contract AbstractCompoundRateKeeper is ICompoundRateKeeper, Initializab function _getMaxRate() private pure returns (uint256) { return type(uint128).max * PRECISION; } + + function _rpow(uint256 x_, uint256 n_, uint256 b_) private pure returns (uint256 z_) { + if (x_ == 0) { + if (n_ == 0) { + z_ = b_; + } else { + z_ = 0; + } + } else { + if (n_ % 2 == 0) { + z_ = b_; + } else { + z_ = x_; + } + uint256 half = b_ / 2; // for rounding + + for (uint256 i = n_ / 2; i >= 1; i = i / 2) { + x_ = (x_ * x_ + half) / b_; + if (i % 2 == 1) { + z_ = (z_ * x_ + half) / b_; + } + } + } + } } diff --git a/contracts/libs/math/DSMath.sol b/contracts/libs/math/DSMath.sol deleted file mode 100644 index a81137cc..00000000 --- a/contracts/libs/math/DSMath.sol +++ /dev/null @@ -1,60 +0,0 @@ -// SPDX-License-Identifier: ALGPL-3.0-or-later-or-later -// from https://github.com/makerdao/dss/blob/master/src/jug.sol -pragma solidity ^0.8.4; - -library DSMath { - /** - * @dev github.com/makerdao/dss implementation of exponentiation by squaring - * @dev nth power of x where x is decimal number with b precision - */ - function rpow(uint256 x, uint256 n, uint256 b) internal pure returns (uint256 z) { - assembly { - switch x - case 0 { - switch n - case 0 { - z := b - } - default { - z := 0 - } - } - default { - switch mod(n, 2) - case 0 { - z := b - } - default { - z := x - } - let half := div(b, 2) // for rounding. - for { - n := div(n, 2) - } n { - n := div(n, 2) - } { - let xx := mul(x, x) - if iszero(eq(div(xx, x), x)) { - revert(0, 0) - } - let xxRound := add(xx, half) - if lt(xxRound, xx) { - revert(0, 0) - } - x := div(xxRound, b) - if mod(n, 2) { - let zx := mul(z, x) - if and(iszero(iszero(x)), iszero(eq(div(zx, x), z))) { - revert(0, 0) - } - let zxRound := add(zx, half) - if lt(zxRound, zx) { - revert(0, 0) - } - z := div(zxRound, b) - } - } - } - } - } -} diff --git a/contracts/mock/libs/math/DSMathMock.sol b/contracts/mock/libs/math/DSMathMock.sol deleted file mode 100644 index b25d8d6c..00000000 --- a/contracts/mock/libs/math/DSMathMock.sol +++ /dev/null @@ -1,10 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity ^0.8.4; - -import {DSMath} from "../../../libs/math/DSMath.sol"; - -contract DSMathMock { - function rpow(uint256 x_, uint256 n_, uint256 b_) external pure returns (uint256) { - return DSMath.rpow(x_, n_, b_); - } -} diff --git a/test/libs/math/DSMath.test.ts b/test/libs/math/DSMath.test.ts deleted file mode 100644 index e45cb00a..00000000 --- a/test/libs/math/DSMath.test.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { ethers } from "hardhat"; -import { expect } from "chai"; -import { Reverter } from "@/test/helpers/reverter"; - -import { DSMathMock } from "@ethers-v6"; - -describe("DSMath", () => { - const reverter = new Reverter(); - - let dsMath: DSMathMock; - - before("setup", async () => { - const DSMathMock = await ethers.getContractFactory("DSMathMock"); - dsMath = await DSMathMock.deploy(); - - await reverter.snapshot(); - }); - - afterEach(reverter.revert); - - describe("rpow()", () => { - it("should calculate power for decimals", async () => { - const BASE = 10n ** 9n; - - const cases = [ - [2, 3, 1, 8], // 2.0 ^ 3 = 8.0 - [150, 2, 10, 2250], // 15.0 ^ 2 = 225.0 - [BASE * 2n, 3, BASE, BASE * 8n], // 2.000_000_000 ^ 3 = 8.000_000_000 - [(BASE * 25n) / 10n, 3, BASE, (BASE * 15625n) / 1000n], - [BASE * 123456n, 3, BASE, BASE * 1881640295202816n], - [(BASE * 5n) / 10n, 2, BASE, (BASE * 25n) / 100n], - ]; - - for (const [base, exponent, mod, expected] of cases) { - const result = await dsMath.rpow(base, exponent, mod); - - expect(result).to.equal(expected); - } - }); - }); -});