-
Notifications
You must be signed in to change notification settings - Fork 35
/
AaveStrategy.sol
221 lines (176 loc) · 7.61 KB
/
AaveStrategy.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
// Neptune Mutual Protocol (https://neptunemutual.com)
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
import "openzeppelin-solidity/contracts/utils/Address.sol";
import "../../Recoverable.sol";
import "../../../interfaces/ILendingStrategy.sol";
import "../../../dependencies/aave/IAaveV2LendingPoolLike.sol";
import "../../../libraries/NTransferUtilV2.sol";
contract AaveStrategy is ILendingStrategy, Recoverable {
using NTransferUtilV2 for IERC20;
using ProtoUtilV1 for IStore;
using StoreKeyUtil for IStore;
using ValidationLibV1 for IStore;
using RegistryLibV1 for IStore;
bytes32 public constant CNAME_STRATEGY_AAVE = "Aave Strategy";
bytes32 private constant _KEY = keccak256(abi.encodePacked("lending", "strategy", "aave", "v2"));
bytes32 public constant NS_DEPOSITS = "deposits";
bytes32 public constant NS_WITHDRAWALS = "withdrawals";
address public immutable depositCertificate;
IAaveV2LendingPoolLike public immutable lendingPool;
mapping(bytes32 => uint256) private _counters;
mapping(bytes32 => uint256) private _depositTotal;
mapping(bytes32 => uint256) private _withdrawalTotal;
constructor(
IStore _s,
IAaveV2LendingPoolLike _lendingPool,
address _aToken
) Recoverable(_s) {
depositCertificate = _aToken;
lendingPool = _lendingPool;
}
function getDepositAsset() public view override returns (IERC20) {
return IERC20(s.getStablecoinAddressInternal());
}
function getDepositCertificate() public view override returns (IERC20) {
return IERC20(depositCertificate);
}
function _drain(IERC20 asset) private {
uint256 amount = asset.balanceOf(address(this));
if (amount > 0) {
asset.ensureTransfer(s.getTreasuryAddressInternal(), amount);
emit Drained(asset, amount);
}
}
/**
* @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));
}
/**
* @dev Lends stablecoin to the Aave protocol
* 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 `aToken` or `stablecoin` can't be manipulated via user input.
*
*/
function deposit(bytes32 coverKey, uint256 amount) external override nonReentrant returns (uint256 aTokenReceived) {
s.mustNotBePaused();
s.senderMustBeProtocolMember();
IVault vault = s.getVault(coverKey);
if (amount == 0) {
return 0;
}
IERC20 stablecoin = getDepositAsset();
IERC20 aToken = getDepositCertificate();
require(stablecoin.balanceOf(address(vault)) >= amount, "Balance insufficient");
// This strategy should never have token balances without any exception, especially `aToken` and `stablecoin`
_drain(aToken);
_drain(stablecoin);
// Transfer stablecoin to this contract; then approve and deposit it to Aave Lending Pool to receive aToken certificates
// stablecoin.ensureTransferFrom(fromVault, address(this), amount);
vault.transferToStrategy(stablecoin, coverKey, getName(), amount);
stablecoin.ensureApproval(address(lendingPool), amount);
lendingPool.deposit(address(getDepositAsset()), amount, address(this), 0);
// Check how many aTokens we received
aTokenReceived = _getCertificateBalance();
require(aTokenReceived > 0, "Deposit to Aave failed");
// Immediately send aTokens to the original vault stablecoin came from
aToken.ensureApproval(address(vault), aTokenReceived);
vault.receiveFromStrategy(aToken, coverKey, getName(), aTokenReceived);
s.addUintByKey(_getDepositsKey(coverKey), amount);
_counters[coverKey] += 1;
_depositTotal[coverKey] += amount;
emit LogDeposit(getName(), _counters[coverKey], amount, aTokenReceived, _depositTotal[coverKey], _withdrawalTotal[coverKey]);
emit Deposited(coverKey, address(vault), amount, aTokenReceived);
}
/**
* @dev Redeems aToken from Aave to receive stablecoin
* Ensure that you `approve` aToken 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 `aToken` 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 aToken = getDepositCertificate();
// This strategy should never have token balances
_drain(aToken);
_drain(stablecoin);
uint256 aTokenRedeemed = aToken.balanceOf(address(vault));
if (aTokenRedeemed == 0) {
return 0;
}
// Transfer aToken to this contract; then approve and send it to the Aave Lending pool get back stablecoin + rewards
vault.transferToStrategy(aToken, coverKey, getName(), aTokenRedeemed);
aToken.ensureApproval(address(lendingPool), aTokenRedeemed);
lendingPool.withdraw(address(stablecoin), aTokenRedeemed, address(this));
// Check how many stablecoins we received
stablecoinWithdrawn = stablecoin.balanceOf(address(this));
require(stablecoinWithdrawn > 0, "Redeeming aToken failed");
// Immediately send stablecoin to the vault aToken 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, aTokenRedeemed, _depositTotal[coverKey], _withdrawalTotal[coverKey]);
emit Withdrawn(coverKey, address(vault), stablecoinWithdrawn, aTokenRedeemed);
}
/**
* @dev Hash key of the Aave 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 Aave withdrawals 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 virtual 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_AAVE;
}
}