-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTokenSwapCloneFactory.sol
More file actions
66 lines (60 loc) · 2.75 KB
/
TokenSwapCloneFactory.sol
File metadata and controls
66 lines (60 loc) · 2.75 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
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.23;
import "../TokenSwap.sol";
import "./CloneFactory.sol";
import "@openzeppelin/contracts/proxy/Clones.sol";
/**
* @title TokenSwapCloneFactory
* @author malteish
* @notice Use this contract to create deterministic clones of TokenSwap contracts
*/
contract TokenSwapCloneFactory is CloneFactory {
constructor(address _implementation) CloneFactory(_implementation) {}
/**
* Create a new clone and return its address. All parameters change the address of the clone.
* @param _rawSalt influences the address of the clone, but not the initialization
* @param _trustedForwarder can not be changed, but is checked for security
* @param _arguments struct with all the initialization parameters
*/
function createTokenSwapClone(
bytes32 _rawSalt,
address _trustedForwarder,
TokenSwapInitializerArguments memory _arguments
) external returns (address) {
bytes32 salt = _getSalt(_rawSalt, _trustedForwarder, _arguments);
TokenSwap tokenSwap = TokenSwap(Clones.cloneDeterministic(implementation, salt));
require(tokenSwap.isTrustedForwarder(_trustedForwarder), "TokenSwapCloneFactory: Unexpected trustedForwarder");
tokenSwap.initialize(_arguments);
emit NewClone(address(tokenSwap));
return address(tokenSwap);
}
/**
* Return the address a clone would have if it was created with these parameters.
* @param _rawSalt influences the address of the clone, but not the initialization
* @param _trustedForwarder can not be changed, but is checked for security
* @param _arguments struct with all the initialization parameters
*/
function predictCloneAddress(
bytes32 _rawSalt,
address _trustedForwarder,
TokenSwapInitializerArguments memory _arguments
) external view returns (address) {
bytes32 salt = _getSalt(_rawSalt, _trustedForwarder, _arguments);
return Clones.predictDeterministicAddress(implementation, salt);
}
/**
* @notice generates a salt from all input parameters
* @param _rawSalt The salt used to deterministically generate the clone address
* @param _trustedForwarder The trustedForwarder that will be used to initialize the clone
* @param _arguments The arguments that will be used to initialize the clone
* @return salt to be used for clone generation
* @dev This function does not check if the clone has already been created
*/
function _getSalt(
bytes32 _rawSalt,
address _trustedForwarder,
TokenSwapInitializerArguments memory _arguments
) internal pure returns (bytes32) {
return keccak256(abi.encode(_rawSalt, _trustedForwarder, _arguments));
}
}