Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test_borrow_apy #91

Merged
merged 4 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/plans/D3MCompoundPlan.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
// 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
// Initially the formula matched the borrow apy on (https://compound.finance/markets/DAI) using blocksPerDay = 6570
// blocksPerDay was later updated to 7200.
//
// https://github.com/compound-finance/compound-protocol/blob/a3214f67b73310d547e00fc578e8355911c9d376/contracts/BaseJumpRateModelV2.sol#L96
//
Expand Down
38 changes: 23 additions & 15 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 All @@ -69,6 +71,9 @@ interface InterestRateModelLike {
}

contract D3MCompoundTest is DSSTest {
Hevm hevm;
gbalabasquer marked this conversation as resolved.
Show resolved Hide resolved
bytes20 constant CHEAT_CODE = bytes20(uint160(uint256(keccak256('hevm cheat code'))));

VatLike vat;
EndLike end;
CErc20Like cDai;
Expand Down Expand Up @@ -97,6 +102,7 @@ contract D3MCompoundTest is DSSTest {
emit log_named_uint("block", block.number);
emit log_named_uint("timestamp", block.timestamp);

hevm = Hevm(address(CHEAT_CODE));
vat = VatLike(0x35D1b3F3D7966A1DFe207aa4514C12a259A0492B);
end = EndLike(0x0e2e8F1D1326A4B9633D96222Ce399c708B19c28);
cDai = CErc20Like(0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643);
Expand Down Expand Up @@ -300,26 +306,28 @@ contract D3MCompoundTest is DSSTest {
assertEq(vat.dai(address(d3mHub)), 0);
}

function test_target_increase() public {
// Lower by 50%
uint256 targetBorrowRate = _setRelBorrowTarget(5000);
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);

uint256 amountSuppliedInitial = cDai.balanceOfUnderlying(address(d3mCompoundPool));
cDai.borrow(1 * WAD);

// Raise by 25%
targetBorrowRate = _setRelBorrowTarget(12500);
assertEqInterest(getBorrowRate(), targetBorrowRate);
uint256 borrowBalanceBefore = cDai.borrowBalanceCurrent(address(this));

uint256 amountSupplied = cDai.balanceOfUnderlying(address(d3mCompoundPool));
assertTrue(amountSupplied > 0);
assertLt(amountSupplied, amountSuppliedInitial);
(uint256 ink, uint256 art) = vat.urns(ilk, address(d3mCompoundPool));
assertEqRounding(ink, amountSupplied);
assertEqRounding(art, amountSupplied);
// fast forward 1 year while accruing interest each day
for (uint256 i = 1; i <= 365; i++) {
hevm.roll(block.number + 7200);
cDai.accrueInterest();
gbalabasquer marked this conversation as resolved.
Show resolved Hide resolved
}

assertEq(vat.gem(ilk, address(d3mCompoundPool)), 0);
assertEq(vat.dai(address(d3mHub)), 0);
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 {
Expand Down