flutter_pancake_swap_sdk

Table of Contents

  1. Introduction
  2. Installation
  3. Usage
  4. Documentation
  5. Examples
  6. Support

Introduction

flutter_pancake_swap_sdk is a Flutter project forked from two prominent web libraries, pancakeswap/sdk and Uniswap SDK v3 Overview. This project aims to adapt these web libraries for mobile platforms, providing users with the functionality of the PancakeSwap and Uniswap platforms.

Installation

(Instructions on how to install the SDK in a Flutter project.)

Usage

The SDK includes several key features such as token declaration and price estimation. Here's a brief overview:

Token Declaration

  • For native tokens: Token.WETH[ChainId]!
  • For creating a token instance: Token(chainId, address, decimal)

LP Pair Tokens

  • To get LP pair tokens: Pair.getAddress(token1, token2)

Price Estimation

  • To estimate prices: Trade.bestTradeExactIn(TokenAmount(tokenFrom, value.amountSwapFrom(decimalFrom)), tokenTo, maxHops)

Documentation

Forked from the PancakeSwap SDK.

For more detailed information, refer to the Uniswap SDK v3 Documentation.

You can refer to the Uniswap SDK documentation uniswap.org.

Examples

Trade? getTrade({
  required List<EthereumAddress> addressSwap,
  required int decimalFrom,
  required int decimalTo,
  required double value,
}) {
  Trade? bestTradeSoFar;
  Token tokenFrom =
  TokenSDK.Token(_buildConfig.chainId, addressSwap[0], decimalFrom);
  Token tokenTo =
  TokenSDK.Token(_buildConfig.chainId, addressSwap[1], decimalTo);

  if (addressSwap[0]
      .hex
      .toLowerCase()
      .contains(_buildConfig.bnbContractAddress.toLowerCase())) {
    tokenFrom = TokenSDK.Token.WETH[ChainId.MAINNET.value]!;
  }

  if (addressSwap[1]
      .hex
      .toLowerCase()
      .contains(_buildConfig.bnbContractAddress.toLowerCase())) {
    tokenTo = TokenSDK.Token.WETH[ChainId.MAINNET.value]!;
  }

  for (int i = 1; i <= maxHops; i++) {
    var bestTrade = Trade.bestTradeExactIn(
      listPair,
      TokenAmount(tokenFrom, value.amountSwapFrom(decimalFrom)),
      tokenTo,
      maxHops: i,
    );
    if (bestTrade.isNotEmpty) {
      final currentTrade = bestTrade[0];
      final valueBool = isTradeBetter(
        bestTradeSoFar,
        currentTrade,
        minimumDelt: betterTradeLessHopsThreshold,
      );
      if (valueBool) {
        bestTradeSoFar = currentTrade;
      }
    }
  }
  tradeInfor = bestTradeSoFar;

  return tradeInfor;
}

Future<List<Pair>> getListPair(Token tokenA, Token tokenB) async {
  List<Pair> listPair = [];
  final listPairA = getPairTokenABaseToken(tokenA);
  final listPairB = getPairTokenBBaseToken(tokenB);
  try {
    final listStablePair = getStableLPPair(tokenA, tokenB);
    listPair = await _contractMultiCallRepository
        .getListPairFromSDK(addressLpToken: [
      ...listStablePair,
      ...listPairA,
      ...listPairB,
    ]);
  } catch (e) {
    Log.info('error get pair swap', e.toString());
  }

  return listPair;
}

Support

Support this project by donating on buymeacoffee.com.