Bunicorn
Launch Testnet App
  • What is Bunicorn?
  • Introduction
    • AMM DEX
    • Liquidity Mining
    • NFT Collectibles
  • Getting Started
    • Wallets that Bunicorn supports
    • Get BEP20 Tokens
    • Connect your wallet
    • Bunicorn testnet 101
  • Bunicorn Exchange TUTORIAL
    • Tutorial for Trader
    • Tutorial for Liquidity Provider
      • Setup Proxy
      • Swap native BNB to WBNB to farm
      • How to Add Liquidity on Bunicorn
        • How to add liquidity to a Flexible pool?
        • How to add liquidity to a Stable Pool?
      • How to create a new Flexible pool?
        • How to Create a New Smart Pool
        • How to Create a New Shared Pool
      • How to Remove Liquidity
    • How to Join Token Play Farms
      • Stake LPs to Token Play Farm
      • Stake Single Asset $TOP
    • How to Join Ancient Buni NFT Farms
      • NFT Farm Ends. What should I do?
      • Stake LPs to Join the Ancient Buni Farm
      • Stake Single Asset
      • Harvest NFTs (Ancient Buni)
      • How to transfer Ancient BUNI NFTs to another address?
    • How to Join Bunicorn NFT Farms
      • Prestaking Farm Ends. What's next?
      • Stake LP to Join NFT Farms
      • Stake Single Asset
      • Harvest NFTs (Chest/Egg)
      • How to unlock ingame items from Chest/Egg NFTs
      • How to Transfer Egg/Chest NFTs to another address
    • Fee Structure
  • FAQ
    • What is a Flexible Pool?
    • What is a Stable Pool?
    • Can users create Stable pools themselves?
    • Are there any constraints for setting up a Bunicorn Flexible pool?
    • What are the advantages of a Stable Pool?
  • Glossary
  • SMART CONTRACTS
    • Flexible Pools
      • Exchange Proxy
      • Smart Order Router
      • On Chain Registry
      • Smart Pools
        • Overview
        • Configurable Rights Pool
        • Component Libraries
          • Rights Manager
      • Interfaces
      • Addresses
      • Events
      • API Index
    • Stable Pools
      • Pool Addresses
      • Swap Execution
      • Providing Liquidity
      • Flash Swaps
      • Contract Addresses
      • BuniCornRouter02
      • Pool
      • Pool (ERC-20)
      • Library
  • Subgraph API
    • Stable Pools
      • Entities
      • Sample queries
    • Flexible Pools
      • Entities
      • Sample queries
Powered by GitBook
On this page
  • Code
  • Constants
  • Internal Functions
  • sortTokens
  • getTradeInfo
  • getReserves
  • quote
  • getAmountOut
  • getAmountIn
  • getAmountsOut
  • getAmountsIn

Was this helpful?

  1. SMART CONTRACTS
  2. Stable Pools

Library

PreviousPool (ERC-20)NextStable Pools

Last updated 3 years ago

Was this helpful?

Code

Constants

PRECISION = 1e18

Internal Functions

sortTokens

function sortTokens(IERC20 tokenA, IERC20 tokenB) internal pure returns (IERC20 token0, IERC20 token1);

Sorts token addresses.

getTradeInfo

function getTradeInfo(address pool, IERC20 tokenA, IERC20 tokenB) internal view returns ( uint256 reserveA, uint256 reserveB, uint256 vReserveA, uint256 vReserveB, uint256 feeInPrecision)

Calls getTradeInfo on the pool for the passed tokens, and returns the actual and virtual reserves of the pool, and the dynamic fee to be charged.

getReserves

function getReserves(address pool, IERC20 tokenA, IERC20 tokenB) internal view returns (uint256 reserveA, uint256 reserveB);

Calls getReserves on the pool for the passed tokens, and returns the results sorted in the order that the parameters were passed in.

quote

function quote(uint256 amountA, uint256 reserveA, uint256 reserveB) internal pure returns (uint256 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.

getAmountOut

function getAmountOut(uint256 amountIn, uint256 reserveIn, uint256 reserveOut, uint256 vReserveIn, uint256 vReserveOut, uint256 feeInPrecision) internal pure returns (uint256 amountOut);

Given an input asset amount and pool reserves (both actual and virtual balances), returns the maximum output amount of the other asset (accounting for fees).

  • Used in getAmountsOut.

getAmountIn

function getAmountIn(uint256 amountOut, uint256 reserveIn, uint256 reserveOut, uint256 vReserveIn, uint256 vReserveOut, uint256 feeInPrecision) internal pure returns (uint256 amountIn);

Returns the minimum input asset amount required to buy the given output asset amount (accounting for fees) given pool reserves (both actal and virtual balances).

  • Used in getAmountsIn.

getAmountsOut

function getAmountsOut(uint256 amountIn, address[] memory poolsPath, IERC20[] memory path) internal view returns (uint256[] memory amounts);

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

Since there are possibly multiple pools per token pair, it is required to specify the pools to be used for the swap. As such, it is a requirement that poolsPath.length = path.length - 1.

  • Useful for calculating optimal token amounts before calling swap.

getAmountsIn

function getAmountsIn(uint256 amountOut, address[] memory poolsPath, IERC20[] memory path) internal view returns (uint256[] memory amounts);

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

Since there are possibly multiple pools per token pair, it is required to specify the pools to be used for the swap. As such, it is a requirement that poolsPath.length = path.length - 1.

  • Useful for calculating optimal token amounts before calling swap.

BunicornLibrary.sol