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
  • Using the Router
  • Obtaining Pool Addresses
  • Examples
  • USDC -> BUSD
  • swapTokensForExactTokens

Was this helpful?

  1. SMART CONTRACTS
  2. Stable Pools

Swap Execution

PreviousPool AddressesNextProviding Liquidity

Last updated 3 years ago

Was this helpful?

Using the Router

We recommend using the for swapping stablecoins in Bunicorn. Before executing the swap, it is recommended that an external price source is use to fix the minimum output tokens receivable when selling a fixed amount of tokens, or maximum amount of input tokens to be used when purchasing a fixed amount of tokens.

Your smart contract should also:

  1. Have enough BNB / tokens when executing the swap

  2. Granted approval to the router when swapping from tokens

Obtaining Pool Addresses

It is necessary to specify which pools are to be used for the token swap. Get the list of before proceeding.

Examples

USDC -> BUSD

swapTokensForExactTokens

Transferring tokens

Before swapping, the contract should be in possession of USDC. The caller can either send the tokens beforehand, or give allowance to the contract to call the transferFrom method. The short code snippet below showcases the latter.

uint256 amountIn = 50 * 10 ** usdc.decimals();
require(usdc.transferFrom(msg.sender, address(this), amountIn), 'transferFrom failed');

Granting Approval

The next step is then to give the router some USDC

require(usdc.approve(address(buniCornRouter), amountIn), 'approve failed');

swapTokensForExactTokens

IERC20[] memory path = new address[](2);
path[0] = usdc; // assuming core is specified as IERC20
path[1] = busd; // assuming usdt is specified as IERC20
buniCornRouter.swapTokensForExactTokens(
    amountOutMin, // should be obtained via a price oracle, either off or on-chain
    poolsPath, // eg. [usdc-busd-pool]
    path,
    msg.sender,
    block.timestamp
);
router
pool addresses