-
Notifications
You must be signed in to change notification settings - Fork 35
/
CompoundStrategy.sol
224 lines (176 loc) · 8.15 KB
/
CompoundStrategy.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
// Neptune Mutual Protocol (https://neptunemutual.com)
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
import "../../Recoverable.sol";
import "../../../interfaces/ILendingStrategy.sol";
import "../../../dependencies/compound/ICompoundERC20DelegatorLike.sol";
import "../../../libraries/NTransferUtilV2.sol";
contract CompoundStrategy is ILendingStrategy, Recoverable {
using NTransferUtilV2 for IERC20;
using ProtoUtilV1 for IStore;
using StoreKeyUtil for IStore;
using ValidationLibV1 for IStore;
using RegistryLibV1 for IStore;
mapping(bytes32 => uint256) private _counters;
mapping(bytes32 => uint256) private _depositTotal;
mapping(bytes32 => uint256) private _withdrawalTotal;
bytes32 public constant CNAME_STRATEGY_COMPOUND = "Compound Strategy";
bytes32 private constant _KEY = keccak256(abi.encodePacked("lending", "strategy", "compound", "v2"));
bytes32 public constant NS_DEPOSITS = "deposits";
bytes32 public constant NS_WITHDRAWALS = "withdrawals";
address public immutable depositCertificate;
ICompoundERC20DelegatorLike public immutable delegator;
constructor(
IStore _s,
ICompoundERC20DelegatorLike _delegator,
address _compoundWrappedStablecoin
) Recoverable(_s) {
depositCertificate = _compoundWrappedStablecoin;
delegator = _delegator;
}
function getDepositAsset() public view override returns (IERC20) {
return IERC20(s.getStablecoinAddressInternal());
}
function getDepositCertificate() public view override returns (IERC20) {
return IERC20(depositCertificate);
}
/**
* @dev Gets info of this strategy by cover key
*
* Warning: this function does not validate the cover key supplied.
*
* @param coverKey Enter the cover key
*/
function getInfo(bytes32 coverKey) external view override returns (LendingStrategyInfoType memory info) {
info.deposits = s.getUintByKey(_getDepositsKey(coverKey));
info.withdrawals = s.getUintByKey(_getWithdrawalsKey(coverKey));
}
function _getCertificateBalance() private view returns (uint256) {
return getDepositCertificate().balanceOf(address(this));
}
function _drain(IERC20 asset) private {
uint256 amount = asset.balanceOf(address(this));
if (amount > 0) {
asset.ensureTransfer(s.getTreasuryAddressInternal(), amount);
emit Drained(asset, amount);
}
}
/**
* @dev Deposits the tokens to Compound
* Ensure that you `approve` stablecoin before you call this function
*
* @custom:suppress-acl This function is only accessible to protocol members
* @custom:suppress-malicious-erc This tokens `aToken` and `stablecoin` are well-known addresses.
* @custom:suppress-address-trust-issue The addresses `compoundWrappedStablecoin` or `stablecoin` can't be manipulated via user input.
*
*/
function deposit(bytes32 coverKey, uint256 amount) external override nonReentrant returns (uint256 compoundWrappedStablecoinMinted) {
s.mustNotBePaused();
s.senderMustBeProtocolMember();
IVault vault = s.getVault(coverKey);
if (amount == 0) {
return 0;
}
IERC20 stablecoin = getDepositAsset();
IERC20 compoundWrappedStablecoin = getDepositCertificate();
require(stablecoin.balanceOf(address(vault)) >= amount, "Balance insufficient");
// This strategy should never have token balances
_drain(compoundWrappedStablecoin);
_drain(stablecoin);
// Transfer stablecoin to this contract; then approve and send it to delegator to mint compoundWrappedStablecoin
vault.transferToStrategy(stablecoin, coverKey, getName(), amount);
stablecoin.ensureApproval(address(delegator), amount);
uint256 result = delegator.mint(amount);
require(result == 0, "Compound delegator mint failed");
// Check how many compoundWrappedStablecoin we received
compoundWrappedStablecoinMinted = _getCertificateBalance();
require(compoundWrappedStablecoinMinted > 0, "Minting cUS$ failed");
// Immediately send compoundWrappedStablecoin to the original vault stablecoin came from
compoundWrappedStablecoin.ensureApproval(address(vault), compoundWrappedStablecoinMinted);
vault.receiveFromStrategy(compoundWrappedStablecoin, coverKey, getName(), compoundWrappedStablecoinMinted);
s.addUintByKey(_getDepositsKey(coverKey), amount);
_counters[coverKey] += 1;
_depositTotal[coverKey] += amount;
emit LogDeposit(getName(), _counters[coverKey], amount, compoundWrappedStablecoinMinted, _depositTotal[coverKey], _withdrawalTotal[coverKey]);
emit Deposited(coverKey, address(vault), amount, compoundWrappedStablecoinMinted);
}
/**
* @dev Redeems compoundWrappedStablecoin from Compound to receive stablecoin
* Ensure that you `approve` compoundWrappedStablecoin before you call this function
*
* @custom:suppress-acl This function is only accessible to protocol members
* @custom:suppress-malicious-erc This tokens `aToken` and `stablecoin` are well-known addresses.
* @custom:suppress-address-trust-issue The addresses `compoundWrappedStablecoin` or `stablecoin` can't be manipulated via user input.
*
*/
function withdraw(bytes32 coverKey) external virtual override nonReentrant returns (uint256 stablecoinWithdrawn) {
s.mustNotBePaused();
s.senderMustBeProtocolMember();
IVault vault = s.getVault(coverKey);
IERC20 stablecoin = getDepositAsset();
IERC20 compoundWrappedStablecoin = getDepositCertificate();
// This strategy should never have token balances without any exception, especially `compoundWrappedStablecoin` and `stablecoin`
_drain(compoundWrappedStablecoin);
_drain(stablecoin);
uint256 compoundWrappedStablecoinRedeemed = compoundWrappedStablecoin.balanceOf(address(vault));
if (compoundWrappedStablecoinRedeemed == 0) {
return 0;
}
// Transfer compoundWrappedStablecoin to this contract; then approve and send it to delegator to redeem stablecoin
vault.transferToStrategy(compoundWrappedStablecoin, coverKey, getName(), compoundWrappedStablecoinRedeemed);
compoundWrappedStablecoin.ensureApproval(address(delegator), compoundWrappedStablecoinRedeemed);
uint256 result = delegator.redeem(compoundWrappedStablecoinRedeemed);
require(result == 0, "Compound delegator redeem failed");
// Check how many stablecoin we received
stablecoinWithdrawn = stablecoin.balanceOf(address(this));
require(stablecoinWithdrawn > 0, "Redeeming cUS$ failed");
// Immediately send stablecoin to the vault compoundWrappedStablecoin came from
stablecoin.ensureApproval(address(vault), stablecoinWithdrawn);
vault.receiveFromStrategy(stablecoin, coverKey, getName(), stablecoinWithdrawn);
s.addUintByKey(_getWithdrawalsKey(coverKey), stablecoinWithdrawn);
_counters[coverKey] += 1;
_withdrawalTotal[coverKey] += stablecoinWithdrawn;
emit LogWithdrawal(getName(), _counters[coverKey], stablecoinWithdrawn, compoundWrappedStablecoinRedeemed, _depositTotal[coverKey], _withdrawalTotal[coverKey]);
emit Withdrawn(coverKey, address(vault), stablecoinWithdrawn, compoundWrappedStablecoinRedeemed);
}
/**
* @dev Hash key of the Compound deposits for the given cover.
*
* Warning: this function does not validate the cover key supplied.
*
* @param coverKey Enter cover key
*
*/
function _getDepositsKey(bytes32 coverKey) private pure returns (bytes32) {
return keccak256(abi.encodePacked(_KEY, coverKey, NS_DEPOSITS));
}
/**
* @dev Hash key of the Compound withdrawal for the given cover.
*
* Warning: this function does not validate the cover key supplied.
*
* @param coverKey Enter cover key
*
*/
function _getWithdrawalsKey(bytes32 coverKey) private pure returns (bytes32) {
return keccak256(abi.encodePacked(_KEY, coverKey, NS_WITHDRAWALS));
}
function getWeight() external pure override returns (uint256) {
return 10_000; // 100%
}
function getKey() external pure override returns (bytes32) {
return _KEY;
}
/**
* @dev Version number of this contract
*/
function version() external pure override returns (bytes32) {
return "v0.1";
}
/**
* @dev Name of this contract
*/
function getName() public pure override returns (bytes32) {
return CNAME_STRATEGY_COMPOUND;
}
}