2019-08-20 16:39:21 -04:00
|
|
|
// https://tornado.cash
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
pragma solidity ^0.5.8;
|
|
|
|
|
|
|
|
import "./Mixer.sol";
|
|
|
|
|
|
|
|
contract ERC20Mixer is Mixer {
|
2019-08-30 06:06:17 -04:00
|
|
|
address public token;
|
2019-08-20 16:39:21 -04:00
|
|
|
|
|
|
|
constructor(
|
2019-10-31 21:14:01 -04:00
|
|
|
IVerifier _verifier,
|
2019-10-06 01:36:01 -04:00
|
|
|
uint256 _denomination,
|
2019-11-03 05:11:22 -05:00
|
|
|
uint32 _merkleTreeHeight,
|
2019-10-31 20:56:24 -04:00
|
|
|
address _operator,
|
2019-10-06 01:36:01 -04:00
|
|
|
address _token
|
2019-11-02 08:35:22 -04:00
|
|
|
) Mixer(_verifier, _denomination, _merkleTreeHeight, _operator) public {
|
2019-08-20 16:39:21 -04:00
|
|
|
token = _token;
|
|
|
|
}
|
|
|
|
|
2019-11-14 12:49:34 -05:00
|
|
|
function _processDeposit() internal {
|
2019-11-14 06:01:11 -05:00
|
|
|
require(msg.value == 0, "ETH value is supposed to be 0 for ERC20 mixer");
|
2019-11-02 08:48:22 -04:00
|
|
|
_safeErc20TransferFrom(msg.sender, address(this), denomination);
|
2019-08-20 16:39:21 -04:00
|
|
|
}
|
|
|
|
|
2019-11-14 12:49:34 -05:00
|
|
|
function _processWithdraw(address payable _recipient, address payable _relayer, uint256 _fee, uint256 _refund) internal {
|
2019-10-06 01:09:26 -04:00
|
|
|
require(msg.value == _refund, "Incorrect refund amount received by the contract");
|
2019-08-20 16:39:21 -04:00
|
|
|
|
2019-11-07 02:04:29 -05:00
|
|
|
_safeErc20Transfer(_recipient, denomination - _fee);
|
2019-09-06 17:22:30 -04:00
|
|
|
if (_fee > 0) {
|
2019-11-02 08:48:22 -04:00
|
|
|
_safeErc20Transfer(_relayer, _fee);
|
2019-08-20 16:39:21 -04:00
|
|
|
}
|
2019-11-15 02:59:06 -05:00
|
|
|
|
2019-10-06 01:09:26 -04:00
|
|
|
if (_refund > 0) {
|
2019-11-15 02:59:06 -05:00
|
|
|
(bool success, ) = _recipient.call.value(_refund)("");
|
|
|
|
if (!success) {
|
|
|
|
// let's return _refund back to the relayer
|
|
|
|
_relayer.transfer(_refund);
|
|
|
|
}
|
2019-10-06 01:09:26 -04:00
|
|
|
}
|
2019-08-20 16:39:21 -04:00
|
|
|
}
|
2019-08-30 06:06:17 -04:00
|
|
|
|
2019-11-02 08:48:22 -04:00
|
|
|
function _safeErc20TransferFrom(address _from, address _to, uint256 _amount) internal {
|
2019-11-04 14:24:16 -05:00
|
|
|
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x23b872dd /* transferFrom */, _from, _to, _amount));
|
2019-08-30 06:06:17 -04:00
|
|
|
require(success, "not enough allowed tokens");
|
2019-09-10 09:31:34 -04:00
|
|
|
|
2019-11-04 14:45:56 -05:00
|
|
|
// if contract returns some data lets make sure that is `true` according to standard
|
2019-08-30 06:06:17 -04:00
|
|
|
if (data.length > 0) {
|
2019-11-03 03:25:58 -05:00
|
|
|
require(data.length == 32, "data length should be either 0 or 32 bytes");
|
2019-11-04 14:24:16 -05:00
|
|
|
success = abi.decode(data, (bool));
|
2019-10-25 07:13:43 -04:00
|
|
|
require(success, "not enough allowed tokens. Token returns false.");
|
2019-08-30 06:06:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-02 08:48:22 -04:00
|
|
|
function _safeErc20Transfer(address _to, uint256 _amount) internal {
|
2019-11-04 14:24:16 -05:00
|
|
|
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0xa9059cbb /* transfer */, _to, _amount));
|
2019-08-30 06:06:17 -04:00
|
|
|
require(success, "not enough tokens");
|
2019-09-10 09:31:34 -04:00
|
|
|
|
2019-11-04 14:45:56 -05:00
|
|
|
// if contract returns some data lets make sure that is `true` according to standard
|
2019-08-30 06:06:17 -04:00
|
|
|
if (data.length > 0) {
|
2019-11-03 03:25:58 -05:00
|
|
|
require(data.length == 32, "data length should be either 0 or 32 bytes");
|
2019-11-04 14:24:16 -05:00
|
|
|
success = abi.decode(data, (bool));
|
2019-10-25 07:13:43 -04:00
|
|
|
require(success, "not enough tokens. Token returns false.");
|
2019-08-30 06:06:17 -04:00
|
|
|
}
|
|
|
|
}
|
2019-08-20 16:39:21 -04:00
|
|
|
}
|