|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity >=0.8.0; |
| 3 | + |
| 4 | +import { Math } from "@openzeppelin/contracts/utils/math/Math.sol"; |
| 5 | + |
| 6 | +import { IRateComputer } from "adrastia-periphery/rates/IRateComputer.sol"; |
| 7 | + |
| 8 | +import { BaseTest } from "../config/BaseTest.t.sol"; |
| 9 | + |
| 10 | +import { PrudentiaInterestRateModel } from "../../ionic/irms/PrudentiaInterestRateModel.sol"; |
| 11 | + |
| 12 | +contract MockRateComputer is IRateComputer { |
| 13 | + mapping(address => uint64) public rates; |
| 14 | + |
| 15 | + function computeRate(address token) external view override returns (uint64) { |
| 16 | + return rates[token]; |
| 17 | + } |
| 18 | + |
| 19 | + function setRate(address token, uint64 rate) public { |
| 20 | + rates[token] = rate; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +contract PrudentiaIrmTest is BaseTest { |
| 25 | + using Math for uint64; |
| 26 | + |
| 27 | + MockRateComputer rateComputer; |
| 28 | + address token; |
| 29 | + PrudentiaInterestRateModel irm; |
| 30 | + uint256 blocksPerYear; |
| 31 | + |
| 32 | + function setUp() public { |
| 33 | + rateComputer = new MockRateComputer(); |
| 34 | + token = address(0x1); |
| 35 | + blocksPerYear = 10512000; |
| 36 | + irm = new PrudentiaInterestRateModel(blocksPerYear, token, rateComputer); |
| 37 | + } |
| 38 | + |
| 39 | + function test_utilizationRate_zeroTotal() public { |
| 40 | + uint256 cash = 0; |
| 41 | + uint256 borrows = 0; |
| 42 | + uint256 reserves = 0; |
| 43 | + |
| 44 | + assertEq(irm.utilizationRate(cash, borrows, reserves), 0); |
| 45 | + } |
| 46 | + |
| 47 | + function test_utilizationRate_zero() public { |
| 48 | + uint256 cash = 100; |
| 49 | + uint256 borrows = 0; |
| 50 | + uint256 reserves = 0; |
| 51 | + |
| 52 | + assertEq(irm.utilizationRate(cash, borrows, reserves), 0); |
| 53 | + } |
| 54 | + |
| 55 | + function test_utilizationRate_50() public { |
| 56 | + uint256 cash = 100; |
| 57 | + uint256 borrows = 100; |
| 58 | + uint256 reserves = 0; |
| 59 | + |
| 60 | + assertEq(irm.utilizationRate(cash, borrows, reserves), 5e17); |
| 61 | + } |
| 62 | + |
| 63 | + function test_utilizationRate_100() public { |
| 64 | + uint256 cash = 0; |
| 65 | + uint256 borrows = 100; |
| 66 | + uint256 reserves = 0; |
| 67 | + |
| 68 | + assertEq(irm.utilizationRate(cash, borrows, reserves), 1e18); |
| 69 | + } |
| 70 | + |
| 71 | + function test_getBorrowRate_100_a() public { |
| 72 | + uint64 rate = 1e18; |
| 73 | + rateComputer.setRate(token, rate); |
| 74 | + |
| 75 | + // These should have no effect |
| 76 | + uint256 cash = 0; |
| 77 | + uint256 borrows = 100; |
| 78 | + uint256 reserves = 0; |
| 79 | + |
| 80 | + assertEq(irm.getBorrowRate(cash, borrows, reserves), rate.ceilDiv(blocksPerYear)); |
| 81 | + } |
| 82 | + |
| 83 | + function test_getBorrowRate_100_b() public { |
| 84 | + uint64 rate = 1e18; |
| 85 | + rateComputer.setRate(token, rate); |
| 86 | + |
| 87 | + // These should have no effect |
| 88 | + uint256 cash = 100; |
| 89 | + uint256 borrows = 100; |
| 90 | + uint256 reserves = 0; |
| 91 | + |
| 92 | + assertEq(irm.getBorrowRate(cash, borrows, reserves), rate.ceilDiv(blocksPerYear)); |
| 93 | + } |
| 94 | + |
| 95 | + function test_getBorrowRate_50() public { |
| 96 | + uint64 rate = 5e17; |
| 97 | + rateComputer.setRate(token, rate); |
| 98 | + |
| 99 | + // These should have no effect |
| 100 | + uint256 cash = 0; |
| 101 | + uint256 borrows = 0; |
| 102 | + uint256 reserves = 0; |
| 103 | + |
| 104 | + assertEq(irm.getBorrowRate(cash, borrows, reserves), rate.ceilDiv(blocksPerYear)); |
| 105 | + } |
| 106 | + |
| 107 | + function test_getBorrowRate_1() public { |
| 108 | + uint64 rate = 1e16; |
| 109 | + rateComputer.setRate(token, rate); |
| 110 | + |
| 111 | + // These should have no effect |
| 112 | + uint256 cash = 0; |
| 113 | + uint256 borrows = 0; |
| 114 | + uint256 reserves = 0; |
| 115 | + |
| 116 | + assertEq(irm.getBorrowRate(cash, borrows, reserves), rate.ceilDiv(blocksPerYear)); |
| 117 | + } |
| 118 | + |
| 119 | + function test_getBorrowRate_0() public { |
| 120 | + uint64 rate = 0; |
| 121 | + rateComputer.setRate(token, rate); |
| 122 | + |
| 123 | + // These should have no effect |
| 124 | + uint256 cash = 0; |
| 125 | + uint256 borrows = 0; |
| 126 | + uint256 reserves = 0; |
| 127 | + |
| 128 | + assertEq(irm.getBorrowRate(cash, borrows, reserves), rate.ceilDiv(blocksPerYear)); |
| 129 | + } |
| 130 | + |
| 131 | + function test_getBorrowRate_1mantissa() public { |
| 132 | + uint64 rate = 1; |
| 133 | + rateComputer.setRate(token, rate); |
| 134 | + |
| 135 | + // These should have no effect |
| 136 | + uint256 cash = 0; |
| 137 | + uint256 borrows = 0; |
| 138 | + uint256 reserves = 0; |
| 139 | + |
| 140 | + assertEq(irm.getBorrowRate(cash, borrows, reserves), 1); // Rounds up to 1. We don't want to return 0. |
| 141 | + } |
| 142 | + |
| 143 | + function test_getSupplyRate_100_100util() public { |
| 144 | + uint64 rate = 1e18; |
| 145 | + rateComputer.setRate(token, rate); |
| 146 | + |
| 147 | + uint256 cash = 0; |
| 148 | + uint256 borrows = 100; |
| 149 | + uint256 reserves = 0; |
| 150 | + uint256 reserveFactorMantissa = 0; |
| 151 | + |
| 152 | + assertEq(irm.getSupplyRate(cash, borrows, reserves, reserveFactorMantissa), rate.ceilDiv(blocksPerYear)); |
| 153 | + } |
| 154 | + |
| 155 | + function test_getSupplyRate_100_50util() public { |
| 156 | + uint64 rate = 1e18; |
| 157 | + rateComputer.setRate(token, rate); |
| 158 | + |
| 159 | + uint256 cash = 100; |
| 160 | + uint256 borrows = 100; |
| 161 | + uint256 reserves = 0; |
| 162 | + uint256 reserveFactorMantissa = 0; |
| 163 | + |
| 164 | + assertEq(irm.getSupplyRate(cash, borrows, reserves, reserveFactorMantissa), rate.ceilDiv(blocksPerYear) / 2); |
| 165 | + } |
| 166 | + |
| 167 | + function test_getSupplyRate_100_1util() public { |
| 168 | + uint64 rate = 1e18; |
| 169 | + rateComputer.setRate(token, rate); |
| 170 | + |
| 171 | + uint256 cash = 99; |
| 172 | + uint256 borrows = 1; |
| 173 | + uint256 reserves = 0; |
| 174 | + uint256 reserveFactorMantissa = 0; |
| 175 | + |
| 176 | + assertEq(irm.getSupplyRate(cash, borrows, reserves, reserveFactorMantissa), rate.ceilDiv(blocksPerYear) / 100); |
| 177 | + } |
| 178 | + |
| 179 | + function test_getSupplyRate_100_0util() public { |
| 180 | + uint64 rate = 1e18; |
| 181 | + rateComputer.setRate(token, rate); |
| 182 | + |
| 183 | + uint256 cash = 100; |
| 184 | + uint256 borrows = 0; |
| 185 | + uint256 reserves = 0; |
| 186 | + uint256 reserveFactorMantissa = 0; |
| 187 | + |
| 188 | + assertEq(irm.getSupplyRate(cash, borrows, reserves, reserveFactorMantissa), 0); |
| 189 | + } |
| 190 | + |
| 191 | + function test_getSupplyRate_0_0util() public { |
| 192 | + uint64 rate = 0; |
| 193 | + rateComputer.setRate(token, rate); |
| 194 | + |
| 195 | + uint256 cash = 0; |
| 196 | + uint256 borrows = 0; |
| 197 | + uint256 reserves = 0; |
| 198 | + uint256 reserveFactorMantissa = 0; |
| 199 | + |
| 200 | + assertEq(irm.getSupplyRate(cash, borrows, reserves, reserveFactorMantissa), 0); |
| 201 | + } |
| 202 | + |
| 203 | + function test_getSupplyRate_0_100util() public { |
| 204 | + uint64 rate = 0; |
| 205 | + rateComputer.setRate(token, rate); |
| 206 | + |
| 207 | + uint256 cash = 0; |
| 208 | + uint256 borrows = 100; |
| 209 | + uint256 reserves = 0; |
| 210 | + uint256 reserveFactorMantissa = 0; |
| 211 | + |
| 212 | + assertEq(irm.getSupplyRate(cash, borrows, reserves, reserveFactorMantissa), 0); |
| 213 | + } |
| 214 | + |
| 215 | + function test_getSupplyRate_0_50util() public { |
| 216 | + uint64 rate = 0; |
| 217 | + rateComputer.setRate(token, rate); |
| 218 | + |
| 219 | + uint256 cash = 50; |
| 220 | + uint256 borrows = 50; |
| 221 | + uint256 reserves = 0; |
| 222 | + uint256 reserveFactorMantissa = 0; |
| 223 | + |
| 224 | + assertEq(irm.getSupplyRate(cash, borrows, reserves, reserveFactorMantissa), 0); |
| 225 | + } |
| 226 | + |
| 227 | + function test_getSupplyRate_0_1util() public { |
| 228 | + uint64 rate = 0; |
| 229 | + rateComputer.setRate(token, rate); |
| 230 | + |
| 231 | + uint256 cash = 99; |
| 232 | + uint256 borrows = 1; |
| 233 | + uint256 reserves = 0; |
| 234 | + uint256 reserveFactorMantissa = 0; |
| 235 | + |
| 236 | + assertEq(irm.getSupplyRate(cash, borrows, reserves, reserveFactorMantissa), 0); |
| 237 | + } |
| 238 | + |
| 239 | + function test_getSupplyRate_100_50util_10rf() public { |
| 240 | + uint64 rate = 1e18; |
| 241 | + rateComputer.setRate(token, rate); |
| 242 | + |
| 243 | + uint256 cash = 100; |
| 244 | + uint256 borrows = 100; |
| 245 | + uint256 reserves = 0; |
| 246 | + uint256 reserveFactorMantissa = 1e17; |
| 247 | + |
| 248 | + assertEq( |
| 249 | + irm.getSupplyRate(cash, borrows, reserves, reserveFactorMantissa), |
| 250 | + (uint256(rate.ceilDiv(blocksPerYear) / 2) * (1e18 - reserveFactorMantissa)) / 1e18 |
| 251 | + ); |
| 252 | + } |
| 253 | + |
| 254 | + function test_getSupplyRate_100_50util_10rf_10reserves() public { |
| 255 | + uint64 rate = 1e18; |
| 256 | + rateComputer.setRate(token, rate); |
| 257 | + |
| 258 | + uint256 cash = 100; |
| 259 | + uint256 borrows = 100; |
| 260 | + uint256 reserves = 10; |
| 261 | + cash += reserves; |
| 262 | + uint256 reserveFactorMantissa = 1e17; |
| 263 | + |
| 264 | + assertEq( |
| 265 | + irm.getSupplyRate(cash, borrows, reserves, reserveFactorMantissa), |
| 266 | + (uint256(rate.ceilDiv(blocksPerYear) / 2) * (1e18 - reserveFactorMantissa)) / 1e18 |
| 267 | + ); |
| 268 | + } |
| 269 | +} |
0 commit comments