synapsecns/sanguine

View on GitHub
packages/synapse-interface/utils/swapFinder/generateSwapPossibilities.ts

Summary

Maintainability
C
7 hrs
Test Coverage
import _ from 'lodash'

import { flattenPausedTokens } from '../flattenPausedTokens'
import { Token } from '../types'
import { getSwapFromChainIds } from './getSwapFromChainIds'
import { getSwapFromTokens } from './getSwapFromTokens'
import { getSwapToTokens } from './getSwapToTokens'
import { findTokenByRouteSymbol } from '../findTokenByRouteSymbol'
import { getSymbol } from '@/utils/getSymbol'

export interface RouteQueryFields {
  fromChainId?: number
  fromTokenRouteSymbol?: string
  toChainId?: number
  toTokenRouteSymbol?: string
}

export const getSwapPossibilities = ({
  fromChainId,
  fromToken,
  toChainId,
  toToken,
}: {
  fromChainId?: number
  fromToken?: Token
  toChainId?: number
  toToken?: Token
}) => {
  const fromTokenRouteSymbol = fromToken && fromToken.routeSymbol
  const toTokenRouteSymbol = toToken && toToken.routeSymbol

  const fromChainIds: number[] = getSwapFromChainIds({
    fromChainId,
    fromTokenRouteSymbol,
    toChainId,
    toTokenRouteSymbol,
  })

  const fromTokens: Token[] = _(
    getSwapFromTokens({
      fromChainId,
      fromTokenRouteSymbol,
      toChainId,
      toTokenRouteSymbol,
    })
  )
    .difference(flattenPausedTokens())
    .map(getSymbol)
    .uniq()
    .map((symbol) => findTokenByRouteSymbol(symbol))
    .compact()
    .value()

  const toTokens: Token[] = _(
    getSwapToTokens({
      fromChainId,
      fromTokenRouteSymbol,
      toChainId,
      toTokenRouteSymbol,
    })
  )
    .difference(flattenPausedTokens())
    .map(getSymbol)
    .uniq()
    .map((symbol) => findTokenByRouteSymbol(symbol))
    .compact()
    .value()

  return {
    fromChainId,
    fromToken,
    toChainId,
    toToken,
    fromChainIds,
    fromTokens,
    toTokens,
  }
}