pragma solidity >=0.5.0; import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol"; import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol"; import "./SafeMath.sol"; library UniswapV2Library { using Math for uint; error IdenticalAddresses(); error ZeroAddress(); error InsufficientAmount(); error InsufficientLiquidity(); error InsufficientInputAmount(); error InsufficientOutputAmount(); error InvalidPath(); // 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) { if (tokenA == tokenB) { revert IdenticalAddresses(); } (token0, token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA); if (token0 == address(0)) { revert ZeroAddress(); } } function pairFor( address factory, address tokenA, address tokenB ) internal view returns (address pair) { pair = IUniswapV2Factory(factory).getPair(tokenA, tokenB); } // 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) { if (amountA == 0) { revert InsufficientAmount(); } if (reserveA == 0 || reserveB == 0) { revert InsufficientLiquidity(); } 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) { if (amountIn == 0) { revert InsufficientInputAmount(); } if (reserveIn == 0 || reserveOut == 0) { revert InsufficientLiquidity(); } 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) { if (amountOut == 0) { revert InsufficientOutputAmount(); } if (reserveIn == 0 || reserveOut == 0) { revert InsufficientLiquidity(); } 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) { if (path.length < 2) { revert InvalidPath(); } 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) { if (path.length < 2) { revert InvalidPath(); } 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); } } }