-
Notifications
You must be signed in to change notification settings - Fork 0
/
UniNftRouter.sol
415 lines (391 loc) · 13.4 KB
/
UniNftRouter.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.21;
import "forge-std/console.sol";
import "forge-std/console2.sol";
import {Hooks} from "v4-core/libraries/Hooks.sol";
import {IPoolManager, PoolId, ILockCallback, PoolKey} from "v4-core/PoolManager.sol";
import {BalanceDelta} from "v4-core/types/BalanceDelta.sol";
import {TickMath} from "v4-core/libraries/TickMath.sol";
import {Currency} from "v4-core/types/Currency.sol";
import {UniNftHook} from "./UniNftHook.sol";
import {UniNftToken} from "./UniNftToken.sol";
import {LibUniNft} from "./LibUniNft.sol";
import {FixedPointMathLib} from "solmate/utils/FixedPointMathLib.sol";
import {LiquidityAmounts} from "v4-periphery/libraries/LiquidityAmounts.sol";
import {PoolIdLibrary} from "v4-core/types/PoolId.sol";
contract UniNftRouter is ILockCallback {
using PoolIdLibrary for PoolKey;
enum LockAction {
CreateInitialPosition,
BuyNft,
SellNft
}
struct CreateInitialPositionData {
UniNftHook hook;
uint128 maxSupply;
uint24 fee;
uint128 value;
bool quote;
}
struct BuyNftData {
UniNftToken token;
uint128 maxPrice;
address receiver;
bytes receiverData;
bool quote;
}
struct SellNftData {
UniNftToken token;
uint256 tokenId;
uint128 minPrice;
address payable receiver;
}
error QuoteBuyRevert(uint256 price);
error QuoteCreateInitialPositionError(uint256 ethNeeded, uint256 price);
error QuoteCreateRevert(UniNftToken nftToken, uint256 ethNeeded, uint256 price);
event Created(UniNftToken token, string name);
IPoolManager public immutable MGR;
mapping (UniNftToken => bool) public isValidToken;
modifier onlyValidToken(UniNftToken token) {
require(isValidToken[token], 'not a valid NFT token');
_;
}
constructor(IPoolManager mgr) {
MGR = mgr;
}
error FailedToRefund();
function create(
string memory nftName,
string memory nftSymbol,
uint128 maxSupply,
string memory tokenUri,
uint24 fee,
uint256 hookSalt
)
external payable
returns (UniNftToken nftToken, uint256 ethUsed)
{
bytes32 saltyHookSalt = keccak256(abi.encode(msg.sender, hookSalt));
UniNftHook hook = new UniNftHook{salt: saltyHookSalt}(MGR, fee, nftName, nftSymbol, tokenUri);
nftToken = hook.NFT();
(ethUsed, ) = abi.decode(MGR.lock(
abi.encode(
LockAction.CreateInitialPosition,
CreateInitialPositionData(hook, maxSupply, fee, uint128(msg.value), false)
)
), (uint256, uint256));
isValidToken[nftToken] = true;
emit Created(nftToken, nftName);
if (ethUsed < msg.value) {
(bool success,) = payable(msg.sender).call{value: msg.value - ethUsed}("");
revert FailedToRefund();
}
}
function quoteCreate(
string memory nftName,
string memory nftSymbol,
uint128 maxSupply,
string memory tokenUri,
uint24 fee,
uint256 hookSalt,
uint256 ethDeposit
)
external
returns (UniNftToken nftToken, uint256 ethUsed, uint256 ethPrice)
{
try this.__quoteCreateAndRevert(
nftName,
nftSymbol,
maxSupply,
tokenUri,
fee,
hookSalt,
ethDeposit,
msg.sender
) {
assert(false);
} catch (bytes memory err) {
bytes4 selector;
assembly { selector := mload(add(err, 0x20)) }
if (selector == QuoteCreateRevert.selector) {
assembly {
nftToken := mload(add(err, 0x24))
ethUsed := mload(add(err, 0x44))
ethPrice := mload(add(err, 0x64))
}
return (nftToken, ethUsed, ethPrice);
}
assembly { revert(add(err, 0x20), mload(err)) }
}
}
function __quoteCreateAndRevert(
string memory nftName,
string memory nftSymbol,
uint128 maxSupply,
string memory tokenUri,
uint24 fee,
uint256 hookSalt,
uint256 ethDeposit,
address caller
)
external
{
bytes32 saltyHookSalt = keccak256(abi.encode(caller, hookSalt));
UniNftHook hook = new UniNftHook{salt: saltyHookSalt}(MGR, fee, nftName, nftSymbol, tokenUri);
UniNftToken nftToken = hook.NFT();
isValidToken[nftToken] = true;
try
MGR.lock(
abi.encode(
LockAction.CreateInitialPosition,
CreateInitialPositionData(hook, maxSupply, fee, uint128(ethDeposit), true)
)
)
{ assert(false); }
catch (bytes memory err) {
uint256 ethUsed;
uint256 ethPrice;
bytes4 selector;
assembly { selector := mload(add(err, 0x20)) }
if (selector == QuoteCreateInitialPositionError.selector) {
assembly {
ethUsed := mload(add(err, 0x24))
ethPrice := mload(add(err, 0x44))
}
revert QuoteCreateRevert(nftToken, ethUsed, ethPrice);
}
assembly { revert(add(err, 0x20), mload(err)) }
}
}
function sellNft(
UniNftToken token,
uint256 tokenId,
uint256 minPrice,
address payable receiver
)
external
returns (uint256 price)
{
token.transferFrom(msg.sender, address(this), tokenId);
price = abi.decode(MGR.lock(
abi.encode(
LockAction.SellNft,
SellNftData(token, tokenId, uint128(minPrice), receiver)
)
), (uint256));
}
function quoteBuyNft(UniNftToken token) external onlyValidToken(token)
returns (uint256 price)
{
try MGR.lock(
abi.encode(
LockAction.BuyNft,
BuyNftData(token, type(uint128).max, address(1), "", true)
)
) {
assert(false);
} catch (bytes memory err) {
bytes4 selector;
assembly { selector := mload(add(err, 0x20)) }
if (selector == QuoteBuyRevert.selector) {
assembly {
price := mload(add(err, 0x24))
}
return price;
}
assembly { revert(add(err, 0x20), mload(err)) }
}
}
function buyNft(
UniNftToken token,
uint256 maxPrice,
address receiver,
bytes memory receiverData
)
external payable onlyValidToken(token)
returns (uint256 price)
{
price = abi.decode(MGR.lock(
abi.encode(
LockAction.BuyNft,
BuyNftData(token, uint128(maxPrice), receiver, receiverData, false)
)
), (uint256));
if (price < msg.value) {
(bool success,) = payable(msg.sender).call{value: msg.value - price}("");
revert FailedToRefund();
}
}
function lockAcquired(bytes calldata data) external returns (bytes memory) {
require(msg.sender == address(MGR), 'not manager');
LockAction action = abi.decode(data, (LockAction));
if (action == LockAction.CreateInitialPosition) {
(, CreateInitialPositionData memory data_) = abi.decode(
data,
(uint256, CreateInitialPositionData)
);
return _createInitialPosition(data_);
} else if (action == LockAction.BuyNft) {
(, BuyNftData memory data_) = abi.decode(
data,
(uint256, BuyNftData)
);
return _buyNft(data_.token, data_.maxPrice, data_.receiver, data_.receiverData, data_.quote);
} else if (action == LockAction.SellNft) {
(, SellNftData memory data_) = abi.decode(
data,
(uint256, SellNftData)
);
return _sellNft(data_.token, data_.tokenId, data_.minPrice, data_.receiver);
}
return "";
}
function _createInitialPosition(CreateInitialPositionData memory data)
private
returns (bytes memory result)
{
PoolKey memory key = LibUniNft.getPoolKey(data.hook, data.fee);
uint160 initialPrice = uint160(
(FixedPointMathLib.sqrt(data.maxSupply * uint128(LibUniNft.RESOLUTION)) << 96) /
FixedPointMathLib.sqrt(data.value)
);
MGR.initialize(key, initialPrice, "");
// TODO: This should be from [initialPrice, inf+)?
int24 tickLower = (TickMath.MIN_TICK / key.tickSpacing) * key.tickSpacing;
int24 tickUpper = (TickMath.MAX_TICK / key.tickSpacing) * key.tickSpacing;
IPoolManager.ModifyPositionParams memory params;
params.tickLower = tickLower;
params.tickUpper = tickUpper;
params.liquidityDelta = int128(LiquidityAmounts.getLiquidityForAmounts(
initialPrice,
TickMath.getSqrtRatioAtTick(tickLower),
TickMath.getSqrtRatioAtTick(tickUpper),
data.value,
data.maxSupply * uint128(LibUniNft.RESOLUTION)
));
// DANGER!
// TODO: This needs to be called by a dedicated addressed controlled by the creator.
BalanceDelta delta = MGR.modifyPosition(key, params, "");
int128 ethNeeded = delta.amount0();
int128 erc20Needed = delta.amount1();
if (data.quote) {
assert(ethNeeded > 0);
revert QuoteCreateInitialPositionError(uint128(ethNeeded), this.quoteBuyNft(data.hook.NFT()));
}
if (ethNeeded > 0) {
MGR.settle{value: uint128(ethNeeded)}(Currency.wrap(address(0)));
} else {
ethNeeded = 0;
}
if (erc20Needed > 0) {
MGR.settle(Currency.wrap(address(data.hook)));
}
return abi.encode(ethNeeded, uint256(0));
}
function _ethToSqrtPrice(uint256 ethPrice) private pure returns (uint160 sqrtPrice) {
return uint160(
(FixedPointMathLib.sqrt(1 * LibUniNft.RESOLUTION) << 96) /
FixedPointMathLib.sqrt(ethPrice)
);
}
function _buyNft(
UniNftToken token,
uint128 maxPrice,
address receiver,
bytes memory receiverData,
bool quote
)
private
returns (bytes memory result)
{
UniNftHook hook = token.HOOK();
PoolKey memory key = hook.getPoolKey();
IPoolManager.SwapParams memory params = IPoolManager.SwapParams({
zeroForOne: true,
amountSpecified: -1 * int128(uint128(LibUniNft.RESOLUTION)),
sqrtPriceLimitX96: _ethToSqrtPrice(maxPrice)
});
BalanceDelta delta = MGR.swap(key, params, abi.encode(receiver, receiverData));
int128 ethAmount = delta.amount0();
int128 erc20Amount = delta.amount1();
if (quote) {
assert(ethAmount >= 0);
revert QuoteBuyRevert(uint128(ethAmount));
}
if (ethAmount > 0) {
MGR.settle{value: uint128(ethAmount)}(Currency.wrap(address(0)));
} else {
ethAmount = 0;
}
if (erc20Amount < 0) {
MGR.take(Currency.wrap(address(hook)), address(1), uint128(-erc20Amount));
}
return abi.encode(ethAmount);
}
function _sellNft(
UniNftToken token,
uint256 tokenId,
uint128 minPrice,
address payable receiver
)
private
returns (bytes memory result)
{
UniNftHook hook = token.HOOK();
PoolKey memory key = hook.getPoolKey();
IPoolManager.SwapParams memory params = IPoolManager.SwapParams({
zeroForOne: false,
amountSpecified: 1 * int128(uint128(LibUniNft.RESOLUTION)),
sqrtPriceLimitX96: _ethToSqrtPrice(minPrice)
});
BalanceDelta delta = MGR.swap(key, params, abi.encode(tokenId));
int128 ethAmount = delta.amount0();
int128 erc20Amount = delta.amount1();
if (ethAmount < 0) {
MGR.take(Currency.wrap(address(0)), receiver, uint128(-ethAmount));
} else {
ethAmount = 0;
}
if (erc20Amount > 0) {
MGR.settle(Currency.wrap(address(hook)));
}
return abi.encode(-ethAmount);
}
function findHookSalt(
string memory nftName,
string memory nftSymbol,
string memory tokenUri,
uint24 fee,
address caller,
uint256 saltStart
)
external view
returns (uint256 salt)
{
uint256 PREFIX = Hooks.AFTER_SWAP_FLAG | Hooks.AFTER_MODIFY_POSITION_FLAG;
uint256 LEADING_BIT_MASK = 255 << 152;
bytes32 initHash = keccak256(abi.encodePacked(
type(UniNftHook).creationCode,
abi.encode(
MGR,
fee,
nftName,
nftSymbol,
tokenUri
)
));
salt = saltStart;
while (true) {
uint256 h = uint256(keccak256(abi.encodePacked(
'\xff',
address(this),
keccak256(abi.encode(caller, salt)),
initHash
)));
if (LEADING_BIT_MASK & h == PREFIX) {
break;
}
++salt;
}
}
}