change fee structure

This commit is contained in:
Roman Storm 2019-09-06 17:22:30 -04:00
parent 0e0c87d533
commit 50872ac342
5 changed files with 65 additions and 94 deletions

View File

@ -15,65 +15,37 @@ import "./Mixer.sol";
contract ERC20Mixer is Mixer { contract ERC20Mixer is Mixer {
address public token; address 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 // ether value to cover network fee (for relayer) and to have some ETH on a brand new address
uint256 public etherFeeDenomination; uint256 public userEther;
constructor( constructor(
address _verifier, address _verifier,
uint256 _etherFeeDenomination, uint256 _userEther,
uint8 _merkleTreeHeight, uint8 _merkleTreeHeight,
uint256 _emptyElement, uint256 _emptyElement,
address payable _operator, address payable _operator,
address _token, address _token,
uint256 _tokenDenomination uint256 _mixDenomination
) Mixer(_verifier, _merkleTreeHeight, _emptyElement, _operator) public { ) Mixer(_verifier, _mixDenomination, _merkleTreeHeight, _emptyElement, _operator) public {
token = _token; token = _token;
tokenDenomination = _tokenDenomination; userEther = _userEther;
etherFeeDenomination = _etherFeeDenomination;
} }
/** function _processDeposit() internal {
@dev Deposit funds into the mixer. The caller must send ETH value equal to `etherFeeDenomination` of this mixer. require(msg.value == userEther, "Please send `userEther` ETH along with transaction");
The caller also has to have at least `tokenDenomination` amount approved for the mixer. safeErc20TransferFrom(msg.sender, address(this), mixDenomination);
@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");
transferFrom(msg.sender, address(this), tokenDenomination);
_deposit(commitment);
emit Deposit(commitment, next_index - 1, block.timestamp);
} }
/** function _processWithdraw(address payable _receiver, uint256 _fee) internal {
@dev Withdraw deposit from the mixer. `a`, `b`, and `c` are zkSNARK proof data, and input is an array of circuit public inputs _receiver.transfer(userEther);
`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 < etherFeeDenomination, "Fee exceeds transfer value"); safeErc20Transfer(_receiver, mixDenomination - _fee);
receiver.transfer(etherFeeDenomination - fee); if (_fee > 0) {
safeErc20Transfer(operator, _fee);
if (fee > 0) { }
operator.transfer(fee);
} }
transfer(receiver, tokenDenomination); function safeErc20TransferFrom(address from, address to, uint256 amount) internal {
emit Withdraw(receiver, nullifierHash, fee);
}
function transferFrom(address from, address to, uint256 amount) internal {
bool success; bool success;
bytes memory data; bytes memory data;
bytes4 transferFromSelector = 0x23b872dd; bytes4 transferFromSelector = 0x23b872dd;
@ -92,7 +64,7 @@ contract ERC20Mixer is Mixer {
} }
} }
function transfer(address to, uint256 amount) internal { function safeErc20Transfer(address to, uint256 amount) internal {
bool success; bool success;
bytes memory data; bytes memory data;
bytes4 transferSelector = 0xa9059cbb; bytes4 transferSelector = 0xa9059cbb;

View File

@ -14,49 +14,23 @@ pragma solidity ^0.5.8;
import "./Mixer.sol"; import "./Mixer.sol";
contract ETHMixer is Mixer { contract ETHMixer is Mixer {
uint256 public etherDenomination;
constructor( constructor(
address _verifier, address _verifier,
uint256 _etherDenomination, uint256 _mixDenomination,
uint8 _merkleTreeHeight, uint8 _merkleTreeHeight,
uint256 _emptyElement, uint256 _emptyElement,
address payable _operator address payable _operator
) Mixer(_verifier, _merkleTreeHeight, _emptyElement, _operator) public { ) Mixer(_verifier, _mixDenomination, _merkleTreeHeight, _emptyElement, _operator) public {
etherDenomination = _etherDenomination;
} }
/** function _processWithdraw(address payable _receiver, uint256 _fee) internal {
@dev Deposit funds into mixer. The caller must send value equal to `etherDenomination` of this mixer. _receiver.transfer(mixDenomination - _fee);
@param commitment the note commitment, which is PedersenHash(nullifier + secret) if (_fee > 0) {
*/ operator.transfer(_fee);
function deposit(uint256 commitment) public payable { }
require(msg.value == etherDenomination, "Please send `etherDenomination` ETH along with transaction"); }
_deposit(commitment);
function _processDeposit() internal {
emit Deposit(commitment, next_index - 1, block.timestamp); require(msg.value == mixDenomination, "Please send `mixDenomination` ETH along with transaction");
}
/**
@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 < etherDenomination, "Fee exceeds transfer value");
receiver.transfer(etherDenomination - fee);
if (fee > 0) {
operator.transfer(fee);
}
emit Withdraw(receiver, nullifierHash, fee);
} }
} }

View File

@ -26,6 +26,7 @@ contract Mixer is MerkleTreeWithHistory {
// we store all commitments just to prevent accidental deposits with the same commitment // we store all commitments just to prevent accidental deposits with the same commitment
mapping(uint256 => bool) public commitments; mapping(uint256 => bool) public commitments;
IVerifier public verifier; IVerifier public verifier;
uint256 public mixDenomination;
event Deposit(uint256 indexed commitment, uint256 leafIndex, uint256 timestamp); event Deposit(uint256 indexed commitment, uint256 leafIndex, uint256 timestamp);
event Withdraw(address to, uint256 nullifierHash, uint256 fee); event Withdraw(address to, uint256 nullifierHash, uint256 fee);
@ -39,31 +40,54 @@ contract Mixer is MerkleTreeWithHistory {
*/ */
constructor( constructor(
address _verifier, address _verifier,
uint256 _mixDenomination,
uint8 _merkleTreeHeight, uint8 _merkleTreeHeight,
uint256 _emptyElement, uint256 _emptyElement,
address payable _operator address payable _operator
) MerkleTreeWithHistory(_merkleTreeHeight, _emptyElement) public { ) MerkleTreeWithHistory(_merkleTreeHeight, _emptyElement) public {
verifier = IVerifier(_verifier); verifier = IVerifier(_verifier);
operator = _operator; operator = _operator;
mixDenomination = _mixDenomination;
} }
/**
function _deposit(uint256 commitment) internal { @dev Deposit funds into mixer. The caller must send value equal to `mixDenomination` of this mixer.
@param commitment the note commitment, which is PedersenHash(nullifier + secret)
*/
/**
@dev Deposit funds into the mixer. The caller must send ETH value equal to `userEther` of this mixer.
The caller also has to have at least `mixDenomination` amount approved for the mixer.
@param commitment the note commitment, which is PedersenHash(nullifier + secret)
*/
function deposit(uint256 commitment) public payable {
require(isDepositsEnabled, "deposits disabled"); require(isDepositsEnabled, "deposits disabled");
require(!commitments[commitment], "The commitment has been submitted"); require(!commitments[commitment], "The commitment has been submitted");
_processDeposit();
_insert(commitment); _insert(commitment);
commitments[commitment] = true; commitments[commitment] = true;
}
function _withdraw(uint256[2] memory a, uint256[2][2] memory b, uint256[2] memory c, uint256[4] memory input) internal { 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 {
uint256 root = input[0]; uint256 root = input[0];
uint256 nullifierHash = input[1]; uint256 nullifierHash = input[1];
address payable receiver = address(input[2]);
uint256 fee = input[3];
require(fee < mixDenomination, "Fee exceeds transfer value");
require(!nullifierHashes[nullifierHash], "The note has been already spent"); require(!nullifierHashes[nullifierHash], "The note has been already spent");
require(isKnownRoot(root), "Cannot find your merkle root"); // Make sure to use a recent one require(isKnownRoot(root), "Cannot find your merkle root"); // Make sure to use a recent one
require(verifier.verifyProof(a, b, c, input), "Invalid withdraw proof"); require(verifier.verifyProof(a, b, c, input), "Invalid withdraw proof");
nullifierHashes[nullifierHash] = true; nullifierHashes[nullifierHash] = true;
_processWithdraw(receiver, fee);
emit Withdraw(receiver, nullifierHash, fee);
} }
function toggleDeposits() external { function toggleDeposits() external {
@ -79,4 +103,8 @@ contract Mixer is MerkleTreeWithHistory {
function isSpent(uint256 nullifier) public view returns(bool) { function isSpent(uint256 nullifier) public view returns(bool) {
return nullifierHashes[nullifier]; return nullifierHashes[nullifier];
} }
function _processDeposit() internal {}
function _processWithdraw(address payable _receiver, uint256 _fee) internal {}
} }

View File

@ -122,7 +122,6 @@ contract('ERC20Mixer', accounts => {
balanceUserAfter.should.be.eq.BN(toBN(balanceUserBefore).sub(toBN(tokenDenomination))) balanceUserAfter.should.be.eq.BN(toBN(balanceUserBefore).sub(toBN(tokenDenomination)))
const { root, path_elements, path_index } = await tree.path(0) const { root, path_elements, path_index } = await tree.path(0)
// Circuit input // Circuit input
const input = stringifyBigInts({ const input = stringifyBigInts({
// public // public
@ -149,7 +148,6 @@ contract('ERC20Mixer', accounts => {
const ethBalanceRecieverBefore = await web3.eth.getBalance(toHex(receiver.toString())) const ethBalanceRecieverBefore = await web3.eth.getBalance(toHex(receiver.toString()))
let isSpent = await mixer.isSpent(input.nullifierHash.toString(16).padStart(66, '0x00000')) let isSpent = await mixer.isSpent(input.nullifierHash.toString(16).padStart(66, '0x00000'))
isSpent.should.be.equal(false) isSpent.should.be.equal(false)
// Uncomment to measure gas usage // Uncomment to measure gas usage
// gas = await mixer.withdraw.estimateGas(pi_a, pi_b, pi_c, publicSignals, { from: relayer, gasPrice: '0' }) // gas = await mixer.withdraw.estimateGas(pi_a, pi_b, pi_c, publicSignals, { from: relayer, gasPrice: '0' })
// console.log('withdraw gas:', gas) // console.log('withdraw gas:', gas)
@ -161,12 +159,11 @@ contract('ERC20Mixer', accounts => {
const balanceRecieverAfter = await token.balanceOf(toHex(receiver.toString())) const balanceRecieverAfter = await token.balanceOf(toHex(receiver.toString()))
const ethBalanceRecieverAfter = await web3.eth.getBalance(toHex(receiver.toString())) const ethBalanceRecieverAfter = await web3.eth.getBalance(toHex(receiver.toString()))
const feeBN = toBN(fee.toString()) const feeBN = toBN(fee.toString())
balanceMixerAfter.should.be.eq.BN(toBN(balanceMixerBefore).sub(toBN(tokenDenomination))) balanceMixerAfter.should.be.eq.BN(toBN(balanceMixerBefore).sub(toBN(value)))
balanceRelayerAfter.should.be.eq.BN(toBN(balanceRelayerBefore)) balanceRelayerAfter.should.be.eq.BN(toBN(balanceRelayerBefore))
ethBalanceOperatorAfter.should.be.eq.BN(toBN(ethBalanceOperatorBefore).add(feeBN)) ethBalanceOperatorAfter.should.be.eq.BN(toBN(ethBalanceOperatorBefore))
balanceRecieverAfter.should.be.eq.BN(toBN(balanceRecieverBefore).add(toBN(tokenDenomination))) balanceRecieverAfter.should.be.eq.BN(toBN(balanceRecieverBefore).add(toBN(tokenDenomination).sub(feeBN)))
ethBalanceRecieverAfter.should.be.eq.BN(toBN(ethBalanceRecieverBefore).add(toBN(value)).sub(feeBN)) ethBalanceRecieverAfter.should.be.eq.BN(toBN(ethBalanceRecieverBefore).add(toBN(value)))
logs[0].event.should.be.equal('Withdraw') logs[0].event.should.be.equal('Withdraw')
logs[0].args.nullifierHash.should.be.eq.BN(toBN(input.nullifierHash.toString())) logs[0].args.nullifierHash.should.be.eq.BN(toBN(input.nullifierHash.toString()))
@ -258,9 +255,9 @@ contract('ERC20Mixer', accounts => {
}) })
it.skip('should work with REAL DAI', async () => { it.skip('should work with REAL DAI', async () => {
// dont forget to specify your token in .env // dont forget to specify your token in .env
// and sent `tokenDenomination` to accounts[0] (0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1) // and send `tokenDenomination` to accounts[0] (0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1)
// run ganache as // run ganache as
// ganache-cli --fork https://kovan.infura.io/v3/27a9649f826b4e31a83e07ae09a87448@13146218 -d --keepAliveTimeout 20 // npx ganache-cli --fork https://kovan.infura.io/v3/27a9649f826b4e31a83e07ae09a87448@13146218 -d --keepAliveTimeout 20
const deposit = generateDeposit() const deposit = generateDeposit()
const user = accounts[4] const user = accounts[4]
const userBal = await token.balanceOf(user) const userBal = await token.balanceOf(user)

View File

@ -90,7 +90,7 @@ contract('ETHMixer', accounts => {
describe('#constructor', () => { describe('#constructor', () => {
it('should initialize', async () => { it('should initialize', async () => {
const etherDenomination = await mixer.etherDenomination() const etherDenomination = await mixer.mixDenomination()
etherDenomination.should.be.eq.BN(toBN(value)) etherDenomination.should.be.eq.BN(toBN(value))
}) })
}) })