github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/test/contracts/uniswap/v2/UniswapV2Library.sol (about) 1 pragma solidity >=0.5.0; 2 3 import './IUniswapV2Pair.sol'; 4 import './IUniswapV2Factory.sol'; 5 6 import "./SafeMath.sol"; 7 8 library UniswapV2Library { 9 using SafeMath for uint; 10 11 // returns sorted token addresses, used to handle return values from pairs sorted in this order 12 function sortTokens(address tokenA, address tokenB) internal pure returns (address token0, address token1) { 13 require(tokenA != tokenB, 'UniswapV2Library: IDENTICAL_ADDRESSES'); 14 (token0, token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA); 15 require(token0 != address(0), 'UniswapV2Library: ZERO_ADDRESS'); 16 } 17 18 // calculates the CREATE2 address for a pair without making any external calls 19 function pairFor(address factory, address tokenA, address tokenB) internal view returns (address pair) { 20 pair = IUniswapV2Factory(factory).getPair(tokenA,tokenB); 21 } 22 23 // fetches and sorts the reserves for a pair 24 function getReserves(address factory, address tokenA, address tokenB) internal view returns (uint reserveA, uint reserveB) { 25 (address token0,) = sortTokens(tokenA, tokenB); 26 (uint reserve0, uint reserve1,) = IUniswapV2Pair(pairFor(factory, tokenA, tokenB)).getReserves(); 27 (reserveA, reserveB) = tokenA == token0 ? (reserve0, reserve1) : (reserve1, reserve0); 28 } 29 30 // given some amount of an asset and pair reserves, returns an equivalent amount of the other asset 31 function quote(uint amountA, uint reserveA, uint reserveB) internal pure returns (uint amountB) { 32 require(amountA > 0, 'UniswapV2Library: INSUFFICIENT_AMOUNT'); 33 require(reserveA > 0 && reserveB > 0, 'UniswapV2Library: INSUFFICIENT_LIQUIDITY'); 34 amountB = amountA.mul(reserveB) / reserveA; 35 } 36 37 // given an input amount of an asset and pair reserves, returns the maximum output amount of the other asset 38 function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) internal pure returns (uint amountOut) { 39 require(amountIn > 0, 'UniswapV2Library: INSUFFICIENT_INPUT_AMOUNT'); 40 require(reserveIn > 0 && reserveOut > 0, 'UniswapV2Library: INSUFFICIENT_LIQUIDITY'); 41 uint amountInWithFee = amountIn.mul(997); 42 uint numerator = amountInWithFee.mul(reserveOut); 43 uint denominator = reserveIn.mul(1000).add(amountInWithFee); 44 amountOut = numerator / denominator; 45 } 46 47 // given an output amount of an asset and pair reserves, returns a required input amount of the other asset 48 function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) internal pure returns (uint amountIn) { 49 require(amountOut > 0, 'UniswapV2Library: INSUFFICIENT_OUTPUT_AMOUNT'); 50 require(reserveIn > 0 && reserveOut > 0, 'UniswapV2Library: INSUFFICIENT_LIQUIDITY'); 51 uint numerator = reserveIn.mul(amountOut).mul(1000); 52 uint denominator = reserveOut.sub(amountOut).mul(997); 53 amountIn = (numerator / denominator).add(1); 54 } 55 56 // performs chained getAmountOut calculations on any number of pairs 57 function getAmountsOut(address factory, uint amountIn, address[] memory path) internal view returns (uint[] memory amounts) { 58 require(path.length >= 2, 'UniswapV2Library: INVALID_PATH'); 59 amounts = new uint[](path.length); 60 amounts[0] = amountIn; 61 for (uint i; i < path.length - 1; i++) { 62 (uint reserveIn, uint reserveOut) = getReserves(factory, path[i], path[i + 1]); 63 amounts[i + 1] = getAmountOut(amounts[i], reserveIn, reserveOut); 64 } 65 } 66 67 // performs chained getAmountIn calculations on any number of pairs 68 function getAmountsIn(address factory, uint amountOut, address[] memory path) internal view returns (uint[] memory amounts) { 69 require(path.length >= 2, 'UniswapV2Library: INVALID_PATH'); 70 amounts = new uint[](path.length); 71 amounts[amounts.length - 1] = amountOut; 72 for (uint i = path.length - 1; i > 0; i--) { 73 (uint reserveIn, uint reserveOut) = getReserves(factory, path[i - 1], path[i]); 74 amounts[i - 1] = getAmountIn(amounts[i], reserveIn, reserveOut); 75 } 76 } 77 }