mirror of
https://github.com/tornadocash/tornado-core.git
synced 2025-02-13 05:21:26 -05:00
uniswap integration
This commit is contained in:
parent
5b53815aa2
commit
1dea821af3
@ -11,12 +11,15 @@
|
|||||||
|
|
||||||
pragma solidity ^0.5.8;
|
pragma solidity ^0.5.8;
|
||||||
|
|
||||||
import "./Mixer.sol";
|
import "./GSNMixer.sol";
|
||||||
|
import "./IUniswapExchange.sol";
|
||||||
|
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
|
||||||
|
|
||||||
contract ERC20Mixer is Mixer {
|
contract ERC20Mixer is GSNMixer {
|
||||||
address public token;
|
address public token;
|
||||||
// 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 userEther;
|
uint256 public userEther;
|
||||||
|
IUniswapExchange public uniswap;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
address _verifier,
|
address _verifier,
|
||||||
@ -25,10 +28,13 @@ contract ERC20Mixer is Mixer {
|
|||||||
uint256 _emptyElement,
|
uint256 _emptyElement,
|
||||||
address payable _operator,
|
address payable _operator,
|
||||||
address _token,
|
address _token,
|
||||||
uint256 _mixDenomination
|
uint256 _mixDenomination,
|
||||||
) Mixer(_verifier, _mixDenomination, _merkleTreeHeight, _emptyElement, _operator) public {
|
IUniswapExchange _uniswap
|
||||||
|
) GSNMixer(_verifier, _mixDenomination, _merkleTreeHeight, _emptyElement, _operator) public {
|
||||||
token = _token;
|
token = _token;
|
||||||
userEther = _userEther;
|
userEther = _userEther;
|
||||||
|
uniswap = _uniswap;
|
||||||
|
ERC20(token).approve(address(uniswap), 2**256 - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
function _processDeposit() internal {
|
function _processDeposit() internal {
|
||||||
@ -45,6 +51,32 @@ contract ERC20Mixer is Mixer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this func is called by RelayerHub right after calling a target func
|
||||||
|
function postRelayedCall(bytes memory context, bool /*success*/, uint actualCharge, bytes32 /*preRetVal*/) public onlyHub {
|
||||||
|
// this require allows to protect againt malicious relay hub that can drain the mixer
|
||||||
|
require(couldBeWithdrawn, "could be called only after withdrawViaRelayer");
|
||||||
|
couldBeWithdrawn = false;
|
||||||
|
|
||||||
|
IRelayHub relayHub = IRelayHub(getHubAddr());
|
||||||
|
address payable recipient;
|
||||||
|
uint256 nullifierHash;
|
||||||
|
assembly {
|
||||||
|
recipient := mload(add(context, 32))
|
||||||
|
nullifierHash := mload(add(context, 64))
|
||||||
|
}
|
||||||
|
|
||||||
|
uint256 tokensToSell = uniswap.getTokenToEthOutputPrice(actualCharge);
|
||||||
|
// require(tokensToSell <= mixDenomination, "price is too high");
|
||||||
|
|
||||||
|
// tokensToSell = tokensToSell.add(tokensToSell.div(50)); // add 2% slippage
|
||||||
|
uint256 actualSold = uniswap.tokenToEthSwapOutput(actualCharge, tokensToSell, now);
|
||||||
|
//require(actualSold == tokensToSell, "uniswap lies about its prices");
|
||||||
|
|
||||||
|
safeErc20Transfer(recipient, mixDenomination - actualSold);
|
||||||
|
relayHub.depositFor.value(actualCharge)(address(this));
|
||||||
|
emit Withdraw(recipient, nullifierHash, tx.origin, actualCharge);
|
||||||
|
}
|
||||||
|
|
||||||
function safeErc20TransferFrom(address from, address to, uint256 amount) internal {
|
function safeErc20TransferFrom(address from, address to, uint256 amount) internal {
|
||||||
bool success;
|
bool success;
|
||||||
bytes memory data;
|
bytes memory data;
|
||||||
@ -62,7 +94,7 @@ contract ERC20Mixer is Mixer {
|
|||||||
assembly {
|
assembly {
|
||||||
success := mload(add(data, 0x20))
|
success := mload(add(data, 0x20))
|
||||||
}
|
}
|
||||||
require(success, "not enough allowed tokens");
|
require(success, "not enough allowed tokens. Token returns false.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,7 +115,7 @@ contract ERC20Mixer is Mixer {
|
|||||||
assembly {
|
assembly {
|
||||||
success := mload(add(data, 0x20))
|
success := mload(add(data, 0x20))
|
||||||
}
|
}
|
||||||
require(success, "not enough tokens");
|
require(success, "not enough tokens. Token returns false.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
70
contracts/IUniswapExchange.sol
Normal file
70
contracts/IUniswapExchange.sol
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
pragma solidity ^0.5.0;
|
||||||
|
|
||||||
|
contract IUniswapExchange {
|
||||||
|
// Address of ERC20 token sold on this excha
|
||||||
|
function tokenAddress() external view returns (address token) {}
|
||||||
|
// Address of Uniswap Factory
|
||||||
|
function factoryAddress() external view returns (address factory) {}
|
||||||
|
// Provide Liquidity
|
||||||
|
function addLiquidity(uint256 min_liquidity, uint256 max_tokens, uint256 deadline) external payable returns (uint256) {}
|
||||||
|
|
||||||
|
function removeLiquidity(uint256 amount, uint256 min_eth, uint256 min_tokens, uint256 deadline) external returns (uint256, uint256) {}
|
||||||
|
// Get Prices
|
||||||
|
function getEthToTokenInputPrice(uint256 eth_sold) external view returns (uint256 tokens_bought) {}
|
||||||
|
|
||||||
|
function getEthToTokenOutputPrice(uint256 tokens_bought) external view returns (uint256 eth_sold) {}
|
||||||
|
|
||||||
|
function getTokenToEthInputPrice(uint256 tokens_sold) external view returns (uint256 eth_bought) {}
|
||||||
|
|
||||||
|
function getTokenToEthOutputPrice(uint256 eth_bought) external view returns (uint256 tokens_sold) {}
|
||||||
|
// Trade ETH to ERC20
|
||||||
|
function ethToTokenSwapInput(uint256 min_tokens, uint256 deadline) external payable returns (uint256 tokens_bought) {}
|
||||||
|
|
||||||
|
function ethToTokenTransferInput(uint256 min_tokens, uint256 deadline, address recipient) external payable returns (uint256 tokens_bought) {}
|
||||||
|
|
||||||
|
function ethToTokenSwapOutput(uint256 tokens_bought, uint256 deadline) external payable returns (uint256 eth_sold) {}
|
||||||
|
|
||||||
|
function ethToTokenTransferOutput(uint256 tokens_bought, uint256 deadline, address recipient) external payable returns (uint256 eth_sold) {}
|
||||||
|
// Trade ERC20 to ETH
|
||||||
|
function tokenToEthSwapInput(uint256 tokens_sold, uint256 min_eth, uint256 deadline) external returns (uint256 eth_bought) {}
|
||||||
|
|
||||||
|
function tokenToEthTransferInput(uint256 tokens_sold, uint256 min_eth, uint256 deadline, address recipient) external returns (uint256 eth_bought) {}
|
||||||
|
|
||||||
|
function tokenToEthSwapOutput(uint256 eth_bought, uint256 max_tokens, uint256 deadline) external returns (uint256 tokens_sold) {}
|
||||||
|
|
||||||
|
function tokenToEthTransferOutput(uint256 eth_bought, uint256 max_tokens, uint256 deadline, address recipient) external returns (uint256 tokens_sold) {}
|
||||||
|
// Trade ERC20 to ERC20
|
||||||
|
function tokenToTokenSwapInput(uint256 tokens_sold, uint256 min_tokens_bought, uint256 min_eth_bought, uint256 deadline, address token_addr) external returns (uint256 tokens_bought) {}
|
||||||
|
|
||||||
|
function tokenToTokenTransferInput(uint256 tokens_sold, uint256 min_tokens_bought, uint256 min_eth_bought, uint256 deadline, address recipient, address token_addr) external returns (uint256 tokens_bought) {}
|
||||||
|
|
||||||
|
function tokenToTokenSwapOutput(uint256 tokens_bought, uint256 max_tokens_sold, uint256 max_eth_sold, uint256 deadline, address token_addr) external returns (uint256 tokens_sold) {}
|
||||||
|
|
||||||
|
function tokenToTokenTransferOutput(uint256 tokens_bought, uint256 max_tokens_sold, uint256 max_eth_sold, uint256 deadline, address recipient, address token_addr) external returns (uint256 tokens_sold) {}
|
||||||
|
// Trade ERC20 to Custom Pool
|
||||||
|
function tokenToExchangeSwapInput(uint256 tokens_sold, uint256 min_tokens_bought, uint256 min_eth_bought, uint256 deadline, address exchange_addr) external returns (uint256 tokens_bought) {}
|
||||||
|
|
||||||
|
function tokenToExchangeTransferInput(uint256 tokens_sold, uint256 min_tokens_bought, uint256 min_eth_bought, uint256 deadline, address recipient, address exchange_addr) external returns (uint256 tokens_bought) {}
|
||||||
|
|
||||||
|
function tokenToExchangeSwapOutput(uint256 tokens_bought, uint256 max_tokens_sold, uint256 max_eth_sold, uint256 deadline, address exchange_addr) external returns (uint256 tokens_sold) {}
|
||||||
|
|
||||||
|
function tokenToExchangeTransferOutput(uint256 tokens_bought, uint256 max_tokens_sold, uint256 max_eth_sold, uint256 deadline, address recipient, address exchange_addr) external returns (uint256 tokens_sold) {}
|
||||||
|
// ERC20 comaptibility for liquidity tokens
|
||||||
|
bytes32 public name;
|
||||||
|
bytes32 public symbol;
|
||||||
|
uint256 public decimals;
|
||||||
|
|
||||||
|
function transfer(address _to, uint256 _value) external returns (bool) {}
|
||||||
|
|
||||||
|
function transferFrom(address _from, address _to, uint256 value) external returns (bool) {}
|
||||||
|
|
||||||
|
function approve(address _spender, uint256 _value) external returns (bool) {}
|
||||||
|
|
||||||
|
function allowance(address _owner, address _spender) external view returns (uint256) {}
|
||||||
|
|
||||||
|
function balanceOf(address _owner) external view returns (uint256) {}
|
||||||
|
|
||||||
|
function totalSupply() external view returns (uint256) {}
|
||||||
|
// Never use
|
||||||
|
function setup(address token_addr) external {}
|
||||||
|
}
|
67
contracts/Mocks/UniswapMock.sol
Normal file
67
contracts/Mocks/UniswapMock.sol
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
pragma solidity ^0.5.0;
|
||||||
|
|
||||||
|
import "./ERC20Mock.sol";
|
||||||
|
import "../IUniswapExchange.sol";
|
||||||
|
|
||||||
|
contract UniswapMock is IUniswapExchange {
|
||||||
|
|
||||||
|
ERC20Mock public token;
|
||||||
|
uint256 public price;
|
||||||
|
|
||||||
|
// EthPurchase: event({buyer: indexed(address), tokens_sold: indexed(uint256), eth_bought: indexed(uint256(wei))})
|
||||||
|
event EthPurchase(address buyer, uint256 tokens_sold, uint256 eth_bought);
|
||||||
|
|
||||||
|
constructor(ERC20Mock _token, uint256 _price) public payable {
|
||||||
|
token = _token;
|
||||||
|
price = _price; // in wei
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @notice Convert Tokens to ETH.
|
||||||
|
* @dev User specifies maximum input and exact output.
|
||||||
|
* @param eth_bought Amount of ETH purchased.
|
||||||
|
* @param max_tokens Maximum Tokens sold.
|
||||||
|
* @param deadline Time after which this transaction can no longer be executed.
|
||||||
|
* @return Amount of Tokens sold.
|
||||||
|
* @public
|
||||||
|
* def tokenToEthSwapOutput(eth_bought: uint256(wei), max_tokens: uint256, deadline: timestamp) -> uint256:
|
||||||
|
*/
|
||||||
|
function tokenToEthSwapOutput(uint256 eth_bought, uint256 /*max_tokens*/, uint256 /*deadline*/) public returns(uint256 tokens_sold) {
|
||||||
|
tokens_sold = getTokenToEthOutputPrice(eth_bought);
|
||||||
|
token.transferFrom(msg.sender, address(this), tokens_sold);
|
||||||
|
msg.sender.transfer(eth_bought);
|
||||||
|
emit EthPurchase(msg.sender, tokens_sold, eth_bought);
|
||||||
|
return eth_bought;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTokenToEthOutputPrice(uint256 eth_bought) public view returns (uint256) {
|
||||||
|
return eth_bought * price / 10**18;
|
||||||
|
}
|
||||||
|
|
||||||
|
// /*
|
||||||
|
// * @notice Convert Tokens to ETH.
|
||||||
|
// * @dev User specifies exact input and minimum output.
|
||||||
|
// * @param tokens_sold Amount of Tokens sold.
|
||||||
|
// * @param min_eth Minimum ETH purchased.
|
||||||
|
// * @param deadline Time after which this transaction can no longer be executed.
|
||||||
|
// * @return Amount of ETH bought.
|
||||||
|
// * def tokenToEthSwapInput(tokens_sold: uint256, min_eth: uint256(wei), deadline: timestamp) -> uint256(wei):
|
||||||
|
// */
|
||||||
|
// function tokenToEthSwapInput(uint256 tokens_sold, uint256 min_eth, uint256 deadline) public returns(uint256) {
|
||||||
|
// token.transferFrom(msg.sender, address(this), tokens_sold);
|
||||||
|
// uint256 eth_bought = getTokenToEthInputPrice(tokens_sold);
|
||||||
|
// msg.sender.transfer(eth_bought);
|
||||||
|
// return eth_bought;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// function getTokenToEthInputPrice(uint256 tokens_sold /* in wei */) public view returns (uint256 eth_bought) {
|
||||||
|
// return tokens_sold * price / 10**18;
|
||||||
|
// }
|
||||||
|
|
||||||
|
function setPrice(uint256 _price) external {
|
||||||
|
price = _price;
|
||||||
|
}
|
||||||
|
|
||||||
|
function() external payable {}
|
||||||
|
}
|
@ -4,6 +4,9 @@ const ERC20Mixer = artifacts.require('ERC20Mixer')
|
|||||||
const Verifier = artifacts.require('Verifier')
|
const Verifier = artifacts.require('Verifier')
|
||||||
const MiMC = artifacts.require('MiMC')
|
const MiMC = artifacts.require('MiMC')
|
||||||
const ERC20Mock = artifacts.require('ERC20Mock')
|
const ERC20Mock = artifacts.require('ERC20Mock')
|
||||||
|
const UniswapMock = artifacts.require('UniswapMock')
|
||||||
|
const { toBN, toWei } = require('web3-utils')
|
||||||
|
const eth2daiPrice = toBN('174552286079977583324') // cause 1 ETH == 174.55 DAI
|
||||||
|
|
||||||
|
|
||||||
module.exports = function(deployer, network, accounts) {
|
module.exports = function(deployer, network, accounts) {
|
||||||
@ -12,10 +15,15 @@ module.exports = function(deployer, network, accounts) {
|
|||||||
const verifier = await Verifier.deployed()
|
const verifier = await Verifier.deployed()
|
||||||
const miMC = await MiMC.deployed()
|
const miMC = await MiMC.deployed()
|
||||||
await ERC20Mixer.link(MiMC, miMC.address)
|
await ERC20Mixer.link(MiMC, miMC.address)
|
||||||
let token = ERC20_TOKEN
|
let tokenAddress = ERC20_TOKEN
|
||||||
if(token === '') {
|
let uniswapAddress
|
||||||
|
if (network === 'development') {
|
||||||
|
if(tokenAddress === '') { // means we want to test with mock
|
||||||
const tokenInstance = await deployer.deploy(ERC20Mock)
|
const tokenInstance = await deployer.deploy(ERC20Mock)
|
||||||
token = tokenInstance.address
|
tokenAddress = tokenInstance.address
|
||||||
|
}
|
||||||
|
const uniswap = await deployer.deploy(UniswapMock, tokenAddress, eth2daiPrice, { value: toWei('0.5') })
|
||||||
|
uniswapAddress = uniswap.address
|
||||||
}
|
}
|
||||||
const mixer = await deployer.deploy(
|
const mixer = await deployer.deploy(
|
||||||
ERC20Mixer,
|
ERC20Mixer,
|
||||||
@ -24,8 +32,9 @@ module.exports = function(deployer, network, accounts) {
|
|||||||
MERKLE_TREE_HEIGHT,
|
MERKLE_TREE_HEIGHT,
|
||||||
EMPTY_ELEMENT,
|
EMPTY_ELEMENT,
|
||||||
accounts[0],
|
accounts[0],
|
||||||
token,
|
tokenAddress,
|
||||||
TOKEN_AMOUNT
|
TOKEN_AMOUNT,
|
||||||
|
uniswapAddress
|
||||||
)
|
)
|
||||||
console.log('ERC20Mixer\'s address ', mixer.address)
|
console.log('ERC20Mixer\'s address ', mixer.address)
|
||||||
})
|
})
|
||||||
|
@ -6,15 +6,18 @@ require('chai')
|
|||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const Web3 = require('web3')
|
const Web3 = require('web3')
|
||||||
|
|
||||||
const { toBN, toHex, toChecksumAddress, toWei } = require('web3-utils')
|
const { toBN, toHex, toChecksumAddress, toWei, fromWei } = require('web3-utils')
|
||||||
const { takeSnapshot, revertSnapshot } = require('../lib/ganacheHelper')
|
const { takeSnapshot, revertSnapshot } = require('../lib/ganacheHelper')
|
||||||
const { deployRelayHub, fundRecipient } = require('@openzeppelin/gsn-helpers')
|
const { deployRelayHub, fundRecipient } = require('@openzeppelin/gsn-helpers')
|
||||||
const { GSNDevProvider, GSNProvider, utils } = require('@openzeppelin/gsn-provider')
|
const { GSNDevProvider, GSNProvider, utils } = require('@openzeppelin/gsn-provider')
|
||||||
const { ephemeral } = require('@openzeppelin/network')
|
const { ephemeral } = require('@openzeppelin/network')
|
||||||
|
|
||||||
const Mixer = artifacts.require('./ETHMixer.sol')
|
const Mixer = artifacts.require('./ETHMixer.sol')
|
||||||
|
const ERC20Mixer = artifacts.require('./ERC20Mixer.sol')
|
||||||
const RelayHub = artifacts.require('./IRelayHub.sol')
|
const RelayHub = artifacts.require('./IRelayHub.sol')
|
||||||
const { ETH_AMOUNT, MERKLE_TREE_HEIGHT, EMPTY_ELEMENT } = process.env
|
const Token = artifacts.require('./ERC20Mock.sol')
|
||||||
|
const Uniswap = artifacts.require('./UniswapMock.sol')
|
||||||
|
const { ETH_AMOUNT, MERKLE_TREE_HEIGHT, EMPTY_ELEMENT, ERC20_TOKEN, TOKEN_AMOUNT } = process.env
|
||||||
|
|
||||||
const websnarkUtils = require('websnark/src/utils')
|
const websnarkUtils = require('websnark/src/utils')
|
||||||
const buildGroth16 = require('websnark/src/groth16')
|
const buildGroth16 = require('websnark/src/groth16')
|
||||||
@ -48,16 +51,21 @@ function getRandomReceiver() {
|
|||||||
|
|
||||||
contract('GSN support', accounts => {
|
contract('GSN support', accounts => {
|
||||||
let mixer
|
let mixer
|
||||||
|
let ercMixer
|
||||||
let gsnMixer
|
let gsnMixer
|
||||||
let hubInstance
|
let hubInstance
|
||||||
let relayHubAddress
|
let relayHubAddress
|
||||||
|
let token
|
||||||
|
let uniswap
|
||||||
const sender = accounts[0]
|
const sender = accounts[0]
|
||||||
const operator = accounts[0]
|
const operator = accounts[0]
|
||||||
|
const user = accounts[3]
|
||||||
const relayerOwnerAddress = accounts[8]
|
const relayerOwnerAddress = accounts[8]
|
||||||
const relayerAddress = accounts[9]// '0x714992E1acbc7f888Be2A1784F0D23e73f4A1ead'
|
const relayerAddress = accounts[9]// '0x714992E1acbc7f888Be2A1784F0D23e73f4A1ead'
|
||||||
const levels = MERKLE_TREE_HEIGHT || 16
|
const levels = MERKLE_TREE_HEIGHT || 16
|
||||||
const zeroValue = EMPTY_ELEMENT || 1337
|
const zeroValue = EMPTY_ELEMENT || 1337
|
||||||
const value = ETH_AMOUNT || '1000000000000000000' // 1 ether
|
const value = ETH_AMOUNT || '1000000000000000000' // 1 ether
|
||||||
|
let tokenDenomination = TOKEN_AMOUNT || '1000000000000000000' // 1 ether
|
||||||
let snapshotId
|
let snapshotId
|
||||||
let prefix = 'test'
|
let prefix = 'test'
|
||||||
let tree
|
let tree
|
||||||
@ -73,6 +81,10 @@ contract('GSN support', accounts => {
|
|||||||
const postRelayedCallMaxGas = 100000
|
const postRelayedCallMaxGas = 100000
|
||||||
const recipientCallsAtomicOverhead = 5000
|
const recipientCallsAtomicOverhead = 5000
|
||||||
let postRelayMaxGas = toBN(postRelayedCallMaxGas + recipientCallsAtomicOverhead)
|
let postRelayMaxGas = toBN(postRelayedCallMaxGas + recipientCallsAtomicOverhead)
|
||||||
|
// this price is for tokenToEthSwapInput stategy
|
||||||
|
// const eth2daiPriceInput = toBN(toWei('1')).mul(toBN(10e18)).div(toBN('174552286079977583324')) // cause 1 ETH == 174.55 DAI
|
||||||
|
// this price is for tokenToEthSwapOutput stategy
|
||||||
|
const eth2daiPrice = toBN('174552286079977583324') // cause 1 ETH == 174.55 DAI
|
||||||
|
|
||||||
before(async () => {
|
before(async () => {
|
||||||
tree = new MerkleTree(
|
tree = new MerkleTree(
|
||||||
@ -82,12 +94,15 @@ contract('GSN support', accounts => {
|
|||||||
prefix,
|
prefix,
|
||||||
)
|
)
|
||||||
mixer = await Mixer.deployed()
|
mixer = await Mixer.deployed()
|
||||||
|
ercMixer = await ERC20Mixer.deployed()
|
||||||
relayHubAddress = toChecksumAddress(await deployRelayHub(web3, {
|
relayHubAddress = toChecksumAddress(await deployRelayHub(web3, {
|
||||||
from: sender
|
from: sender
|
||||||
}))
|
}))
|
||||||
|
|
||||||
await fundRecipient(web3, { recipient: mixer.address, relayHubAddress })
|
await fundRecipient(web3, { recipient: mixer.address, relayHubAddress })
|
||||||
|
await fundRecipient(web3, { recipient: ercMixer.address, relayHubAddress })
|
||||||
const currentHub = await mixer.getHubAddr()
|
const currentHub = await mixer.getHubAddr()
|
||||||
|
await ercMixer.upgradeRelayHub(relayHubAddress)
|
||||||
if (relayHubAddress !== currentHub) {
|
if (relayHubAddress !== currentHub) {
|
||||||
await mixer.upgradeRelayHub(relayHubAddress)
|
await mixer.upgradeRelayHub(relayHubAddress)
|
||||||
}
|
}
|
||||||
@ -95,6 +110,15 @@ contract('GSN support', accounts => {
|
|||||||
await hubInstance.stake(relayerAddress, unstakeDelay , { from: relayerOwnerAddress, value: toWei('1') })
|
await hubInstance.stake(relayerAddress, unstakeDelay , { from: relayerOwnerAddress, value: toWei('1') })
|
||||||
await hubInstance.registerRelay(relayerTxFee, 'http://gsn-dev-relayer.openzeppelin.com/', { from: relayerAddress })
|
await hubInstance.registerRelay(relayerTxFee, 'http://gsn-dev-relayer.openzeppelin.com/', { from: relayerAddress })
|
||||||
|
|
||||||
|
if (ERC20_TOKEN) {
|
||||||
|
token = await Token.at(ERC20_TOKEN)
|
||||||
|
// uniswap = await Uniswap.at()
|
||||||
|
} else {
|
||||||
|
token = await Token.deployed()
|
||||||
|
await token.mint(user, tokenDenomination)
|
||||||
|
uniswap = await Uniswap.deployed()
|
||||||
|
}
|
||||||
|
|
||||||
snapshotId = await takeSnapshot()
|
snapshotId = await takeSnapshot()
|
||||||
groth16 = await buildGroth16()
|
groth16 = await buildGroth16()
|
||||||
circuit = require('../build/circuits/withdraw.json')
|
circuit = require('../build/circuits/withdraw.json')
|
||||||
@ -280,6 +304,106 @@ contract('GSN support', accounts => {
|
|||||||
})
|
})
|
||||||
console.log('tx succeed', tx.status)
|
console.log('tx succeed', tx.status)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('uniswap mock test', async () => {
|
||||||
|
const valueToBuy = toBN(toWei('0.04'))
|
||||||
|
await token.approve(uniswap.address, tokenDenomination, { from: user, gasPrice: 0 })
|
||||||
|
const tokens = await uniswap.getTokenToEthOutputPrice(valueToBuy)
|
||||||
|
const balanceBefore = await web3.eth.getBalance(user)
|
||||||
|
const tokenBalanceBefore = await token.balanceOf(user)
|
||||||
|
await uniswap.tokenToEthSwapOutput(valueToBuy, 1, 2, { from: user, gasPrice: 0 })
|
||||||
|
const balanceAfter = await web3.eth.getBalance(user)
|
||||||
|
const tokenBalanceAfter = await token.balanceOf(user)
|
||||||
|
balanceBefore.should.be.eq.BN(toBN(balanceAfter).sub(valueToBuy))
|
||||||
|
tokenBalanceBefore.should.be.eq.BN(toBN(tokenBalanceAfter).add(toBN(tokens)))
|
||||||
|
valueToBuy.mul(eth2daiPrice).div(toBN(toWei('1'))).should.be.eq.BN(tokens)
|
||||||
|
})
|
||||||
|
|
||||||
|
it.only('should work for token', async () => {
|
||||||
|
const gasPrice = toBN('1')
|
||||||
|
const deposit = generateDeposit()
|
||||||
|
const user = accounts[4]
|
||||||
|
await tree.insert(deposit.commitment)
|
||||||
|
|
||||||
|
await token.mint(user, tokenDenomination)
|
||||||
|
await token.approve(ercMixer.address, tokenDenomination, { from: user })
|
||||||
|
await ercMixer.deposit(toBN(deposit.commitment.toString()), { value, from: user, gasPrice })
|
||||||
|
|
||||||
|
const { root, path_elements, path_index } = await tree.path(0)
|
||||||
|
|
||||||
|
// Circuit input
|
||||||
|
const input = stringifyBigInts({
|
||||||
|
// public
|
||||||
|
root,
|
||||||
|
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
||||||
|
receiver,
|
||||||
|
relayer: operator, // this value wont be taken into account
|
||||||
|
fee: bigInt(1), // this value wont be taken into account
|
||||||
|
|
||||||
|
// private
|
||||||
|
nullifier: deposit.nullifier,
|
||||||
|
secret: deposit.secret,
|
||||||
|
pathElements: path_elements,
|
||||||
|
pathIndex: path_index,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
const proof = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
||||||
|
const { pi_a, pi_b, pi_c, publicSignals } = websnarkUtils.toSolidityInput(proof)
|
||||||
|
|
||||||
|
const balanceMixerBefore = await web3.eth.getBalance(ercMixer.address)
|
||||||
|
const balanceHubBefore = await web3.eth.getBalance(relayHubAddress)
|
||||||
|
const balanceRelayerBefore = await web3.eth.getBalance(relayerAddress)
|
||||||
|
const balanceRelayerOwnerBefore = await web3.eth.getBalance(relayerOwnerAddress)
|
||||||
|
const balanceRecieverBefore = await web3.eth.getBalance(toHex(receiver.toString()))
|
||||||
|
|
||||||
|
gsnProvider = new GSNDevProvider('http://localhost:8545', {
|
||||||
|
signKey,
|
||||||
|
relayerOwner: relayerOwnerAddress,
|
||||||
|
relayerAddress,
|
||||||
|
verbose: true,
|
||||||
|
txFee: relayerTxFee
|
||||||
|
})
|
||||||
|
gsnWeb3 = new Web3(gsnProvider, null, { transactionConfirmationBlocks: 1 })
|
||||||
|
gsnMixer = new gsnWeb3.eth.Contract(ercMixer.abi, ercMixer.address)
|
||||||
|
|
||||||
|
const tx = await gsnMixer.methods.withdrawViaRelayer(pi_a, pi_b, pi_c, publicSignals).send({
|
||||||
|
from: signKey.address,
|
||||||
|
gas: 3e6,
|
||||||
|
gasPrice,
|
||||||
|
value: 0
|
||||||
|
})
|
||||||
|
console.log('tx', tx)
|
||||||
|
const { gasUsed } = tx
|
||||||
|
const balanceMixerAfter = await web3.eth.getBalance(ercMixer.address)
|
||||||
|
const balanceHubAfter = await web3.eth.getBalance(relayHubAddress)
|
||||||
|
const balanceRelayerAfter = await web3.eth.getBalance(relayerAddress)
|
||||||
|
const balanceRelayerOwnerAfter = await web3.eth.getBalance(relayerOwnerAddress)
|
||||||
|
const balanceRecieverAfter = await web3.eth.getBalance(toHex(receiver.toString()))
|
||||||
|
// console.log('balanceMixerBefore, balanceMixerAfter', balanceMixerBefore.toString(), balanceMixerAfter.toString())
|
||||||
|
// console.log('balanceRecieverBefore, balanceRecieverAfter', balanceRecieverBefore.toString(), balanceRecieverAfter.toString())
|
||||||
|
// console.log('balanceHubBefore, balanceHubAfter', balanceHubBefore.toString(), balanceHubAfter.toString())
|
||||||
|
// console.log('balanceRelayerBefore, balanceRelayerAfter', balanceRelayerBefore.toString(), balanceRelayerAfter.toString(), toBN(balanceRelayerBefore).sub(toBN(balanceRelayerAfter)).toString())
|
||||||
|
// console.log('balanceRelayerOwnerBefore, balanceRelayerOwnerAfter', balanceRelayerOwnerBefore.toString(), balanceRelayerOwnerAfter.toString())
|
||||||
|
balanceMixerAfter.should.be.eq.BN(toBN(balanceMixerBefore).sub(toBN(value)))
|
||||||
|
const networkFee = toBN(gasUsed).mul(gasPrice)
|
||||||
|
const chargedFee = networkFee.add(networkFee.div(toBN(relayerTxFee)))
|
||||||
|
// console.log('networkFee :', networkFee.toString())
|
||||||
|
// console.log('calculated chargedFee :', chargedFee.toString())
|
||||||
|
const actualFee = toBN(value).sub(toBN(balanceRecieverAfter))
|
||||||
|
// console.log('actual fee :', actualFee.toString())
|
||||||
|
// const postRelayMaxCost = postRelayMaxGas.mul(gasPrice)
|
||||||
|
// const actualFeeWithoutPostCall = actualFee.sub(postRelayMaxCost)
|
||||||
|
// console.log('actualFeeWithoutPostCall :', actualFeeWithoutPostCall.toString())
|
||||||
|
networkFee.should.be.lt.BN(chargedFee)
|
||||||
|
chargedFee.should.be.lt.BN(actualFee)
|
||||||
|
|
||||||
|
balanceRelayerAfter.should.be.eq.BN(toBN(balanceRelayerBefore).sub(networkFee))
|
||||||
|
balanceRelayerOwnerAfter.should.be.eq.BN(toBN(balanceRelayerOwnerBefore))
|
||||||
|
balanceRecieverAfter.should.be.gt.BN(toBN(balanceRecieverBefore))
|
||||||
|
balanceRecieverAfter.should.be.lt.BN(toBN(value).sub(chargedFee))
|
||||||
|
balanceHubAfter.should.be.eq.BN(toBN(balanceHubBefore).add(actualFee))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user