> ## Documentation Index
> Fetch the complete documentation index at: https://docs.soroswap.finance/llms.txt
> Use this file to discover all available pages before exploring further.

# SoroswapLibrary

> SoroswapLibrary crate: how to add it, and reference for its pricing and helper functions.

The SoroswapLibrary is a rust crate that anyone can implement in their smart contracts. Check all the documentation here: [https://docs.rs/soroswap-library/latest/soroswap\_library/](https://docs.rs/soroswap-library/latest/soroswap_library/)

## Code

You can find the Soroswap library code on the [Soroswap GitHub repository](https://github.com/soroswap/core/tree/main/contracts/library). in [https://github.com/soroswap/core/tree/main/contracts/library](https://github.com/soroswap/core/tree/main/contracts/library)

## Usage as a crate

1.- Add this to your Cargo.toml:

```toml theme={null}
[dependencies]
soroswap-library = "2.0.0"
```

Check [crates.io/crates/soroswap-library](https://crates.io/crates/soroswap-library) for the current version.

2.- Import it:

```
use soroswap_library;
```

3.- Use it:

```
let quote = soroswap_library::quote(amount_a, reserve_a, reserve_b)

```

## Public functions

Every function below is `pub` in the crate and returns a `Result`, with
`SoroswapLibraryError` as the error type. Signatures are from version 2.0.0.

### sort\_tokens

```rust theme={null}
pub fn sort_tokens(token_a: Address, token_b: Address)
    -> Result<(Address, Address), SoroswapLibraryError>;
```

Sorts two token addresses into the canonical order a pair uses. Errors if the two
addresses are identical.

### pair\_for

```rust theme={null}
pub fn pair_for(e: Env, factory: Address, token_a: Address, token_b: Address)
    -> Result<Address, SoroswapLibraryError>;
```

Returns the address of the pair for two tokens.

### get\_reserves\_with\_factory

```rust theme={null}
pub fn get_reserves_with_factory(e: Env, factory: Address, token_a: Address, token_b: Address)
    -> Result<(i128, i128), SoroswapLibraryError>;
```

Fetches the reserves for a pair, looking the pair up through the factory, and returns
them in the order the caller asked for rather than the pair's internal order.

### get\_reserves\_with\_pair

```rust theme={null}
pub fn get_reserves_with_pair(e: Env, pair: Address, token_a: Address, token_b: Address)
    -> Result<(i128, i128), SoroswapLibraryError>;
```

The same, for a pair address you already have. Saves the factory lookup.

### quote

```rust theme={null}
pub fn quote(amount_a: i128, reserve_a: i128, reserve_b: i128)
    -> Result<i128, SoroswapLibraryError>;
```

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

* Useful for calculating optimal token amounts before calling `deposit`.

### get\_amount\_out

```rust theme={null}
pub fn get_amount_out(amount_in: i128, reserve_in: i128, reserve_out: i128)
    -> Result<i128, SoroswapLibraryError>;
```

Given an input amount and the reserves, returns the maximum output amount of the other
asset, fees accounted for.

* Used in `get_amounts_out`.

### get\_amount\_in

```rust theme={null}
pub fn get_amount_in(amount_out: i128, reserve_in: i128, reserve_out: i128)
    -> Result<i128, SoroswapLibraryError>;
```

Returns the minimum input amount required to buy a given output amount, fees accounted
for.

* Used in `get_amounts_in`.

### get\_amounts\_out

```rust theme={null}
pub fn get_amounts_out(e: Env, factory: Address, amount_in: i128, path: Vec<Address>)
    -> Result<Vec<i128>, SoroswapLibraryError>;
```

Walks a path of token addresses, calling `get_reserves_with_factory` and then
`get_amount_out` for each hop, and returns every intermediate amount.

* Useful for calculating optimal token amounts before calling `swap`.

### get\_amounts\_in

```rust theme={null}
pub fn get_amounts_in(e: Env, factory: Address, amount_out: i128, path: Vec<Address>)
    -> Result<Vec<i128>, SoroswapLibraryError>;
```

The same walk in reverse, using `get_amount_in`, returning every preceding minimum
input amount.

* Useful for calculating optimal token amounts before calling `swap`.

This Soroswap library is designed to facilitate efficient and precise token swapping and handling in the Soroswap.Finance ecosystem. It includes a range of functions to support various aspects of token management, from sorting token addresses to calculating reserves and performing chained calculations for different pairs. These functions are crucial for the optimal functioning of Soroswap in the Stellar network.
