ERC20Mixer.sol 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // https://tornado.cash
  2. /*
  3. * d888888P dP a88888b. dP
  4. * 88 88 d8' `88 88
  5. * 88 .d8888b. 88d888b. 88d888b. .d8888b. .d888b88 .d8888b. 88 .d8888b. .d8888b. 88d888b.
  6. * 88 88' `88 88' `88 88' `88 88' `88 88' `88 88' `88 88 88' `88 Y8ooooo. 88' `88
  7. * 88 88. .88 88 88 88 88. .88 88. .88 88. .88 dP Y8. .88 88. .88 88 88 88
  8. * dP `88888P' dP dP dP `88888P8 `88888P8 `88888P' 88 Y88888P' `88888P8 `88888P' dP dP
  9. * ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
  10. */
  11. pragma solidity ^0.5.8;
  12. import "./Mixer.sol";
  13. contract ERC20Mixer is Mixer {
  14. address public token;
  15. // ether value to cover network fee (for relayer) and to have some ETH on a brand new address
  16. uint256 public userEther;
  17. constructor(
  18. address _verifier,
  19. uint256 _userEther,
  20. uint8 _merkleTreeHeight,
  21. uint256 _emptyElement,
  22. address payable _operator,
  23. address _token,
  24. uint256 _denomination
  25. ) Mixer(_verifier, _denomination, _merkleTreeHeight, _emptyElement, _operator) public {
  26. token = _token;
  27. userEther = _userEther;
  28. }
  29. function _processDeposit() internal {
  30. require(msg.value == userEther, "Please send `userEther` ETH along with transaction");
  31. safeErc20TransferFrom(msg.sender, address(this), denomination);
  32. }
  33. function _processWithdraw(address payable _receiver, address payable _relayer, uint256 _fee) internal {
  34. _receiver.transfer(userEther);
  35. safeErc20Transfer(_receiver, denomination - _fee);
  36. if (_fee > 0) {
  37. safeErc20Transfer(_relayer, _fee);
  38. }
  39. }
  40. function safeErc20TransferFrom(address from, address to, uint256 amount) internal {
  41. bool success;
  42. bytes memory data;
  43. bytes4 transferFromSelector = 0x23b872dd;
  44. (success, data) = token.call(
  45. abi.encodeWithSelector(
  46. transferFromSelector,
  47. from, to, amount
  48. )
  49. );
  50. require(success, "not enough allowed tokens");
  51. // if contract returns some data let's make sure that is `true` according to standard
  52. if (data.length > 0) {
  53. assembly {
  54. success := mload(add(data, 0x20))
  55. }
  56. require(success, "not enough allowed tokens");
  57. }
  58. }
  59. function safeErc20Transfer(address to, uint256 amount) internal {
  60. bool success;
  61. bytes memory data;
  62. bytes4 transferSelector = 0xa9059cbb;
  63. (success, data) = token.call(
  64. abi.encodeWithSelector(
  65. transferSelector,
  66. to, amount
  67. )
  68. );
  69. require(success, "not enough tokens");
  70. // if contract returns some data let's make sure that is `true` according to standard
  71. if (data.length > 0) {
  72. assembly {
  73. success := mload(add(data, 0x20))
  74. }
  75. require(success, "not enough tokens");
  76. }
  77. }
  78. }