forked from neptune-mutual-blue/protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVaultLiquidity.sol
191 lines (158 loc) · 7.04 KB
/
VaultLiquidity.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
// Neptune Mutual Protocol (https://neptunemutual.com)
// SPDX-License-Identifier: BUSL-1.1
import "./VaultBase.sol";
pragma solidity ^0.8.0;
abstract contract VaultLiquidity is VaultBase {
using ProtoUtilV1 for IStore;
using RegistryLibV1 for IStore;
using NTransferUtilV2 for IERC20;
/**
* @dev Transfers stablecoins to claims processor contracts for claims payout.
* Uses the hooks `preTransferGovernance` and `postTransferGovernance` on the vault delegate contract.
*
* @custom:suppress-acl This function is only callable by the claims processor as checked in `preTransferGovernance` and `postTransferGovernace`
* @custom:suppress-pausable
*
*/
function transferGovernance(
bytes32 coverKey,
address to,
uint256 amount
) external override nonReentrant {
require(coverKey == key, "Forbidden");
require(amount > 0, "Please specify amount");
/******************************************************************************************
PRE
******************************************************************************************/
address stablecoin = delgate().preTransferGovernance(msg.sender, coverKey, to, amount);
/******************************************************************************************
BODY
******************************************************************************************/
IERC20(stablecoin).ensureTransfer(to, amount);
/******************************************************************************************
POST
******************************************************************************************/
delgate().postTransferGovernance(msg.sender, coverKey, to, amount);
emit GovernanceTransfer(to, amount);
}
/**
* @dev Adds liquidity to the specified cover contract.
* Uses the hooks `preAddLiquidity` and `postAddLiquidity` on the vault delegate contract.
*
* @custom:suppress-acl This is a publicly accessible feature
* @custom:suppress-pausable
*
* @param coverKey Enter the cover key
* @param amount Enter the amount of liquidity token to supply.
* @param npmStakeToAdd Enter the amount of NPM token to stake.
*
*/
function addLiquidity(
bytes32 coverKey,
uint256 amount,
uint256 npmStakeToAdd,
bytes32 referralCode
) external override nonReentrant {
require(coverKey == key, "Forbidden");
require(amount > 0, "Please specify amount");
/******************************************************************************************
PRE
******************************************************************************************/
(uint256 podsToMint, uint256 previousNpmStake) = delgate().preAddLiquidity(msg.sender, coverKey, amount, npmStakeToAdd);
require(podsToMint > 0, "Can't determine PODs");
/******************************************************************************************
BODY
******************************************************************************************/
IERC20(sc).ensureTransferFrom(msg.sender, address(this), amount);
if (npmStakeToAdd > 0) {
IERC20(s.getNpmTokenAddress()).ensureTransferFrom(msg.sender, address(this), npmStakeToAdd);
}
super._mint(msg.sender, podsToMint);
/******************************************************************************************
POST
******************************************************************************************/
delgate().postAddLiquidity(msg.sender, coverKey, amount, npmStakeToAdd);
emit PodsIssued(msg.sender, podsToMint, amount, referralCode);
if (previousNpmStake == 0) {
emit Entered(coverKey, msg.sender);
}
emit NpmStaken(msg.sender, npmStakeToAdd);
}
/**
* @dev Removes liquidity from the specified cover contract
* Uses the hooks `preRemoveLiquidity` and `postRemoveLiquidity` on the vault delegate contract.
*
* @custom:suppress-acl This is a publicly accessible feature
* @custom:suppress-pausable
*
* @param coverKey Enter the cover key
* @param podsToRedeem Enter the amount of pods to redeem
* @param npmStakeToRemove Enter the amount of NPM stake to remove.
*/
function removeLiquidity(
bytes32 coverKey,
uint256 podsToRedeem,
uint256 npmStakeToRemove,
bool exit
) external override nonReentrant {
require(coverKey == key, "Forbidden");
require(podsToRedeem > 0 || npmStakeToRemove > 0, "Please specify amount");
/******************************************************************************************
PRE
******************************************************************************************/
(address stablecoin, uint256 stablecoinToRelease) = delgate().preRemoveLiquidity(msg.sender, coverKey, podsToRedeem, npmStakeToRemove, exit);
/******************************************************************************************
BODY
******************************************************************************************/
if(podsToRedeem > 0) {
IERC20(address(this)).ensureTransferFrom(msg.sender, address(this), podsToRedeem);
IERC20(stablecoin).ensureTransfer(msg.sender, stablecoinToRelease);
}
super._burn(address(this), podsToRedeem);
// Unstake NPM tokens
if (npmStakeToRemove > 0) {
IERC20(s.getNpmTokenAddress()).ensureTransfer(msg.sender, npmStakeToRemove);
}
/******************************************************************************************
POST
******************************************************************************************/
delgate().postRemoveLiquidity(msg.sender, coverKey, podsToRedeem, npmStakeToRemove, exit);
emit PodsRedeemed(msg.sender, podsToRedeem, stablecoinToRelease);
if (exit) {
emit Exited(coverKey, msg.sender);
}
if (npmStakeToRemove > 0) {
emit NpmUnstaken(msg.sender, npmStakeToRemove);
}
}
/**
* @dev Calculates the amount of PODS to mint for the given amount of liquidity to transfer
*/
function calculatePods(uint256 forStablecoinUnits) external view override returns (uint256) {
return delgate().calculatePodsImplementation(key, forStablecoinUnits);
}
/**
* @dev Calculates the amount of stablecoins to withdraw for the given amount of PODs to redeem
*/
function calculateLiquidity(uint256 podsToBurn) external view override returns (uint256) {
return delgate().calculateLiquidityImplementation(key, podsToBurn);
}
/**
* @dev Returns the stablecoin balance of this vault
* This also includes amounts lent out in lending strategies
*/
function getStablecoinBalanceOf() external view override returns (uint256) {
return delgate().getStablecoinBalanceOfImplementation(key);
}
/**
* @dev Accrues interests from external straties
*
* @custom:suppress-acl This is a publicly accessible feature
* @custom:suppress-pausable Validated in `accrueInterestImplementation`
*
*/
function accrueInterest() external override nonReentrant {
delgate().accrueInterestImplementation(msg.sender, key);
emit InterestAccrued(key);
}
}