Last active
August 27, 2019 21:27
-
-
Save k06a/4b17e3e579b7de12b9957c5e4dc00506 to your computer and use it in GitHub Desktop.
Minterest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.0; | |
import "@openzeppelin/contracts/math/SafeMath.sol"; | |
import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | |
import "./UniversalERC20.sol"; | |
contract Minterest is ERC20 { | |
using SafeMath for uint256; | |
using UniversalERC20 for IERC20; | |
struct MovingPrice { | |
uint256 time; | |
uint256 value; | |
uint256 speed; | |
} | |
uint256 public constant MIN_INTEREST = 2e18; | |
uint256 public constant MAX_INTEREST = 22e18; | |
IERC20 public token; | |
MovingPrice public movingPrice; | |
uint256 public totalLiquidity; | |
constructor(IERC20 _token) public { | |
token = _token; | |
} | |
// View methods | |
function price() public view returns(uint256) { | |
return priceForTime(now); | |
} | |
function priceForTime(uint256 time) public view returns(uint256) { | |
return movingPrice.value.add(time.sub(movingPrice.time).mul(movingPrice.speed)); | |
} | |
function interest() public returns(uint256) { | |
return interestForLiquidityAndBalance(totalLiquidity, token.universalBalanceOf(address(this))); | |
} | |
function interestForLiquidityAndBalance(uint256 liquidity, uint256 balance) public pure returns(uint256) { | |
return MIN_INTEREST.add(MAX_INTEREST.sub(MIN_INTEREST).mul(balance).div(liquidity)); | |
} | |
// Non-view methods | |
function deposit(uint256 value) public payable { | |
_mint(msg.sender, value.mul(price()).div(1e18)); | |
token.universalTransferFrom(msg.sender, address(this), value); | |
_updateSpeed(movingPrice.speed.mul(totalLiquidity).div(totalLiquidity.add(value))); | |
totalLiquidity = totalLiquidity.add(value); | |
} | |
function withdraw(uint256 amount) public { | |
uint256 value = amount.mul(1e18).div(price()); | |
_burn(msg.sender, amount); | |
token.universalTransfer(msg.sender, value); | |
_updateSpeed(movingPrice.speed.mul(totalLiquidity).div(totalLiquidity.sub(value))); | |
totalLiquidity = totalLiquidity.sub(value); | |
} | |
function borrow(uint256 amount) public { | |
// TODO: Fix | |
// ... | |
// TODO: Fix | |
// _updateSpeed(movingPrice.speed.mul(totalLiquidity).div(totalLiquidity.sub(value))); | |
} | |
function repay(uint256 amount) public payable { | |
// TODO: Fix | |
// ... | |
// TODO: Fix | |
// _updateSpeed(movingPrice.speed.mul(totalLiquidity).div(totalLiquidity.add(value))); | |
} | |
// Internal methods | |
function _updateSpeed(uint256 newSpeed) internal { | |
movingPrice = MovingPrice({ | |
time: now, | |
value: price(), | |
speed: newSpeed | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment