feat: expand non-evm relay and route planning support
This commit is contained in:
25
contracts/vendor/sushiswap-v2/libraries/Math.sol
vendored
Normal file
25
contracts/vendor/sushiswap-v2/libraries/Math.sol
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
// SPDX-License-Identifier: GPL-3.0
|
||||
|
||||
pragma solidity =0.6.12;
|
||||
|
||||
// a library for performing various math operations
|
||||
|
||||
library Math {
|
||||
function min(uint x, uint y) internal pure returns (uint z) {
|
||||
z = x < y ? x : y;
|
||||
}
|
||||
|
||||
// babylonian method (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method)
|
||||
function sqrt(uint y) internal pure returns (uint z) {
|
||||
if (y > 3) {
|
||||
z = y;
|
||||
uint x = y / 2 + 1;
|
||||
while (x < z) {
|
||||
z = x;
|
||||
x = (y / x + x) / 2;
|
||||
}
|
||||
} else if (y != 0) {
|
||||
z = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
19
contracts/vendor/sushiswap-v2/libraries/SafeMath.sol
vendored
Normal file
19
contracts/vendor/sushiswap-v2/libraries/SafeMath.sol
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
// SPDX-License-Identifier: GPL-3.0
|
||||
|
||||
pragma solidity =0.6.12;
|
||||
|
||||
// a library for performing overflow-safe math, courtesy of DappHub (https://github.com/dapphub/ds-math)
|
||||
|
||||
library SafeMathUniswap {
|
||||
function add(uint x, uint y) internal pure returns (uint z) {
|
||||
require((z = x + y) >= x, 'ds-math-add-overflow');
|
||||
}
|
||||
|
||||
function sub(uint x, uint y) internal pure returns (uint z) {
|
||||
require((z = x - y) <= x, 'ds-math-sub-underflow');
|
||||
}
|
||||
|
||||
function mul(uint x, uint y) internal pure returns (uint z) {
|
||||
require(y == 0 || (z = x * y) / y == x, 'ds-math-mul-overflow');
|
||||
}
|
||||
}
|
||||
29
contracts/vendor/sushiswap-v2/libraries/TransferHelper.sol
vendored
Normal file
29
contracts/vendor/sushiswap-v2/libraries/TransferHelper.sol
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
// SPDX-License-Identifier: GPL-3.0
|
||||
|
||||
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');
|
||||
}
|
||||
}
|
||||
22
contracts/vendor/sushiswap-v2/libraries/UQ112x112.sol
vendored
Normal file
22
contracts/vendor/sushiswap-v2/libraries/UQ112x112.sol
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
// SPDX-License-Identifier: GPL-3.0
|
||||
|
||||
pragma solidity =0.6.12;
|
||||
|
||||
// a library for handling binary fixed point numbers (https://en.wikipedia.org/wiki/Q_(number_format))
|
||||
|
||||
// range: [0, 2**112 - 1]
|
||||
// resolution: 1 / 2**112
|
||||
|
||||
library UQ112x112 {
|
||||
uint224 constant Q112 = 2**112;
|
||||
|
||||
// encode a uint112 as a UQ112x112
|
||||
function encode(uint112 y) internal pure returns (uint224 z) {
|
||||
z = uint224(y) * Q112; // never overflows
|
||||
}
|
||||
|
||||
// divide a UQ112x112 by a uint112, returning a UQ112x112
|
||||
function uqdiv(uint224 x, uint112 y) internal pure returns (uint224 z) {
|
||||
z = x / uint224(y);
|
||||
}
|
||||
}
|
||||
84
contracts/vendor/sushiswap-v2/libraries/UniswapV2Library.sol
vendored
Normal file
84
contracts/vendor/sushiswap-v2/libraries/UniswapV2Library.sol
vendored
Normal file
@@ -0,0 +1,84 @@
|
||||
// SPDX-License-Identifier: GPL-3.0
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user