diff --git a/src/modules/liquidity/pages/liquidity/hooks/helpers/find-dex.ts b/src/modules/liquidity/pages/liquidity/hooks/helpers/find-dex.ts new file mode 100644 index 000000000..255d906c5 --- /dev/null +++ b/src/modules/liquidity/pages/liquidity/hooks/helpers/find-dex.ts @@ -0,0 +1,53 @@ +import { + chooseDex, + ContractOrAddress, + DexNotFoundError, + Factories, + FoundDex, + isFA2Token, + Token as QuipuswapSdkToken, + toContract, + toContractAddress +} from '@quipuswap/sdk'; +import { TezosToolkit } from '@taquito/taquito'; + +import { resolveOrNull } from '@shared/helpers'; + +export async function findDex( + tezos: TezosToolkit, + { fa1_2Factory, fa2Factory }: Factories, + token: QuipuswapSdkToken +): Promise { + let factories = isFA2Token(token) ? fa2Factory : fa1_2Factory; + if (!Array.isArray(factories)) { + factories = [factories]; + } + + const tokenAddress = toContractAddress(token.contract); + const t2dexQuery = isFA2Token(token) ? [tokenAddress, token.id] : tokenAddress; + + const dexes: FoundDex[] = []; + await Promise.all( + factories.map(async factory => { + const facContract = await toContract(tezos, factory); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const facStorage = await facContract.storage(); + const dexAddress = await resolveOrNull(facStorage.token_to_exchange.get(t2dexQuery)); + + if (dexAddress) { + const dexContract = await toContract(tezos, dexAddress as ContractOrAddress); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const dexStorage = await dexContract.storage(); + dexes.push(new FoundDex(dexContract, dexStorage)); + } + }) + ); + + if (dexes.length > 1) { + return dexes.sort(chooseDex)[0]; + } else if (dexes.length === 1) { + return dexes[0]; + } else { + throw new DexNotFoundError(); + } +} diff --git a/src/modules/liquidity/pages/liquidity/hooks/helpers/load-tez-dex.ts b/src/modules/liquidity/pages/liquidity/hooks/helpers/load-tez-dex.ts index 239859039..8a111acb5 100644 --- a/src/modules/liquidity/pages/liquidity/hooks/helpers/load-tez-dex.ts +++ b/src/modules/liquidity/pages/liquidity/hooks/helpers/load-tez-dex.ts @@ -1,9 +1,10 @@ -import { findDex, FoundDex, Token as QuipuswapSdkToken } from '@quipuswap/sdk'; +import { FoundDex, Token as QuipuswapSdkToken } from '@quipuswap/sdk'; import { TezosToolkit } from '@taquito/taquito'; import { FACTORIES } from '@config/config'; import { Nullable, SupportedNetworks, Token } from '@shared/types'; +import { findDex } from './find-dex'; import { findNotTezToken } from '../../liquidity-cards/helpers'; export const loadTezDex = async ({