tornado-core/contracts/ERC20Tornado.sol

60 lines
2.0 KiB
Solidity
Raw Normal View History

2019-08-20 20:39:21 +00:00
// https://tornado.cash
/*
2021-02-11 06:23:18 +00:00
* d888888P dP a88888b. dP
* 88 88 d8' `88 88
* 88 .d8888b. 88d888b. 88d888b. .d8888b. .d888b88 .d8888b. 88 .d8888b. .d8888b. 88d888b.
* 88 88' `88 88' `88 88' `88 88' `88 88' `88 88' `88 88 88' `88 Y8ooooo. 88' `88
* 88 88. .88 88 88 88 88. .88 88. .88 88. .88 dP Y8. .88 88. .88 88 88 88
* dP `88888P' dP dP dP `88888P8 `88888P8 `88888P' 88 Y88888P' `88888P8 `88888P' dP dP
* ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
*/
2019-08-20 20:39:21 +00:00
2021-02-11 06:03:43 +00:00
// SPDX-License-Identifier: MIT
2021-03-11 20:05:59 +00:00
pragma solidity ^0.7.0;
2019-08-20 20:39:21 +00:00
2019-12-13 13:49:19 +00:00
import "./Tornado.sol";
2021-03-10 16:28:44 +00:00
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
2019-08-20 20:39:21 +00:00
2019-12-13 13:49:19 +00:00
contract ERC20Tornado is Tornado {
2021-03-10 16:28:44 +00:00
using SafeERC20 for IERC20;
2021-10-28 11:45:16 +00:00
IERC20 public token;
2019-08-20 20:39:21 +00:00
constructor(
2019-11-01 01:14:01 +00:00
IVerifier _verifier,
2021-02-11 07:00:53 +00:00
IHasher _hasher,
uint256 _denomination,
uint32 _merkleTreeHeight,
2021-03-10 16:28:44 +00:00
IERC20 _token
2021-03-11 20:05:59 +00:00
) Tornado(_verifier, _hasher, _denomination, _merkleTreeHeight) {
2019-08-20 20:39:21 +00:00
token = _token;
}
2021-02-11 06:03:43 +00:00
function _processDeposit() internal override {
2019-12-13 13:49:19 +00:00
require(msg.value == 0, "ETH value is supposed to be 0 for ERC20 instance");
2021-03-10 16:28:44 +00:00
token.safeTransferFrom(msg.sender, address(this), denomination);
2019-08-20 20:39:21 +00:00
}
2021-02-11 06:23:18 +00:00
function _processWithdraw(
address payable _recipient,
address payable _relayer,
uint256 _fee,
uint256 _refund
) internal override {
require(msg.value == _refund, "Incorrect refund amount received by the contract");
2019-08-20 20:39:21 +00:00
2021-03-10 16:28:44 +00:00
token.safeTransfer(_recipient, denomination - _fee);
2019-09-06 21:22:30 +00:00
if (_fee > 0) {
2021-03-10 16:28:44 +00:00
token.safeTransfer(_relayer, _fee);
2019-08-20 20:39:21 +00:00
}
if (_refund > 0) {
2021-02-11 06:03:43 +00:00
(bool success, ) = _recipient.call{ value: _refund }("");
if (!success) {
// let's return _refund back to the relayer
_relayer.transfer(_refund);
}
}
2019-08-20 20:39:21 +00:00
}
}