Soroswap.Finance Docs
  • Welcome 👋🏼
    • What is Soroswap Finance?
    • Getting Started
      • Wallet Setup and Connection
      • How to Swap
      • Provide Liquidity
      • How the Aggregator Works
  • Concepts
    • AMM
    • Liquidity Pools
    • Swap
    • Fees
    • Slippage
    • Router
    • SDEX
    • Aggregator
    • Trustlines
    • Advanced Topics
      • Pricing
      • Understanding Returns
      • Security
      • Research
  • Soroswap AMM (DEX)
    • How Soroswap AMM works
    • Ecosystem Participants
    • Audits
    • Technical Reference
      • Smart contracts
        • SoroswapPair
        • SoroswapFactory
        • SoroswapRouter
        • SoroswapLibrary
      • Deployed Addresses
      • Error Codes
      • Using Soroswap with TypeScript
      • Smart Contract Integration
      • Deploy Soroswap Yourself
        • Setup your environment.
        • Experiment the Pair contract
        • Experiment the Factory Contract.
        • Deployments.
        • Using the Soroswap Testnet
    • Glossary
  • Soroswap Aggregator
    • Supported AMMs
    • Audits
    • Technical Reference
      • How Soroswap Aggregator works
      • Design
      • Technical Overview
      • Aggregator Operation
      • Smart Contracts
        • SoroswapAggregator
        • Adapter Trait
        • SoroswapAdapter
      • Inspirations
        • 1inch
      • Other AMMs in Soroban
        • Phoenix
    • Disclaimer
  • Swap Route API
  • Soroswap Info
  • Tutorials
    • Installing Freighter
    • Soroswap sections
    • Adding Liquidity
    • Doing Swap
    • Remove Liquidity
    • Using Stellar Classic Assets
      • Wrapping Stellar Classic Assets
      • Swap Stellar Classic Assets
      • Test Stellar Classic Assets
    • Bridge using Pendulum
    • Conclusions
  • Partnerships
    • Collaboration with Mercury and SubQuery
    • Business Partnerships
  • Support & Resources
    • About Us
    • General FAQ
    • Additional Resources
Powered by GitBook
On this page
  • Code
  • Usage as a crate
  • Internal Functions
Edit on GitHub
  1. Soroswap AMM (DEX)
  2. Technical Reference
  3. Smart contracts

SoroswapLibrary

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:

[dependencies] soroswap-library = ""

2.- Import it:

use soroswap_library;

3.- Use it:

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

Internal Functions

sort_tokens

fn sort_tokens(token_a: Address, token_b: Address) -> (Address, Address);

Sorts token addresses.

pair_for

fn pair_for(e: Env, factory: Address, token_a: Address, token_b: Address) -> Address;

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

get_reserves

fn get_reserves(e: Env, factory: Address, token_a: Address, token_b: Address) -> (i128, i128);

Fetches and sorts the reserves for a pair.

quote

fn quote(amount_a: i128, reserve_a: i128, reserve_b: i128) -> i128;

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

  • Useful for calculating optimal token amounts before calling deposit.

get_amount_out

fn get_amount_out(amount_in: i128, reserve_in: i128, reserve_out: i128) -> i128;

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

  • Used in get_amounts_out.

get_amount_in

fn get_amount_in(amount_out: i128, reserve_in: i128, reserve_out: i128) -> i128;

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

  • Used in get_amounts_in.

get_amounts_out

fn get_amounts_out(e: Env, factory: Address, amount_in: i128, path: Vec<Address>) -> Vec<i128>;

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

  • Useful for calculating optimal token amounts before calling swap.

get_amounts_in

fn get_amounts_in(e: Env, factory: Address, amount_out: i128, path: Vec<Address>) -> Vec<i128>;

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

  • 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.

PreviousSoroswapRouterNextDeployed Addresses

Last updated 8 months ago

Page cover image