Library

Code

UniswapV2Library.sol

Read-Only Functions

factory

function factory() external pure returns (address);

Returns the factory address.

sortTokens

function sortTokens(address tokenA, address tokenB) internal pure returns (address token0, address token1);

Sorts token addresses.

pairFor

function pairFor(address tokenA, address tokenB) internal pure returns (address pair);

Calculates the address for a pair without making any external calls (see Pair Addresses).

getReserves

function getReserves(address tokenA, address tokenB) internal view returns (uint reserveA, uint reserveB);

Calls getReserves on the pair for the passed tokens, and returns the results sorted in the order that the parameters were passed in.

quote

function quote(uint amountA, uint reserveA, uint reserveB) public pure returns (uint amountB);

Given some asset amount and reserves, returns an amount of the other asset representing equivalent value.

  • Useful for calculating optimal token amounts before calling mint.

getAmountOut

function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) public pure returns (uint amountOut);

Given an input asset amount, returns the maximum output amount of the other asset (accounting for fees) given reserves.

getAmountIn

function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) public pure returns (uint amountIn)'

Returns the minimum input asset amount required to buy the given output asset amount (accounting for fees) given reserves.

getAmountsOut

function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts)'

Given an input asset amount and an array of token addresses, calculates all subsequent maximum output token amounts by calling getReserves for each pair of token addresses in the path in turn, and using these to call getAmountOut.

  • Useful for calculating optimal token amounts before calling swap.

getAmountsIn

function getAmountsIn(uint amountOut, address[] memory path) public view returns (uint[] memory amounts);

Given an output asset amount and an array of token addresses, calculates all preceding minimum input token amounts by calling getReserves for each pair of token addresses in the path in turn, and using these to call getAmountIn.

  • Useful for calculating optimal token amounts before calling swap.

Interface

interface IUniswapV2Library {
function factory() external pure returns (address);

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);
}

ABI

[
{
"constant": true,
"inputs": [],
"name": "factory",
"outputs": [{ "internalType": "address", "name": "", "type": "address" }],
"payable": false,
"stateMutability": "pure",
"type": "function"
},
{
"constant": true,
"inputs": [
{ "internalType": "uint256", "name": "amountOut", "type": "uint256" },
{ "internalType": "uint256", "name": "reserveIn", "type": "uint256" },
{ "internalType": "uint256", "name": "reserveOut", "type": "uint256" }
],
"name": "getAmountIn",
"outputs": [{ "internalType": "uint256", "name": "amountIn", "type": "uint256" }],
"payable": false,
"stateMutability": "pure",
"type": "function"
},
{
"constant": true,
"inputs": [
{ "internalType": "uint256", "name": "amountIn", "type": "uint256" },
{ "internalType": "uint256", "name": "reserveIn", "type": "uint256" },
{ "internalType": "uint256", "name": "reserveOut", "type": "uint256" }
],
"name": "getAmountOut",
"outputs": [{ "internalType": "uint256", "name": "amountOut", "type": "uint256" }],
"payable": false,
"stateMutability": "pure",
"type": "function"
},
{
"constant": true,
"inputs": [
{ "internalType": "uint256", "name": "amountOut", "type": "uint256" },
{ "internalType": "address[]", "name": "path", "type": "address[]" }
],
"name": "getAmountsIn",
"outputs": [{ "internalType": "uint256[]", "name": "amounts", "type": "uint256[]" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{ "internalType": "uint256", "name": "amountIn", "type": "uint256" },
{ "internalType": "address[]", "name": "path", "type": "address[]" }
],
"name": "getAmountsOut",
"outputs": [{ "internalType": "uint256[]", "name": "amounts", "type": "uint256[]" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{ "internalType": "uint256", "name": "amountA", "type": "uint256" },
{ "internalType": "uint256", "name": "reserveA", "type": "uint256" },
{ "internalType": "uint256", "name": "reserveB", "type": "uint256" }
],
"name": "quote",
"outputs": [{ "internalType": "uint256", "name": "amountB", "type": "uint256" }],
"payable": false,
"stateMutability": "pure",
"type": "function"
}
]

This site is a work in progress. Found an issue?