anonymity-mining/contracts/TornadoProxy.sol

145 lines
4.4 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";
2021-02-19 20:17:18 +03:00
contract TornadoProxy {
2020-12-15 18:08:37 +03:00
using SafeERC20 for IERC20;
event EncryptedNote(address indexed sender, bytes encryptedNote);
2021-03-21 01:01:44 +03:00
event InstanceStateUpdated(ITornadoInstance indexed instance, InstanceState state);
2021-03-21 01:01:35 +03:00
event TornadoTreesUpdated(ITornadoTrees addr);
2021-03-06 15:44:17 +03:00
2021-03-22 20:30:01 +03:00
enum InstanceState { DISABLED, ENABLED, MINEABLE }
2021-03-11 22:11:33 +03:00
2021-02-10 23:32:30 +03:00
struct Instance {
2021-03-11 20:12:11 +03:00
bool isERC20;
2021-03-11 22:11:33 +03:00
IERC20 token;
InstanceState state;
}
struct Tornado {
ITornadoInstance addr;
Instance instance;
2021-02-10 23:32:30 +03:00
}
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-03-11 22:11:33 +03:00
mapping(ITornadoInstance => Instance) 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,
2021-03-11 22:11:33 +03:00
Tornado[] 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-03-11 20:12:11 +03:00
_updateInstance(_instances[i]);
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-03-11 22:11:33 +03:00
Instance memory instance = instances[_tornado];
2021-03-22 20:30:01 +03:00
require(instance.state != InstanceState.DISABLED, "The instance is not supported");
2020-12-15 18:08:37 +03:00
2021-03-11 22:11:33 +03:00
if (instance.isERC20) {
instance.token.safeTransferFrom(msg.sender, address(this), _tornado.denomination());
}
2020-12-15 18:08:37 +03:00
_tornado.deposit{ value: msg.value }(_commitment);
2021-03-11 20:12:11 +03:00
2021-03-22 20:30:01 +03:00
if (instance.state == InstanceState.MINEABLE) {
2021-02-10 23:32:30 +03:00
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-03-11 22:11:33 +03:00
Instance memory instance = instances[_tornado];
2021-03-22 20:30:01 +03:00
require(instance.state != 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-03-22 20:30:01 +03:00
if (instance.state == InstanceState.MINEABLE) {
2021-02-10 23:32:30 +03:00
tornadoTrees.registerWithdrawal(address(_tornado), _nullifierHash);
}
}
2021-03-16 00:05:33 +03:00
function backupNotes(bytes[] calldata _encryptedNotes) external {
for (uint256 i = 0; i < _encryptedNotes.length; i++) {
emit EncryptedNote(msg.sender, _encryptedNotes[i]);
}
}
2021-03-11 22:11:33 +03:00
function updateInstance(Tornado calldata _tornado) external onlyGovernance {
_updateInstance(_tornado);
2021-02-10 23:32:30 +03:00
}
2021-03-21 01:00:59 +03:00
function setTornadoTreesContract(ITornadoTrees _tornadoTrees) external onlyGovernance {
tornadoTrees = _tornadoTrees;
2021-03-21 01:01:35 +03:00
emit TornadoTreesUpdated(_tornadoTrees);
2020-12-15 18:08:37 +03:00
}
/// @dev Method to claim junk and accidentally sent tokens
function rescueTokens(
IERC20 _token,
address payable _to,
2021-03-21 00:56:39 +03:00
uint256 _amount
2020-12-15 18:08:37 +03:00
) external onlyGovernance {
require(_to != address(0), "TORN: can not send to zero address");
if (_token == IERC20(0)) {
// for Ether
uint256 totalBalance = address(this).balance;
2021-03-21 00:56:39 +03:00
uint256 balance = Math.min(totalBalance, _amount);
2020-12-15 18:08:37 +03:00
_to.transfer(balance);
} else {
// any other erc20
uint256 totalBalance = _token.balanceOf(address(this));
2021-03-21 00:56:39 +03:00
uint256 balance = Math.min(totalBalance, _amount);
2020-12-15 18:08:37 +03:00
require(balance > 0, "TORN: trying to send 0 balance");
_token.safeTransfer(_to, balance);
}
}
2021-03-11 20:12:11 +03:00
2021-03-11 22:11:33 +03:00
function _updateInstance(Tornado memory _tornado) internal {
instances[_tornado.addr] = _tornado.instance;
if (_tornado.instance.isERC20) {
IERC20 token = IERC20(_tornado.addr.token());
require(token == _tornado.instance.token, "Incorrect token");
2021-03-12 15:09:05 +03:00
uint256 allowance = token.allowance(address(this), address(_tornado.addr));
2021-03-22 20:30:01 +03:00
if (_tornado.instance.state != InstanceState.DISABLED && allowance == 0) {
2021-03-12 15:09:05 +03:00
token.safeApprove(address(_tornado.addr), uint256(-1));
2021-03-22 20:30:01 +03:00
} else if (_tornado.instance.state == InstanceState.DISABLED && allowance != 0) {
2021-03-12 15:09:05 +03:00
token.safeApprove(address(_tornado.addr), 0);
}
2021-03-11 20:12:11 +03:00
}
2021-03-21 01:01:44 +03:00
emit InstanceStateUpdated(_tornado.addr, _tornado.instance.state);
2021-03-11 20:12:11 +03:00
}
2020-12-15 18:08:37 +03:00
}