-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathPerpetualAtlanticVaultLP.sol
302 lines (243 loc) · 9.04 KB
/
PerpetualAtlanticVaultLP.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.19;
// Contracts
import { ERC20 } from "solmate/src/tokens/ERC20.sol";
import { FixedPointMathLib } from "solmate/src/utils/FixedPointMathLib.sol";
import { IERC20WithBurn } from "../interfaces/IERC20WithBurn.sol";
// Libraries
import { Strings } from "@openzeppelin/contracts/utils/Strings.sol";
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import { SafeTransferLib } from "solmate/src/utils/SafeTransferLib.sol";
// Interfaces
import { IPerpetualAtlanticVault } from "./IPerpetualAtlanticVault.sol";
import { IPerpetualAtlanticVaultLP } from "./IPerpetualAtlanticVaultLP.sol";
/**
* @title PerpetaulAtlanticVault LP Token
*/
contract PerpetualAtlanticVaultLP is ERC20, IPerpetualAtlanticVaultLP {
using SafeERC20 for IERC20WithBurn;
using SafeTransferLib for ERC20;
using FixedPointMathLib for uint256;
// ================================ EVENTS ================================ //
event Deposit(
address indexed caller,
address indexed owner,
uint256 assets,
uint256 shares
);
event Withdraw(
address indexed caller,
address indexed receiver,
address indexed owner,
uint256 assets,
uint256 shares
);
// ================================ STATE VARIABLES ================================ //
/// @dev The address of the perpetual Atlantic Vault contract creating the lp token
IPerpetualAtlanticVault public perpetualAtlanticVault;
/// @dev The collateral token
ERC20 public collateral;
/// @dev The symbol reperesenting the underlying asset of the perpetualatlanticvault lp
string public underlyingSymbol;
/// @dev The symbol representing the collateral token of the perpetualatlanticvault lp
string public collateralSymbol;
/// @dev Total collateral available
uint256 private _totalCollateral;
/// @dev Active collateral
uint256 private _activeCollateral;
/// @dev Total rdpx available
uint256 private _rdpxCollateral;
/// @dev address of rdpx token
address public rdpx;
/// @dev address of the rdpx rdpxV2Core contract
address public rdpxRdpxV2Core;
// ================================ CONSTRUCTOR ================================ //
/**
* @param _perpetualAtlanticVault The address of the perpetual atlantic vault contract creating the lp token
* @param _rdpxRdpxV2Core The address of the rdpx rdpxV2Core contract
* @param _collateral The address of the collateral asset in the perpetualatlanticvault contract
* @param _collateralSymbol The symbol of the collateral asset token
* @param _rdpx The address of the rdpx token
*/
constructor(
address _perpetualAtlanticVault,
address _rdpxRdpxV2Core,
address _collateral,
address _rdpx,
string memory _collateralSymbol
)
ERC20(
"PerpetualAtlanticVault LP Token",
_collateralSymbol,
ERC20(_collateral).decimals()
)
{
require(
_perpetualAtlanticVault != address(0) || _rdpx != address(0),
"ZERO_ADDRESS"
);
perpetualAtlanticVault = IPerpetualAtlanticVault(_perpetualAtlanticVault);
rdpxRdpxV2Core = _rdpxRdpxV2Core;
collateralSymbol = _collateralSymbol;
rdpx = _rdpx;
collateral = ERC20(_collateral);
symbol = string.concat(_collateralSymbol, "-LP");
collateral.approve(_perpetualAtlanticVault, type(uint256).max);
ERC20(rdpx).approve(_perpetualAtlanticVault, type(uint256).max);
}
// ================================ PUBLIC FUNCTIONS ================================ //
/**
* @notice deposit into ERC4626 token
* @param assets assets
* @param receiver receiver
* @return shares shares of LP tokens minted
*/
function deposit(
uint256 assets,
address receiver
) public virtual returns (uint256 shares) {
// Check for rounding error since we round down in previewDeposit.
require((shares = previewDeposit(assets)) != 0, "ZERO_SHARES");
perpetualAtlanticVault.updateFunding();
// Need to transfer before minting or ERC777s could reenter.
collateral.transferFrom(msg.sender, address(this), assets);
_mint(receiver, shares);
_totalCollateral += assets;
emit Deposit(msg.sender, receiver, assets, shares);
}
/**
* @notice redeem ERC4626 token
* @param shares shares
* @param receiver receiver
* @param owner owner
* @return assets native tokens to be received
* @return rdpxAmount rdpx tokens to be received
*/
function redeem(
uint256 shares,
address receiver,
address owner
) public returns (uint256 assets, uint256 rdpxAmount) {
perpetualAtlanticVault.updateFunding();
if (msg.sender != owner) {
uint256 allowed = allowance[owner][msg.sender]; // Saves gas for limited approvals.
if (allowed != type(uint256).max) {
allowance[owner][msg.sender] = allowed - shares;
}
}
(assets, rdpxAmount) = redeemPreview(shares);
// Check for rounding error since we round down in previewRedeem.
require(assets != 0, "ZERO_ASSETS");
_rdpxCollateral -= rdpxAmount;
beforeWithdraw(assets, shares);
_burn(owner, shares);
collateral.transfer(receiver, assets);
IERC20WithBurn(rdpx).safeTransfer(receiver, rdpxAmount);
emit Withdraw(msg.sender, receiver, owner, assets, shares);
}
// ================================ PERP VAULT FUNCTIONS ================================ //
/// @inheritdoc IPerpetualAtlanticVaultLP
function lockCollateral(uint256 amount) public onlyPerpVault {
_activeCollateral += amount;
}
/// @inheritdoc IPerpetualAtlanticVaultLP
function unlockLiquidity(uint256 amount) public onlyPerpVault {
_activeCollateral -= amount;
}
/// @inheritdoc IPerpetualAtlanticVaultLP
function addProceeds(uint256 proceeds) public onlyPerpVault {
require(
collateral.balanceOf(address(this)) >= _totalCollateral + proceeds,
"Not enough collateral token was sent"
);
_totalCollateral += proceeds;
}
/// @inheritdoc IPerpetualAtlanticVaultLP
function subtractLoss(uint256 loss) public onlyPerpVault {
require(
collateral.balanceOf(address(this)) == _totalCollateral - loss,
"Not enough collateral was sent out"
);
_totalCollateral -= loss;
}
/// @inheritdoc IPerpetualAtlanticVaultLP
function addRdpx(uint256 amount) public onlyPerpVault {
require(
IERC20WithBurn(rdpx).balanceOf(address(this)) >= _rdpxCollateral + amount,
"Not enough rdpx token was sent"
);
_rdpxCollateral += amount;
}
// ================================ INTERNAL FUNCTUONS ================================ //
function _convertToAssets(
uint256 shares
) internal view virtual returns (uint256 assets, uint256 rdpxAmount) {
uint256 supply = totalSupply;
return
(supply == 0)
? (shares, 0)
: (
shares.mulDivDown(totalCollateral(), supply),
shares.mulDivDown(_rdpxCollateral, supply)
);
}
function _beforeTokenTransfer(
address from,
address,
uint256
) internal virtual {}
// ================================ VIEWS ================================ //
/// @notice Returns the total active collateral
function activeCollateral() public view returns (uint256) {
return _activeCollateral;
}
/// @notice Returns the total collateral
function totalCollateral() public view returns (uint256) {
return _totalCollateral;
}
/// @notice Returns the total rdpx collateral
function rdpxCollateral() public view returns (uint256) {
return _rdpxCollateral;
}
/// @notice Returns the total available collateral
function totalAvailableCollateral() public view returns (uint256) {
return _totalCollateral - _activeCollateral;
}
// ================================ PUBLIC VIEW FUNCTIONS ================================ //
/// @notice Returns the amount of collateral and rdpx per share
function redeemPreview(
uint256 shares
) public view returns (uint256, uint256) {
return _convertToAssets(shares);
}
/// @notice Returns the amount of shares recieved for a given amount of assets
function previewDeposit(uint256 assets) public view returns (uint256) {
return convertToShares(assets);
}
/// @notice Returns the amount of shares recieved for a given amount of assets
function convertToShares(
uint256 assets
) public view returns (uint256 shares) {
uint256 supply = totalSupply;
uint256 rdpxPriceInAlphaToken = perpetualAtlanticVault.getUnderlyingPrice();
uint256 totalVaultCollateral = totalCollateral() +
((_rdpxCollateral * rdpxPriceInAlphaToken) / 1e8);
return
supply == 0 ? assets : assets.mulDivDown(supply, totalVaultCollateral);
}
function beforeWithdraw(uint256 assets, uint256 /*shares*/) internal {
require(
assets <= totalAvailableCollateral(),
"Not enough available assets to satisfy withdrawal"
);
_totalCollateral -= assets;
}
// ================================ MODIFIERS ================================ //
modifier onlyPerpVault() {
require(
msg.sender == address(perpetualAtlanticVault),
"Only the perp vault can call this function"
);
_;
}
}