-
Notifications
You must be signed in to change notification settings - Fork 0
/
UniNftHook.sol
121 lines (109 loc) · 3.62 KB
/
UniNftHook.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
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.21;
import {Hooks} from 'v4-core/libraries/Hooks.sol';
import {IPoolManager} from "v4-core/interfaces/IPoolManager.sol";
import {PoolKey} from "v4-core/types/PoolKey.sol";
import {PoolId} from "v4-core/types/PoolId.sol";
import {Currency} from "v4-core/types/Currency.sol";
import {BaseHook} from './BaseHook.sol';
import {UniNftToken} from "./UniNftToken.sol";
import {LibUniNft} from "./LibUniNft.sol";
import {BalanceDelta} from "v4-core/types/BalanceDelta.sol";
import {PoolIdLibrary} from "v4-core/types/PoolId.sol";
contract UniNftHook is BaseHook {
using PoolIdLibrary for PoolKey;
UniNftToken public immutable NFT;
uint256 public constant decimals = 18;
uint256 totalSupply;
bool isPoolSeeded;
uint24 _fee;
string public name;
string public symbol;
modifier onlyManager() {
require(msg.sender == address(poolManager), 'not manager');
_;
}
constructor(
IPoolManager mgr,
uint24 fee,
string memory name_,
string memory symbol_,
string memory tokenUri
) BaseHook(mgr) {
NFT = new UniNftToken(mgr, this, fee, name_, symbol_, tokenUri);
name = name_;
symbol = symbol_;
_fee = fee;
}
function getPoolKey() external view returns (PoolKey memory key) {
key = LibUniNft.getPoolKey(this, _fee);
}
function balanceOf(address owner) external view returns (uint256) {
if (owner == address(poolManager)) {
return totalSupply;
}
return 0;
}
function transfer(address, uint256) external pure returns (bool) {
// No-op.
return true;
}
function getHooksCalls() public pure override(BaseHook) returns (Hooks.Calls memory) {
return Hooks.Calls({
beforeInitialize: false,
afterInitialize: false,
beforeModifyPosition: false,
afterModifyPosition: true,
beforeSwap: false,
afterSwap: true,
beforeDonate: false,
afterDonate: false
});
}
function afterModifyPosition(
address,
PoolKey calldata,
IPoolManager.ModifyPositionParams calldata,
BalanceDelta delta,
bytes calldata
)
external override onlyManager
returns (bytes4)
{
if (!isPoolSeeded) {
assert(delta.amount1() >= 0);
isPoolSeeded = true;
totalSupply += uint128(delta.amount1());
}
return this.afterModifyPosition.selector;
}
function afterSwap(
address sender,
PoolKey calldata,
IPoolManager.SwapParams calldata,
BalanceDelta delta,
bytes calldata hookData
)
external override onlyManager
returns (bytes4)
{
int128 ethNeeded = delta.amount0();
int128 erc20Needed = delta.amount1();
int256 tokenCount = erc20Needed / int256(LibUniNft.RESOLUTION);
require(tokenCount * int256(LibUniNft.RESOLUTION) == erc20Needed, 'must swap a whole NFT');
if (tokenCount < 0) {
// Buy
(address receiver, bytes memory receiverData) = abi.decode(hookData, (address, bytes));
NFT.claimOrMint(receiver, receiverData);
totalSupply -= uint128(-erc20Needed);
assert(ethNeeded >= 0);
} else {
// Sell
(uint256 tokenId) = abi.decode(hookData, (uint256));
NFT.stash(sender, tokenId);
assert(ethNeeded <= 0);
totalSupply += uint128(erc20Needed);
}
return this.afterSwap.selector;
}
}