-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathRSIManager.sol
189 lines (150 loc) · 5.92 KB
/
RSIManager.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// SPDX-License-Identifier: MIT
pragma experimental ABIEncoderV2;
pragma solidity ^0.7.1;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "../interfaces/ISynthetix.sol";
import "../interfaces/IExperiPie.sol";
import "../interfaces/IPriceReferenceFeed.sol";
contract RSISynthetixManager {
address public immutable assetShort;
address public immutable assetLong;
bytes32 public immutable assetShortKey;
bytes32 public immutable assetLongKey;
IPriceReferenceFeed public immutable priceFeed;
IExperiPie public immutable basket;
ISynthetix public immutable synthetix;
struct RoundData {
uint80 roundId;
int256 answer;
uint256 startedAt;
uint256 updatedAt;
uint80 answeredInRound;
}
event Rebalanced(address indexed basket, address indexed fromToken, address indexed toToken);
constructor(
address _assetShort,
address _assetLong,
bytes32 _assetShortKey,
bytes32 _assetLongKey,
address _priceFeed,
address _basket,
address _synthetix
) {
assetShort = _assetShort;
assetLong = _assetLong;
assetShortKey = _assetShortKey;
assetLongKey = _assetLongKey;
priceFeed = IPriceReferenceFeed(_priceFeed);
basket = IExperiPie(_basket);
synthetix = ISynthetix(_synthetix);
}
function rebalance() external {
RoundData memory roundData = readLatestRound();
require(roundData.updatedAt > 0, "Round not complete");
if(roundData.answer <= 30 * 10**18) {
// long
long();
return;
} else if(roundData.answer >= 70 * 10**18) {
// Short
short();
return;
}
}
function long() internal {
IERC20 currentToken = IERC20(getCurrentToken());
require(address(currentToken) == assetShort, "Can only long when short");
uint256 currentTokenBalance = currentToken.balanceOf(address(basket));
address[] memory targets = new address[](4);
bytes[] memory data = new bytes[](4);
uint256[] memory values = new uint256[](4);
// lock pool
targets[0] = address(basket);
// lock for 30
data[0] = setLockData(block.number + 30);
// Swap on synthetix
targets[1] = address(synthetix);
data[1] = abi.encodeWithSelector(synthetix.exchange.selector, assetShortKey, currentTokenBalance, assetLongKey);
// Remove current token
targets[2] = address(basket);
data[2] = abi.encodeWithSelector(basket.removeToken.selector, assetShort);
// Add new token
targets[3] = address(basket);
data[3] = abi.encodeWithSelector(basket.addToken.selector, assetLong);
// Do calls
basket.call(targets, data, values);
// sanity checks
require(currentToken.balanceOf(address(basket)) == 0, "Current token balance should be zero");
require(IERC20(assetLong).balanceOf(address(basket)) >= 10**6, "Amount too small");
emit Rebalanced(address(basket), assetShort, assetLong);
}
function short() internal {
IERC20 currentToken = IERC20(getCurrentToken());
require(address(currentToken) == assetLong, "Can only short when long");
uint256 currentTokenBalance = currentToken.balanceOf(address(basket));
address[] memory targets = new address[](4);
bytes[] memory data = new bytes[](4);
uint256[] memory values = new uint256[](4);
// lock pool
targets[0] = address(basket);
// lock for 30
data[0] = setLockData(block.number + 30);
// Swap on synthetix
targets[1] = address(synthetix);
data[1] = abi.encodeWithSelector(synthetix.exchange.selector, assetLongKey, currentTokenBalance, assetShortKey);
// Remove current token
targets[2] = address(basket);
data[2] = abi.encodeWithSelector(basket.removeToken.selector, assetLong);
// Add new token
targets[3] = address(basket);
data[3] = abi.encodeWithSelector(basket.addToken.selector, assetShort);
// Do calls
basket.call(targets, data, values);
// sanity checks
require(currentToken.balanceOf(address(basket)) == 0, "Current token balance should be zero");
// Catched by addToken in the basket itself
// require(IERC20(assetShort).balanceOf(address(basket)) >= 10**6, "Amount too small");
emit Rebalanced(address(basket), assetShort, assetLong);
}
function getCurrentToken() public view returns(address) {
address[] memory tokens = basket.getTokens();
require(tokens.length == 1, "RSI Pie can only have 1 asset at the time");
return tokens[0];
}
function setLockData(uint256 _block) internal returns(bytes memory data) {
bytes memory data = abi.encodeWithSelector(basket.setLock.selector, _block);
return data;
}
function readRound(uint256 _round) public view returns(RoundData memory data) {
(
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
) = priceFeed.getRoundData(uint80(_round));
return RoundData({
roundId: roundId,
answer: answer,
startedAt: startedAt,
updatedAt: updatedAt,
answeredInRound: answeredInRound
});
}
function readLatestRound() public view returns(RoundData memory data) {
(
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
) = priceFeed.latestRoundData();
return RoundData({
roundId: roundId,
answer: answer,
startedAt: startedAt,
updatedAt: updatedAt,
answeredInRound: answeredInRound
});
}
}