-
Notifications
You must be signed in to change notification settings - Fork 4
/
LiquidityManager.sol
251 lines (197 loc) · 8.54 KB
/
LiquidityManager.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
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity ^0.8.4;
import { Multicall } from "./Multicall.sol";
import { Payment } from "./Payment.sol";
import { SelfPermit } from "./SelfPermit.sol";
import { ILendgine } from "../core/interfaces/ILendgine.sol";
import { IPairMintCallback } from "../core/interfaces/callback/IPairMintCallback.sol";
import { FullMath } from "../libraries/FullMath.sol";
import { LendgineAddress } from "./libraries/LendgineAddress.sol";
/// @notice Manages liquidity provider positions
/// @author Kyle Scott (kyle@numoen.com)
contract LiquidityManager is Multicall, Payment, SelfPermit, IPairMintCallback {
/*//////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
event AddLiquidity(
address indexed from,
address indexed lendgine,
uint256 liquidity,
uint256 size,
uint256 amount0,
uint256 amount1,
address indexed to
);
event RemoveLiquidity(
address indexed from,
address indexed lendgine,
uint256 liquidity,
uint256 size,
uint256 amount0,
uint256 amount1,
address indexed to
);
event Collect(address indexed from, address indexed lendgine, uint256 amount, address indexed to);
/*//////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/
error LivelinessError();
error AmountError();
error ValidationError();
error PositionInvalidError();
error CollectError();
/*//////////////////////////////////////////////////////////////
STORAGE
//////////////////////////////////////////////////////////////*/
address public immutable factory;
struct Position {
uint256 size;
uint256 rewardPerPositionPaid;
uint256 tokensOwed;
}
/// @notice Owner to lendgine to position
mapping(address => mapping(address => Position)) public positions;
/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
constructor(address _factory, address _weth) Payment(_weth) {
factory = _factory;
}
/*//////////////////////////////////////////////////////////////
LIVELINESS MODIFIER
//////////////////////////////////////////////////////////////*/
modifier checkDeadline(uint256 deadline) {
if (deadline < block.timestamp) revert LivelinessError();
_;
}
/*//////////////////////////////////////////////////////////////
CALLBACK
//////////////////////////////////////////////////////////////*/
struct PairMintCallbackData {
address token0;
address token1;
uint256 token0Exp;
uint256 token1Exp;
uint256 upperBound;
uint256 amount0;
uint256 amount1;
address payer;
}
/// @notice callback that sends the underlying tokens for the specified amount of liquidity shares
function pairMintCallback(uint256, bytes calldata data) external {
PairMintCallbackData memory decoded = abi.decode(data, (PairMintCallbackData));
address lendgine = LendgineAddress.computeAddress(
factory, decoded.token0, decoded.token1, decoded.token0Exp, decoded.token1Exp, decoded.upperBound
);
if (lendgine != msg.sender) revert ValidationError();
if (decoded.amount0 > 0) pay(decoded.token0, decoded.payer, msg.sender, decoded.amount0);
if (decoded.amount1 > 0) pay(decoded.token1, decoded.payer, msg.sender, decoded.amount1);
}
/*//////////////////////////////////////////////////////////////
LIQUIDITY MANAGER LOGIC
//////////////////////////////////////////////////////////////*/
struct AddLiquidityParams {
address token0;
address token1;
uint256 token0Exp;
uint256 token1Exp;
uint256 upperBound;
uint256 liquidity;
uint256 amount0Min;
uint256 amount1Min;
uint256 sizeMin;
address recipient;
uint256 deadline;
}
/// @notice Add liquidity to a liquidity position
function addLiquidity(AddLiquidityParams calldata params) external payable checkDeadline(params.deadline) {
address lendgine = LendgineAddress.computeAddress(
factory, params.token0, params.token1, params.token0Exp, params.token1Exp, params.upperBound
);
uint256 r0 = ILendgine(lendgine).reserve0();
uint256 r1 = ILendgine(lendgine).reserve1();
uint256 totalLiquidity = ILendgine(lendgine).totalLiquidity();
uint256 amount0;
uint256 amount1;
if (totalLiquidity == 0) {
amount0 = params.amount0Min;
amount1 = params.amount1Min;
} else {
amount0 = FullMath.mulDivRoundingUp(params.liquidity, r0, totalLiquidity);
amount1 = FullMath.mulDivRoundingUp(params.liquidity, r1, totalLiquidity);
}
if (amount0 < params.amount0Min || amount1 < params.amount1Min) revert AmountError();
uint256 size = ILendgine(lendgine).deposit(
address(this),
params.liquidity,
abi.encode(
PairMintCallbackData({
token0: params.token0,
token1: params.token1,
token0Exp: params.token0Exp,
token1Exp: params.token1Exp,
upperBound: params.upperBound,
amount0: amount0,
amount1: amount1,
payer: msg.sender
})
)
);
if (size < params.sizeMin) revert AmountError();
Position memory position = positions[params.recipient][lendgine]; // SLOAD
(, uint256 rewardPerPositionPaid,) = ILendgine(lendgine).positions(address(this));
position.tokensOwed += FullMath.mulDiv(position.size, rewardPerPositionPaid - position.rewardPerPositionPaid, 1e18);
position.rewardPerPositionPaid = rewardPerPositionPaid;
position.size += size;
positions[params.recipient][lendgine] = position; // SSTORE
emit AddLiquidity(msg.sender, lendgine, params.liquidity, size, amount0, amount1, params.recipient);
}
struct RemoveLiquidityParams {
address token0;
address token1;
uint256 token0Exp;
uint256 token1Exp;
uint256 upperBound;
uint256 size;
uint256 amount0Min;
uint256 amount1Min;
address recipient;
uint256 deadline;
}
/// @notice Removes from a liquidity position
function removeLiquidity(RemoveLiquidityParams calldata params) external payable checkDeadline(params.deadline) {
address lendgine = LendgineAddress.computeAddress(
factory, params.token0, params.token1, params.token0Exp, params.token1Exp, params.upperBound
);
address recipient = params.recipient == address(0) ? address(this) : params.recipient;
(uint256 amount0, uint256 amount1, uint256 liquidity) = ILendgine(lendgine).withdraw(recipient, params.size);
if (amount0 < params.amount0Min || amount1 < params.amount1Min) revert AmountError();
Position memory position = positions[msg.sender][lendgine]; // SLOAD
(, uint256 rewardPerPositionPaid,) = ILendgine(lendgine).positions(address(this));
position.tokensOwed += FullMath.mulDiv(position.size, rewardPerPositionPaid - position.rewardPerPositionPaid, 1e18);
position.rewardPerPositionPaid = rewardPerPositionPaid;
position.size -= params.size;
positions[msg.sender][lendgine] = position; // SSTORE
emit RemoveLiquidity(msg.sender, lendgine, liquidity, params.size, amount0, amount1, recipient);
}
struct CollectParams {
address lendgine;
address recipient;
uint256 amountRequested;
}
/// @notice Collects interest owed to the callers liqudity position
function collect(CollectParams calldata params) external payable returns (uint256 amount) {
ILendgine(params.lendgine).accruePositionInterest();
address recipient = params.recipient == address(0) ? address(this) : params.recipient;
Position memory position = positions[msg.sender][params.lendgine]; // SLOAD
(, uint256 rewardPerPositionPaid,) = ILendgine(params.lendgine).positions(address(this));
position.tokensOwed += FullMath.mulDiv(position.size, rewardPerPositionPaid - position.rewardPerPositionPaid, 1e18);
position.rewardPerPositionPaid = rewardPerPositionPaid;
amount = params.amountRequested > position.tokensOwed ? position.tokensOwed : params.amountRequested;
position.tokensOwed -= amount;
positions[msg.sender][params.lendgine] = position; // SSTORE
uint256 collectAmount = ILendgine(params.lendgine).collect(recipient, amount);
if (collectAmount != amount) revert CollectError(); // extra check for safety
emit Collect(msg.sender, params.lendgine, amount, recipient);
}
}