Removed SafeMath from WadRayMath, PercentageMath

This commit is contained in:
The3D 2020-08-22 12:04:34 +02:00
parent 45cb9ab680
commit 06fbe2d2a4
5 changed files with 2879 additions and 58 deletions

View File

@ -8,7 +8,7 @@ usePlugin('buidler-typechain');
usePlugin('solidity-coverage');
usePlugin('@nomiclabs/buidler-waffle');
usePlugin('@nomiclabs/buidler-etherscan');
//usePlugin('buidler-gas-reporter');
usePlugin('buidler-gas-reporter');
const DEFAULT_BLOCK_GAS_LIMIT = 10000000;
const DEFAULT_GAS_PRICE = 10;

View File

@ -1,8 +1,6 @@
// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.6.8;
import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol';
/**
* @title PercentageMath library
* @author Aave
@ -12,7 +10,6 @@ import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol';
**/
library PercentageMath {
using SafeMath for uint256;
uint256 constant PERCENTAGE_FACTOR = 1e4; //percentage plus two decimals
uint256 constant HALF_PERCENT = PERCENTAGE_FACTOR / 2;
@ -24,7 +21,19 @@ library PercentageMath {
* @return the percentage of value
**/
function percentMul(uint256 value, uint256 percentage) internal pure returns (uint256) {
return HALF_PERCENT.add(value.mul(percentage)).div(PERCENTAGE_FACTOR);
if(value == 0){
return 0;
}
uint256 result = value*percentage;
require(result/value == percentage, "PercentageMath: Multiplication overflow");
result+=HALF_PERCENT;
require(result >= HALF_PERCENT, "PercentageMath: Addition overflow");
return result/PERCENTAGE_FACTOR;
}
/**
@ -34,8 +43,17 @@ library PercentageMath {
* @return the value divided the percentage
**/
function percentDiv(uint256 value, uint256 percentage) internal pure returns (uint256) {
require(percentage != 0, "PercentageMath: Division by 0");
uint256 halfPercentage = percentage / 2;
uint256 result = value*PERCENTAGE_FACTOR;
return halfPercentage.add(value.mul(PERCENTAGE_FACTOR)).div(percentage);
require(result/PERCENTAGE_FACTOR == value, "PercentageMath: Multiplication overflow");
result += halfPercentage;
require(result >= halfPercentage, "PercentageMath: Addition overflow");
return result/percentage;
}
}

View File

@ -1,8 +1,6 @@
// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.6.8;
import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol';
/**
* @title WadRayMath library
* @author Aave
@ -10,7 +8,6 @@ import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol';
**/
library WadRayMath {
using SafeMath for uint256;
uint256 internal constant WAD = 1e18;
uint256 internal constant halfWAD = WAD / 2;
@ -56,7 +53,20 @@ library WadRayMath {
* @return the result of a*b, in wad
**/
function wadMul(uint256 a, uint256 b) internal pure returns (uint256) {
return halfWAD.add(a.mul(b)).div(WAD);
if(a == 0){
return 0;
}
uint256 result = a*b;
require(result/a == b, "WadRayMath: Multiplication overflow");
result+=halfWAD;
require(result >= halfWAD, "WadRayMath: Addition overflow");
return result/WAD;
}
/**
@ -66,9 +76,19 @@ library WadRayMath {
* @return the result of a/b, in wad
**/
function wadDiv(uint256 a, uint256 b) internal pure returns (uint256) {
require(b != 0, "WadRayMath: Division by 0");
uint256 halfB = b / 2;
return halfB.add(a.mul(WAD)).div(b);
uint256 result = a*WAD;
require(result/WAD == a, "WadRayMath: Multiplication overflow");
result += halfB;
require(result >= halfB, "WadRayMath: Addition overflow");
return result/b;
}
/**
@ -78,7 +98,19 @@ library WadRayMath {
* @return the result of a*b, in ray
**/
function rayMul(uint256 a, uint256 b) internal pure returns (uint256) {
return halfRAY.add(a.mul(b)).div(RAY);
if(a == 0){
return 0;
}
uint256 result = a*b;
require(result/a == b, "WadRayMath: Multiplication overflow");
result+=halfRAY;
require(result >= halfRAY, "WadRayMath: Addition overflow");
return result/RAY;
}
/**
@ -88,9 +120,20 @@ library WadRayMath {
* @return the result of a/b, in ray
**/
function rayDiv(uint256 a, uint256 b) internal pure returns (uint256) {
require(b != 0, "WadRayMath: Division by 0");
uint256 halfB = b / 2;
return halfB.add(a.mul(RAY)).div(b);
uint256 result = a*RAY;
require(result/RAY == a, "WadRayMath: Multiplication overflow");
result += halfB;
require(result >= halfB, "WadRayMath: Addition overflow");
return result/b;
}
/**
@ -100,8 +143,10 @@ library WadRayMath {
**/
function rayToWad(uint256 a) internal pure returns (uint256) {
uint256 halfRatio = WAD_RAY_RATIO / 2;
uint256 result = halfRatio+a;
require(result >= halfRatio, "WadRayMath: Addition overflow");
return halfRatio.add(a).div(WAD_RAY_RATIO);
return result/WAD_RAY_RATIO;
}
/**
@ -110,6 +155,8 @@ library WadRayMath {
* @return a converted in ray
**/
function wadToRay(uint256 a) internal pure returns (uint256) {
return a.mul(WAD_RAY_RATIO);
uint256 result = a*WAD_RAY_RATIO;
require(result/WAD_RAY_RATIO == a, "WadRayMath: Multiplication overflow");
return result;
}
}

View File

@ -5,7 +5,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x58F132FBB86E21545A4Bace3C19f1C05d86d7A22",
"address": "0x7B8e91D6e994c222A57ADB9615A5d55F7BEd9f6e",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -15,7 +15,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xa4bcDF64Cdd5451b6ac3743B414124A6299B65FF",
"address": "0x0Be2E67Ba29F7CA3093386693e0E142B9e6a55Ef",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -25,7 +25,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x5A0773Ff307Bf7C71a832dBB5312237fD3437f9F",
"address": "0x02043fC67620cCC132b0CEA385AbBb5aa4e06766",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -53,7 +53,7 @@
"address": "0x6642B57e4265BAD868C17Fc1d1F4F88DBBA04Aa8"
},
"localhost": {
"address": "0x9EC0480CF106d6dc1c7849BA141a56F874170F97"
"address": "0x6EB0cD6b4149d378863EA1bc2F67Fa7d9EF8A734"
}
},
"LendingPoolDataProvider": {
@ -66,7 +66,7 @@
"address": "0xD9273d497eDBC967F39d419461CfcF382a0A822e"
},
"localhost": {
"address": "0x6642B57e4265BAD868C17Fc1d1F4F88DBBA04Aa8"
"address": "0xBB44FCfd30C89073F19713a978e451A237aC2e36"
}
},
"PriceOracle": {
@ -75,7 +75,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x099d9fF8F818290C8b5B7Db5bFca84CEebd2714c",
"address": "0x5fAeB1862A8F53338BB9c5614EE52aee0A3eed3B",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -85,7 +85,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xAF6BA11790D1942625C0c2dA07da19AB63845cfF",
"address": "0x21AA9B6ffD04550C504a70A693D158319385Efe8",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -95,7 +95,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xD83D2773a7873ae2b5f8Fb92097e20a8C64F691E",
"address": "0x0c37447827539CA1885B9e3BE76c33590e40833a",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -105,7 +105,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xf91aC1098F3b154671Ce83290114aaE45ac0225f",
"address": "0x025acC37dA555270B821260F39539937085F13D6",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -115,7 +115,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x830bceA96E56DBC1F8578f75fBaC0AF16B32A07d",
"address": "0x049F2C09e1d8C2ba59BE6A7Ff069B3632171a4dc",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -169,7 +169,7 @@
"address": "0x2B681757d757fbB80cc51c6094cEF5eE75bF55aA"
},
"localhost": {
"address": "0x3bDA11B584dDff7F66E0cFe1da1562c92B45db60"
"address": "0xBB36dAA26Fcfc04CAC1dAcD460AF09Df3622FF51"
}
},
"WalletBalanceProvider": {
@ -178,7 +178,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x392E5355a0e88Bd394F717227c752670fb3a8020",
"address": "0x81EDb206d8172f85d62fc91d03B5ae6C73CeF75B",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -188,7 +188,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x7c2C195CD6D34B8F845992d380aADB2730bB9C6F",
"address": "0x5aFF0C1AC4662850FDd2373fad858616Ef8fD459",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -198,7 +198,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x8858eeB3DfffA017D4BCE9801D340D36Cf895CCf",
"address": "0x1F1Fb19B5209E95Cd97Af747072eA6Ed362DF1d6",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -208,7 +208,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x0078371BDeDE8aAc7DeBfFf451B74c5EDB385Af7",
"address": "0x6876B8Bc59cb68A5cAB8C4F9983Ee023E0726D2E",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -218,7 +218,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xf4e77E5Da47AC3125140c470c71cBca77B5c638c",
"address": "0x58741177c588c5304a9dd02A7BAF7cB19962cA9d",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -228,7 +228,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x3619DbE27d7c1e7E91aA738697Ae7Bc5FC3eACA5",
"address": "0x888c0eEFc330b0B25eAfe5098DfcE04902142925",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -238,7 +238,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x038B86d9d8FAFdd0a02ebd1A476432877b0107C8",
"address": "0x283BF0d396dB5a0d4477817fd99D4198FCf48836",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -248,7 +248,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x1A1FEe7EeD918BD762173e4dc5EfDB8a78C924A8",
"address": "0xcb17C9195d26e2d9c35Fd2202FfAd723Eb6b9B13",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -258,7 +258,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x500D1d6A4c7D8Ae28240b47c8FCde034D827fD5e",
"address": "0x61f131d9Eea8EB1F606035569471D4e7fed03eC4",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -268,7 +268,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xc4905364b78a742ccce7B890A89514061E47068D",
"address": "0x8720da7Bc69d35800937CD0CB2a88517Ab681a34",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -278,7 +278,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xD6C850aeBFDC46D7F4c207e445cC0d6B0919BDBe",
"address": "0x9005f841b010be4f5e9AAaf740B7B7b0611c2E79",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -288,7 +288,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x8B5B7a6055E54a36fF574bbE40cf2eA68d5554b3",
"address": "0x60cBD760B2Fd5bd4503D33710eB7A67c4b878099",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -298,7 +298,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xEcc0a6dbC0bb4D51E4F84A315a9e5B0438cAD4f0",
"address": "0xF2568BDC779A28534FfDE719edeBb6FaD8750C9C",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -308,7 +308,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x20Ce94F404343aD2752A2D01b43fa407db9E0D00",
"address": "0x0fB27075d4F9361E175459334c0D77A81cD9C835",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -318,7 +318,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x1d80315fac6aBd3EfeEbE97dEc44461ba7556160",
"address": "0xE8a2Cf61d731Cf9f46Dc34F64538229C41865146",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -328,7 +328,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x2D8553F9ddA85A9B3259F6Bf26911364B85556F5",
"address": "0x0326Ab87B77A453569B5CA1686a92f9dCAfC08b6",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -338,7 +338,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x52d3b94181f8654db2530b0fEe1B19173f519C52",
"address": "0x5f3dCDFEdCcAaa98AfE9FAbb5ac348D4FbCa8Be8",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -348,7 +348,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xd15468525c35BDBC1eD8F2e09A00F8a173437f2f",
"address": "0x5033b2C3b7Fc8C359175158Dde0a57fB86C6eCb4",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -358,7 +358,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x7e35Eaf7e8FBd7887ad538D4A38Df5BbD073814a",
"address": "0x20F17A5F6764149Ac22E17AD2b7D68A3232974bE",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -368,7 +368,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x5bcb88A0d20426e451332eE6C4324b0e663c50E0",
"address": "0x6A3c3947F3E89BEAB768458b50B06ceB3CFC4539",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -378,7 +378,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x3521eF8AaB0323004A6dD8b03CE890F4Ea3A13f5",
"address": "0x54fa46633E6F369e4Bf26560d20AF698b84F3676",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -388,7 +388,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x53369fd4680FfE3DfF39Fc6DDa9CfbfD43daeA2E",
"address": "0xCE05F088253a85e86491bc6267E99304B8941663",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -398,7 +398,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xB00cC45B4a7d3e1FEE684cFc4417998A1c183e6d",
"address": "0xA7e7aa6Cf177b8081B0077AfF3EC748F27cBAfc8",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -408,7 +408,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x58F132FBB86E21545A4Bace3C19f1C05d86d7A22",
"address": "0x7B8e91D6e994c222A57ADB9615A5d55F7BEd9f6e",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -417,7 +417,7 @@
"address": "0xDf73fC454FA018051D4a1509e63D11530A59DE10"
},
"localhost": {
"address": "0x3b050AFb4ac4ACE646b31fF3639C1CD43aC31460"
"address": "0xd2b69b0ba7d62f6122B3FCdc3c79C15A1E51E9e2"
}
},
"StableDebtToken": {
@ -426,7 +426,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xA0AB1cB92A4AF81f84dCd258155B5c25D247b54E",
"address": "0x8330f3ab4680A70C76Fa55D886155f39c6800aE4",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -436,13 +436,13 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x5f7134cd38C826a7649f9Cc47dda24d834DD2967",
"address": "0xCafc5D24cf5a0aFd027C1c3aEE54FD844b5Eb2d0",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
"AToken": {
"localhost": {
"address": "0xE91bBe8ee03560E3dda2786f95335F5399813Ca0",
"address": "0x1b12f84d85e5EFdF07F992ACe35E832F630Ed4b7",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"buidlerevm": {
@ -456,7 +456,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x7f23223A2FAf869962B38f5eC4aAB7f37454A45e",
"address": "0x3a8e062Df7c52d69654e36d412131aa73aE8677b",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -466,7 +466,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xf784709d2317D872237C4bC22f867d1BAe2913AB",
"address": "0xd0975173C2a54Bf501f2a9253b59Fb006f73f54A",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -476,7 +476,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x1203D1b97BF6E546c00C45Cda035D3010ACe1180",
"address": "0xF11Ca2128CC189FcD2315A7D652BB9B4e0a88530",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -486,7 +486,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x8733AfE8174BA7c04c6CD694bD673294079b7E10",
"address": "0xc0099450FDd004D080655eAacB83E2A846E18D1B",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
}

2756
test.log Normal file

File diff suppressed because it is too large Load Diff