dsa-connectors-2.0/contracts/arbitrum/common/math.sol

55 lines
1.4 KiB
Solidity
Raw Permalink Normal View History

2024-06-22 17:40:51 +00:00
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
import {SafeMath} from "@openzeppelin/contracts/utils/math/SafeMath.sol";
contract DSMath {
2024-06-18 11:46:33 +00:00
uint constant WAD = 10 ** 18;
uint constant RAY = 10 ** 27;
function add(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.add(x, y);
2024-06-18 11:46:33 +00:00
}
function sub(uint x, uint y) internal pure virtual returns (uint z) {
z = SafeMath.sub(x, y);
2024-06-18 11:46:33 +00:00
}
function mul(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.mul(x, y);
2024-06-18 11:46:33 +00:00
}
function div(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.div(x, y);
2024-06-18 11:46:33 +00:00
}
function wmul(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.add(SafeMath.mul(x, y), WAD / 2) / WAD;
2024-06-18 11:46:33 +00:00
}
function wdiv(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.add(SafeMath.mul(x, WAD), y / 2) / y;
2024-06-18 11:46:33 +00:00
}
function rdiv(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.add(SafeMath.mul(x, RAY), y / 2) / y;
2024-06-18 11:46:33 +00:00
}
function rmul(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.add(SafeMath.mul(x, y), RAY / 2) / RAY;
2024-06-18 11:46:33 +00:00
}
function toInt(uint x) internal pure returns (int y) {
y = int(x);
require(y >= 0, "int-overflow");
2024-06-18 11:46:33 +00:00
}
function toUint(int256 x) internal pure returns (uint256) {
require(x >= 0, "int-overflow");
return uint256(x);
2024-06-18 11:46:33 +00:00
}
function toRad(uint wad) internal pure returns (uint rad) {
rad = mul(wad, 10 ** 27);
}
2024-06-22 17:40:51 +00:00
}