-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSfpyFactory.sol
More file actions
60 lines (51 loc) · 2.04 KB
/
SfpyFactory.sol
File metadata and controls
60 lines (51 loc) · 2.04 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import './interfaces/ISfpyFactory.sol';
import './SfpyPool.sol';
contract SfpyFactory is ISfpyFactory {
address private _owner;
mapping(address => address) private _pools;
address[] private _allPools;
/// @dev sets the owner of the factory
/// @dev responsible for creating pools
constructor(address o) {
_owner = o;
}
/// @dev given an address of an ERC-20 token, creates a pool
/// @dev and initializes it. This is gas optimized to use the
/// @dev CREATE2 op code when creating a pool.
/// @param token the ERC-20 token to create the pool for
function createPool(address token) external override returns (address created) {
require(msg.sender == _owner, 'SFPY: FORBIDDEN');
require(token != address(0), 'SFPY: ZERO_ADDRESS');
require(_pools[token] == address(0), 'SFPY: POOL_EXISTS');
bytes memory bytecode = type(SfpyPool).creationCode;
bytes32 salt = keccak256(abi.encodePacked(token));
assembly {
created := create2(0, add(bytecode, 32), mload(bytecode), salt)
}
ISfpyPool(created).initialize(token);
_pools[token] = created;
_allPools.push(created);
emit PoolCreated(token, created);
}
/// @dev returns the total number of pools created
function pools() external view override returns (uint256) {
return _allPools.length;
}
/// @dev given an address, returns the address of the underlying pool
/// @param token the address of the underlying ERC-20 token
function pool(address token) external view override returns (address) {
return _pools[token];
}
/// @dev sets the owner of the factory
/// @param o the new owner
function setOwner(address o) external override {
require(msg.sender == _owner, 'SFPY: FORBIDDEN');
_owner = o;
}
/// @dev returns the current owner of the factory
function owner() external view override returns (address) {
return _owner;
}
}