-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathPositionManager.sol
248 lines (216 loc) · 7.72 KB
/
PositionManager.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
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.24;
pragma abicoder v2;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "./libraries/LiquidityAmounts.sol";
import "./libraries/TickMath.sol";
import "./libraries/FixedPoint128.sol";
import "./interfaces/IPositionManager.sol";
import "./interfaces/IPool.sol";
import "./interfaces/IPoolManager.sol";
contract PositionManager is IPositionManager, ERC721 {
// 保存 PoolManager 合约地址
IPoolManager public poolManager;
/// @dev The ID of the next token that will be minted. Skips 0
uint176 private _nextId = 1;
constructor(address _poolManger) ERC721("WTFSwapPosition", "WTFP") {
poolManager = IPoolManager(_poolManger);
}
// 用一个 mapping 来存放所有 Position 的信息
mapping(uint256 => PositionInfo) public positions;
// 获取全部的 Position 信息
function getAllPositions()
external
view
override
returns (PositionInfo[] memory positionInfo)
{
positionInfo = new PositionInfo[](_nextId - 1);
for (uint32 i = 0; i < _nextId - 1; i++) {
positionInfo[i] = positions[i + 1];
}
return positionInfo;
}
function getSender() public view returns (address) {
return msg.sender;
}
function _blockTimestamp() internal view virtual returns (uint256) {
return block.timestamp;
}
modifier checkDeadline(uint256 deadline) {
require(_blockTimestamp() <= deadline, "Transaction too old");
_;
}
function mint(
MintParams calldata params
)
external
payable
override
checkDeadline(params.deadline)
returns (
uint256 positionId,
uint128 liquidity,
uint256 amount0,
uint256 amount1
)
{
// mint 一个 NFT 作为 position 发给 LP
// NFT 的 tokenId 就是 positionId
// 通过 MintParams 里面的 token0 和 token1 以及 index 获取对应的 Pool
// 调用 poolManager 的 getPool 方法获取 Pool 地址
address _pool = poolManager.getPool(
params.token0,
params.token1,
params.index
);
IPool pool = IPool(_pool);
// 通过获取 pool 相关信息,结合 params.amount0Desired 和 params.amount1Desired 计算这次要注入的流动性
uint160 sqrtPriceX96 = pool.sqrtPriceX96();
uint160 sqrtRatioAX96 = TickMath.getSqrtPriceAtTick(pool.tickLower());
uint160 sqrtRatioBX96 = TickMath.getSqrtPriceAtTick(pool.tickUpper());
liquidity = LiquidityAmounts.getLiquidityForAmounts(
sqrtPriceX96,
sqrtRatioAX96,
sqrtRatioBX96,
params.amount0Desired,
params.amount1Desired
);
// data 是 mint 后回调 PositionManager 会额外带的数据
// 需要 PoistionManger 实现回调,在回调中给 Pool 打钱
bytes memory data = abi.encode(
params.token0,
params.token1,
params.index,
msg.sender
);
(amount0, amount1) = pool.mint(address(this), liquidity, data);
_mint(params.recipient, (positionId = _nextId++));
(
,
uint256 feeGrowthInside0LastX128,
uint256 feeGrowthInside1LastX128,
,
) = pool.getPosition(address(this));
positions[positionId] = PositionInfo({
id: positionId,
owner: params.recipient,
token0: params.token0,
token1: params.token1,
index: params.index,
fee: pool.fee(),
liquidity: liquidity,
tickLower: pool.tickLower(),
tickUpper: pool.tickUpper(),
tokensOwed0: 0,
tokensOwed1: 0,
feeGrowthInside0LastX128: feeGrowthInside0LastX128,
feeGrowthInside1LastX128: feeGrowthInside1LastX128
});
}
modifier isAuthorizedForToken(uint256 tokenId) {
address owner = ERC721.ownerOf(tokenId);
require(_isAuthorized(owner, msg.sender, tokenId), "Not approved");
_;
}
function burn(
uint256 positionId
)
external
override
isAuthorizedForToken(positionId)
returns (uint256 amount0, uint256 amount1)
{
PositionInfo storage position = positions[positionId];
// 通过 isAuthorizedForToken 检查 positionId 是否有权限
// 移除流动性,但是 token 还是保留在 pool 中,需要再调用 collect 方法才能取回 token
// 通过 positionId 获取对应 LP 的流动性
uint128 _liquidity = position.liquidity;
// 调用 Pool 的方法给 LP 退流动性
address _pool = poolManager.getPool(
position.token0,
position.token1,
position.index
);
IPool pool = IPool(_pool);
(amount0, amount1) = pool.burn(_liquidity);
// 计算这部分流动性产生的手续费
(
,
uint256 feeGrowthInside0LastX128,
uint256 feeGrowthInside1LastX128,
,
) = pool.getPosition(address(this));
position.tokensOwed0 +=
uint128(amount0) +
uint128(
FullMath.mulDiv(
feeGrowthInside0LastX128 -
position.feeGrowthInside0LastX128,
position.liquidity,
FixedPoint128.Q128
)
);
position.tokensOwed1 +=
uint128(amount1) +
uint128(
FullMath.mulDiv(
feeGrowthInside1LastX128 -
position.feeGrowthInside1LastX128,
position.liquidity,
FixedPoint128.Q128
)
);
// 更新 position 的信息
position.feeGrowthInside0LastX128 = feeGrowthInside0LastX128;
position.feeGrowthInside1LastX128 = feeGrowthInside1LastX128;
position.liquidity = 0;
}
function collect(
uint256 positionId,
address recipient
)
external
override
isAuthorizedForToken(positionId)
returns (uint256 amount0, uint256 amount1)
{
// 通过 isAuthorizedForToken 检查 positionId 是否有权限
// 调用 Pool 的方法给 LP 退流动性
PositionInfo storage position = positions[positionId];
address _pool = poolManager.getPool(
position.token0,
position.token1,
position.index
);
IPool pool = IPool(_pool);
(amount0, amount1) = pool.collect(
recipient,
position.tokensOwed0,
position.tokensOwed1
);
// position 已经彻底没用了,销毁
position.tokensOwed0 = 0;
position.tokensOwed1 = 0;
_burn(positionId);
}
function mintCallback(
uint256 amount0,
uint256 amount1,
bytes calldata data
) external override {
// 检查 callback 的合约地址是否是 Pool
(address token0, address token1, uint32 index, address payer) = abi
.decode(data, (address, address, uint32, address));
address _pool = poolManager.getPool(token0, token1, index);
require(_pool == msg.sender, "Invalid callback caller");
// 在这里给 Pool 打钱,需要用户先 approve 足够的金额,这里才会成功
if (amount0 > 0) {
IERC20(token0).transferFrom(payer, msg.sender, amount0);
}
if (amount1 > 0) {
IERC20(token1).transferFrom(payer, msg.sender, amount1);
}
}
}