Skip to content

Commit

Permalink
add similar existing pool config check for v3
Browse files Browse the repository at this point in the history
  • Loading branch information
MattPereira committed Dec 11, 2024
1 parent 507424e commit 13cdcb8
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
34 changes: 32 additions & 2 deletions packages/nextjs/app/v3/_components/ChooseInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useEffect } from "react";
import { PoolType } from "@balancer/sdk";
import { TextField } from "~~/components/common";
import { useBoostableWhitelist, usePoolCreationStore, useUserDataStore } from "~~/hooks/v3";
import { ArrowTopRightOnSquareIcon, ExclamationTriangleIcon } from "@heroicons/react/24/outline";
import { Alert, TextField } from "~~/components/common";
import { useBoostableWhitelist, useCheckIfV3PoolExists, usePoolCreationStore, useUserDataStore } from "~~/hooks/v3";
import { MAX_POOL_NAME_LENGTH } from "~~/utils/constants";

/**
Expand All @@ -13,6 +14,11 @@ export const ChooseInfo = () => {
const { updateUserData, hasEditedPoolInformation } = useUserDataStore();
const { data: boostableWhitelist } = useBoostableWhitelist();

const { existingPools } = useCheckIfV3PoolExists(
poolType,
tokenConfigs.map(token => token.address),
);

useEffect(() => {
if (poolType) {
const symbol = tokenConfigs
Expand Down Expand Up @@ -66,6 +72,30 @@ export const ChooseInfo = () => {
/>
</div>
</div>
{existingPools && existingPools.length > 0 && (
<Alert showIcon={false} type="warning">
<div className="mb-3 flex items-center gap-2">
<ExclamationTriangleIcon className="w-5 h-5" /> Warning: The following pools have already been created with
a similar configuration
</div>
<ol className="">
{/* TODO: Replace with production link instead of test.balancer.fi */}
{existingPools.map(pool => (
<li key={pool.address}>
<a
target="_blank"
rel="noopener noreferrer"
className="hover:underline text-blue-500 flex justify-end items-center gap-2"
href={`https://test.balancer.fi/pools/${pool.chain.toLowerCase()}/v3/${pool.address}`}
>
{pool.symbol}
<ArrowTopRightOnSquareIcon className="w-4 h-4 mt-0.5" />
</a>
</li>
))}
</ol>
</Alert>
)}
</div>
);
};
1 change: 1 addition & 0 deletions packages/nextjs/hooks/v3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from "./useBoostableWhitelist";
export * from "./useValidateHooksContract";
export * from "./useValidateRateProvider";
export * from "./useUserDataStore";
export * from "./useCheckIfV3PoolExists";
71 changes: 71 additions & 0 deletions packages/nextjs/hooks/v3/useCheckIfV3PoolExists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { AllowedPoolTypes } from "./usePoolCreationStore";
import { useQuery } from "@tanstack/react-query";
import { type Address } from "viem";
import { useApiConfig } from "~~/hooks/balancer";

export type ExistingPool = {
chain: "string";
address: Address;
name: string;
symbol: string;
type: "string";
protocolVersion: number;
allTokens: {
address: Address;
weight: string;
}[];
dynamicData: {
swapFee: string;
};
};

/**
* Fetch all v3 poolsto see if user is trying to create a similar pool
*/
export const useCheckIfV3PoolExists = (type: AllowedPoolTypes | undefined, tokenAddresses: Address[]) => {
const { url, chainName } = useApiConfig();

const query = `
{
poolGetPools (where: {chainIn:[${chainName}], poolTypeIn:[${type?.toUpperCase()}], tokensIn:[${tokenAddresses
.map(address => `"${address}"`)
.join(",")}], protocolVersionIn:[3], tagNotIn: ["BLACK_LISTED"]}) {
chain
address
type
name
symbol
protocolVersion
dynamicData {
swapFee
}
allTokens {
address
weight
}
}
}
`;

const { data: existingPools } = useQuery<ExistingPool[]>({
queryKey: ["existingPools", type, chainName, tokenAddresses],
queryFn: async () => {
if (!type || !tokenAddresses) return {};

const response = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ query }),
});

const json = await response.json();

if (!response.ok) {
throw new Error("Error fetching token list from balancer API");
}
return json.data.poolGetPools;
},
});

return { existingPools };
};

0 comments on commit 13cdcb8

Please sign in to comment.