-
Notifications
You must be signed in to change notification settings - Fork 0
/
OwnershipAllowlistHooks.sol
215 lines (174 loc) · 6.46 KB
/
OwnershipAllowlistHooks.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
import {IERC20} from "@openzeppelin/token/ERC20/IERC20.sol";
import {IERC721} from "@openzeppelin/token/ERC721/IERC721.sol";
import {IERC1155} from "@openzeppelin/token/ERC1155/IERC1155.sol";
import {PoolId} from "@uniV4/src/types/PoolId.sol";
import {IPoolManager} from "@uniV4/src/interfaces/IPoolManager.sol";
import {PoolKey} from "@uniV4/src/types/PoolKey.sol";
import {PoolId, PoolIdLibrary} from "@uniV4/src/types/PoolId.sol";
import {BalanceDelta} from "@uniV4/src/types/BalanceDelta.sol";
import {BaseHook} from "../BaseHook.sol";
/// @notice These hooks (inspired by https://github.com/wagmiwiz/nft-owners-only-uniswap-hook) restricts adding liqudity,
/// swapping, and donating depending on whether the tx.origin owns a specified token. Supported token standards
/// include ERC20s, ERC721s, and ERC1155s.
/// @dev Contracts below use tx.origin rather than msg.sender to determine ownership. The reason for this is so that
/// ownership isn't queried from the swap router, but instead the originator of the transaction. This does however
/// introduce the risk of malicious contracts exploiting user's owned assets to add liquidity, swap, or donate
/// without owning the asset themselves.
/// @notice This hooks restricts access given a minimum balance of an ERC20 token.
contract ERC20OwnershipAllowlistHook is BaseHook {
using PoolIdLibrary for PoolKey;
/// ERRORS ///
error MinBalanceNotHeld();
/// IMMUTABLES ///
IERC20 public immutable erc20;
uint256 public immutable minBalance;
/// CONSTRUCTOR ///
constructor(
IPoolManager _poolManager,
IERC20 _erc20,
uint256 _minBalance
) BaseHook(_poolManager) {
erc20 = _erc20;
minBalance = _minBalance;
}
/// OVERRIDES ///
function beforeAddLiquidity(
address,
PoolKey calldata,
IPoolManager.ModifyLiquidityParams calldata,
bytes calldata
) external view override poolManagerOnly returns (bytes4) {
if (!_senderHoldsMinTokens(tx.origin)) revert MinBalanceNotHeld();
return this.beforeAddLiquidity.selector;
}
function beforeSwap(
address,
PoolKey calldata,
IPoolManager.SwapParams calldata,
bytes calldata
) external view override poolManagerOnly returns (bytes4) {
if (!_senderHoldsMinTokens(tx.origin)) revert MinBalanceNotHeld();
return this.beforeSwap.selector;
}
function beforeDonate(
address,
PoolKey calldata,
uint256,
uint256,
bytes calldata
) external view override poolManagerOnly returns (bytes4) {
if (!_senderHoldsMinTokens(tx.origin)) revert MinBalanceNotHeld();
return this.beforeDonate.selector;
}
/// INTERNALS ///
function _senderHoldsMinTokens(address _sender) internal view returns (bool) {
return (erc20.balanceOf(_sender) >= minBalance);
}
}
/// @notice This hooks restricts access given a minimum balance of an ERC721 token
contract ERC721OwnershipAllowlistHook is BaseHook {
using PoolIdLibrary for PoolKey;
/// ERRORS ///
error MinBalanceNotHeld();
/// IMMUTABLES ///
IERC721 public immutable erc721;
uint256 public immutable minBalance;
/// CONSTRUCTOR ///
constructor(
IPoolManager _poolManager,
IERC721 _erc721,
uint256 _minBalance
) BaseHook(_poolManager) {
erc721 = _erc721;
minBalance = _minBalance;
}
/// OVERRIDES ///
function beforeAddLiquidity(
address,
PoolKey calldata,
IPoolManager.ModifyLiquidityParams calldata,
bytes calldata
) external view override poolManagerOnly returns (bytes4) {
if (!_senderHoldsMinTokens(tx.origin)) revert MinBalanceNotHeld();
return this.beforeAddLiquidity.selector;
}
function beforeSwap(
address,
PoolKey calldata,
IPoolManager.SwapParams calldata,
bytes calldata
) external view override poolManagerOnly returns (bytes4) {
if (!_senderHoldsMinTokens(tx.origin)) revert MinBalanceNotHeld();
return this.beforeSwap.selector;
}
function beforeDonate(
address,
PoolKey calldata,
uint256,
uint256,
bytes calldata
) external view override poolManagerOnly returns (bytes4) {
if (!_senderHoldsMinTokens(tx.origin)) revert MinBalanceNotHeld();
return this.beforeDonate.selector;
}
/// INTERNALS ///
function _senderHoldsMinTokens(address _sender) internal view returns (bool) {
return (erc721.balanceOf(_sender) >= minBalance);
}
}
/// @notice This hooks restricts access given a minimum balance of an ERC1155 token
contract ERC1155OwnershipAllowlistHook is BaseHook {
using PoolIdLibrary for PoolKey;
/// ERRORS ///
error MinBalanceNotHeld();
/// IMMUTABLES ///
IERC1155 public immutable erc1155;
uint256 public immutable tokenId;
uint256 public immutable minBalance;
/// CONSTRUCTOR ///
constructor(
IPoolManager _poolManager,
IERC1155 _erc1155,
uint256 _tokenId,
uint256 _minBalance
) BaseHook(_poolManager) {
erc1155 = _erc1155;
tokenId = _tokenId;
minBalance = _minBalance;
}
/// OVERRIDES ///
function beforeAddLiquidity(
address,
PoolKey calldata,
IPoolManager.ModifyLiquidityParams calldata,
bytes calldata
) external view override poolManagerOnly returns (bytes4) {
if (!_senderHoldsMinTokens(tx.origin)) revert MinBalanceNotHeld();
return this.beforeAddLiquidity.selector;
}
function beforeSwap(
address,
PoolKey calldata,
IPoolManager.SwapParams calldata,
bytes calldata
) external view override poolManagerOnly returns (bytes4) {
if (!_senderHoldsMinTokens(tx.origin)) revert MinBalanceNotHeld();
return this.beforeSwap.selector;
}
function beforeDonate(
address,
PoolKey calldata,
uint256,
uint256,
bytes calldata
) external view override poolManagerOnly returns (bytes4) {
if (!_senderHoldsMinTokens(tx.origin)) revert MinBalanceNotHeld();
return this.beforeDonate.selector;
}
/// INTERNALS ///
function _senderHoldsMinTokens(address _sender) internal view returns (bool) {
return (erc1155.balanceOf(_sender, tokenId) >= minBalance);
}
}