Adding logic to get token info and remove swap counter that's not needed anymore

This commit is contained in:
2025-10-14 17:53:07 -04:00
parent c8b8ad0148
commit 63a96b9cf1
4 changed files with 287 additions and 111 deletions

View File

@@ -0,0 +1,166 @@
'use client';
import { useState, useEffect } from 'react';
import { usePublicClient } from 'wagmi';
import chainInfo from '@/app/liquidity-party.json';
import IPartyPlannerABI from '@/contracts/IPartyPlannerABI';
import { ERC20ABI } from '@/contracts/ERC20ABI';
export function useGetAllTokens(offset: number = 0, limit: number = 100) {
const publicClient = usePublicClient();
const [mounted, setMounted] = useState(false);
const [tokens, setTokens] = useState<readonly `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) return;
const fetchTokens = 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[chainId.toString()]?.IPartyPlanner;
if (!address) {
setError('IPartyPlanner contract not found for current chain');
setTokens([]);
return;
}
// Call getAllTokens function
const result = await publicClient.readContract({
address,
abi: IPartyPlannerABI,
functionName: 'getAllTokens',
args: [BigInt(offset), BigInt(limit)],
});
setTokens(result);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to fetch tokens');
} finally {
setLoading(false);
}
};
fetchTokens();
}, [publicClient, mounted, offset, limit]);
return {
tokens,
loading,
error,
isReady: mounted,
};
}
export interface TokenDetails {
address: `0x${string}`;
name: string;
symbol: string;
decimals: number;
balance: bigint;P
}
export function useTokenDetails(userAddress: `0x${string}` | undefined) {
const publicClient = usePublicClient();
const { tokens, loading: tokensLoading, isReady } = useGetAllTokens();
const [tokenDetails, setTokenDetails] = useState<TokenDetails[] | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
if (!isReady || tokensLoading || !tokens || !publicClient || !userAddress) {
setLoading(tokensLoading || !isReady);
return;
}
const fetchTokenDetails = async () => {
try {
setLoading(true);
setError(null);
if (tokens.length === 0) {
setTokenDetails([]);
return;
}
const details: TokenDetails[] = [];
// Make individual calls for each token
for (const tokenAddress of tokens) {
try {
const [name, symbol, decimals, balance] = await Promise.all([
publicClient.readContract({
address: tokenAddress,
abi: ERC20ABI,
functionName: 'name',
}).catch(() => 'Unknown'),
publicClient.readContract({
address: tokenAddress,
abi: ERC20ABI,
functionName: 'symbol',
}).catch(() => '???'),
publicClient.readContract({
address: tokenAddress,
abi: ERC20ABI,
functionName: 'decimals',
}).catch(() => 18),
publicClient.readContract({
address: tokenAddress,
abi: ERC20ABI,
functionName: 'balanceOf',
args: [userAddress],
}).catch(() => BigInt(0)),
]);
details.push({
address: tokenAddress,
name: name as string,
symbol: symbol as string,
decimals: Number(decimals),
balance: balance as bigint,
});
} catch (err) {
// Add token with fallback values if individual call fails
details.push({
address: tokenAddress,
name: 'Unknown',
symbol: '???',
decimals: 18,
balance: BigInt(0),
});
}
}
setTokenDetails(details);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to fetch token details');
} finally {
setLoading(false);
}
};
fetchTokenDetails();
}, [tokens, tokensLoading, publicClient, isReady, userAddress]);
return {
tokenDetails,
loading,
error,
};
}