added working logic on swap and error handling

This commit is contained in:
2025-11-11 18:58:48 -04:00
parent 2fbebed014
commit 1fa6d686f4
2 changed files with 48 additions and 10 deletions

View File

@@ -134,19 +134,20 @@ export function useGetPoolsByToken(tokenAddress: `0x${string}` | undefined, offs
setLoading(true);
setError(null);
// Get chain ID and contract address
// Get chain ID and contract addresses
const chainId = await publicClient.getChainId();
const address = (chainInfo as Record<string, { v1: { PartyPlanner: string; PartyPoolViewer: string } }>)[chainId.toString()]?.v1?.PartyPlanner;
const plannerAddress = (chainInfo as Record<string, { v1: { PartyPlanner: string; PartyInfo: string } }>)[chainId.toString()]?.v1?.PartyPlanner;
const partyInfoAddress = (chainInfo as Record<string, { v1: { PartyPlanner: string; PartyInfo: string } }>)[chainId.toString()]?.v1?.PartyInfo;
if (!address) {
setError('IPartyPlanner contract not found for current chain');
if (!plannerAddress || !partyInfoAddress) {
setError('IPartyPlanner or PartyInfo contract not found for current chain');
setAvailableTokens([]);
return;
}
// Call getPoolsByToken function
const poolsResult = await publicClient.readContract({
address: address as `0x${string}`,
address: plannerAddress as `0x${string}`,
abi: IPartyPlannerABI,
functionName: 'getPoolsByToken',
args: [tokenAddress, BigInt(offset), BigInt(limit)],
@@ -164,11 +165,41 @@ export function useGetPoolsByToken(tokenAddress: `0x${string}` | undefined, offs
return;
}
// Filter pools to only working ones
const workingPools: `0x${string}`[] = [];
for (const poolAddress of poolsResult) {
try {
const isWorking = await publicClient.readContract({
address: partyInfoAddress as `0x${string}`,
abi: IPartyInfoABI,
functionName: 'working',
args: [poolAddress],
}) as boolean;
if (isWorking) {
workingPools.push(poolAddress);
}
} catch (err) {
console.error(`Error checking if pool ${poolAddress} is working:`, err);
}
}
// If no working pools found, set error message
if (workingPools.length === 0 && poolsResult.length > 0) {
setError('This token is no longer supported. All pools containing this token are currently inactive.');
setAvailableTokens([]);
return;
} else if (workingPools.length === 0) {
setError('No pools found for this token.');
setAvailableTokens([]);
return;
}
// Map to store available tokens with their swap routes
const tokenRoutesMap = new Map<string, AvailableToken>();
// For each pool, fetch all tokens and track indices
for (const poolAddress of poolsResult) {
// For each working pool, fetch all tokens and track indices
for (const poolAddress of workingPools) {
try {
const tokensInPool = await publicClient.readContract({
address: poolAddress,