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

Update DAIInterestRateModel to v4 (fixes #230) #231

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
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
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity ^0.8.10;

import "./JumpRateModelV2.sol";

/**
* @title Compound's DAIInterestRateModel Contract (version 3)
* @author Compound (modified by Dharma Labs)
* @notice The parameterized model described in section 2.4 of the original Compound Protocol whitepaper.
* Version 3 modifies the interest rate model in Version 2 by increasing the initial "gap" or slope of
* the model prior to the "kink" from 2% to 4%, and enabling updateable parameters.
* @title Compound's DAIInterestRateModel Contract (version 4)
* @author Compound, Dharma (modified by Maker Growth)
* @notice Version 4 modifies the number of seconds per block to 12,
* and takes the stability fee of ETH-B as a reference.
*/
contract DAIInterestRateModelV3 is JumpRateModelV2 {
contract DAIInterestRateModelV4 is JumpRateModelV2 {
uint256 private constant BASE = 1e18;
uint256 private constant RAY_BASE = 1e27;
uint256 private constant RAY_TO_BASE_SCALE = 1e9;
uint256 private constant SECONDS_PER_BLOCK = 15;
uint256 private constant SECONDS_PER_BLOCK = 12;

/**
* @notice The additional margin per block separating the base borrow rate from the roof.
*/
uint public gapPerBlock;

/**
* @notice The assumed (1 - reserve factor) used to calculate the minimum borrow rate (reserve factor = 0.05)
* @notice The assumed (1 - reserve factor) used to calculate the minimum borrow rate (reserve factor = 0.15)
* @dev This reflects the reserve factor on the DAI market at the time of implementation.
*/
uint public constant assumedOneMinusReserveFactorMantissa = 0.95e18;
uint public constant assumedOneMinusReserveFactorMantissa = 0.85e18;

PotLike pot;
JugLike jug;
Expand Down Expand Up @@ -84,16 +83,16 @@ contract DAIInterestRateModelV3 is JumpRateModelV2 {
*/
function dsrPerBlock() public view returns (uint) {
return (pot.dsr() - RAY_BASE) // scaled RAY_BASE aka RAY, and includes an extra "ONE" before subtraction
/ RAY_TO_BASE_SCALE // descale to BASE
* SECONDS_PER_BLOCK; // seconds per block
fmorisan marked this conversation as resolved.
Show resolved Hide resolved
/ RAY_TO_BASE_SCALE // descale to BASE
fmorisan marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @notice Resets the baseRate and multiplier per block based on the stability fee and Dai savings rate
*/
function poke() public {
(uint duty, ) = jug.ilks("ETH-A");
uint stabilityFeePerBlock = (duty + jug.base() - RAY_BASE) / RAY_TO_BASE_SCALE * SECONDS_PER_BLOCK;
(uint duty, ) = jug.ilks("ETH-B");
uint stabilityFeePerBlock = (duty + jug.base() - RAY_BASE) * SECONDS_PER_BLOCK / RAY_TO_BASE_SCALE;

// We ensure the minimum borrow rate >= DSR / (1 - reserve factor)
baseRatePerBlock = dsrPerBlock() * BASE / assumedOneMinusReserveFactorMantissa;
Expand Down
2 changes: 1 addition & 1 deletion scenario/src/Builder/InterestRateModelBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {getContract, getTestContract} from '../Contract';
const FixedInterestRateModel = getTestContract('InterestRateModelHarness');
const WhitePaperInterestRateModel = getContract('WhitePaperInterestRateModel');
const JumpRateModel = getContract('JumpRateModel');
const DAIInterestRateModel = getContract('DAIInterestRateModelV3');
const DAIInterestRateModel = getContract('DAIInterestRateModelV4');
const JumpRateModelV2 = getContract('JumpRateModelV2');
const LegacyJumpRateModelV2 = getContract('LegacyJumpRateModelV2');

Expand Down
15 changes: 8 additions & 7 deletions tests/Models/DAIInterestRateModelTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
getSupplyRate
} = require('../Utils/Compound');

const assumedSecondsPerBlock = 12;
const blocksPerYear = 2102400;
const secondsPerYear = 60 * 60 * 24 * 365;

Expand All @@ -16,8 +17,8 @@ function utilizationRate(cash, borrows, reserves) {

function baseRoofRateFn(dsr, duty, mkrBase, jump, kink, cash, borrows, reserves) {
const assumedOneMinusReserveFactor = 0.95;
const stabilityFeePerBlock = (duty + mkrBase - 1) * 15;
const dsrPerBlock = (dsr - 1) * 15;
const stabilityFeePerBlock = (duty + mkrBase - 1) * assumedSecondsPerBlock;
const dsrPerBlock = (dsr - 1) * assumedSecondsPerBlock;
const gapPerBlock = 0.04 / blocksPerYear;
const jumpPerBlock = jump / blocksPerYear;

Expand All @@ -39,7 +40,7 @@ function baseRoofRateFn(dsr, duty, mkrBase, jump, kink, cash, borrows, reserves)
}

function daiSupplyRate(dsr, duty, mkrBase, jump, kink, cash, borrows, reserves, reserveFactor = 0.1) {
const dsrPerBlock = (dsr - 1) * 15;
const dsrPerBlock = (dsr - 1) * assumedSecondsPerBlock;
const ur = utilizationRate(cash, borrows, reserves);
const borrowRate = baseRoofRateFn(dsr, duty, mkrBase, jump, kink, cash, borrows, reserves);
const underlying = cash + borrows - reserves;
Expand Down Expand Up @@ -68,15 +69,15 @@ async function getKovanFork() {
return {kovan, root, accounts};
}

describe('DAIInterestRateModelV3', () => {
describe('DAIInterestRateModelV4', () => {
describe("constructor", () => {
it("sets jug and ilk address and pokes", async () => {
// NB: Going back a certain distance requires an archive node, currently that add-on is $250/mo
// https://community.infura.io/t/error-returned-error-project-id-does-not-have-access-to-archive-state/847
const {kovan, root, accounts} = await getKovanFork();

// TODO: Get contract craz
let {contract: model} = await saddle.deployFull('DAIInterestRateModelV3', [
let {contract: model} = await saddle.deployFull('DAIInterestRateModelV4', [
etherUnsigned(0.8e18),
etherUnsigned(0.9e18),
"0xea190dbdc7adf265260ec4da6e9675fd4f5a78bb",
Expand Down Expand Up @@ -152,7 +153,7 @@ describe('DAIInterestRateModelV3', () => {
etherUnsigned(perSecondBase)
]);

const daiIRM = await deploy('DAIInterestRateModelV3', [
const daiIRM = await deploy('DAIInterestRateModelV4', [
etherUnsigned(jump),
etherUnsigned(kink),
pot._address,
Expand Down Expand Up @@ -229,7 +230,7 @@ describe('DAIInterestRateModelV3', () => {
etherUnsigned(perSecondBase)
]);

const daiIRM = await deploy('DAIInterestRateModelV3', [
const daiIRM = await deploy('DAIInterestRateModelV4', [
etherUnsigned(jump),
etherUnsigned(kink),
pot._address,
Expand Down