adding token from pool logic and updating ABIs to support new smart contracts
This commit is contained in:
@@ -4,6 +4,7 @@ import { useState, useEffect } from 'react';
|
||||
import { usePublicClient } from 'wagmi';
|
||||
import chainInfo from '@/app/liquidity-party.json';
|
||||
import IPartyPlannerABI from '@/contracts/IPartyPlannerABI';
|
||||
import IPartyPoolABI from '@/contracts/IPartyPoolABI';
|
||||
import { ERC20ABI } from '@/contracts/ERC20ABI';
|
||||
|
||||
export function useGetAllTokens(offset: number = 0, limit: number = 100) {
|
||||
@@ -73,7 +74,127 @@ export interface TokenDetails {
|
||||
name: string;
|
||||
symbol: string;
|
||||
decimals: number;
|
||||
balance: bigint;P
|
||||
balance: bigint;
|
||||
index: number;
|
||||
}
|
||||
|
||||
export function useGetPoolsByToken(tokenAddress: `0x${string}` | undefined, offset: number = 0, limit: number = 100) {
|
||||
const publicClient = usePublicClient();
|
||||
const [mounted, setMounted] = useState(false);
|
||||
const [availableTokens, setAvailableTokens] = useState<`0x${string}`[] | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
// Handle hydration for Next.js static export
|
||||
useEffect(() => {
|
||||
setMounted(true);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!mounted || !tokenAddress) {
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const fetchPoolsFromTokens = async () => {
|
||||
if (!publicClient) {
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
|
||||
// Get chain ID and contract address
|
||||
const chainId = await publicClient.getChainId();
|
||||
const address = (chainInfo as Record<string, { IPartyPlanner: string; IPartyPoolViewer: string }>)[chainId.toString()]?.IPartyPlanner;
|
||||
|
||||
if (!address) {
|
||||
setError('IPartyPlanner contract not found for current chain');
|
||||
setAvailableTokens([]);
|
||||
return;
|
||||
}
|
||||
|
||||
// Call getPoolsByToken function
|
||||
const poolsResult = await publicClient.readContract({
|
||||
address: address as `0x${string}`,
|
||||
abi: IPartyPlannerABI,
|
||||
functionName: 'getPoolsByToken',
|
||||
args: [tokenAddress, BigInt(offset), BigInt(limit)],
|
||||
});
|
||||
|
||||
console.log('Pools for token', tokenAddress, ':', poolsResult);
|
||||
|
||||
// Get the symbol of the originally selected token
|
||||
const selectedTokenSymbol = await publicClient.readContract({
|
||||
address: tokenAddress,
|
||||
abi: ERC20ABI,
|
||||
functionName: 'symbol',
|
||||
}).catch(() => null);
|
||||
|
||||
if (!selectedTokenSymbol) {
|
||||
setAvailableTokens([]);
|
||||
return;
|
||||
}
|
||||
|
||||
// For each pool, fetch all tokens in that pool
|
||||
const allTokensInPools: `0x${string}`[] = [];
|
||||
for (const poolAddress of poolsResult) {
|
||||
try {
|
||||
const tokensInPool = await publicClient.readContract({
|
||||
address: poolAddress,
|
||||
abi: IPartyPoolABI,
|
||||
functionName: 'allTokens',
|
||||
}) as readonly `0x${string}`[];
|
||||
|
||||
// Add all tokens from this pool
|
||||
allTokensInPools.push(...tokensInPool);
|
||||
} catch (err) {
|
||||
console.error('Error fetching tokens from pool', poolAddress, err);
|
||||
}
|
||||
}
|
||||
|
||||
// Remove duplicates by address
|
||||
const uniqueTokenAddresses = Array.from(new Set(allTokensInPools));
|
||||
|
||||
// Fetch symbols for all tokens and filter out those matching the selected token's symbol
|
||||
const filteredTokens: `0x${string}`[] = [];
|
||||
for (const token of uniqueTokenAddresses) {
|
||||
try {
|
||||
const tokenSymbol = await publicClient.readContract({
|
||||
address: token,
|
||||
abi: ERC20ABI,
|
||||
functionName: 'symbol',
|
||||
}).catch(() => null);
|
||||
|
||||
// Only include tokens with different symbols
|
||||
if (tokenSymbol && tokenSymbol !== selectedTokenSymbol) {
|
||||
filteredTokens.push(token);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error fetching symbol for token', token, err);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Available tokens to swap to (excluding', selectedTokenSymbol, '):', filteredTokens);
|
||||
setAvailableTokens(filteredTokens);
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Failed to fetch pools and tokens');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
fetchPoolsFromTokens();
|
||||
}, [publicClient, mounted, tokenAddress, offset, limit]);
|
||||
|
||||
return {
|
||||
availableTokens,
|
||||
loading,
|
||||
error,
|
||||
isReady: mounted,
|
||||
};
|
||||
}
|
||||
|
||||
export function useTokenDetails(userAddress: `0x${string}` | undefined) {
|
||||
@@ -102,7 +223,8 @@ export function useTokenDetails(userAddress: `0x${string}` | undefined) {
|
||||
const details: TokenDetails[] = [];
|
||||
|
||||
// Make individual calls for each token
|
||||
for (const tokenAddress of tokens) {
|
||||
for (let i = 0; i < tokens.length; i++) {
|
||||
const tokenAddress = tokens[i];
|
||||
try {
|
||||
const [name, symbol, decimals, balance] = await Promise.all([
|
||||
publicClient.readContract({
|
||||
@@ -134,6 +256,7 @@ export function useTokenDetails(userAddress: `0x${string}` | undefined) {
|
||||
symbol: symbol as string,
|
||||
decimals: Number(decimals),
|
||||
balance: balance as bigint,
|
||||
index: i,
|
||||
});
|
||||
} catch (err) {
|
||||
// Add token with fallback values if individual call fails
|
||||
@@ -143,6 +266,7 @@ export function useTokenDetails(userAddress: `0x${string}` | undefined) {
|
||||
symbol: '???',
|
||||
decimals: 18,
|
||||
balance: BigInt(0),
|
||||
index: i,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user