Skip to content

Commit

Permalink
Test streaming and redistribution on SM
Browse files Browse the repository at this point in the history
  • Loading branch information
alsco77 committed Nov 22, 2020
1 parent 9c1246b commit ce8aefd
Show file tree
Hide file tree
Showing 9 changed files with 549 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { InitializableAbstractIntegration, MassetHelpers, IERC20, SafeMath } fro


/**
* @title AaveIntegration
* @title AaveV2Integration
* @author Stability Labs Pty. Ltd.
* @notice A simple connection to deposit and withdraw bAssets from Aave
* @dev VERSION: 1.0
Expand Down
40 changes: 24 additions & 16 deletions contracts/savings/SavingsManager.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
pragma solidity 0.5.16;

// TODO - remove
import { console } from "hardhat/console.sol";

// External
import { IMasset } from "../interfaces/IMasset.sol";
import { ISavingsContract } from "../interfaces/ISavingsContract.sol";
Expand Down Expand Up @@ -165,8 +168,8 @@ contract SavingsManager is ISavingsManager, PausableModule {
external
onlyGovernor
{
// Greater than 90% upto 100%
require(_savingsRate > 9e17 && _savingsRate <= 1e18, "Must be a valid rate");
// Greater than 60% upto 100%
require(_savingsRate >= 6e17 && _savingsRate <= 1e18, "Must be a valid rate");
savingsRate = _savingsRate;
emit SavingsRateChanged(_savingsRate);
}
Expand Down Expand Up @@ -202,28 +205,33 @@ contract SavingsManager is ISavingsManager, PausableModule {
uint256 previousBatch = lastBatchCollected[_mAsset];
uint256 timeSincePreviousBatch = currentTime.sub(previousBatch);
require(timeSincePreviousBatch > 12 hours, "Cannot deposit twice in 12 hours");
lastBatchCollected[_mAsset] = now;
lastBatchCollected[_mAsset] = currentTime;

// Batch collect
IMasset mAsset = IMasset(_mAsset);
(uint256 interestCollected, uint256 totalSupply) = mAsset.collectPlatformInterest();

// Validate APY
uint256 apy = _validateCollection(totalSupply, interestCollected, timeSincePreviousBatch);
console.log("\ncollectAndStreamInterest: interest %s vs totalSupply %s", interestCollected, totalSupply);
if(interestCollected > 0){
// Validate APY
uint256 apy = _validateCollection(totalSupply, interestCollected, timeSincePreviousBatch);

// Get remaining rewards
uint256 leftover = _allUnclaimedRewards(_mAsset);
// Get remaining rewards
uint256 leftover = _allUnclaimedRewards(_mAsset);

// Bundle this yield in with liquidator yield
// If (remainingTime < 24h)
// take all remaining, add this, stream over next 24h
// else stream over remainingTime
uint256 end = streamEnd[_mAsset];
uint256 remaining = end > currentTime ? end.sub(currentTime) : 0;
uint256 newDuration = remaining < ONE_DAY ? ONE_DAY : remaining;
_initialiseStream(_mAsset, interestCollected.add(leftover), newDuration);
// Bundle this yield in with liquidator yield
// If (remainingTime < 24h)
// take all remaining, add this, stream over next 24h
// else stream over remainingTime
uint256 end = streamEnd[_mAsset];
uint256 remaining = end > currentTime ? end.sub(currentTime) : 0;
uint256 newDuration = remaining < ONE_DAY ? ONE_DAY : remaining;
_initialiseStream(_mAsset, interestCollected.add(leftover), newDuration);

emit InterestCollected(_mAsset, interestCollected, totalSupply, apy);
}

emit InterestCollected(_mAsset, interestCollected, totalSupply, apy);
emit InterestCollected(_mAsset, interestCollected, totalSupply, 0);
}

function _allUnclaimedRewards(address _mAsset) internal view returns (uint256 leftover) {
Expand Down
16 changes: 16 additions & 0 deletions contracts/z_mocks/masset/MockMasset.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ contract MockMasset is MockERC20 {
{}

uint256 private amountToMint = 0;
uint256 private platformAmountToMint = 0;

// Inject amount of tokens to mint
function setAmountForCollectInterest(uint256 _amount) public {
amountToMint = _amount;
}
// Inject amount of tokens to mint
function setAmountForPlatformInterest(uint256 _amount) public {
platformAmountToMint = _amount;
}

function collectInterest()
external
Expand All @@ -40,6 +45,17 @@ contract MockMasset is MockERC20 {
amountToMint = 0;
}

function collectPlatformInterest()
external
returns (uint256 totalInterestGained, uint256 newSupply)
{
_mint(msg.sender, platformAmountToMint);
totalInterestGained = platformAmountToMint;
newSupply = totalSupply();
// Set back to zero
platformAmountToMint = 0;
}

}
contract MockMasset1 is MockERC20 {

Expand Down
12 changes: 12 additions & 0 deletions contracts/z_mocks/savings/MockRevenueRecipient.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pragma solidity 0.5.16;

import { IRevenueRecipient } from "../../interfaces/ISavingsManager.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract MockRevenueRecipient is IRevenueRecipient {


function depositFunds(address _mAsset, uint256 _amount) external {
IERC20(_mAsset).transferFrom(msg.sender, address(this), _amount);
}
}
5 changes: 5 additions & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require("hardhat-gas-reporter");
require("solidity-coverage");
require("@nomiclabs/hardhat-truffle5");
require("hardhat-typechain");
require("@tenderly/hardhat-tenderly");

require("ts-node/register");
require("tsconfig-paths/register");
Expand Down Expand Up @@ -32,4 +33,8 @@ export default {
outDir: "types/generated",
target: "truffle-v5",
},
tenderly: {
username: "mStable",
project: "mStable-contracts",
},
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
},
"dependencies": {
"@nomiclabs/hardhat-truffle5": "^2.0.0",
"@tenderly/hardhat-tenderly": "^1.0.3",
"hardhat": "^2.0.3",
"hardhat-gas-reporter": "^1.0.1",
"hardhat-typechain": "^0.3.3",
Expand All @@ -93,4 +94,4 @@
"directories": {
"test": "test"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ const c_AaveLendingPool = artifacts.require("IAaveLendingPoolV2");
const c_ERC20 = artifacts.require("ERC20Detailed");
const c_AaveAToken = artifacts.require("IAaveATokenV1");
const c_DelayedProxyAdmin = artifacts.require("DelayedProxyAdmin");
const c_InitializableProxy = artifacts.require(
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
"@openzeppelin/upgrades/InitializableAdminUpgradeabilityProxy",
) as t.InitializableAdminUpgradeabilityProxyContract;
const c_InitializableProxy = artifacts.require("InitializableAdminUpgradeabilityProxy");
const c_AaveIntegration = artifacts.require("MockAaveIntegration");

contract("AaveIntegration", async (accounts) => {
Expand Down
Loading

0 comments on commit ce8aefd

Please sign in to comment.