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

Feature/new interest model #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
40 changes: 40 additions & 0 deletions contracts/funding/GentleInterestModel.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*

Copyright 2019 The Hydro Protocol Foundation

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

pragma solidity ^0.5.8;
pragma experimental ABIEncoderV2;

contract GentleInterestModel {
uint256 constant BASE = 10**18;

/**
* @param borrowRatio a decimal with 18 decimals
*/
function polynomialInterestModel(uint256 borrowRatio) external pure returns(uint256) {
// 0.1r + 0.2r^16 + 0.2*r^32

// the valid range of borrowRatio is [0, 1]
uint256 r = borrowRatio > BASE ? BASE : borrowRatio;
uint256 r16 = r*r/BASE; // r^2
r16 = r16*r16/BASE; // r^4
r16 = r16*r16/BASE; // r^8
r16 = r16*r16/BASE; // r^16

return r / 10 + r16 / 5 + r16 * r16 / BASE / 5;
}
}
56 changes: 56 additions & 0 deletions contracts/helper/Eth2daiDirect.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
*Submitted for verification at Etherscan.io on 2018-03-16
*/

pragma solidity ^0.5.8;

contract Eth2daiInterface {
// sellAllAmount(ERC20 pay_gem, uint pay_amt, ERC20 buy_gem, uint min_fill_amount)
function sellAllAmount(address, uint, address, uint) public returns (uint);
}

contract TokenInterface {
function balanceOf(address) public returns (uint);
function allowance(address, address) public returns (uint);
function approve(address, uint) public;
function transfer(address,uint) public returns (bool);
function transferFrom(address, address, uint) public returns (bool);
function deposit() public payable;
function withdraw(uint) public;
}

contract Eth2daiDirect {

Eth2daiInterface public constant eth2dai = Eth2daiInterface(0x39755357759cE0d7f32dC8dC45414CCa409AE24e);
TokenInterface public constant wethToken = TokenInterface(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
TokenInterface public constant daiToken = TokenInterface(0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359);

function marketBuyEth(
uint256 payDaiAmount,
uint256 minBuyEthAmount
)
public
{
daiToken.transferFrom(msg.sender, address(this), payDaiAmount);
uint256 fillAmount = eth2dai.sellAllAmount(address(daiToken), payDaiAmount, address(wethToken), minBuyEthAmount);
wethToken.withdraw(fillAmount);
msg.sender.transfer(fillAmount);
}

function marketSellEth(
uint256 payEthAmount,
uint256 minBuyDaiAmount
)
public
payable
{
require(msg.value == payEthAmount, "MSG_VALUE_NOT_MATCH");
wethToken.deposit.value(msg.value)();
uint256 fillAmount = eth2dai.sellAllAmount(address(wethToken), payEthAmount, address(daiToken), minBuyDaiAmount);
daiToken.transfer(msg.sender, fillAmount);
}

function() external payable {
require(msg.sender == address(wethToken), "CONTRACT_NOT_PAYABLE");
}
}