# NostraSwapLibrary

## Code <a href="#code" id="code"></a>

//add code link

## Internal Functions <a href="#internal-functions" id="internal-functions"></a>

### sortTokens <a href="#sorttokens" id="sorttokens"></a>

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

Sorts token addresses.

### pairFor <a href="#pairfor" id="pairfor"></a>

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

Calculates the address for a pair without making any external calls (see [Pair Addresses](https://uniswap.org/docs/v2/javascript-SDK/getting-pair-addresses/)).

### getReserves <a href="#getreserves" id="getreserves"></a>

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

Calls [getReserves](https://uniswap.org/docs/v2/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 <a href="#quote" id="quote"></a>

```
function quote(uint amountA, uint reserveA, uint reserveB) internal pure returns (uint amountB);Copy
```

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://uniswap.org/docs/v2/smart-contracts/pair#mint-1).

### getAmountOut <a href="#getamountout" id="getamountout"></a>

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

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

* Used in [getAmountsOut](https://uniswap.org/docs/v2/smart-contracts/library/#getamountsout).

### getAmountIn <a href="#getamountin" id="getamountin"></a>

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

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

* Used in [getAmountsIn](https://uniswap.org/docs/v2/smart-contracts/library/#getamountsin).

### getAmountsOut <a href="#getamountsout" id="getamountsout"></a>

```
function getAmountsOut(uint amountIn, address[] memory path) internal view returns (uint[] memory amounts);Copy
```

Given an *input* asset amount and an array of token addresses, calculates all subsequent maximum *output* token amounts by calling [getReserves](https://uniswap.org/docs/v2/smart-contracts/library/#getreserves) for each pair of token addresses in the path in turn, and using these to call [getAmountOut](https://uniswap.org/docs/v2/smart-contracts/library/#getamountout).

* Useful for calculating optimal token amounts before calling [swap](https://uniswap.org/docs/v2/smart-contracts/pair#swap-1).

### getAmountsIn <a href="#getamountsin" id="getamountsin"></a>

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

Given an *output* asset amount and an array of token addresses, calculates all preceding minimum *input* token amounts by calling [getReserves](https://uniswap.org/docs/v2/smart-contracts/library/#getreserves) for each pair of token addresses in the path in turn, and using these to call [getAmountIn](https://uniswap.org/docs/v2/smart-contracts/library/#getamountin).

* Useful for calculating optimal token amounts before calling [swap](https://uniswap.org/docs/v2/smart-contracts/pair#swap-1).
