# Library

### Code[​](https://docs.uniswap.org/contracts/v2/reference/smart-contracts/library#code) <a href="#code" id="code"></a>

[`DragonswapLibrary.sol`](https://github.com/dragonswap-app/dragonswap-core/blob/master/contracts/libraries/DragonswapLibrary.sol)

## Internal Functions

### sortTokens[​](https://docs.uniswap.org/contracts/v2/reference/smart-contracts/library#sorttokens) <a href="#sorttokens" id="sorttokens"></a>

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

Sorts token addresses.

### pairFor[​](https://docs.uniswap.org/contracts/v2/reference/smart-contracts/library#pairfor) <a href="#pairfor" id="pairfor"></a>

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

Calculates the address for a pair without making any external calls via the dragonswap SDK.

### getReserves[​](https://docs.uniswap.org/contracts/v2/reference/smart-contracts/library#getreserves) <a href="#getreserves" id="getreserves"></a>

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

Calls [getReserves](https://docs.dragonswap.app/dragonswap/resources/developer-resources/smart-contracts/pair#getreserves) on the pair for the passed tokens, and returns the results sorted in the order that the parameters were passed in.

### quote[​](https://docs.uniswap.org/contracts/v2/reference/smart-contracts/library#quote) <a href="#quote" id="quote"></a>

```solidity
function quote(uint amountA, uint reserveA, uint reserveB) internal 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](https://docs.dragonswap.app/dragonswap/resources/developer-resources/smart-contracts/pair#mint-1).

### getAmountOut[​](https://docs.uniswap.org/contracts/v2/reference/smart-contracts/library#getamountout) <a href="#getamountout" id="getamountout"></a>

```solidity
function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) internal pure returns (uint amountOut);
```

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

* Used in [getAmountsOut](#getamountsout).

### getAmountIn[​](https://docs.uniswap.org/contracts/v2/reference/smart-contracts/library#getamountin) <a href="#getamountin" id="getamountin"></a>

```solidity
function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) internal pure returns (uint amountIn);
```

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

* Used in [getAmountsIn](#getamountsin).

### getAmountsOut[​](https://docs.uniswap.org/contracts/v2/reference/smart-contracts/library#getamountsout) <a href="#getamountsout" id="getamountsout"></a>

```solidity
function getAmountsOut(uint amountIn, address[] memory path) internal 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](#getreserves) for each pair of token addresses in the path in turn, and using these to call [getAmountOut](#getamountout).

* Useful for calculating optimal token amounts before calling [swap](https://docs.dragonswap.app/dragonswap/resources/developer-resources/smart-contracts/pair#swap-1).

### getAmountsIn[​](https://docs.uniswap.org/contracts/v2/reference/smart-contracts/library#getamountsin) <a href="#getamountsin" id="getamountsin"></a>

```solidity
function getAmountsIn(address factory, uint amountOut, address[] memory path) internal 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](#getreserves) for each pair of token addresses in the path in turn, and using these to call [getAmountIn](#getamountin).

* Useful for calculating optimal token amounts before calling [swap](https://docs.dragonswap.app/dragonswap/resources/developer-resources/smart-contracts/pair#swap-1).
