From 476d033c310795db75781d7dfbbc2d603ca76474 Mon Sep 17 00:00:00 2001 From: MathisGD Date: Tue, 8 Aug 2023 23:52:26 +0200 Subject: [PATCH] refactor: remove BlueLib and dependencies --- src/libraries/BlueLib.sol | 38 --------------------------------- src/libraries/SharesMath.sol | 28 ------------------------ test/forge/Blue.t.sol | 2 -- test/forge/SharesMath.t.sol | 41 ------------------------------------ 4 files changed, 109 deletions(-) delete mode 100644 src/libraries/BlueLib.sol delete mode 100644 test/forge/SharesMath.t.sol diff --git a/src/libraries/BlueLib.sol b/src/libraries/BlueLib.sol deleted file mode 100644 index 580c13e53..000000000 --- a/src/libraries/BlueLib.sol +++ /dev/null @@ -1,38 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.0; - -import {Id, Market, IBlue} from "../interfaces/IBlue.sol"; - -import {MarketLib} from "./MarketLib.sol"; -import {SharesMath} from "./SharesMath.sol"; - -library BlueLib { - using MarketLib for Market; - using SharesMath for uint256; - - function withdrawAmount(IBlue blue, Market memory market, uint256 amount, address onBehalf, address receiver) - internal - returns (uint256 shares) - { - Id id = market.id(); - shares = amount.toWithdrawShares(blue.totalSupply(id), blue.totalSupplyShares(id)); - - uint256 maxShares = blue.supplyShares(id, address(this)); - if (shares > maxShares) shares = maxShares; - - blue.withdraw(market, shares, onBehalf, receiver); - } - - function repayAmount(IBlue blue, Market memory market, uint256 amount, address onBehalf, bytes memory data) - internal - returns (uint256 shares) - { - Id id = market.id(); - shares = amount.toRepayShares(blue.totalBorrow(id), blue.totalBorrowShares(id)); - - uint256 maxShares = blue.borrowShares(id, address(this)); - if (shares > maxShares) shares = maxShares; - - blue.repay(market, shares, onBehalf, data); - } -} diff --git a/src/libraries/SharesMath.sol b/src/libraries/SharesMath.sol index 9f1dab24f..3d9728c3d 100644 --- a/src/libraries/SharesMath.sol +++ b/src/libraries/SharesMath.sol @@ -34,32 +34,4 @@ library SharesMath { function toAssetsUp(uint256 shares, uint256 totalAssets, uint256 totalShares) internal pure returns (uint256) { return shares.mulDivUp(totalAssets + VIRTUAL_ASSETS, totalShares + VIRTUAL_SHARES); } - - /// @dev Calculates the amount of shares corresponding to an exact amount of supply to withdraw. - /// Note: only works as long as totalSupplyShares + VIRTUAL_SHARES >= totalSupply + VIRTUAL_ASSETS. - function toWithdrawShares(uint256 amount, uint256 totalSupply, uint256 totalSupplyShares) - internal - pure - returns (uint256) - { - uint256 sharesMin = toSharesDown(amount, totalSupply, totalSupplyShares); - uint256 sharesMax = toSharesUp(amount + 1, totalSupply, totalSupplyShares); - - return (sharesMin + sharesMax) / 2; - } - - /// @dev Calculates the amount of shares corresponding to an exact amount of debt to repay. - /// Note: only works as long as totalBorrowShares + VIRTUAL_SHARES >= totalBorrow + VIRTUAL_ASSETS. - function toRepayShares(uint256 amount, uint256 totalBorrow, uint256 totalBorrowShares) - internal - pure - returns (uint256) - { - if (amount == 0) return 0; - - uint256 sharesMin = toSharesDown(amount - 1, totalBorrow, totalBorrowShares); - uint256 sharesMax = toSharesUp(amount, totalBorrow, totalBorrowShares); - - return (sharesMin + sharesMax) / 2; - } } diff --git a/test/forge/Blue.t.sol b/test/forge/Blue.t.sol index 8b1aa68f1..da3188548 100644 --- a/test/forge/Blue.t.sol +++ b/test/forge/Blue.t.sol @@ -8,7 +8,6 @@ import {SigUtils} from "./helpers/SigUtils.sol"; import "src/Blue.sol"; import {SharesMath} from "src/libraries/SharesMath.sol"; -import {BlueLib} from "src/libraries/BlueLib.sol"; import { IBlueLiquidateCallback, IBlueRepayCallback, @@ -26,7 +25,6 @@ contract BlueTest is IBlueRepayCallback, IBlueLiquidateCallback { - using BlueLib for IBlue; using MarketLib for Market; using SharesMath for uint256; using stdStorage for StdStorage; diff --git a/test/forge/SharesMath.t.sol b/test/forge/SharesMath.t.sol deleted file mode 100644 index 5165bab90..000000000 --- a/test/forge/SharesMath.t.sol +++ /dev/null @@ -1,41 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.0; - -import "forge-std/Test.sol"; -import "forge-std/console2.sol"; - -import {SharesMath} from "src/libraries/SharesMath.sol"; - -contract SharesMathTest is Test { - using SharesMath for uint256; - - function testToSupplyShares(uint256 amount, uint256 supplyShares, uint256 totalSupply, uint256 totalSupplyShares) - public - { - totalSupplyShares = bound(totalSupplyShares, SharesMath.VIRTUAL_SHARES, type(uint128).max); - totalSupply = bound(totalSupply, 0, totalSupplyShares); - - supplyShares = bound(supplyShares, 0, totalSupplyShares); - - amount = bound(amount, 0, supplyShares.toAssetsDown(totalSupply, totalSupplyShares)); - - assertEq( - amount, amount.toWithdrawShares(totalSupply, totalSupplyShares).toAssetsDown(totalSupply, totalSupplyShares) - ); - } - - function testToBorrowShares(uint256 amount, uint256 borrowShares, uint256 totalBorrow, uint256 totalBorrowShares) - public - { - totalBorrowShares = bound(totalBorrowShares, SharesMath.VIRTUAL_SHARES, type(uint128).max); - totalBorrow = bound(totalBorrow, 0, totalBorrowShares); - - borrowShares = bound(borrowShares, 0, totalBorrowShares); - - amount = bound(amount, 0, borrowShares.toAssetsDown(totalBorrow, totalBorrowShares)); - - assertEq( - amount, amount.toRepayShares(totalBorrow, totalBorrowShares).toAssetsUp(totalBorrow, totalBorrowShares) - ); - } -}