anonymity-mining/contracts/TornadoProxy.sol

107 lines
3.3 KiB
Solidity
Raw Normal View History

2020-12-15 18:08:37 +03:00
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
2021-02-10 23:32:30 +03:00
pragma experimental ABIEncoderV2;
2020-12-15 18:08:37 +03:00
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import "@openzeppelin/contracts/math/Math.sol";
import "./interfaces/ITornadoInstance.sol";
import "./interfaces/ITornadoTrees.sol";
import "torn-token/contracts/ENS.sol";
contract TornadoProxy is EnsResolve {
using SafeERC20 for IERC20;
event EncryptedNote(address indexed sender, bytes encryptedNote);
2021-02-10 23:32:30 +03:00
enum InstanceState { Disabled, Enabled, Mineable }
struct Instance {
bytes32 instance;
InstanceState state;
}
2020-12-15 18:08:37 +03:00
2021-02-10 23:32:30 +03:00
ITornadoTrees public tornadoTrees;
2020-12-15 18:08:37 +03:00
address public immutable governance;
2021-02-10 23:32:30 +03:00
mapping(ITornadoInstance => InstanceState) public instances;
2020-12-15 18:08:37 +03:00
modifier onlyGovernance() {
require(msg.sender == governance, "Not authorized");
_;
}
constructor(
2021-02-10 23:32:30 +03:00
address _tornadoTrees,
address _governance,
Instance[] memory _instances
2020-12-15 18:08:37 +03:00
) public {
2021-02-10 23:32:30 +03:00
tornadoTrees = ITornadoTrees(_tornadoTrees);
governance = _governance;
2020-12-15 18:08:37 +03:00
for (uint256 i = 0; i < _instances.length; i++) {
2021-02-10 23:32:30 +03:00
instances[ITornadoInstance(resolve(_instances[i].instance))] = _instances[i].state;
2020-12-15 18:08:37 +03:00
}
}
2020-12-15 22:39:34 +03:00
function deposit(
ITornadoInstance _tornado,
bytes32 _commitment,
bytes calldata _encryptedNote
) external payable {
2021-02-10 23:32:30 +03:00
require(instances[_tornado] != InstanceState.Disabled, "The instance is not supported");
2020-12-15 18:08:37 +03:00
_tornado.deposit{ value: msg.value }(_commitment);
2021-02-10 23:32:30 +03:00
if (instances[_tornado] == InstanceState.Mineable) {
tornadoTrees.registerDeposit(address(_tornado), _commitment);
}
2020-12-15 18:08:37 +03:00
emit EncryptedNote(msg.sender, _encryptedNote);
}
function withdraw(
ITornadoInstance _tornado,
bytes calldata _proof,
bytes32 _root,
bytes32 _nullifierHash,
address payable _recipient,
address payable _relayer,
uint256 _fee,
uint256 _refund
) external payable {
2021-02-10 23:32:30 +03:00
require(instances[_tornado] != InstanceState.Disabled, "The instance is not supported");
2020-12-15 18:08:37 +03:00
_tornado.withdraw{ value: msg.value }(_proof, _root, _nullifierHash, _recipient, _relayer, _fee, _refund);
2021-02-10 23:32:30 +03:00
if (instances[_tornado] == InstanceState.Mineable) {
tornadoTrees.registerWithdrawal(address(_tornado), _nullifierHash);
}
}
function updateInstance(ITornadoInstance _instance, InstanceState _state) external onlyGovernance {
instances[_instance] = _state;
}
function setTornadoTreesContract(address _instance) external onlyGovernance {
tornadoTrees = ITornadoTrees(_instance);
2020-12-15 18:08:37 +03:00
}
/// @dev Method to claim junk and accidentally sent tokens
function rescueTokens(
IERC20 _token,
address payable _to,
uint256 _balance
) external onlyGovernance {
require(_to != address(0), "TORN: can not send to zero address");
if (_token == IERC20(0)) {
// for Ether
uint256 totalBalance = address(this).balance;
uint256 balance = _balance == 0 ? totalBalance : Math.min(totalBalance, _balance);
_to.transfer(balance);
} else {
// any other erc20
uint256 totalBalance = _token.balanceOf(address(this));
uint256 balance = _balance == 0 ? totalBalance : Math.min(totalBalance, _balance);
require(balance > 0, "TORN: trying to send 0 balance");
_token.safeTransfer(_to, balance);
}
}
}