tornado-anonymity-mining/contracts/TornadoProxy.sol

145 lines
4.5 KiB
Solidity
Raw Normal View History

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