Optimized MathUtils, ReserveLogic

This commit is contained in:
The3D 2020-08-22 13:01:41 +02:00
parent 06fbe2d2a4
commit 4d054dd56d
4 changed files with 94 additions and 83 deletions

View File

@ -120,28 +120,30 @@ library ReserveLogic {
* a formal specification.
* @param reserve the reserve object
**/
function updateCumulativeIndexesAndTimestamp(ReserveData storage reserve) internal {
function updateCumulativeIndexesAndTimestamp(ReserveData storage reserve) internal {
uint256 currentLiquidityRate = reserve.currentLiquidityRate;
//only cumulating if there is any income being produced
if (
IERC20(reserve.variableDebtTokenAddress).totalSupply() > 0 ||
IERC20(reserve.stableDebtTokenAddress).totalSupply() > 0
) {
if (currentLiquidityRate > 0) {
uint40 lastUpdateTimestamp = reserve.lastUpdateTimestamp;
uint256 cumulatedLiquidityInterest = MathUtils.calculateLinearInterest(
reserve.currentLiquidityRate,
reserve.lastUpdateTimestamp
currentLiquidityRate,
lastUpdateTimestamp
);
reserve.lastLiquidityIndex = cumulatedLiquidityInterest.rayMul(
reserve.lastLiquidityIndex
);
reserve.lastLiquidityIndex = cumulatedLiquidityInterest.rayMul(reserve.lastLiquidityIndex);
uint256 cumulatedVariableBorrowInterest = MathUtils.calculateCompoundedInterest(
reserve.currentVariableBorrowRate,
reserve.lastUpdateTimestamp
);
reserve.lastVariableBorrowIndex = cumulatedVariableBorrowInterest.rayMul(
reserve.lastVariableBorrowIndex
);
//as the liquidity rate might come only from stable rate loans, we need to ensure
//that there is actual variable debt before accumulating
if (IERC20(reserve.variableDebtTokenAddress).totalSupply() > 0) {
uint256 cumulatedVariableBorrowInterest = MathUtils.calculateCompoundedInterest(
reserve.currentVariableBorrowRate,
lastUpdateTimestamp
);
reserve.lastVariableBorrowIndex = cumulatedVariableBorrowInterest.rayMul(
reserve.lastVariableBorrowIndex
);
}
}
//solium-disable-next-line
@ -198,6 +200,14 @@ library ReserveLogic {
reserve.interestRateStrategyAddress = interestRateStrategyAddress;
}
struct UpdateInterestRatesLocalVars {
uint256 currentAvgStableRate;
uint256 availableLiquidity;
address stableDebtTokenAddress;
uint256 newLiquidityRate;
uint256 newStableRate;
uint256 newVariableRate;
}
/**
* @dev Updates the reserve current stable borrow rate Rf, the current variable borrow rate Rv and the current liquidity rate Rl.
* Also updates the lastUpdateTimestamp value. Please refer to the whitepaper for further information.
@ -211,33 +221,34 @@ library ReserveLogic {
uint256 liquidityAdded,
uint256 liquidityTaken
) internal {
uint256 currentAvgStableRate = IStableDebtToken(reserve.stableDebtTokenAddress)
.getAverageStableRate();
UpdateInterestRatesLocalVars memory vars;
uint256 balance = IERC20(reserveAddress).balanceOf(reserve.aTokenAddress);
vars.stableDebtTokenAddress = reserve.stableDebtTokenAddress;
vars.currentAvgStableRate = IStableDebtToken(vars.stableDebtTokenAddress).getAverageStableRate();
vars.availableLiquidity = IERC20(reserveAddress).balanceOf(reserve.aTokenAddress);
(
uint256 newLiquidityRate,
uint256 newStableRate,
uint256 newVariableRate
vars.newLiquidityRate,
vars.newStableRate,
vars.newVariableRate
) = IReserveInterestRateStrategy(reserve.interestRateStrategyAddress).calculateInterestRates(
reserveAddress,
balance.add(liquidityAdded).sub(liquidityTaken),
IERC20(reserve.stableDebtTokenAddress).totalSupply(),
vars.availableLiquidity.add(liquidityAdded).sub(liquidityTaken),
IERC20(vars.stableDebtTokenAddress).totalSupply(),
IERC20(reserve.variableDebtTokenAddress).totalSupply(),
currentAvgStableRate
vars.currentAvgStableRate
);
reserve.currentLiquidityRate = newLiquidityRate;
reserve.currentStableBorrowRate = newStableRate;
reserve.currentVariableBorrowRate = newVariableRate;
reserve.currentLiquidityRate = vars.newLiquidityRate;
reserve.currentStableBorrowRate = vars.newStableRate;
reserve.currentVariableBorrowRate = vars.newVariableRate;
emit ReserveDataUpdated(
reserveAddress,
newLiquidityRate,
newStableRate,
currentAvgStableRate,
newVariableRate,
vars.newLiquidityRate,
vars.newStableRate,
vars.currentAvgStableRate,
vars.newVariableRate,
reserve.lastLiquidityIndex,
reserve.lastVariableBorrowIndex
);

View File

@ -55,17 +55,17 @@ library MathUtils {
return WadRayMath.ray();
}
uint256 expMinusOne = exp.sub(1);
uint256 expMinusOne = exp-1;
uint256 expMinusTwo = exp > 2 ? exp.sub(2) : 0;
uint256 expMinusTwo = exp > 2 ? exp-2 : 0;
uint256 ratePerSecond = rate.div(31536000);
uint256 ratePerSecond = rate/SECONDS_PER_YEAR;
uint256 basePowerTwo = ratePerSecond.rayMul(ratePerSecond);
uint256 basePowerThree = basePowerTwo.rayMul(ratePerSecond);
uint256 secondTerm = exp.mul(expMinusOne).mul(basePowerTwo).div(2);
uint256 thirdTerm = exp.mul(expMinusOne).mul(expMinusTwo).mul(basePowerThree).div(6);
uint256 secondTerm = exp.mul(expMinusOne).mul(basePowerTwo)/2;
uint256 thirdTerm = exp.mul(expMinusOne).mul(expMinusTwo).mul(basePowerThree)/6;
return WadRayMath.ray().add(ratePerSecond.mul(exp)).add(secondTerm).add(thirdTerm);
}

View File

@ -5,7 +5,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x7B8e91D6e994c222A57ADB9615A5d55F7BEd9f6e",
"address": "0x463Ff14E7AA1312b897638AA40deA4FE95065D9d",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -15,7 +15,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x0Be2E67Ba29F7CA3093386693e0E142B9e6a55Ef",
"address": "0x4B9b22A3Ae2465Fa849cf33fDAA26E73e12d0524",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -25,7 +25,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x02043fC67620cCC132b0CEA385AbBb5aa4e06766",
"address": "0x429444559a38F377d62a74EDB41FA33499cBa254",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -53,7 +53,7 @@
"address": "0x6642B57e4265BAD868C17Fc1d1F4F88DBBA04Aa8"
},
"localhost": {
"address": "0x6EB0cD6b4149d378863EA1bc2F67Fa7d9EF8A734"
"address": "0xeB83C1577c44B99eB783e8eCC740cCcB39e6038a"
}
},
"LendingPoolDataProvider": {
@ -66,7 +66,7 @@
"address": "0xD9273d497eDBC967F39d419461CfcF382a0A822e"
},
"localhost": {
"address": "0xBB44FCfd30C89073F19713a978e451A237aC2e36"
"address": "0xdFeCf1CAA3cDd9852cE7fD029720386bE2d5211e"
}
},
"PriceOracle": {
@ -75,7 +75,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x5fAeB1862A8F53338BB9c5614EE52aee0A3eed3B",
"address": "0x85E78da53D4bdEb2ffF1CD95bfFb5989419a42F0",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -85,7 +85,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x21AA9B6ffD04550C504a70A693D158319385Efe8",
"address": "0xA4FF07Cd85f153004AaD52b39eD96C5Ad9732CBD",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -95,7 +95,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x0c37447827539CA1885B9e3BE76c33590e40833a",
"address": "0x5F81EB3b93AC6D83aCe2Ae179A936F6A8ECb2651",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -105,7 +105,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x025acC37dA555270B821260F39539937085F13D6",
"address": "0x07C1cd8182AAda58009D3b547295A64046679666",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -115,7 +115,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x049F2C09e1d8C2ba59BE6A7Ff069B3632171a4dc",
"address": "0xF25e04520a404a7C93194fE45aCB370E2168E73A",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -169,7 +169,7 @@
"address": "0x2B681757d757fbB80cc51c6094cEF5eE75bF55aA"
},
"localhost": {
"address": "0xBB36dAA26Fcfc04CAC1dAcD460AF09Df3622FF51"
"address": "0x03049DF4d8730C375CAe2c13a542cCE873369e20"
}
},
"WalletBalanceProvider": {
@ -178,7 +178,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x81EDb206d8172f85d62fc91d03B5ae6C73CeF75B",
"address": "0x99b0df288A2Ddf84850157b04ef653833e668abE",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -188,7 +188,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x5aFF0C1AC4662850FDd2373fad858616Ef8fD459",
"address": "0x377eb7cBF694dd5a81c0381d4275d47941cc30F0",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -198,7 +198,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x1F1Fb19B5209E95Cd97Af747072eA6Ed362DF1d6",
"address": "0x052C908CD1fE0944428598f923289b9069D949b7",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -208,7 +208,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x6876B8Bc59cb68A5cAB8C4F9983Ee023E0726D2E",
"address": "0x3e2b7c9bbfF5bb238f7Ee1Da97b4Ff7f4B367d1F",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -218,7 +218,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x58741177c588c5304a9dd02A7BAF7cB19962cA9d",
"address": "0x803B0Efe9d0D03d814f22a1ce6934ce00C6ad34E",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -228,7 +228,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x888c0eEFc330b0B25eAfe5098DfcE04902142925",
"address": "0x74cAC7EE27ad5e6fa3157df4968BB51D6Fc71105",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -238,7 +238,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x283BF0d396dB5a0d4477817fd99D4198FCf48836",
"address": "0x34634C72A8a87F8901B4B5669E7B1Ddc85e4D94d",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -248,7 +248,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xcb17C9195d26e2d9c35Fd2202FfAd723Eb6b9B13",
"address": "0xA191888d7d41e93db29D05507F0a81D6B01e9C87",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -258,7 +258,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x61f131d9Eea8EB1F606035569471D4e7fed03eC4",
"address": "0x7077FAaD4f62226913c99971C56885c9Ccc4A272",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -268,7 +268,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x8720da7Bc69d35800937CD0CB2a88517Ab681a34",
"address": "0x8Ec0d65FA416f7F38Ea82768c2F242426Cf25F26",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -278,7 +278,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x9005f841b010be4f5e9AAaf740B7B7b0611c2E79",
"address": "0x0E287EACa131b5d80FA3d73e3d55a657bee2f5ee",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -288,7 +288,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x60cBD760B2Fd5bd4503D33710eB7A67c4b878099",
"address": "0x88A3c52bdD1f300D7471683d058b9C086A0477f2",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -298,7 +298,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xF2568BDC779A28534FfDE719edeBb6FaD8750C9C",
"address": "0xd014958E8666bAc96134EE289C04A0F246aaE606",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -308,7 +308,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x0fB27075d4F9361E175459334c0D77A81cD9C835",
"address": "0x495719D5350d7E7bc192DfCAb32E77C7e35534C3",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -318,7 +318,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xE8a2Cf61d731Cf9f46Dc34F64538229C41865146",
"address": "0xcc714eA59Ce23ed56A5909dadEbe6EB8323C8111",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -328,7 +328,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x0326Ab87B77A453569B5CA1686a92f9dCAfC08b6",
"address": "0xbe0b1af73419afC9754e8cA9B0Cf2C11b1A4f3AE",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -338,7 +338,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x5f3dCDFEdCcAaa98AfE9FAbb5ac348D4FbCa8Be8",
"address": "0x754159f9ae9277ca60E55ab0b06862Fb513d87B7",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -348,7 +348,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x5033b2C3b7Fc8C359175158Dde0a57fB86C6eCb4",
"address": "0xdee2a1ccb44676064284b424313bBdf1673ab0a2",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -358,7 +358,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x20F17A5F6764149Ac22E17AD2b7D68A3232974bE",
"address": "0xdb86D18F071330B129F6E456DFF37231044BFB05",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -368,7 +368,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x6A3c3947F3E89BEAB768458b50B06ceB3CFC4539",
"address": "0x05Fc03089bdf5E55E8864CA5df0d9D728d880842",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -378,7 +378,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x54fa46633E6F369e4Bf26560d20AF698b84F3676",
"address": "0x20cB8a49019650A2bCF31E8E43925FDeaCd4e9b0",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -388,7 +388,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xCE05F088253a85e86491bc6267E99304B8941663",
"address": "0xFab2Da6dc0B0a846848A8cF2799ba85D4309D073",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -398,7 +398,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xA7e7aa6Cf177b8081B0077AfF3EC748F27cBAfc8",
"address": "0xFD34D9D62dF5f8867ac399637C32EB6F91efA697",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -408,7 +408,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x7B8e91D6e994c222A57ADB9615A5d55F7BEd9f6e",
"address": "0x463Ff14E7AA1312b897638AA40deA4FE95065D9d",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -417,7 +417,7 @@
"address": "0xDf73fC454FA018051D4a1509e63D11530A59DE10"
},
"localhost": {
"address": "0xd2b69b0ba7d62f6122B3FCdc3c79C15A1E51E9e2"
"address": "0xe3962a83c698CC25DFF81F98B08a9c2e749B367C"
}
},
"StableDebtToken": {
@ -426,7 +426,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x8330f3ab4680A70C76Fa55D886155f39c6800aE4",
"address": "0xC277bEF631f1B3F6F4D05a125Cbcb27bDA349240",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -436,13 +436,13 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xCafc5D24cf5a0aFd027C1c3aEE54FD844b5Eb2d0",
"address": "0xFa0FaAC558C92d751aD68E6C90FEf09fdA204749",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
"AToken": {
"localhost": {
"address": "0x1b12f84d85e5EFdF07F992ACe35E832F630Ed4b7",
"address": "0xcc130B9925E9c083df4fc6dC3f60BE3d6B68Bf13",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"buidlerevm": {
@ -456,7 +456,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0x3a8e062Df7c52d69654e36d412131aa73aE8677b",
"address": "0xDf5b1bff699b50c68F36aC9817d2b2988554013e",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -466,7 +466,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xd0975173C2a54Bf501f2a9253b59Fb006f73f54A",
"address": "0xF11D0A572031bE913eb3A36B6337098fA9532721",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -476,7 +476,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xF11Ca2128CC189FcD2315A7D652BB9B4e0a88530",
"address": "0xF68e61A94d6e5fa2b5061156a51a6714A30b13FA",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
},
@ -486,7 +486,7 @@
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
"address": "0xc0099450FDd004D080655eAacB83E2A846E18D1B",
"address": "0x94FD7F068bB3F75e9FD4f77A13ddaF2B0ef99f0c",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}
}

View File

@ -1459,8 +1459,8 @@ const calcExpectedLiquidityIndex = (reserveData: ReserveData, timestamp: BigNumb
};
const calcExpectedVariableBorrowIndex = (reserveData: ReserveData, timestamp: BigNumber) => {
//if utilization rate is 0, nothing to compound
if (reserveData.utilizationRate.eq('0')) {
//if totalBorrowsVariable is 0, nothing to compound
if (reserveData.totalBorrowsVariable.eq('0')) {
return reserveData.variableBorrowIndex;
}