This repository has been archived by the owner on Sep 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
HegicPutOptions.sol
139 lines (127 loc) · 4.47 KB
/
HegicPutOptions.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* SPDX-License-Identifier: GPL-3.0-or-later
* Hegic
* Copyright (C) 2020 Hegic Protocol
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
pragma solidity 0.6.8;
import "./HegicOptions.sol";
/**
* @author 0mllwntrmt3
* @title Hegic ETH Put Options
* @notice ETH Put Options Contract
*/
contract HegicPutOptions is HegicOptions {
IUniswapV2Router01 public uniswapRouter;
HegicERCPool public pool;
uint256 public maxSpread = 95;
IERC20 internal token;
/**
* @param _token The address of stable ERC20 token contract
* @param _priceProvider The address of ChainLink ETH/USD price feed contract
* @param _uniswapRouter The address of Uniswap Router contract
*/
constructor(
IERC20 _token,
AggregatorInterface _priceProvider,
IUniswapV2Router01 _uniswapRouter
)
public
HegicOptions(_priceProvider, HegicOptions.OptionType.Put)
{
token = _token;
uniswapRouter = _uniswapRouter;
pool = new HegicERCPool(token);
approve();
}
/**
* @notice Can be used to update the contract in critical situations
* in the first 90 days after deployment
*/
function transferPoolOwnership() external onlyOwner {
require(now < contractCreationTimestamp + 90 days);
pool.transferOwnership(owner());
}
/**
* @notice Used for adjusting the spread limit
* @param value New maxSpread value
*/
function setMaxSpread(uint256 value) external onlyOwner {
require(value <= 95, "Spread limit is too small");
maxSpread = value;
}
/**
* @notice Used for changing the lockup period
* @param value New period value
*/
function setLockupPeriod(uint256 value) external onlyOwner {
require(value <= 60 days, "Lockup period is too large");
pool.setLockupPeriod(value);
}
/**
* @notice Allows the ERC pool contract to receive and send tokens
*/
function approve() public {
require(
token.approve(address(pool), uint256(-1)),
"token approve failed"
);
}
/**
* @notice Sends premiums to the ERC liquidity pool contract
*/
function sendPremium(uint256 amount) internal override returns (uint premium) {
uint currentPrice = uint(priceProvider.latestAnswer());
address[] memory path = new address[](2);
path[0] = uniswapRouter.WETH();
path[1] = address(token);
uint[] memory amounts = uniswapRouter.swapExactETHForTokens {
value: amount
}(
amount.mul(currentPrice).mul(maxSpread).div(1e10),
path,
address(this),
now
);
premium = amounts[amounts.length - 1];
pool.sendPremium(premium);
}
/**
* @notice Locks the amount required for an option
* @param option A specific option contract
*/
function lockFunds(Option memory option) internal override {
pool.lock(option.amount.mul(option.strike).div(PRICE_DECIMALS));
}
/**
* @notice Sends profits in DAI from the ERC pool to a put option holder's address
* @param option A specific option contract
*/
function payProfit(Option memory option) internal override returns (uint profit) {
uint currentPrice = uint(priceProvider.latestAnswer());
require(option.strike >= currentPrice, "Current price is too high");
profit = option.strike.sub(currentPrice).mul(option.amount).div(PRICE_DECIMALS);
pool.send(option.holder, profit);
unlockFunds(option);
}
/**
* @notice Unlocks the amount that was locked in a put option contract
* @param option A specific option contract
*/
function unlockFunds(Option memory option) internal override {
pool.unlockPremium(option.premium);
pool.unlock(option.amount.mul(option.strike).div(PRICE_DECIMALS));
}
}