tornado-core/contracts/ERC20Mixer.sol

86 lines
2.7 KiB
Solidity
Raw Normal View History

2019-08-20 22:39:21 +02: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 12:06:17 +02:00
address public token;
2019-08-27 22:42:24 +02:00
// ether value to cover network fee (for relayer) and to have some ETH on a brand new address
2019-09-06 23:22:30 +02:00
uint256 public userEther;
2019-08-20 22:39:21 +02:00
constructor(
address _verifier,
2019-09-06 23:22:30 +02:00
uint256 _userEther,
2019-08-20 22:39:21 +02:00
uint8 _merkleTreeHeight,
uint256 _emptyElement,
address payable _operator,
2019-08-30 12:06:17 +02:00
address _token,
2019-09-06 23:22:30 +02:00
uint256 _mixDenomination
) Mixer(_verifier, _mixDenomination, _merkleTreeHeight, _emptyElement, _operator) public {
2019-08-20 22:39:21 +02:00
token = _token;
2019-09-06 23:22:30 +02:00
userEther = _userEther;
2019-08-20 22:39:21 +02:00
}
2019-09-06 23:22:30 +02:00
function _processDeposit() internal {
require(msg.value == userEther, "Please send `userEther` ETH along with transaction");
safeErc20TransferFrom(msg.sender, address(this), mixDenomination);
2019-08-20 22:39:21 +02:00
}
2019-09-06 23:22:30 +02:00
function _processWithdraw(address payable _receiver, uint256 _fee) internal {
_receiver.transfer(userEther);
2019-08-20 22:39:21 +02:00
2019-09-06 23:22:30 +02:00
safeErc20Transfer(_receiver, mixDenomination - _fee);
if (_fee > 0) {
safeErc20Transfer(operator, _fee);
2019-08-20 22:39:21 +02:00
}
}
2019-08-30 12:06:17 +02:00
2019-09-06 23:22:30 +02:00
function safeErc20TransferFrom(address from, address to, uint256 amount) internal {
2019-08-30 12:06:17 +02:00
bool success;
bytes memory data;
bytes4 transferFromSelector = 0x23b872dd;
(success, data) = token.call(
abi.encodeWithSelector(
transferFromSelector,
from, to, amount
)
);
require(success, "not enough allowed tokens");
if (data.length > 0) {
assembly {
success := mload(add(data, 0x20))
}
require(success, "not enough allowed tokens");
}
}
2019-09-06 23:22:30 +02:00
function safeErc20Transfer(address to, uint256 amount) internal {
2019-08-30 12:06:17 +02:00
bool success;
bytes memory data;
bytes4 transferSelector = 0xa9059cbb;
(success, data) = token.call(
abi.encodeWithSelector(
transferSelector,
to, amount
)
);
require(success, "not enough tokens");
if (data.length > 0) {
assembly {
success := mload(add(data, 0x20))
}
require(success, "not enough tokens");
}
}
2019-08-20 22:39:21 +02:00
}