added working logic on swap and error handling
This commit is contained in:
@@ -36,7 +36,7 @@ export function SwapForm() {
|
||||
const { tokenDetails, loading } = useTokenDetails(address);
|
||||
|
||||
// Get available tokens for the selected "from" token
|
||||
const { availableTokens } = useGetPoolsByToken(selectedFromToken?.address);
|
||||
const { availableTokens, error: poolsError } = useGetPoolsByToken(selectedFromToken?.address);
|
||||
|
||||
// Only calculate swap amounts when both tokens are selected
|
||||
// Use useMemo to prevent creating a new array reference on every render
|
||||
@@ -326,7 +326,7 @@ export function SwapForm() {
|
||||
))
|
||||
) : selectedFromToken ? (
|
||||
<div className="px-4 py-3 text-sm text-muted-foreground">
|
||||
{loading ? 'Loading available tokens...' : 'No tokens available for swap'}
|
||||
{loading ? 'Loading available tokens...' : poolsError || 'No tokens available for swap'}
|
||||
</div>
|
||||
) : (
|
||||
<div className="px-4 py-3 text-sm text-muted-foreground">
|
||||
@@ -339,6 +339,13 @@ export function SwapForm() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Error message for unsupported tokens */}
|
||||
{poolsError && selectedFromToken && (
|
||||
<div className="px-4 py-3 bg-destructive/10 border border-destructive/20 rounded-lg">
|
||||
<p className="text-sm text-destructive">{poolsError}</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Gas Estimate, Slippage, and Fees */}
|
||||
{isConnected && fromAmount && toAmount && (
|
||||
<div className="px-4 py-2 bg-muted/30 rounded-lg space-y-2">
|
||||
@@ -377,7 +384,7 @@ export function SwapForm() {
|
||||
<Button
|
||||
className="w-full h-14 text-lg"
|
||||
onClick={() => setIsReviewModalOpen(true)}
|
||||
disabled={!isConnected || !fromAmount || !toAmount}
|
||||
disabled={!isConnected || !fromAmount || !toAmount || !!poolsError}
|
||||
>
|
||||
{!isConnected
|
||||
? t('swap.connectWalletToSwap')
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user