-
Notifications
You must be signed in to change notification settings - Fork 41
/
HSCompound.sol
278 lines (253 loc) · 8.57 KB
/
HSCompound.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// SPDX-License-Identifier: MIT
pragma solidity 0.8.10;
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "../HandlerBase.sol";
import "../maker/IDSProxy.sol";
import "./ICToken.sol";
import "./IComptroller.sol";
contract HSCompound is HandlerBase {
using SafeERC20 for IERC20;
// prettier-ignore
address public constant FCOMPOUND_ACTIONS = 0x05EF8eb657027927fAB9b279138f0189CB144976;
// prettier-ignore
address public constant COMPTROLLER = 0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B;
// prettier-ignore
address public constant CETH_ADDRESS = 0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5;
// prettier-ignore
address public constant COMP_ADDRESS = 0xc00e94Cb662C3520282E6f5717214004A7f26888;
function getContractName() public pure override returns (string memory) {
return "HSCompound";
}
modifier isDSProxyOwner(address dsProxy) {
address sender = _getSender();
_requireMsg(
IDSProxy(dsProxy).owner() == sender,
"General",
"Not owner of the DSProxy"
);
_;
}
function deposit(
address dsProxy,
address token,
uint256 amount
) external payable isDSProxyOwner(dsProxy) {
_deposit(dsProxy, token, amount);
}
function withdraw(
address dsProxy,
address token,
uint256 amount
) external payable isDSProxyOwner(dsProxy) {
_withdraw(dsProxy, token, amount);
if (token != NATIVE_TOKEN_ADDRESS) _updateToken(token);
}
function borrow(
address dsProxy,
address cTokenIn,
address cTokenBorrow,
uint256 cAmountIn,
uint256 uBorrowAmount,
bool needEnterMarket
) external payable isDSProxyOwner(dsProxy) {
if (cAmountIn > 0) {
_deposit(dsProxy, cTokenIn, cAmountIn);
}
if (needEnterMarket) {
_enterMarket(dsProxy, cTokenIn);
}
if (uBorrowAmount > 0) {
address underlying;
if (cTokenBorrow == CETH_ADDRESS) {
underlying = NATIVE_TOKEN_ADDRESS;
} else {
underlying = _getToken(cTokenBorrow);
}
// Execute borrow, borrowed token will stay in the DSProxy
try
IDSProxy(dsProxy).execute(
FCOMPOUND_ACTIONS,
abi.encodeWithSelector(
// selector of "borrow(address,uint256)"
0x4b8a3529,
cTokenBorrow,
uBorrowAmount
)
)
{} catch Error(string memory reason) {
_revertMsg("borrow", reason);
} catch {
_revertMsg("borrow");
}
// Withdraw borrowed token from the DSProxy
_withdraw(dsProxy, underlying, uBorrowAmount);
// Update borrowed token
if (underlying != NATIVE_TOKEN_ADDRESS) _updateToken(underlying);
}
}
function repayBorrow(
address dsProxy,
address cTokenRepay,
address cTokenWithdraw,
uint256 uRepayAmount,
uint256 cWithdrawAmount
) external payable isDSProxyOwner(dsProxy) {
// Execute repay only when `uRepayAmount` is greater than 0
if (uRepayAmount > 0) {
if (cTokenRepay == CETH_ADDRESS) {
// Execute ether repay
try
IDSProxy(dsProxy).execute{value: uRepayAmount}(
FCOMPOUND_ACTIONS,
abi.encodeWithSelector(
// selector of "repayBorrow(address,uint256)"
0xabdb5ea8,
cTokenRepay,
uRepayAmount
)
)
{} catch Error(string memory reason) {
_revertMsg("repayBorrow", reason);
} catch {
_revertMsg("repayBorrow");
}
} else {
// Approve repay token to DSProxy
address underlying = _getToken(cTokenRepay);
_tokenApprove(underlying, dsProxy, uRepayAmount);
// Execute token repay
try
IDSProxy(dsProxy).execute(
FCOMPOUND_ACTIONS,
abi.encodeWithSelector(
// selector of "repayBorrow(address,uint256)"
0xabdb5ea8,
cTokenRepay,
uRepayAmount
)
)
{} catch Error(string memory reason) {
_revertMsg("repayBorrow", reason);
} catch {
_revertMsg("repayBorrow");
}
_tokenApproveZero(underlying, dsProxy);
}
}
if (cWithdrawAmount > 0) {
// Withdraw collateral from DSProxy
_withdraw(dsProxy, cTokenWithdraw, cWithdrawAmount);
// Update collateral token
_updateToken(cTokenWithdraw);
}
}
function enterMarket(
address dsProxy,
address cToken
) external payable isDSProxyOwner(dsProxy) {
_enterMarket(dsProxy, cToken);
}
function enterMarkets(
address dsProxy,
address[] calldata cTokens
) external payable isDSProxyOwner(dsProxy) {
try
IDSProxy(dsProxy).execute(
FCOMPOUND_ACTIONS,
abi.encodeWithSelector(
// selector of "enterMarkets(address[])"
0xc2998238,
cTokens
)
)
{} catch Error(string memory reason) {
_revertMsg("enterMarkets", reason);
} catch {
_revertMsg("enterMarkets");
}
}
function exitMarket(
address dsProxy,
address cToken
) external payable isDSProxyOwner(dsProxy) {
try
IDSProxy(dsProxy).execute(
FCOMPOUND_ACTIONS,
abi.encodeWithSelector(
// selector of "exitMarket(address)"
0xede4edd0,
cToken
)
)
{} catch Error(string memory reason) {
_revertMsg("exitMarket", reason);
} catch {
_revertMsg("exitMarket");
}
}
function claimComp(
address dsProxy
) external payable isDSProxyOwner(dsProxy) returns (uint256) {
try IComptroller(COMPTROLLER).claimComp(dsProxy) {} catch Error(
string memory reason
) {
_revertMsg("claimComp", reason);
} catch {
_revertMsg("claimComp");
}
uint256 balance = IERC20(COMP_ADDRESS).balanceOf(dsProxy);
// Withdraw whole COMP balance of DSProxy
_withdraw(dsProxy, COMP_ADDRESS, balance);
_updateToken(COMP_ADDRESS);
return balance;
}
/* ========== INTERNAL FUNCTIONS ========== */
function _deposit(address dsProxy, address token, uint256 amount) internal {
if (token == NATIVE_TOKEN_ADDRESS) {
address payable dsProxyPayable = payable(dsProxy);
dsProxyPayable.transfer(amount);
} else {
IERC20(token).safeTransfer(dsProxy, amount);
}
}
function _withdraw(
address dsProxy,
address token,
uint256 amount
) internal {
try
IDSProxy(dsProxy).execute(
FCOMPOUND_ACTIONS,
abi.encodeWithSelector(
// selector of "withdraw(address,uint256)"
0xf3fef3a3,
token,
amount
)
)
{} catch Error(string memory reason) {
_revertMsg("_withdraw", reason);
} catch {
_revertMsg("_withdraw");
}
}
function _enterMarket(address dsProxy, address cToken) internal {
try
IDSProxy(dsProxy).execute(
FCOMPOUND_ACTIONS,
abi.encodeWithSelector(
// selector of "enterMarket(address)"
0x3fe5d425,
cToken
)
)
{} catch Error(string memory reason) {
_revertMsg("_enterMarket", reason);
} catch {
_revertMsg("_enterMarket");
}
}
function _getToken(address token) internal view returns (address) {
return ICToken(token).underlying();
}
}