showing burn amounts in the redeem all function. ALso adding an isworking flag for staking and ustaking

This commit is contained in:
2025-11-11 17:15:26 -04:00
parent dab02db690
commit c69f0a47de
2 changed files with 110 additions and 26 deletions

View File

@@ -7,7 +7,7 @@ import { Input } from '@/components/ui/input';
import { Button } from '@/components/ui/button';
import { ChevronDown, CheckCircle, XCircle, Loader2, ArrowDownUp } from 'lucide-react';
import { useAccount, usePublicClient } from 'wagmi';
import { useGetAllPools, useTokenDetails, useSwapMintAmounts, useBurnSwapAmounts, useLPTokenBalance, type PoolDetails, type TokenDetails, type BurnSwapAmounts } from '@/hooks/usePartyPlanner';
import { useGetAllPools, useTokenDetails, useSwapMintAmounts, useBurnSwapAmounts, useLPTokenBalance, useBurnAmounts, type PoolDetails, type TokenDetails, type BurnSwapAmounts } from '@/hooks/usePartyPlanner';
import { useSwapMint, useBurnSwap, useBurn, type ActualSwapMintAmounts, type ActualBurnSwapAmounts, type ActualBurnAmounts } from '@/hooks/usePartyPool';
import { formatUnits, parseUnits } from 'viem';
import IPartyPoolABI from '@/contracts/IPartyPoolABI';
@@ -143,6 +143,12 @@ export function StakeForm({ defaultMode = 'stake' }: StakeFormProps) {
mode === 'unstake' && !redeemAll ? inputTokenIndex : undefined
);
// Fetch burn amounts (for unstake mode when redeeming all)
const { burnAmounts, loading: burnAmountsLoading } = useBurnAmounts(
mode === 'unstake' && redeemAll ? selectedPool?.address : undefined,
mode === 'unstake' && redeemAll ? maxAmountIn : undefined
);
// Fetch token details for the selected pool when Redeem All is active
useEffect(() => {
if (!publicClient || !selectedPool || mode !== 'unstake' || !redeemAll) {
@@ -618,15 +624,18 @@ export function StakeForm({ defaultMode = 'stake' }: StakeFormProps) {
)}
{/* Redeem All Tokens Display */}
{mode === 'unstake' && redeemAll && poolTokens.length > 0 && (
{mode === 'unstake' && redeemAll && poolTokens.length > 0 && !isAmountExceedingBalance && (
<div className="px-4 py-3 bg-muted/30 rounded-lg space-y-2">
<div className="text-sm font-medium mb-2">You will receive:</div>
<div className="space-y-1">
{poolTokens.map((token) => (
{poolTokens.map((token, index) => (
<div key={token.address} className="text-sm flex justify-between">
<span className="text-muted-foreground">{token.symbol}</span>
<span className="text-xs text-muted-foreground">
(proportional to pool composition)
<span className="text-muted-foreground">{token.symbol}:</span>
<span className="font-medium">
{burnAmountsLoading ? 'Calculating...' : burnAmounts && burnAmounts[index]
? `${Number(formatUnits(burnAmounts[index], token.decimals)).toLocaleString('en-US', { maximumFractionDigits: 6 })}`
: '(proportional to pool composition)'
}
</span>
</div>
))}

View File

@@ -6,6 +6,7 @@ import chainInfo from '@/contracts/liqp-deployments.json';
import IPartyPlannerABI from '@/contracts/IPartyPlannerABI';
import IPartyPoolABI from '@/contracts/IPartyPoolABI';
import IPartyPoolViewerABI from '@/contracts/IPartyPoolViewerABI';
import IPartyInfoABI from '@/contracts/IPartyInfoABI';
import { ERC20ABI } from '@/contracts/ERC20ABI';
export function useGetAllTokens(offset: number = 0, limit: number = 100) {
@@ -360,12 +361,13 @@ export function useGetAllPools(offset: number = 0, limit: number = 100) {
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');
setPools([]);
setPoolDetails([]);
return;
@@ -373,7 +375,7 @@ export function useGetAllPools(offset: number = 0, limit: number = 100) {
// Call getAllPools function
const result = await publicClient.readContract({
address: address as `0x${string}`,
address: plannerAddress as `0x${string}`,
abi: IPartyPlannerABI,
functionName: 'getAllPools',
args: [BigInt(offset), BigInt(limit)],
@@ -381,11 +383,11 @@ export function useGetAllPools(offset: number = 0, limit: number = 100) {
setPools(result);
// Fetch details for each pool
// Fetch details for each pool and check if it's working
const details: PoolDetails[] = [];
for (const poolAddress of result) {
try {
const [name, symbol, tokens] = await Promise.all([
const [name, symbol, tokens, isWorking] = await Promise.all([
publicClient.readContract({
address: poolAddress,
abi: ERC20ABI,
@@ -401,23 +403,26 @@ export function useGetAllPools(offset: number = 0, limit: number = 100) {
abi: IPartyPoolABI,
functionName: 'allTokens',
}).catch(() => [] as readonly `0x${string}`[]),
publicClient.readContract({
address: partyInfoAddress as `0x${string}`,
abi: IPartyInfoABI,
functionName: 'working',
args: [poolAddress],
}).catch(() => false),
]);
// Only add pool if it's working
if (isWorking) {
details.push({
address: poolAddress,
name: name as string,
symbol: symbol as string,
tokens: tokens as readonly `0x${string}`[],
});
}
} catch (err) {
console.error('Error fetching pool details for', poolAddress, err);
// Add pool with fallback values
details.push({
address: poolAddress,
name: 'Unknown Pool',
symbol: 'POOL',
tokens: [],
});
// Skip pools that fail to load
}
}
@@ -689,3 +694,73 @@ export function useLPTokenBalance(
isReady: mounted,
};
}
export function useBurnAmounts(
poolAddress: `0x${string}` | undefined,
lpTokenAmount: bigint | undefined
) {
const publicClient = usePublicClient();
const [mounted, setMounted] = useState(false);
const [burnAmounts, setBurnAmounts] = useState<bigint[] | null>(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
setMounted(true);
}, []);
useEffect(() => {
if (!mounted || !poolAddress || !lpTokenAmount || lpTokenAmount === 0n) {
setBurnAmounts(null);
setLoading(false);
return;
}
const fetchBurnAmounts = async () => {
if (!publicClient) {
setLoading(false);
return;
}
try {
setLoading(true);
setError(null);
// Get chain ID and PartyInfo contract address
const chainId = await publicClient.getChainId();
const partyInfoAddress = (chainInfo as Record<string, { v1: { PartyInfo: string } }>)[chainId.toString()]?.v1?.PartyInfo;
if (!partyInfoAddress) {
setError('PartyInfo contract not found for current chain');
setBurnAmounts(null);
return;
}
// Call burnAmounts function
const amounts = await publicClient.readContract({
address: partyInfoAddress as `0x${string}`,
abi: IPartyPoolViewerABI,
functionName: 'burnAmounts',
args: [poolAddress, lpTokenAmount],
}) as bigint[];
setBurnAmounts(amounts);
} catch (err) {
console.error('Error fetching burn amounts:', err);
setError(err instanceof Error ? err.message : 'Failed to fetch burn amounts');
setBurnAmounts(null);
} finally {
setLoading(false);
}
};
fetchBurnAmounts();
}, [publicClient, mounted, poolAddress, lpTokenAmount]);
return {
burnAmounts,
loading,
error,
isReady: mounted,
};
}