Tinyman Docs
  • Tinyman V2 Overview
  • About Tinyman
  • Protocol Specification
    • Pool
      • Pool Creation
      • Adding Liquidity
      • Removing Liquidity
    • Swap
    • Fees
    • Flash Loan
    • Flash Swap
    • Additional Notes
  • FAQ
    • Migration FAQ
  • Fees
  • Permissioned Methods
    • Roles
    • Methods
    • Related Non-Permissioned Methods
  • Formulae
  • Disclaimer
  • Contracts
  • Audits & Security
  • Token and Governance
    • TINY Token Details
    • Governance Details
      • Overview
      • Governance Vault
      • Governance Rewards
      • Governance Process
      • TINY Farming
  • V2 Integration
    • Protocol Methods
      • Bootstrap
      • Add Initial Liquidity
      • Add Subsequent Liquidity
      • Remove Liquidity
      • Swap
      • Flash Loan
      • Flash Swap
    • State Data
    • Oracle Data
    • Calculating Quotes
    • Official SDKs
  • Swap Router
    • Transaction Specification
  • Swap Widget
    • Customization Preferences
    • How to export your Widget
  • Lending Pools
  • Trigger Orders & Recurring Orders
  • Liquid Staking
    • Liquid Staking
  • Tinyman V1
    • Overview
    • Tinyman AMM Basics
      • Creating Pools
      • Slippage & Excess
      • Farming
    • FAQ
    • Fees
    • Design Doc
    • Contracts
    • Tinyman Testnet
    • Disclaimer
    • Audits
    • Security
  • V1 Integration
    • Pool Lookup
    • Transaction Specifications
      • Bootstrap Pool
      • OptIn
      • Swap
      • Mint
      • Burn
      • Redeem
      • Create Validator App
      • Redeem Protocol Fees
    • Official SDKs
    • Community SDKs
  • Tinyman Presentations
  • Known Issues
    • 2021-11-12 - Pool overflow errors
Powered by GitBook
On this page
  • Swap
  • Fixed Input
  • Fixed Output

Was this helpful?

  1. V2 Integration

Calculating Quotes

PreviousOracle DataNextOfficial SDKs

Last updated 1 year ago

Was this helpful?

Calculating quotes for swaps and other operations requires knowledge of the current pool state including fee parameters, reserves, issued protocol tokens, etc. This information can be retrieved from the local state of the Tinyman app in the pool account. It is important to use fresh state data when calculating quotes as pool ratios and prices can change quickly.

Swap

Fixed Input

See

Fixed Input Swap from asset 1 to asset 2

total_fee = (input_amount * state["total_fee_share"]) / 10000 swap_amount = input_amount - total_fee k = state["asset_1_reserves"] * state["asset_2_reserves"] swap_output = state["asset_2_reserves"] - (floor(k / (state["asset_1_reserves"] + swap_amount)) + 1)

Allowing for a slippage of 1%, min_output_amount should be set as swap_output * 0.99

Fixed Output

See

Fixed Output Swap from asset 1 to asset 2

k = state["asset_1_reserves"] * state["asset_2_reserves"] swap_amount = (floor(k / (state["asset_2_reserves"] - output_amount)) + 1) - state["asset_1_reserves"] input_amount = floor((swap_amount * 10000) / (10000 - state["total_fee_share"]))

Allowing for a slippage of 1%, input_amount should be set as input_amount * 1.01

Formula IV.A
Formula IV.B