2020-12-15 16:08:37 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
pragma solidity ^0.6.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-11 20:11:33 +01:00
|
|
|
event InstanceStateUpdate(ITornadoInstance indexed instance, InstanceState state);
|
2021-03-06 13:44:17 +01:00
|
|
|
|
2021-02-10 21:32:30 +01: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
|
2020-12-15 16:08:37 +01: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
|
|
|
|
) external payable {
|
2021-03-11 20:11:33 +01:00
|
|
|
Instance memory instance = instances[_tornado];
|
|
|
|
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-11 20:11:33 +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
|
|
|
|
) external payable {
|
2021-03-11 20:11:33 +01:00
|
|
|
Instance memory instance = instances[_tornado];
|
|
|
|
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-11 20:11:33 +01:00
|
|
|
if (instance.state == InstanceState.Mineable) {
|
2021-02-10 21:32:30 +01:00
|
|
|
tornadoTrees.registerWithdrawal(address(_tornado), _nullifierHash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 22:05:33 +01:00
|
|
|
function backupNotes(bytes[] calldata _encryptedNotes) external {
|
|
|
|
for (uint256 i = 0; i < _encryptedNotes.length; i++) {
|
|
|
|
emit EncryptedNote(msg.sender, _encryptedNotes[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 20:11:33 +01:00
|
|
|
function updateInstance(Tornado calldata _tornado) external onlyGovernance {
|
|
|
|
_updateInstance(_tornado);
|
2021-02-10 21:32:30 +01:00
|
|
|
}
|
|
|
|
|
2021-03-11 18:12:11 +01:00
|
|
|
function setTornadoTreesContract(address _tornadoTrees) external onlyGovernance {
|
|
|
|
tornadoTrees = ITornadoTrees(_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,
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
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));
|
|
|
|
|
|
|
|
if (_tornado.instance.state != InstanceState.Disabled && allowance == 0) {
|
|
|
|
token.safeApprove(address(_tornado.addr), uint256(-1));
|
|
|
|
} else if (_tornado.instance.state == InstanceState.Disabled && allowance != 0) {
|
|
|
|
token.safeApprove(address(_tornado.addr), 0);
|
|
|
|
}
|
2021-03-11 18:12:11 +01:00
|
|
|
}
|
2021-03-12 13:09:05 +01:00
|
|
|
emit InstanceStateUpdate(_tornado.addr, _tornado.instance.state);
|
2021-03-11 18:12:11 +01:00
|
|
|
}
|
2020-12-15 16:08:37 +01:00
|
|
|
}
|