Skip to content

Commit

Permalink
fix v1 dex pulling
Browse files Browse the repository at this point in the history
  • Loading branch information
Digberi committed Sep 13, 2023
1 parent aa92042 commit 0a02038
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
53 changes: 53 additions & 0 deletions src/modules/liquidity/pages/liquidity/hooks/helpers/find-dex.ts
Original file line number Diff line number Diff line change
@@ -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<FoundDex> {
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<any>();
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<any>();
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();
}
}
Original file line number Diff line number Diff line change
@@ -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 ({
Expand Down

0 comments on commit 0a02038

Please sign in to comment.