Skip to content

Commit

Permalink
Add test_borrow_apy (#91)
Browse files Browse the repository at this point in the history
* Add test_borrow_apy

* hevm => vm

* Make comment about Compound UI a bit clearer

* Add back a test that was falsely removed

Co-authored-by: rockyfour <[email protected]>
  • Loading branch information
rockyfour and rockyfour authored Nov 23, 2022
1 parent 42c4620 commit 4de0e6c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/plans/D3MCompoundPlan.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down
26 changes: 26 additions & 0 deletions src/tests/integration/D3MCompound.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 4de0e6c

Please sign in to comment.