mirror of
https://github.com/tornadocash/tornado-core.git
synced 2025-07-22 06:49:13 -04:00
uniswap integration
This commit is contained in:
parent
5b53815aa2
commit
1dea821af3
5 changed files with 316 additions and 14 deletions
|
@ -11,12 +11,15 @@
|
|||
|
||||
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;
|
||||
// ether value to cover network fee (for relayer) and to have some ETH on a brand new address
|
||||
uint256 public userEther;
|
||||
IUniswapExchange public uniswap;
|
||||
|
||||
constructor(
|
||||
address _verifier,
|
||||
|
@ -25,10 +28,13 @@ contract ERC20Mixer is Mixer {
|
|||
uint256 _emptyElement,
|
||||
address payable _operator,
|
||||
address _token,
|
||||
uint256 _mixDenomination
|
||||
) Mixer(_verifier, _mixDenomination, _merkleTreeHeight, _emptyElement, _operator) public {
|
||||
uint256 _mixDenomination,
|
||||
IUniswapExchange _uniswap
|
||||
) GSNMixer(_verifier, _mixDenomination, _merkleTreeHeight, _emptyElement, _operator) public {
|
||||
token = _token;
|
||||
userEther = _userEther;
|
||||
uniswap = _uniswap;
|
||||
ERC20(token).approve(address(uniswap), 2**256 - 1);
|
||||
}
|
||||
|
||||
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 {
|
||||
bool success;
|
||||
bytes memory data;
|
||||
|
@ -62,7 +94,7 @@ contract ERC20Mixer is Mixer {
|
|||
assembly {
|
||||
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 {
|
||||
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 {}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue