2

I was trying to understand how the token swap prices are determined in PancakeSwap by looking at codes and finally got to the point where best price is calculated based on some values.

The method is below:

    /**
 * Given a list of pairs, and a fixed amount in, returns the top `maxNumResults` trades that go from an input token
 * amount to an output token, making at most `maxHops` hops.
 * Note this does not consider aggregation, as routes are linear. It's possible a better route exists by splitting
 * the amount in among multiple routes.
 * @param pairs the pairs to consider in finding the best trade
 * @param currencyAmountIn exact amount of input currency to spend
 * @param currencyOut the desired currency out
 * @param maxNumResults maximum number of results to return
 * @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
 * @param currentPairs used in recursion; the current list of pairs
 * @param originalAmountIn used in recursion; the original value of the currencyAmountIn parameter
 * @param bestTrades used in recursion; the current list of best trades
 */
static bestTradeExactIn(pairs: Pair[], currencyAmountIn: CurrencyAmount, currencyOut: Currency, { maxNumResults, maxHops }?: BestTradeOptions, currentPairs?: Pair[], originalAmountIn?: CurrencyAmount, bestTrades?: Trade[]): Trade[];

The code above is written in Trade Class. As far as I know, no process is written but it apparently returns some value where it is used like below:

const currentTrade: Trade | null =
      Trade.bestTradeExactIn(allowedPairs, currencyAmountIn, currencyOut, { maxHops: i, maxNumResults: 1 })[0] ??
      null
    console.log('currentTrade : ', currentTrade)

How come does "Trade.bestTradeExactIn(args...)" return value without any code for this method?

PancakeSwap frontend code : https://github.com/pancakeswap/pancake-frontend

TylerH
  • 20,799
  • 66
  • 75
  • 101
Masa
  • 362
  • 2
  • 14
  • 1
    Is the file you found this have an extension that ends in `.d.ts`? If so, it's a TS _definition_ file and like a header file in C or C++ it doesn't actually contain the implementation. Its only job is to share the types involved so that you can write accurate code against the _real_ library (wherever it actually lives) – Sean Vieira Jun 06 '22 at 03:21
  • @SeanVieira Thank you for your reply! As you mentioned, it is written in .d.ts file. So I need to find the real library file (.ts or .js) to understand how that method actually works, right? – Masa Jun 07 '22 at 00:06
  • 2
    Absolutely correct @Masa. If you look for `bestTradeExactIn` in pancake-frontend you'll see that `Trade.bestTradeExactIn` comes from `@pancakeswap/sdk`. Looking at their Github organization we see the `pancake-swap-sdk` repository and sure enough: https://github.com/pancakeswap/pancake-swap-sdk/blob/d2903d4c136cd11eeda0f3f874a838632fd75704/src/entities/trade.ts#L251-L260 – Sean Vieira Jun 07 '22 at 12:53

0 Answers0