additional eth for the recipient

This commit is contained in:
Alexey 2019-08-27 23:42:24 +03:00
parent 0f5a5df522
commit 9f33aadd9d
14 changed files with 113 additions and 83 deletions

View file

@ -16,38 +16,61 @@ import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract ERC20Mixer is Mixer {
IERC20 public token;
// mixed token amount
uint256 public tokenDenomination;
// ether value to cover network fee (for relayer) and to have some ETH on a brand new address
uint256 public etherFeeDenomination;
constructor(
address _verifier,
uint256 _transferValue,
uint256 _etherFeeDenomination,
uint8 _merkleTreeHeight,
uint256 _emptyElement,
address payable _operator,
IERC20 _token
) Mixer(_verifier, _transferValue, _merkleTreeHeight, _emptyElement, _operator) public {
IERC20 _token,
uint256 _tokenDenomination
) Mixer(_verifier, _merkleTreeHeight, _emptyElement, _operator) public {
token = _token;
tokenDenomination = _tokenDenomination;
etherFeeDenomination = _etherFeeDenomination;
}
function deposit(uint256 commitment) public {
require(token.transferFrom(msg.sender, address(this), transferValue), "Approve before using");
/**
@dev Deposit funds into the mixer. The caller must send ETH value equal to `etherFeeDenomination` of this mixer.
The caller also has to have at least `tokenDenomination` amount approved for the mixer.
@param commitment the note commitment, which is PedersenHash(nullifier + secret)
*/
function deposit(uint256 commitment) public payable {
require(msg.value == etherFeeDenomination, "Please send `etherFeeDenomination` ETH along with transaction");
require(token.transferFrom(msg.sender, address(this), tokenDenomination), "Approve before using");
_deposit(commitment);
emit Deposit(commitment, next_index - 1, block.timestamp);
}
/**
@dev Withdraw deposit from the mixer. `a`, `b`, and `c` are zkSNARK proof data, and input is an array of circuit public inputs
`input` array consists of:
- merkle root of all deposits in the mixer
- hash of unique deposit nullifier to prevent double spends
- the receiver of funds
- optional fee that goes to the transaction sender (usually a relay)
*/
function withdraw(uint256[2] memory a, uint256[2][2] memory b, uint256[2] memory c, uint256[4] memory input) public {
_withdraw(a, b, c, input);
address receiver = address(input[2]);
address payable receiver = address(input[2]);
uint256 fee = input[3];
uint256 nullifierHash = input[1];
require(fee < transferValue, "Fee exceeds transfer value");
token.transfer(receiver, transferValue - fee);
require(fee < etherFeeDenomination, "Fee exceeds transfer value");
receiver.transfer(etherFeeDenomination - fee);
if (fee > 0) {
token.transfer(operator, fee);
operator.transfer(fee);
}
token.transfer(receiver, tokenDenomination);
emit Withdraw(receiver, nullifierHash, fee);
}
}

View file

@ -14,30 +14,45 @@ pragma solidity ^0.5.8;
import "./Mixer.sol";
contract ETHMixer is Mixer {
uint256 public etherDenomination;
constructor(
address _verifier,
uint256 _transferValue,
uint256 _etherDenomination,
uint8 _merkleTreeHeight,
uint256 _emptyElement,
address payable _operator
) Mixer(_verifier, _transferValue, _merkleTreeHeight, _emptyElement, _operator) public {}
) Mixer(_verifier, _merkleTreeHeight, _emptyElement, _operator) public {
etherDenomination = _etherDenomination;
}
/**
@dev Deposit funds into mixer. The caller must send value equal to `etherDenomination` of this mixer.
@param commitment the note commitment, which is PedersenHash(nullifier + secret)
*/
function deposit(uint256 commitment) public payable {
require(msg.value == transferValue, "Please send `transferValue` ETH along with transaction");
require(msg.value == etherDenomination, "Please send `etherDenomination` ETH along with transaction");
_deposit(commitment);
emit Deposit(commitment, next_index - 1, block.timestamp);
}
/**
@dev Withdraw deposit from the mixer. `a`, `b`, and `c` are zkSNARK proof data, and input is an array of circuit public inputs
`input` array consists of:
- merkle root of all deposits in the mixer
- hash of unique deposit nullifier to prevent double spends
- the receiver of funds
- optional fee that goes to the transaction sender (usually a relay)
*/
function withdraw(uint256[2] memory a, uint256[2][2] memory b, uint256[2] memory c, uint256[4] memory input) public {
_withdraw(a, b, c, input);
address payable receiver = address(input[2]);
uint256 fee = input[3];
uint256 nullifierHash = input[1];
require(fee < transferValue, "Fee exceeds transfer value");
receiver.transfer(transferValue - fee);
require(fee < etherDenomination, "Fee exceeds transfer value");
receiver.transfer(etherDenomination - fee);
if (fee > 0) {
operator.transfer(fee);
}

View file

@ -18,7 +18,6 @@ contract IVerifier {
}
contract Mixer is MerkleTreeWithHistory {
uint256 public transferValue;
bool public isDepositsEnabled = true;
// operator can disable new deposits in case of emergency
// it also receives a relayer fee
@ -34,24 +33,20 @@ contract Mixer is MerkleTreeWithHistory {
/**
@dev The constructor
@param _verifier the address of SNARK verifier for this contract
@param _transferValue the value for all deposits in this contract in wei
@param _merkleTreeHeight the height of deposits' Merkle Tree
@param _emptyElement default element of the deposits' Merkle Tree
@param _operator operator address (see operator above)
*/
constructor(
address _verifier,
uint256 _transferValue,
uint8 _merkleTreeHeight,
uint256 _emptyElement,
address payable _operator
) MerkleTreeWithHistory(_merkleTreeHeight, _emptyElement) public {
verifier = IVerifier(_verifier);
transferValue = _transferValue;
operator = _operator;
}
/**
@dev Deposit funds into mixer. The caller must send value equal to `transferValue` of this mixer.
@param commitment the note commitment, which is PedersenHash(nullifier + secret)
*/
function _deposit(uint256 commitment) internal {
require(isDepositsEnabled, "deposits disabled");
require(!commitments[commitment], "The commitment has been submitted");
@ -59,14 +54,6 @@ contract Mixer is MerkleTreeWithHistory {
commitments[commitment] = true;
}
/**
@dev Withdraw deposit from the mixer. `a`, `b`, and `c` are zkSNARK proof data, and input is an array of circuit public inputs
`input` array consists of:
- merkle root of all deposits in the mixer
- hash of unique deposit nullifier to prevent double spends
- the receiver of funds
- optional fee that goes to the transaction sender (usually a relay)
*/
function _withdraw(uint256[2] memory a, uint256[2][2] memory b, uint256[2] memory c, uint256[4] memory input) internal {
uint256 root = input[0];
uint256 nullifierHash = input[1];