-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add similar existing pool config check for v3
- Loading branch information
1 parent
507424e
commit 13cdcb8
Showing
3 changed files
with
104 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
}; |