forked from filterswap/filterswap-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilterFactory.sol
More file actions
375 lines (289 loc) · 13.9 KB
/
FilterFactory.sol
File metadata and controls
375 lines (289 loc) · 13.9 KB
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
//SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8;
interface IFilterManager {
function treasuryAddress() external view returns (address);
function factoryAddress() external view returns (address);
function isTokenVerified(address) external view returns (bool);
function isLiquidityLocked(address, address) external view returns (bool);
function swapLiquidityProviderFee() external view returns (uint);
function swapTreasuryFee() external view returns (uint);
}
interface IFilterFactory {
event PairCreated(address indexed, address indexed, address, uint);
}
interface IFilterPair {
function initialize(address, address, address) external;
}
interface IERC20 {
function transfer(address, uint) external returns (bool);
function balanceOf(address) external view returns (uint);
}
interface IFilterCallee {
function filterCall(address, uint, uint, bytes calldata) external;
}
library Math {
function min(uint x, uint y) internal pure returns (uint z) {
z = x < y ? x : y;
}
function sqrt(uint y) internal pure returns (uint z) {
if (y > 3) {
z = y;
uint x = y / 2 + 1;
while (x < z) {
z = x;
x = (y / x + x) / 2;
}
}
else if (y != 0) z = 1;
}
}
library UQ112x112 {
uint224 constant Q112 = 2**112;
function encode(uint112 y) internal pure returns (uint224 z) {
z = uint224(y) * Q112;
}
function uqdiv(uint224 x, uint112 y) internal pure returns (uint224 z) {
z = x / uint224(y);
}
}
contract FilterERC20 {
string public constant name = "Filter LPs";
string public constant symbol = "Filter-LP";
uint8 public constant decimals = 18;
uint public totalSupply;
mapping(address => uint) public balanceOf;
mapping(address => mapping(address => uint)) public allowance;
IFilterManager ERC20FilterManager;
bytes32 public DOMAIN_SEPARATOR;
bytes32 public constant PERMIT_TYPEHASH = 0x5fae9ec55a1e547936e0e74d606b44cd5f912f9adcd0bba561fea62d570259e9;
mapping(address => uint) public nonces;
event Approval(address indexed, address indexed, uint);
event Transfer(address indexed, address indexed, uint);
constructor() {
DOMAIN_SEPARATOR = keccak256(
abi.encode(
keccak256("EIP712Domain(string name,string version,uint chainId,address verifyingContract)"),
keccak256(bytes(name)),
keccak256(bytes("1")),
97,
address(this)
)
);
}
function _mint(address to, uint value) internal {
totalSupply += value;
balanceOf[to] += value;
emit Transfer(address(0), to, value);
}
function _burn(address from, uint value) internal {
balanceOf[from] -= value;
totalSupply -= value;
emit Transfer(from, address(0), value);
}
function _approve(address owner, address spender, uint value) private {
allowance[owner][spender] = value;
emit Approval(owner, spender, value);
}
function _transfer(address from, address to, uint value) private {
balanceOf[from] -= value;
balanceOf[to] += value;
emit Transfer(from, to, value);
}
function approve(address spender, uint value) external returns (bool) {
_approve(msg.sender, spender, value);
return true;
}
function transfer(address to, uint value) external returns (bool) {
require(!ERC20FilterManager.isLiquidityLocked(msg.sender, address(this)), "FilterPair: LP_TRANSFER_LOCKED");
_transfer(msg.sender, to, value);
return true;
}
function transferFrom(address from, address to, uint value) external returns (bool) {
require(!ERC20FilterManager.isLiquidityLocked(from, address(this)), "FilterPair: LP_TRANSFER_LOCKED");
if (allowance[from][msg.sender] != type(uint).max) allowance[from][msg.sender] -= value;
_transfer(from, to, value);
return true;
}
function permit(address owner, address spender, uint value, uint deadline, uint8 v, bytes32 r, bytes32 s) external {
require(deadline >= block.timestamp, "FilterPair: EXPIRED");
bytes32 digest = keccak256(abi.encodePacked("\x19\x01", DOMAIN_SEPARATOR, keccak256(abi.encode(PERMIT_TYPEHASH, owner, spender, value, nonces[owner]++, deadline))));
address recoveredAddress = ecrecover(digest, v, r, s);
require(recoveredAddress != address(0) && recoveredAddress == owner, "FilterPair: INVALID_SIGNATURE");
_approve(owner, spender, value);
}
}
contract FilterPair is FilterERC20 {
using UQ112x112 for uint224;
uint public constant MINIMUM_LIQUIDITY = 10**3;
IFilterManager filterManager;
address public token0;
address public token1;
uint112 private reserve0;
uint112 private reserve1;
uint32 private blockTimestampLast;
uint public price0CumulativeLast;
uint public price1CumulativeLast;
uint public kLast;
bool private unlocked = true;
event Mint(address indexed, uint, uint);
event Burn(address indexed, uint, uint, address indexed);
event Swap(address indexed, uint, uint, uint, uint, address indexed);
event Sync(uint112, uint112);
modifier reentrancyLock() {
require(unlocked == true, "FilterPair: LOCKED");
unlocked = false;
_;
unlocked = true;
}
function getReserves() public view returns (uint112 _reserve0, uint112 _reserve1, uint32 _blockTimestampLast) {
_reserve0 = reserve0;
_reserve1 = reserve1;
_blockTimestampLast = blockTimestampLast;
}
function _safeTransfer(address token, address to, uint value) private {
require(IERC20(token).transfer(to, value), "FilterPair: TRANSFER_FAILED");
}
function initialize(address _token0, address _token1, address _managerAddress) external {
filterManager = IFilterManager(_managerAddress);
ERC20FilterManager = IFilterManager(_managerAddress);
require(msg.sender == filterManager.factoryAddress(), "FilterPair: FORBIDDEN");
token0 = _token0;
token1 = _token1;
}
function _update(uint balance0, uint balance1, uint112 _reserve0, uint112 _reserve1) private {
uint32 blockTimestamp = uint32(block.timestamp % 2**32);
uint32 timeElapsed = blockTimestamp - blockTimestampLast;
if (timeElapsed > 0 && _reserve0 != 0 && _reserve1 != 0) {
price0CumulativeLast += uint(UQ112x112.encode(_reserve1).uqdiv(_reserve0)) * timeElapsed;
price1CumulativeLast += uint(UQ112x112.encode(_reserve0).uqdiv(_reserve1)) * timeElapsed;
}
reserve0 = uint112(balance0);
reserve1 = uint112(balance1);
blockTimestampLast = blockTimestamp;
emit Sync(reserve0, reserve1);
}
function _mintFee(uint112 _reserve0, uint112 _reserve1) private returns (bool feeOn) {
feeOn = (filterManager.swapLiquidityProviderFee() + filterManager.swapTreasuryFee()) > 0;
if (feeOn) {
if (kLast != 0) {
uint rootK = Math.sqrt(uint(_reserve0) * uint(_reserve1));
uint rootKLast = Math.sqrt(kLast);
if (rootK > rootKLast) {
uint numerator = totalSupply * (rootK - rootKLast);
uint denominator = (rootK * filterManager.swapTreasuryFee()) + rootKLast;
uint liquidity = numerator / denominator;
if (liquidity > 0) _mint(filterManager.treasuryAddress(), liquidity);
}
}
}
else if (kLast != 0) kLast = 0;
}
function mint(address to) external reentrancyLock returns (uint liquidity) {
(uint112 _reserve0, uint112 _reserve1, ) = getReserves();
uint balance0 = IERC20(token0).balanceOf(address(this));
uint balance1 = IERC20(token1).balanceOf(address(this));
uint amount0 = balance0 - uint(_reserve0);
uint amount1 = balance1 - uint(_reserve1);
bool feeOn = _mintFee(_reserve0, _reserve1);
uint _totalSupply = totalSupply;
if (_totalSupply == 0) {
liquidity = Math.sqrt(amount0 * amount1) - MINIMUM_LIQUIDITY;
_mint(address(0), MINIMUM_LIQUIDITY);
}
else liquidity = Math.min((amount0 * _totalSupply) / uint(_reserve0), (amount1 * _totalSupply) / uint(_reserve1));
require(liquidity > 0, "FilterPair: INSUFFICIENT_LIQUIDITY_MINTED");
_mint(to, liquidity);
_update(balance0, balance1, _reserve0, _reserve1);
if (feeOn) kLast = uint(reserve0) * uint(reserve1);
emit Mint(msg.sender, amount0, amount1);
}
function burn(address to) external reentrancyLock returns (uint amount0, uint amount1) {
(uint112 _reserve0, uint112 _reserve1, ) = getReserves();
address _token0 = token0;
address _token1 = token1;
uint balance0 = IERC20(_token0).balanceOf(address(this));
uint balance1 = IERC20(_token1).balanceOf(address(this));
uint liquidity = balanceOf[address(this)];
bool feeOn = _mintFee(_reserve0, _reserve1);
uint _totalSupply = totalSupply;
amount0 = (liquidity * balance0) / _totalSupply;
amount1 = (liquidity * balance1) / _totalSupply;
require(amount0 > 0 && amount1 > 0, "FilterPair: INSUFFICIENT_LIQUIDITY_BURNED");
_burn(address(this), liquidity);
_safeTransfer(_token0, to, amount0);
_safeTransfer(_token1, to, amount1);
balance0 = IERC20(_token0).balanceOf(address(this));
balance1 = IERC20(_token1).balanceOf(address(this));
_update(balance0, balance1, _reserve0, _reserve1);
if (feeOn) kLast = uint(reserve0) * uint(reserve1);
emit Burn(msg.sender, amount0, amount1, to);
}
function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external reentrancyLock {
require(amount0Out > 0 || amount1Out > 0, "FilterPair: INSUFFICIENT_OUTPUT_AMOUNT");
(uint112 _reserve0, uint112 _reserve1, ) = getReserves();
require(amount0Out < uint(_reserve0) && amount1Out < uint(_reserve1), "FilterPair: INSUFFICIENT_LIQUIDITY");
uint balance0;
uint balance1;
{
address _token0 = token0;
address _token1 = token1;
require(to != _token0 && to != _token1, "FilterPair: INVALID_TO");
if (amount0Out > 0) _safeTransfer(_token0, to, amount0Out);
if (amount1Out > 0) _safeTransfer(_token1, to, amount1Out);
if (data.length > 0) IFilterCallee(to).filterCall(msg.sender, amount0Out, amount1Out, data);
balance0 = IERC20(_token0).balanceOf(address(this));
balance1 = IERC20(_token1).balanceOf(address(this));
}
uint amount0In = balance0 > uint(_reserve0) - amount0Out ? balance0 - (uint(_reserve0) - amount0Out) : 0;
uint amount1In = balance1 > uint(_reserve1) - amount1Out ? balance1 - (uint(_reserve1) - amount1Out) : 0;
require(amount0In > 0 || amount1In > 0, "FilterPair: INSUFFICIENT_INPUT_AMOUNT");
{
uint balance0Adjusted = ((balance0 * 10000) - (amount0In * (filterManager.swapLiquidityProviderFee() + filterManager.swapTreasuryFee()))) / 10;
uint balance1Adjusted = ((balance1 * 10000) - (amount1In * (filterManager.swapLiquidityProviderFee() + filterManager.swapTreasuryFee()))) / 10;
require((balance0Adjusted * balance1Adjusted) >= (uint(_reserve0) * uint(_reserve1)) * (1000**2), "FilterPair: K");
}
_update(balance0, balance1, _reserve0, _reserve1);
emit Swap(msg.sender, amount0In, amount1In, amount0Out, amount1Out, to);
}
function skim(address to) external reentrancyLock {
address _token0 = token0;
address _token1 = token1;
_safeTransfer(_token0, to, IERC20(_token0).balanceOf(address(this)) - reserve0);
_safeTransfer(_token1, to, IERC20(_token1).balanceOf(address(this)) - reserve1);
}
function sync() external reentrancyLock {
_update(IERC20(token0).balanceOf(address(this)), IERC20(token1).balanceOf(address(this)), reserve0, reserve1);
}
}
contract FilterFactory is IFilterFactory {
address public managerAddress;
IFilterManager filterManager;
bytes32 public constant INIT_CODE_PAIR_HASH = keccak256(abi.encodePacked(type(FilterPair).creationCode));
mapping(address => mapping(address => address)) public getPair;
address[] public allPairs;
constructor(address _managerAddress) {
managerAddress = _managerAddress;
filterManager = IFilterManager(managerAddress);
}
function allPairsLength() external view returns (uint) {
return allPairs.length;
}
function createPair(address tokenA, address tokenB) external returns (address pair) {
require(filterManager.isTokenVerified(tokenA) && filterManager.isTokenVerified(tokenB), "FilterPair: UNVERIFIED_TOKEN_PAIR");
require(tokenA != tokenB, "FilterPair: IDENTICAL_ADDRESSES");
(address token0, address token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
require(token0 != address(0), "FilterPair: ZERO_ADDRESS");
require(token1 != address(0), "FilterPair: ZERO_ADDRESS");
require(getPair[token0][token1] == address(0), "FilterPair: PAIR_EXISTS");
bytes memory bytecode = type(FilterPair).creationCode;
bytes32 salt = keccak256(abi.encodePacked(token0, token1));
assembly {
pair := create2(0, add(bytecode, 32), mload(bytecode), salt)
}
IFilterPair(pair).initialize(token0, token1, managerAddress);
getPair[token0][token1] = pair;
getPair[token1][token0] = pair;
allPairs.push(pair);
emit PairCreated(token0, token1, pair, allPairs.length);
}
}