forked from VodkaFinance/sushiswap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
8 changed files
with
717 additions
and
0 deletions.
There are no files selected for viewing
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,95 @@ | ||
pragma solidity >=0.6.2; | ||
|
||
interface IUniswapV2Router01 { | ||
function factory() external pure returns (address); | ||
function WETH() external pure returns (address); | ||
|
||
function addLiquidity( | ||
address tokenA, | ||
address tokenB, | ||
uint amountADesired, | ||
uint amountBDesired, | ||
uint amountAMin, | ||
uint amountBMin, | ||
address to, | ||
uint deadline | ||
) external returns (uint amountA, uint amountB, uint liquidity); | ||
function addLiquidityETH( | ||
address token, | ||
uint amountTokenDesired, | ||
uint amountTokenMin, | ||
uint amountETHMin, | ||
address to, | ||
uint deadline | ||
) external payable returns (uint amountToken, uint amountETH, uint liquidity); | ||
function removeLiquidity( | ||
address tokenA, | ||
address tokenB, | ||
uint liquidity, | ||
uint amountAMin, | ||
uint amountBMin, | ||
address to, | ||
uint deadline | ||
) external returns (uint amountA, uint amountB); | ||
function removeLiquidityETH( | ||
address token, | ||
uint liquidity, | ||
uint amountTokenMin, | ||
uint amountETHMin, | ||
address to, | ||
uint deadline | ||
) external returns (uint amountToken, uint amountETH); | ||
function removeLiquidityWithPermit( | ||
address tokenA, | ||
address tokenB, | ||
uint liquidity, | ||
uint amountAMin, | ||
uint amountBMin, | ||
address to, | ||
uint deadline, | ||
bool approveMax, uint8 v, bytes32 r, bytes32 s | ||
) external returns (uint amountA, uint amountB); | ||
function removeLiquidityETHWithPermit( | ||
address token, | ||
uint liquidity, | ||
uint amountTokenMin, | ||
uint amountETHMin, | ||
address to, | ||
uint deadline, | ||
bool approveMax, uint8 v, bytes32 r, bytes32 s | ||
) external returns (uint amountToken, uint amountETH); | ||
function swapExactTokensForTokens( | ||
uint amountIn, | ||
uint amountOutMin, | ||
address[] calldata path, | ||
address to, | ||
uint deadline | ||
) external returns (uint[] memory amounts); | ||
function swapTokensForExactTokens( | ||
uint amountOut, | ||
uint amountInMax, | ||
address[] calldata path, | ||
address to, | ||
uint deadline | ||
) external returns (uint[] memory amounts); | ||
function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline) | ||
external | ||
payable | ||
returns (uint[] memory amounts); | ||
function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline) | ||
external | ||
returns (uint[] memory amounts); | ||
function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) | ||
external | ||
returns (uint[] memory amounts); | ||
function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline) | ||
external | ||
payable | ||
returns (uint[] memory amounts); | ||
|
||
function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB); | ||
function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) external pure returns (uint amountOut); | ||
function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) external pure returns (uint amountIn); | ||
function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts); | ||
function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts); | ||
} |
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,44 @@ | ||
pragma solidity >=0.6.2; | ||
|
||
import './IUniswapV2Router01.sol'; | ||
|
||
interface IUniswapV2Router02 is IUniswapV2Router01 { | ||
function removeLiquidityETHSupportingFeeOnTransferTokens( | ||
address token, | ||
uint liquidity, | ||
uint amountTokenMin, | ||
uint amountETHMin, | ||
address to, | ||
uint deadline | ||
) external returns (uint amountETH); | ||
function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens( | ||
address token, | ||
uint liquidity, | ||
uint amountTokenMin, | ||
uint amountETHMin, | ||
address to, | ||
uint deadline, | ||
bool approveMax, uint8 v, bytes32 r, bytes32 s | ||
) external returns (uint amountETH); | ||
|
||
function swapExactTokensForTokensSupportingFeeOnTransferTokens( | ||
uint amountIn, | ||
uint amountOutMin, | ||
address[] calldata path, | ||
address to, | ||
uint deadline | ||
) external; | ||
function swapExactETHForTokensSupportingFeeOnTransferTokens( | ||
uint amountOutMin, | ||
address[] calldata path, | ||
address to, | ||
uint deadline | ||
) external payable; | ||
function swapExactTokensForETHSupportingFeeOnTransferTokens( | ||
uint amountIn, | ||
uint amountOutMin, | ||
address[] calldata path, | ||
address to, | ||
uint deadline | ||
) external; | ||
} |
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,7 @@ | ||
pragma solidity >=0.5.0; | ||
|
||
interface IWETH { | ||
function deposit() external payable; | ||
function transfer(address to, uint value) external returns (bool); | ||
function withdraw(uint) external; | ||
} |
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,29 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
pragma solidity >=0.6.0; | ||
|
||
// helper methods for interacting with ERC20 tokens and sending ETH that do not consistently return true/false | ||
library TransferHelper { | ||
function safeApprove(address token, address to, uint value) internal { | ||
// bytes4(keccak256(bytes('approve(address,uint256)'))); | ||
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x095ea7b3, to, value)); | ||
require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: APPROVE_FAILED'); | ||
} | ||
|
||
function safeTransfer(address token, address to, uint value) internal { | ||
// bytes4(keccak256(bytes('transfer(address,uint256)'))); | ||
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0xa9059cbb, to, value)); | ||
require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: TRANSFER_FAILED'); | ||
} | ||
|
||
function safeTransferFrom(address token, address from, address to, uint value) internal { | ||
// bytes4(keccak256(bytes('transferFrom(address,address,uint256)'))); | ||
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x23b872dd, from, to, value)); | ||
require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: TRANSFER_FROM_FAILED'); | ||
} | ||
|
||
function safeTransferETH(address to, uint value) internal { | ||
(bool success,) = to.call{value:value}(new bytes(0)); | ||
require(success, 'TransferHelper: ETH_TRANSFER_FAILED'); | ||
} | ||
} |
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,82 @@ | ||
pragma solidity >=0.5.0; | ||
|
||
import '../interfaces/IUniswapV2Pair.sol'; | ||
|
||
import "./SafeMath.sol"; | ||
|
||
library UniswapV2Library { | ||
using SafeMathUniswap for uint; | ||
|
||
// returns sorted token addresses, used to handle return values from pairs sorted in this order | ||
function sortTokens(address tokenA, address tokenB) internal pure returns (address token0, address token1) { | ||
require(tokenA != tokenB, 'UniswapV2Library: IDENTICAL_ADDRESSES'); | ||
(token0, token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA); | ||
require(token0 != address(0), 'UniswapV2Library: ZERO_ADDRESS'); | ||
} | ||
|
||
// calculates the CREATE2 address for a pair without making any external calls | ||
function pairFor(address factory, address tokenA, address tokenB) internal pure returns (address pair) { | ||
(address token0, address token1) = sortTokens(tokenA, tokenB); | ||
pair = address(uint(keccak256(abi.encodePacked( | ||
hex'ff', | ||
factory, | ||
keccak256(abi.encodePacked(token0, token1)), | ||
hex'e18a34eb0e04b04f7a0ac29a6e80748dca96319b42c54d679cb821dca90c6303' // init code hash | ||
)))); | ||
} | ||
|
||
// fetches and sorts the reserves for a pair | ||
function getReserves(address factory, address tokenA, address tokenB) internal view returns (uint reserveA, uint reserveB) { | ||
(address token0,) = sortTokens(tokenA, tokenB); | ||
(uint reserve0, uint reserve1,) = IUniswapV2Pair(pairFor(factory, tokenA, tokenB)).getReserves(); | ||
(reserveA, reserveB) = tokenA == token0 ? (reserve0, reserve1) : (reserve1, reserve0); | ||
} | ||
|
||
// given some amount of an asset and pair reserves, returns an equivalent amount of the other asset | ||
function quote(uint amountA, uint reserveA, uint reserveB) internal pure returns (uint amountB) { | ||
require(amountA > 0, 'UniswapV2Library: INSUFFICIENT_AMOUNT'); | ||
require(reserveA > 0 && reserveB > 0, 'UniswapV2Library: INSUFFICIENT_LIQUIDITY'); | ||
amountB = amountA.mul(reserveB) / reserveA; | ||
} | ||
|
||
// given an input amount of an asset and pair reserves, returns the maximum output amount of the other asset | ||
function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) internal pure returns (uint amountOut) { | ||
require(amountIn > 0, 'UniswapV2Library: INSUFFICIENT_INPUT_AMOUNT'); | ||
require(reserveIn > 0 && reserveOut > 0, 'UniswapV2Library: INSUFFICIENT_LIQUIDITY'); | ||
uint amountInWithFee = amountIn.mul(997); | ||
uint numerator = amountInWithFee.mul(reserveOut); | ||
uint denominator = reserveIn.mul(1000).add(amountInWithFee); | ||
amountOut = numerator / denominator; | ||
} | ||
|
||
// given an output amount of an asset and pair reserves, returns a required input amount of the other asset | ||
function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) internal pure returns (uint amountIn) { | ||
require(amountOut > 0, 'UniswapV2Library: INSUFFICIENT_OUTPUT_AMOUNT'); | ||
require(reserveIn > 0 && reserveOut > 0, 'UniswapV2Library: INSUFFICIENT_LIQUIDITY'); | ||
uint numerator = reserveIn.mul(amountOut).mul(1000); | ||
uint denominator = reserveOut.sub(amountOut).mul(997); | ||
amountIn = (numerator / denominator).add(1); | ||
} | ||
|
||
// performs chained getAmountOut calculations on any number of pairs | ||
function getAmountsOut(address factory, uint amountIn, address[] memory path) internal view returns (uint[] memory amounts) { | ||
require(path.length >= 2, 'UniswapV2Library: INVALID_PATH'); | ||
amounts = new uint[](path.length); | ||
amounts[0] = amountIn; | ||
for (uint i; i < path.length - 1; i++) { | ||
(uint reserveIn, uint reserveOut) = getReserves(factory, path[i], path[i + 1]); | ||
amounts[i + 1] = getAmountOut(amounts[i], reserveIn, reserveOut); | ||
} | ||
} | ||
|
||
// performs chained getAmountIn calculations on any number of pairs | ||
function getAmountsIn(address factory, uint amountOut, address[] memory path) internal view returns (uint[] memory amounts) { | ||
require(path.length >= 2, 'UniswapV2Library: INVALID_PATH'); | ||
amounts = new uint[](path.length); | ||
amounts[amounts.length - 1] = amountOut; | ||
for (uint i = path.length - 1; i > 0; i--) { | ||
(uint reserveIn, uint reserveOut) = getReserves(factory, path[i - 1], path[i]); | ||
amounts[i - 1] = getAmountIn(amounts[i], reserveIn, reserveOut); | ||
} | ||
} | ||
} |