tornado-pool-factory/contracts/InstanceFactory.sol

106 lines
3.7 KiB
Solidity
Raw Normal View History

2021-10-24 21:54:07 +02:00
// SPDX-License-Identifier: MIT
2022-02-16 14:50:46 +01:00
pragma solidity 0.7.6;
2021-10-24 21:54:07 +02:00
pragma abicoder v2;
import { Address } from "@openzeppelin/contracts/utils/Address.sol";
2022-07-26 19:07:04 +02:00
import { Initializable } from "@openzeppelin/contracts/proxy/Initializable.sol";
import "@openzeppelin/contracts/proxy/Clones.sol";
2021-10-24 21:54:07 +02:00
import "./ERC20TornadoCloneable.sol";
2022-07-28 12:26:39 +02:00
import "./ETHTornadoCloneable.sol";
2022-02-17 21:15:19 +01:00
2022-07-26 19:07:04 +02:00
contract InstanceFactory is Initializable {
2021-10-24 21:54:07 +02:00
using Clones for address;
using Address for address;
2021-10-24 21:54:07 +02:00
2022-07-26 19:07:04 +02:00
address public admin;
2022-07-28 12:26:39 +02:00
address public ERC20Impl;
address public nativeCurImpl;
2021-10-24 21:54:07 +02:00
address public verifier;
address public hasher;
uint32 public merkleTreeHeight;
event NewTreeHeightSet(uint32 indexed newTreeHeight);
2022-07-28 12:26:39 +02:00
event NewImplementationSet(address indexed ERC20Impl, address indexed nativeCurImpl, address verifier, address hasher);
event NewInstanceCloneCreated(address indexed clone);
2022-07-26 19:07:04 +02:00
modifier onlyAdmin() {
require(admin == msg.sender, "IF: caller is not the admin");
_;
}
/**
* @notice initialize function for upgradeability
* @dev this contract will be deployed behind a proxy and should not assign values at logic address,
* params left out because self explainable
* */
function initialize(
2021-10-24 21:54:07 +02:00
address _verifier,
address _hasher,
2022-02-16 14:50:46 +01:00
uint32 _merkleTreeHeight,
2022-07-26 19:07:04 +02:00
address _admin
) public initializer {
2021-10-24 21:54:07 +02:00
verifier = _verifier;
hasher = _hasher;
merkleTreeHeight = _merkleTreeHeight;
2022-07-26 19:07:04 +02:00
admin = _admin;
2022-02-17 21:15:19 +01:00
2022-07-28 12:26:39 +02:00
ERC20TornadoCloneable ERC20ImplContract = new ERC20TornadoCloneable(_verifier, _hasher);
ERC20Impl = address(ERC20ImplContract);
ETHTornadoCloneable nativeCurImplContract = new ETHTornadoCloneable(_verifier, _hasher);
nativeCurImpl = address(nativeCurImplContract);
2022-02-16 14:50:46 +01:00
}
/**
* @dev Creates new Tornado instance.
* @param _denomination denomination of new Tornado instance
2022-07-28 12:26:39 +02:00
* @param _token address of ERC20 token for a new instance, if zero address, then it will be ETH
*/
2022-07-28 12:26:39 +02:00
function createInstanceClone(uint256 _denomination, address _token) public virtual onlyAdmin returns (address clone) {
return _createInstanceClone(_denomination, _token);
}
function _createInstanceClone(uint256 _denomination, address _token) internal returns (address clone) {
2022-02-16 14:50:46 +01:00
bytes32 salt = keccak256(abi.encodePacked(_denomination, _token));
2022-07-28 12:26:39 +02:00
if (_token == address(0)) {
clone = nativeCurImpl.predictDeterministicAddress(salt);
if (!clone.isContract()) {
nativeCurImpl.cloneDeterministic(salt);
emit NewInstanceCloneCreated(clone);
ETHTornadoCloneable(clone).init(_denomination, merkleTreeHeight);
}
} else {
clone = ERC20Impl.predictDeterministicAddress(salt);
if (!clone.isContract()) {
ERC20Impl.cloneDeterministic(salt);
emit NewInstanceCloneCreated(clone);
ERC20TornadoCloneable(clone).init(_denomination, merkleTreeHeight, _token);
}
}
2022-07-28 12:26:39 +02:00
return clone;
2022-02-16 14:50:46 +01:00
}
function getInstanceAddress(uint256 _denomination, address _token) public view returns (address) {
bytes32 salt = keccak256(abi.encodePacked(_denomination, _token));
2022-07-28 12:26:39 +02:00
if (_token == address(0)) {
return nativeCurImpl.predictDeterministicAddress(salt);
} else {
return ERC20Impl.predictDeterministicAddress(salt);
}
2022-02-16 14:50:46 +01:00
}
2022-07-26 19:07:04 +02:00
function setMerkleTreeHeight(uint32 _merkleTreeHeight) external onlyAdmin {
2021-10-24 21:54:07 +02:00
merkleTreeHeight = _merkleTreeHeight;
emit NewTreeHeightSet(merkleTreeHeight);
2021-10-24 21:54:07 +02:00
}
2022-07-26 19:07:04 +02:00
function generateNewImplementation(address _verifier, address _hasher) external onlyAdmin {
2022-03-15 16:20:36 +01:00
verifier = _verifier;
hasher = _hasher;
2022-07-28 12:26:39 +02:00
ERC20Impl = address(new ERC20TornadoCloneable(_verifier, _hasher));
nativeCurImpl = address(new ETHTornadoCloneable(_verifier, _hasher));
emit NewImplementationSet(ERC20Impl, nativeCurImpl, _verifier, _hasher);
2021-10-24 21:54:07 +02:00
}
}