mirror of
https://github.com/tornadocash/tornado-core.git
synced 2025-08-10 07:39:59 -04:00
work in progress for gsn
This commit is contained in:
parent
7c1b20b693
commit
e3d77b4e1d
13 changed files with 4673 additions and 7 deletions
157
contracts/GSNProxy.sol
Normal file
157
contracts/GSNProxy.sol
Normal file
|
@ -0,0 +1,157 @@
|
|||
pragma solidity ^0.5.8;
|
||||
// contract we {}
|
||||
|
||||
import "./IUniswapExchange.sol";
|
||||
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
||||
import "@openzeppelin/contracts-ethereum-package/contracts/GSN/GSNRecipient.sol";
|
||||
import "@openzeppelin/contracts-ethereum-package/contracts/GSN/IRelayHub.sol";
|
||||
import "@openzeppelin/contracts-ethereum-package/contracts/ownership/Ownable.sol";
|
||||
|
||||
contract IMixer {
|
||||
function withdraw(uint256[8] calldata proof, uint256[6] calldata input) external payable;
|
||||
function checkWithdrawalValidity(uint256[8] calldata proof, uint256[6] calldata input) external view;
|
||||
function denomination() external view returns(uint256);
|
||||
function token() external view returns(address); // only for ERC20 version
|
||||
}
|
||||
|
||||
contract GSNProxy is GSNRecipient, Ownable {
|
||||
IMixer public mixer;
|
||||
IUniswapExchange public uniswap;
|
||||
IERC20 public token;
|
||||
|
||||
constructor(address _mixer, address _uniswap) public {
|
||||
mixer = IMixer(_mixer);
|
||||
if (_uniswap != address(0)) {
|
||||
uniswap = IUniswapExchange(_uniswap);
|
||||
require(mixer.token() == uniswap.tokenAddress(), "mixer and uniswap have different tokens");
|
||||
token = IERC20(uniswap.tokenAddress());
|
||||
} else {
|
||||
// todo: require that mixer is ETH version?
|
||||
}
|
||||
}
|
||||
|
||||
// Allow to refill mixer balance
|
||||
function () external payable {}
|
||||
|
||||
modifier onlyHub() {
|
||||
require(msg.sender == getHubAddr(), "only relay hub");
|
||||
_;
|
||||
}
|
||||
|
||||
/**
|
||||
@dev Checks fee and calls mixer withdraw
|
||||
*/
|
||||
function withdraw(uint256[8] calldata proof, uint256[6] calldata input) external {
|
||||
mixer.withdraw.value(refund)(proof, input);
|
||||
// todo: check that we received expected fee?
|
||||
}
|
||||
|
||||
// gsn related stuff
|
||||
// this func is called by a Relayer via the RelayerHub before sending a tx
|
||||
function acceptRelayedCall(
|
||||
address /*relay*/,
|
||||
address /*from*/,
|
||||
bytes memory encodedFunction,
|
||||
uint256 /*transactionFee*/,
|
||||
uint256 /*gasPrice*/,
|
||||
uint256 /*gasLimit*/,
|
||||
uint256 /*nonce*/,
|
||||
bytes memory /*approvalData*/,
|
||||
uint256 maxPossibleCharge
|
||||
) public view returns (uint256, bytes memory) {
|
||||
// think of a withdraw dry-run
|
||||
if (!compareBytesWithSelector(encodedFunction, this.withdraw.selector)) {
|
||||
return (1, "Only withdrawViaRelayer can be called");
|
||||
}
|
||||
|
||||
bytes memory proof;
|
||||
bytes memory root;
|
||||
uint256 fee;
|
||||
uint256 refund;
|
||||
assembly {
|
||||
let dataPointer := add(encodedFunction, 32)
|
||||
let nullifierPointer := mload(add(dataPointer, 4)) // 4 + (8 * 32) + (32) == selector + proof + root
|
||||
let recipientPointer := mload(add(dataPointer, 324)) // 4 + (8 * 32) + (32) + (32) == selector + proof + root + nullifier
|
||||
mstore(recipient, 64) // save array length
|
||||
mstore(add(recipient, 32), recipientPointer) // save recipient address
|
||||
mstore(add(recipient, 64), nullifierPointer) // save nullifier address
|
||||
}
|
||||
//mixer.checkWithdrawalValidity(proof, inputs)
|
||||
// todo: duplicate withdraw checks?
|
||||
|
||||
if (token != IERC20(0)) {
|
||||
// todo maybe static exchange rate?
|
||||
if (uniswap.getTokenToEthInputPrice(fee) < maxPossibleCharge + refund) {
|
||||
return (11, "Fee is too low");
|
||||
}
|
||||
} else {
|
||||
// refund is expected to be 0, checked by mixer contract
|
||||
if (fee < maxPossibleCharge + refund) {
|
||||
return (11, "Fee is too low");
|
||||
}
|
||||
}
|
||||
|
||||
if (mixer.checkWithdrawalValidity()) {
|
||||
|
||||
}
|
||||
|
||||
return _approveRelayedCall();
|
||||
}
|
||||
|
||||
// this func is called by RelayerHub right before calling a target func
|
||||
function preRelayedCall(bytes calldata /*context*/) onlyHub external returns (bytes32) {}
|
||||
function postRelayedCall(bytes memory /*context*/, bool /*success*/, uint actualCharge, bytes32 /*preRetVal*/) onlyHub public {
|
||||
IRelayHub(getHubAddr()).depositFor.value(actualCharge)(address(this));
|
||||
}
|
||||
|
||||
function compareBytesWithSelector(bytes memory data, bytes4 sel) internal pure returns (bool) {
|
||||
return data[0] == sel[0]
|
||||
&& data[1] == sel[1]
|
||||
&& data[2] == sel[2]
|
||||
&& data[3] == sel[3];
|
||||
}
|
||||
|
||||
// Admin functions
|
||||
|
||||
function withdrawFundsFromHub(uint256 amount, address payable dest) onlyOwner external {
|
||||
IRelayHub(getHubAddr()).withdraw(amount, dest);
|
||||
}
|
||||
|
||||
function upgradeRelayHub(address newRelayHub) onlyOwner external {
|
||||
_upgradeRelayHub(newRelayHub);
|
||||
}
|
||||
|
||||
function withdrawEther(uint256 amount) onlyOwner external {
|
||||
msg.sender.transfer(amount);
|
||||
}
|
||||
|
||||
function withdrawTokens(uint256 amount) onlyOwner external {
|
||||
safeErc20Transfer(msg.sender, amount);
|
||||
}
|
||||
|
||||
function sellTokens(uint256 amount, uint256 min_eth) onlyOwner external {
|
||||
token.approve(address(uniswap), amount);
|
||||
uniswap.tokenToEthSwapInput(amount, min_eth, now);
|
||||
}
|
||||
|
||||
function safeErc20Transfer(address to, uint256 amount) internal {
|
||||
bool success;
|
||||
bytes memory data;
|
||||
bytes4 transferSelector = 0xa9059cbb;
|
||||
(success, data) = address(token).call(
|
||||
abi.encodeWithSelector(
|
||||
transferSelector,
|
||||
to, amount
|
||||
)
|
||||
);
|
||||
require(success, "not enough tokens");
|
||||
|
||||
// if contract returns some data let's make sure that is `true` according to standard
|
||||
if (data.length > 0) {
|
||||
assembly {
|
||||
success := mload(add(data, 0x20))
|
||||
}
|
||||
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 exchange
|
||||
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 {}
|
||||
}
|
|
@ -90,16 +90,34 @@ contract Mixer is MerkleTreeWithHistory {
|
|||
address payable relayer = address(input[3]);
|
||||
uint256 fee = input[4];
|
||||
uint256 refund = input[5];
|
||||
|
||||
require(fee < denomination, "Fee exceeds transfer value");
|
||||
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(verifier.verifyProof(proof, input), "Invalid withdraw proof");
|
||||
|
||||
nullifierHashes[nullifierHash] = true;
|
||||
_processWithdraw(receiver, relayer, fee, refund);
|
||||
emit Withdraw(receiver, nullifierHash, relayer, fee);
|
||||
}
|
||||
|
||||
// todo: use this function in withdraw?
|
||||
/**
|
||||
@dev same checks as `withdraw` implemented as a view function. Used for relayers.
|
||||
*/
|
||||
function checkWithdrawalValidity(uint256[8] calldata proof, uint256[6] calldata input) external view {
|
||||
uint256 root = input[0];
|
||||
uint256 nullifierHash = input[1];
|
||||
//address payable receiver = address(input[2]);
|
||||
//address payable relayer = address(input[3]);
|
||||
uint256 fee = input[4];
|
||||
uint256 refund = input[5];
|
||||
require(fee < denomination, "Fee exceeds transfer value");
|
||||
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(verifier.verifyProof(proof, input), "Invalid withdraw proof");
|
||||
}
|
||||
|
||||
/** @dev this function is defined in a child contract */
|
||||
function _processWithdraw(address payable _receiver, address payable _relayer, uint256 _fee, uint256 _refund) internal {}
|
||||
|
||||
|
|
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 tokenAddress;
|
||||
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 {
|
||||
tokenAddress = _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);
|
||||
tokenAddress.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) {
|
||||
tokenAddress.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