This repository has been archived by the owner on Jul 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
744 additions
and
3 deletions.
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
contracts/asset-proxy/contracts/src/bridges/UniswapV2Bridge.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
Copyright 2020 ZeroEx Intl. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
pragma solidity ^0.5.9; | ||
pragma experimental ABIEncoderV2; | ||
|
||
import "@0x/contracts-erc20/contracts/src/interfaces/IERC20Token.sol"; | ||
import "@0x/contracts-erc20/contracts/src/interfaces/IEtherToken.sol"; | ||
import "@0x/contracts-erc20/contracts/src/LibERC20Token.sol"; | ||
import "@0x/contracts-exchange-libs/contracts/src/IWallet.sol"; | ||
import "@0x/contracts-utils/contracts/src/LibAddressArray.sol"; | ||
import "@0x/contracts-utils/contracts/src/DeploymentConstants.sol"; | ||
import "../interfaces/IUniswapV2Router01.sol"; | ||
import "../interfaces/IERC20Bridge.sol"; | ||
|
||
|
||
// solhint-disable space-after-comma | ||
// solhint-disable not-rely-on-time | ||
contract UniswapV2Bridge is | ||
IERC20Bridge, | ||
IWallet, | ||
DeploymentConstants | ||
{ | ||
using LibAddressArray for address[]; | ||
|
||
struct TransferState { | ||
address fromTokenAddress; | ||
uint256 fromTokenBalance; | ||
uint256 boughtAmount; | ||
} | ||
|
||
/// @dev Callback for `IERC20Bridge`. Tries to buy `amount` of | ||
/// `toTokenAddress` tokens by selling the entirety of the `fromTokenAddress` | ||
/// token encoded in the bridge data. | ||
/// @param toTokenAddress The token to buy and transfer to `to`. | ||
/// @param from The maker (this contract). | ||
/// @param to The recipient of the bought tokens. | ||
/// @param amount Minimum amount of `toTokenAddress` tokens to buy. | ||
/// @param bridgeData The abi-encoded "from" token address. | ||
/// @return success The magic bytes if successful. | ||
function bridgeTransferFrom( | ||
address toTokenAddress, | ||
address from, | ||
address to, | ||
uint256 amount, | ||
bytes calldata bridgeData | ||
) | ||
external | ||
returns (bytes4 success) | ||
{ | ||
// hold variables to get around stack depth limitations | ||
TransferState memory state; | ||
|
||
// Decode the bridge data to get the `fromTokenAddress`. | ||
(state.fromTokenAddress) = abi.decode(bridgeData, (address)); | ||
|
||
// Just transfer the tokens if they're the same. | ||
if (state.fromTokenAddress == toTokenAddress) { // NOT TESTED | ||
LibERC20Token.transfer(state.fromTokenAddress, to, amount); | ||
return BRIDGE_SUCCESS; | ||
} | ||
|
||
// Get our balance of `fromTokenAddress` token. | ||
state.fromTokenBalance = IERC20Token(state.fromTokenAddress).balanceOf(address(this)); | ||
|
||
// // Grant the Uniswap router an allowance. // FIXME: REVERTING | ||
// LibERC20Token.approve( | ||
// state.fromTokenAddress, | ||
// _getUniswapV2Router01Address(), | ||
// // state.fromTokenBalance | ||
// uint256(-1) | ||
// ); | ||
|
||
// Convert directly from fromTokenAddress to toTokenAddress | ||
address[] memory path = new address[](2); | ||
path = path.append(state.fromTokenAddress); | ||
path = path.append(toTokenAddress); | ||
|
||
// Buy as much `toTokenAddress` token with `fromTokenAddress` token | ||
// and transfer it to `to`. | ||
IUniswapV2Router01 router = IUniswapV2Router01(_getUniswapV2Router01Address()); | ||
uint[] memory amounts = router.swapExactTokensForTokens( | ||
// Sell all tokens we hold. | ||
state.fromTokenBalance, | ||
// Minimum buy amount. | ||
amount, | ||
// Convert `fromTokenAddress` to `toTokenAddress`. | ||
path, | ||
// Recipient is `to`. | ||
to, | ||
// Expires after this block. | ||
block.timestamp | ||
); | ||
|
||
state.boughtAmount = amounts[1]; | ||
|
||
emit ERC20BridgeTransfer( | ||
// input token | ||
state.fromTokenAddress, | ||
// output token | ||
toTokenAddress, | ||
// input token amount | ||
state.fromTokenBalance, | ||
// output token amount | ||
state.boughtAmount, | ||
from, | ||
to | ||
); | ||
|
||
return BRIDGE_SUCCESS; | ||
} | ||
|
||
/// @dev `SignatureType.Wallet` callback, so that this bridge can be the maker | ||
/// and sign for itself in orders. Always succeeds. | ||
/// @return magicValue Success bytes, always. | ||
function isValidSignature( | ||
bytes32, | ||
bytes calldata | ||
) | ||
external | ||
view | ||
returns (bytes4 magicValue) | ||
{ | ||
return LEGACY_WALLET_MAGIC_VALUE; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
contracts/asset-proxy/contracts/src/interfaces/IUniswapV2Router01.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
Copyright 2020 ZeroEx Intl. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
pragma solidity ^0.5.9; | ||
|
||
|
||
interface IUniswapV2Router01 { | ||
|
||
/// @dev Swaps an exact amount of input tokens for as many output tokens as possible, along the route determined by the path. | ||
/// The first element of path is the input token, the last is the output token, and any intermediate elements represent | ||
/// intermediate pairs to trade through (if, for example, a direct pair does not exist). | ||
/// @param amountIn The amount of input tokens to send. | ||
/// @param amountOutMin The minimum amount of output tokens that must be received for the transaction not to revert. | ||
/// @param path An array of token addresses. path.length must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity. | ||
/// @param to Recipient of the output tokens. | ||
/// @param deadline Unix timestamp after which the transaction will revert. | ||
/// @return amounts The input token amount and all subsequent output token amounts. | ||
function swapExactTokensForTokens( | ||
uint amountIn, | ||
uint amountOutMin, | ||
address[] calldata path, | ||
address to, | ||
uint deadline | ||
) external returns (uint[] memory amounts); | ||
|
||
// /// @dev Receive an exact amount of output tokens for as few input tokens as possible, along the route determined by the path. | ||
// /// The first element of path is the input token, the last is the output token, and any intermediate elements represent | ||
// /// intermediate pairs to trade through (if, for example, a direct pair does not exist). | ||
// /// * msg.sender should have already given the router an allowance of at least amountInMax on the input token. | ||
// /// @param amountOut The amount of output tokens to receive. | ||
// /// @param amountInMax The maximum amount of input tokens that can be required before the transaction reverts. | ||
// /// @param path An array of token addresses. path.length must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity. | ||
// /// @param to Recipient of the output tokens. | ||
// /// @param deadline Unix timestamp after which the transaction will revert. | ||
// /// @return amounts The input token amount and all subsequent output token amounts. | ||
// function swapTokensForExactTokens( | ||
// uint amountOut, | ||
// uint amountInMax, | ||
// address[] calldata path, | ||
// address to, | ||
// uint deadline | ||
// ) external returns (uint[] memory amounts); | ||
|
||
// /// @dev Given some asset amount and reserves, returns an amount of the other asset representing equivalent value. | ||
// /// @param amountA The amount of assetA. | ||
// /// @param reserveA The reserves of assetA. Call `getReserves` on the UniswapV2Router01 contract for this value. | ||
// /// @param reserveB The reserves of assetB. Call `getReserves` on the UniswapV2Router01 contract for this value. | ||
// function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB); | ||
} |
Oops, something went wrong.