-
Notifications
You must be signed in to change notification settings - Fork 5
/
KatanaGovernance.sol
405 lines (344 loc) · 11.1 KB
/
KatanaGovernance.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;
import { OwnableUpgradeable } from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import { EnumerableSet } from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
import { Address } from "@openzeppelin/contracts/utils/Address.sol";
import { IKatanaV3Factory } from "@katana/v3-contracts/core/interfaces/IKatanaV3Factory.sol";
import { IKatanaV2Factory } from "./interfaces/IKatanaV2Factory.sol";
import { IKatanaV2Pair } from "@katana/v3-contracts/periphery/interfaces/IKatanaV2Pair.sol";
import { IKatanaGovernance } from "@katana/v3-contracts/external/interfaces/IKatanaGovernance.sol";
contract KatanaGovernance is OwnableUpgradeable, IKatanaV2Factory, IKatanaGovernance {
using EnumerableSet for EnumerableSet.AddressSet;
/// @dev Revert error when the length of the array is invalid.
error InvalidLength();
/// @dev Revert error when the caller is not authorized.
error Unauthorized();
/// @dev Gap for upgradeability.
uint256[50] private __gap;
/// @dev Indicates the token is unauthorized for trade.
uint40 private constant UNAUTHORIZED = 0;
/// @dev Indicates the token is publicly allowed for trade.
uint40 private constant AUTHORIZED = type(uint40).max;
/// @dev The v2 factory contract.
IKatanaV2Factory private _v2Factory;
/// @dev The mapping of token to permission.
mapping(address token => Permission) private _permission;
/// @dev The unique set of tokens.
EnumerableSet.AddressSet private _tokens;
/// @inheritdoc IKatanaGovernance
/// @dev The mapping of allowed actors.
mapping(address actor => bool) public isAllowedActor;
/// @dev The v3 factory address.
IKatanaV3Factory private _v3Factory;
/// @dev The position manager address.
address private _positionManager;
/// @dev The router address.
address private _router;
constructor() {
_disableInitializers();
}
function initialize(address admin, address factory) external initializer {
_setFactory(factory);
__Ownable_init_unchained(admin);
IKatanaV2Pair pair;
uint40 until = AUTHORIZED;
bool[] memory statusesPlaceHolder;
address[] memory allowedPlaceHolder;
uint256 length = IKatanaV2Factory(factory).allPairsLength();
for (uint256 i; i < length; ++i) {
pair = IKatanaV2Pair(IKatanaV2Factory(factory).allPairs(i));
_setPermission(pair.token0(), until, allowedPlaceHolder, statusesPlaceHolder);
_setPermission(pair.token1(), until, allowedPlaceHolder, statusesPlaceHolder);
}
}
function initializeV2(
address v3Factory,
address positionManager,
address v3Migrator,
address legacyPermissionedRouter,
address aggregateRouter
) external reinitializer(2) {
_v3Factory = IKatanaV3Factory(v3Factory);
_positionManager = positionManager;
_setRouter(aggregateRouter);
_setAllowedActor(v3Migrator, true);
_setAllowedActor(legacyPermissionedRouter, true);
_setAllowedActor(aggregateRouter, true);
}
/**
* @inheritdoc IKatanaGovernance
*/
function setRouter(address router) external onlyOwner {
_setRouter(router);
}
/**
* @inheritdoc IKatanaGovernance
*/
function setAllowedActor(address actor, bool allowed) external onlyOwner {
_setAllowedActor(actor, allowed);
}
/**
* @inheritdoc IKatanaGovernance
*/
function toggleFlashLoanPermission() external onlyOwner {
_v3Factory.toggleFlashLoanPermission();
}
/**
* @inheritdoc IKatanaGovernance
*/
function enableFeeAmount(uint24 fee, int24 tickSpacing, uint8 feeProtocolNum, uint8 feeProtocolDen)
external
onlyOwner
{
_v3Factory.enableFeeAmount(fee, tickSpacing, feeProtocolNum, feeProtocolDen);
}
/**
* @inheritdoc IKatanaV2Factory
*/
function createPair(address tokenA, address tokenB) external returns (address pair) {
address sender = _msgSender();
address[] memory tokens = new address[](2);
tokens[0] = tokenA;
tokens[1] = tokenB;
if (!isAuthorized(tokens, sender)) revert Unauthorized();
pair = _v2Factory.createPair(tokenA, tokenB);
}
/**
* @inheritdoc IKatanaGovernance
*/
function createPairAndSetPermission(
address tokenA,
address tokenB,
uint40 whitelistUntil,
address[] calldata alloweds,
bool[] calldata statuses
) external onlyOwner returns (address pair) {
pair = _v2Factory.createPair(tokenA, tokenB);
_setPermission(tokenA, whitelistUntil, alloweds, statuses);
_setPermission(tokenB, whitelistUntil, alloweds, statuses);
}
/**
* @inheritdoc IKatanaGovernance
*/
function setPermission(address token, uint40 whitelistUntil, address[] calldata alloweds, bool[] calldata statuses)
external
onlyOwner
{
_setPermission(token, whitelistUntil, alloweds, statuses);
}
/**
* @inheritdoc IKatanaV2Factory
*/
function setPairImplementation(address impl) external onlyOwner {
_v2Factory.setPairImplementation(impl);
}
/**
* @inheritdoc IKatanaV2Factory
*/
function setAllowedAll(bool shouldAllow) external onlyOwner {
_v2Factory.setAllowedAll(shouldAllow);
}
/**
* @inheritdoc IKatanaV2Factory
*/
function setTreasury(address newTreasury) external onlyOwner {
_v2Factory.setTreasury(newTreasury);
_v3Factory.setTreasury(newTreasury);
}
/**
* @inheritdoc IKatanaV2Factory
*/
function getPair(address tokenA, address tokenB) external view returns (address pair) {
return _v2Factory.getPair(tokenA, tokenB);
}
/**
* @inheritdoc IKatanaV2Factory
*/
function allPairs(uint256 index) external view returns (address pair) {
return _v2Factory.allPairs(index);
}
/**
* @inheritdoc IKatanaV2Factory
*/
function allPairsLength() external view returns (uint256) {
return _v2Factory.allPairsLength();
}
/**
* @inheritdoc IKatanaV2Factory
*/
function treasury() external view returns (address) {
return _v2Factory.treasury();
}
/**
* @inheritdoc IKatanaV2Factory
*/
function pairImplementation() external view returns (address) {
return _v2Factory.pairImplementation();
}
/**
* @inheritdoc IKatanaV2Factory
*/
function INIT_CODE_PAIR_HASH() external view returns (bytes32) {
return _v2Factory.INIT_CODE_PAIR_HASH();
}
/**
* @inheritdoc IKatanaGovernance
*/
function getRouter() external view returns (address) {
return _router;
}
/**
* @inheritdoc IKatanaGovernance
*/
function getV2Factory() external view returns (address) {
return address(_v2Factory);
}
/**
* @inheritdoc IKatanaGovernance
*/
function getV3Factory() external view override returns (address) {
return address(_v3Factory);
}
/**
* @inheritdoc IKatanaGovernance
*/
function getPositionManager() external view override returns (address) {
return _positionManager;
}
/**
* @inheritdoc IKatanaGovernance
*/
function isAuthorized(address token, address account) public view returns (bool authorized) {
if (_isSkipped(account)) return true;
authorized = _isAuthorized(_permission[token], account);
}
/**
* @inheritdoc IKatanaGovernance
*/
function isAuthorized(address[] memory tokens, address account) public view returns (bool authorized) {
if (_isSkipped(account)) return true;
uint256 length = tokens.length;
for (uint256 i; i < length; ++i) {
if (!_isAuthorized(_permission[tokens[i]], account)) return false;
}
return true;
}
/**
* @inheritdoc IKatanaGovernance
*/
function getWhitelistUntil(address token) external view returns (uint40) {
return _permission[token].whitelistUntil;
}
/**
* @inheritdoc IKatanaGovernance
*/
function getWhitelistedTokensFor(address account)
external
view
returns (address[] memory tokens, uint40[] memory whitelistUntils)
{
unchecked {
uint256 length = _tokens.length();
tokens = new address[](length);
whitelistUntils = new uint40[](length);
uint256 count;
address token;
uint40 whitelistUntil;
Permission storage $;
for (uint256 i; i < length; ++i) {
token = _tokens.at(i);
$ = _permission[token];
whitelistUntil = $.whitelistUntil;
if (block.timestamp < whitelistUntil && $.allowed[account]) {
tokens[count] = token;
whitelistUntils[count] = whitelistUntil;
++count;
}
}
assembly {
mstore(tokens, count)
mstore(whitelistUntils, count)
}
}
}
/**
* @inheritdoc IKatanaGovernance
*/
function getManyTokensWhitelistInfo()
external
view
returns (address[] memory tokens, uint40[] memory whitelistedUntils)
{
tokens = _tokens.values();
uint256 length = tokens.length;
whitelistedUntils = new uint40[](length);
for (uint256 i; i < length; ++i) {
whitelistedUntils[i] = _permission[tokens[i]].whitelistUntil;
}
}
/**
* @inheritdoc IKatanaV2Factory
*/
function allowedAll() public view returns (bool) {
return _v2Factory.allowedAll();
}
/**
* @dev Sets the address of the factory contract.
* Can only be called by the contract owner.
*/
function _setFactory(address factory) private {
_v2Factory = IKatanaV2Factory(factory);
emit FactoryUpdated(_msgSender(), factory);
}
/**
* @dev Sets the permission for a token.
* @param token The address of the token.
* @param whitelistUntil The end of the whitelist duration in seconds.
* @param alloweds The array of addresses to be allowed in whitelist duration.
* @param statuses The corresponding array of statuses (whether allowed or not).
*/
function _setPermission(address token, uint40 whitelistUntil, address[] memory alloweds, bool[] memory statuses)
private
{
uint256 length = alloweds.length;
if (length != statuses.length) revert InvalidLength();
Permission storage $ = _permission[token];
$.whitelistUntil = whitelistUntil;
_tokens.add(token);
for (uint256 i; i < length; ++i) {
$.allowed[alloweds[i]] = statuses[i];
}
emit PermissionUpdated(_msgSender(), token, whitelistUntil, alloweds, statuses);
}
/**
* @dev Checks if an account is authorized.
* @param account The address of the account to check authorization for.
* @return A boolean indicating whether the account is authorized or not.
*/
function _isAuthorized(Permission storage $, address account) private view returns (bool) {
uint256 expiry = $.whitelistUntil;
if (expiry == UNAUTHORIZED) return false;
if (expiry == AUTHORIZED || block.timestamp > expiry) return true;
return $.allowed[account];
}
/**
* @dev Returns whether the account is skipped from authorization checks.
*/
function _isSkipped(address account) internal view returns (bool) {
return isAllowedActor[account] || allowedAll() || account == owner();
}
/**
* @dev Sets the router address.
*/
function _setRouter(address router) internal {
emit RouterUpdated(_msgSender(), _router, router);
_router = router;
}
/**
* @dev Sets the actor as allowed or not.
*/
function _setAllowedActor(address actor, bool allowed) internal {
isAllowedActor[actor] = allowed;
emit AllowedActorUpdated(actor, allowed);
}
}