diff --git a/src/plans/D3MCompoundPlan.sol b/src/plans/D3MCompoundPlan.sol index a9495bbf..f4b4347c 100644 --- a/src/plans/D3MCompoundPlan.sol +++ b/src/plans/D3MCompoundPlan.sol @@ -23,7 +23,7 @@ // apy to rate per block off-chain conversion info: // apy = ((1 + (borrowRatePerBlock / WAD) * blocksPerDay) ^ 365 - 1) * 100 // (0) borrowRatePerBlock = ((((apy / 100) + 1) ^ (1 / 365) - 1) / blocksPerDay) * WAD -// ATTOW the formula matches the borrow apy on (https://compound.finance/markets/DAI) using blocksPerDay = 6570 +// ATTOW the formula matches the borrow apy on (https://compound.finance/markets/DAI) using blocksPerDay = 7200 // // https://github.com/compound-finance/compound-protocol/blob/a3214f67b73310d547e00fc578e8355911c9d376/contracts/BaseJumpRateModelV2.sol#L96 // diff --git a/src/tests/integration/D3MCompound.t.sol b/src/tests/integration/D3MCompound.t.sol index c0a4da30..f73b1949 100644 --- a/src/tests/integration/D3MCompound.t.sol +++ b/src/tests/integration/D3MCompound.t.sol @@ -45,6 +45,8 @@ interface CErc20Like { function balanceOfUnderlying(address owner) external returns (uint256); function repayBorrow(uint256 repayAmount) external returns (uint256); function exchangeRateCurrent() external returns (uint256); + function accrueInterest() external returns (uint256); + function borrowBalanceCurrent(address) external returns (uint256); } interface CEthLike { @@ -322,6 +324,30 @@ contract D3MCompoundTest is DSSTest { assertEq(vat.dai(address(d3mHub)), 0); } + function test_borrow_apy() public { + // target 2% borrow apy, see top of D3MCompoundPlan for the formula explanation + uint256 targetBorrowRate = 7535450719; // ((2.00 / 100) + 1) ^ (1 / 365) - 1) / 7200) * 10^18 + + d3mCompoundPlan.file("barb", targetBorrowRate); + d3mHub.exec(ilk); + assertEqInterest(getBorrowRate(), targetBorrowRate); + + cDai.borrow(1 * WAD); + + uint256 borrowBalanceBefore = cDai.borrowBalanceCurrent(address(this)); + + // fast forward 1 year while accruing interest each day + for (uint256 i = 1; i <= 365; i++) { + vm.roll(block.number + 7200); + cDai.accrueInterest(); + } + + uint256 borrowBalanceAfter = cDai.borrowBalanceCurrent(address(this)); + + // rates compound so we tolerate a larger difference + assertEqApprox(borrowBalanceAfter, borrowBalanceBefore * 102 / 100, INTEREST_RATE_TOLERANCE * 5); + } + function test_barb_zero() public { uint256 targetBorrowRate = _setRelBorrowTarget(7500);