From 60e7a3d9f0a705dad74e79f48897e972df833018 Mon Sep 17 00:00:00 2001 From: Lucas Manuel Date: Mon, 26 Aug 2024 09:06:23 -0400 Subject: [PATCH] feat: Add OZ math lib (#34) * forge install: openzeppelin-contracts v5.0.2 * fix: import oz lib --- .gitmodules | 3 +++ foundry.toml | 3 ++- lib/openzeppelin-contracts | 1 + src/PSM3.sol | 20 ++++++++------------ 4 files changed, 14 insertions(+), 13 deletions(-) create mode 160000 lib/openzeppelin-contracts diff --git a/.gitmodules b/.gitmodules index 8db1840..71e2578 100644 --- a/.gitmodules +++ b/.gitmodules @@ -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 diff --git a/foundry.toml b/foundry.toml index 6a16c5d..2828806 100644 --- a/foundry.toml +++ b/foundry.toml @@ -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/", ] diff --git a/lib/openzeppelin-contracts b/lib/openzeppelin-contracts new file mode 160000 index 0000000..dbb6104 --- /dev/null +++ b/lib/openzeppelin-contracts @@ -0,0 +1 @@ +Subproject commit dbb6104ce834628e473d2173bbc9d47f81a9eec3 diff --git a/src/PSM3.sol b/src/PSM3.sol index 9c08312..0cc3408 100644 --- a/src/PSM3.sol +++ b/src/PSM3.sol @@ -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"; @@ -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 ); } @@ -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 ); } @@ -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); } /**********************************************************************************************/ @@ -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); }