Skip to main content
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/

Code

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

Usage as a crate

1.- Add this to your Cargo.toml:
Check crates.io/crates/soroswap-library for the current version. 2.- Import it:
3.- Use it:

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

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

pair_for

Returns the address of the pair for two tokens.

get_reserves_with_factory

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

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

quote

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

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

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

get_amounts_out

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

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.