Skip to content

Commit

Permalink
feat: Add OZ math lib (#34)
Browse files Browse the repository at this point in the history
* forge install: openzeppelin-contracts

v5.0.2

* fix: import oz lib
  • Loading branch information
lucas-manuel authored Aug 26, 2024
1 parent ecf841b commit 60e7a3d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "lib/xchain-dsr-oracle"]
path = lib/xchain-dsr-oracle
url = https://github.com/marsfoundation/xchain-dsr-oracle
[submodule "lib/openzeppelin-contracts"]
path = lib/openzeppelin-contracts
url = https://github.com/openzeppelin/openzeppelin-contracts
3 changes: 2 additions & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ runs = 1_000_000
remappings = [
"ds-test/=lib/erc20-helpers/lib/forge-std/lib/ds-test/src/",
"erc20-helpers/=lib/erc20-helpers/src/",
"forge-std/=lib/forge-std/src/"
"forge-std/=lib/forge-std/src/",
"openzeppelin-contracts/=lib/openzeppelin-contracts/contracts/",
]
1 change: 1 addition & 0 deletions lib/openzeppelin-contracts
Submodule openzeppelin-contracts added at dbb610
20 changes: 8 additions & 12 deletions src/PSM3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { IERC20 } from "erc20-helpers/interfaces/IERC20.sol";

import { SafeERC20 } from "erc20-helpers/SafeERC20.sol";

import { Math } from "openzeppelin-contracts/contracts/utils/math/Math.sol";

import { IPSM3 } from "src/interfaces/IPSM3.sol";
import { IRateProviderLike } from "src/interfaces/IRateProviderLike.sol";

Expand Down Expand Up @@ -302,8 +304,8 @@ contract PSM3 is IPSM3 {

if (!roundUp) return amount * 1e27 / rate * _asset2Precision / assetPrecision;

return _divUp(
_divUp(amount * 1e27, rate) * _asset2Precision,
return Math.ceilDiv(
Math.ceilDiv(amount * 1e27, rate) * _asset2Precision,
assetPrecision
);
}
Expand All @@ -315,8 +317,8 @@ contract PSM3 is IPSM3 {

if (!roundUp) return amount * rate / 1e27 * assetPrecision / _asset2Precision;

return _divUp(
_divUp(amount * rate, 1e27) * assetPrecision,
return Math.ceilDiv(
Math.ceilDiv(amount * rate, 1e27) * assetPrecision,
_asset2Precision
);
}
Expand All @@ -331,7 +333,7 @@ contract PSM3 is IPSM3 {
{
if (!roundUp) return amount * convertAssetPrecision / assetPrecision;

return _divUp(amount * convertAssetPrecision, assetPrecision);
return Math.ceilDiv(amount * convertAssetPrecision, assetPrecision);
}

/**********************************************************************************************/
Expand All @@ -341,17 +343,11 @@ contract PSM3 is IPSM3 {
function _convertToSharesRoundUp(uint256 assetValue) internal view returns (uint256) {
uint256 totalValue = totalAssets();
if (totalValue != 0) {
return _divUp(assetValue * totalShares, totalValue);
return Math.ceilDiv(assetValue * totalShares, totalValue);
}
return assetValue;
}

function _divUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
unchecked {
z = x != 0 ? ((x - 1) / y) + 1 : 0;
}
}

function _isValidAsset(address asset) internal view returns (bool) {
return asset == address(asset0) || asset == address(asset1) || asset == address(asset2);
}
Expand Down

0 comments on commit 60e7a3d

Please sign in to comment.