showing burn amounts in the redeem all function. ALso adding an isworking flag for staking and ustaking
This commit is contained in:
@@ -7,7 +7,7 @@ import { Input } from '@/components/ui/input';
|
|||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
import { ChevronDown, CheckCircle, XCircle, Loader2, ArrowDownUp } from 'lucide-react';
|
import { ChevronDown, CheckCircle, XCircle, Loader2, ArrowDownUp } from 'lucide-react';
|
||||||
import { useAccount, usePublicClient } from 'wagmi';
|
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 { useSwapMint, useBurnSwap, useBurn, type ActualSwapMintAmounts, type ActualBurnSwapAmounts, type ActualBurnAmounts } from '@/hooks/usePartyPool';
|
||||||
import { formatUnits, parseUnits } from 'viem';
|
import { formatUnits, parseUnits } from 'viem';
|
||||||
import IPartyPoolABI from '@/contracts/IPartyPoolABI';
|
import IPartyPoolABI from '@/contracts/IPartyPoolABI';
|
||||||
@@ -143,6 +143,12 @@ export function StakeForm({ defaultMode = 'stake' }: StakeFormProps) {
|
|||||||
mode === 'unstake' && !redeemAll ? inputTokenIndex : undefined
|
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
|
// Fetch token details for the selected pool when Redeem All is active
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!publicClient || !selectedPool || mode !== 'unstake' || !redeemAll) {
|
if (!publicClient || !selectedPool || mode !== 'unstake' || !redeemAll) {
|
||||||
@@ -618,15 +624,18 @@ export function StakeForm({ defaultMode = 'stake' }: StakeFormProps) {
|
|||||||
)}
|
)}
|
||||||
|
|
||||||
{/* Redeem All Tokens Display */}
|
{/* 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="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="text-sm font-medium mb-2">You will receive:</div>
|
||||||
<div className="space-y-1">
|
<div className="space-y-1">
|
||||||
{poolTokens.map((token) => (
|
{poolTokens.map((token, index) => (
|
||||||
<div key={token.address} className="text-sm flex justify-between">
|
<div key={token.address} className="text-sm flex justify-between">
|
||||||
<span className="text-muted-foreground">{token.symbol}</span>
|
<span className="text-muted-foreground">{token.symbol}:</span>
|
||||||
<span className="text-xs text-muted-foreground">
|
<span className="font-medium">
|
||||||
(proportional to pool composition)
|
{burnAmountsLoading ? 'Calculating...' : burnAmounts && burnAmounts[index]
|
||||||
|
? `${Number(formatUnits(burnAmounts[index], token.decimals)).toLocaleString('en-US', { maximumFractionDigits: 6 })}`
|
||||||
|
: '(proportional to pool composition)'
|
||||||
|
}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import chainInfo from '@/contracts/liqp-deployments.json';
|
|||||||
import IPartyPlannerABI from '@/contracts/IPartyPlannerABI';
|
import IPartyPlannerABI from '@/contracts/IPartyPlannerABI';
|
||||||
import IPartyPoolABI from '@/contracts/IPartyPoolABI';
|
import IPartyPoolABI from '@/contracts/IPartyPoolABI';
|
||||||
import IPartyPoolViewerABI from '@/contracts/IPartyPoolViewerABI';
|
import IPartyPoolViewerABI from '@/contracts/IPartyPoolViewerABI';
|
||||||
|
import IPartyInfoABI from '@/contracts/IPartyInfoABI';
|
||||||
import { ERC20ABI } from '@/contracts/ERC20ABI';
|
import { ERC20ABI } from '@/contracts/ERC20ABI';
|
||||||
|
|
||||||
export function useGetAllTokens(offset: number = 0, limit: number = 100) {
|
export function useGetAllTokens(offset: number = 0, limit: number = 100) {
|
||||||
@@ -360,12 +361,13 @@ export function useGetAllPools(offset: number = 0, limit: number = 100) {
|
|||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
|
|
||||||
// Get chain ID and contract address
|
// Get chain ID and contract addresses
|
||||||
const chainId = await publicClient.getChainId();
|
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) {
|
if (!plannerAddress || !partyInfoAddress) {
|
||||||
setError('IPartyPlanner contract not found for current chain');
|
setError('IPartyPlanner or PartyInfo contract not found for current chain');
|
||||||
setPools([]);
|
setPools([]);
|
||||||
setPoolDetails([]);
|
setPoolDetails([]);
|
||||||
return;
|
return;
|
||||||
@@ -373,7 +375,7 @@ export function useGetAllPools(offset: number = 0, limit: number = 100) {
|
|||||||
|
|
||||||
// Call getAllPools function
|
// Call getAllPools function
|
||||||
const result = await publicClient.readContract({
|
const result = await publicClient.readContract({
|
||||||
address: address as `0x${string}`,
|
address: plannerAddress as `0x${string}`,
|
||||||
abi: IPartyPlannerABI,
|
abi: IPartyPlannerABI,
|
||||||
functionName: 'getAllPools',
|
functionName: 'getAllPools',
|
||||||
args: [BigInt(offset), BigInt(limit)],
|
args: [BigInt(offset), BigInt(limit)],
|
||||||
@@ -381,11 +383,11 @@ export function useGetAllPools(offset: number = 0, limit: number = 100) {
|
|||||||
|
|
||||||
setPools(result);
|
setPools(result);
|
||||||
|
|
||||||
// Fetch details for each pool
|
// Fetch details for each pool and check if it's working
|
||||||
const details: PoolDetails[] = [];
|
const details: PoolDetails[] = [];
|
||||||
for (const poolAddress of result) {
|
for (const poolAddress of result) {
|
||||||
try {
|
try {
|
||||||
const [name, symbol, tokens] = await Promise.all([
|
const [name, symbol, tokens, isWorking] = await Promise.all([
|
||||||
publicClient.readContract({
|
publicClient.readContract({
|
||||||
address: poolAddress,
|
address: poolAddress,
|
||||||
abi: ERC20ABI,
|
abi: ERC20ABI,
|
||||||
@@ -401,23 +403,26 @@ export function useGetAllPools(offset: number = 0, limit: number = 100) {
|
|||||||
abi: IPartyPoolABI,
|
abi: IPartyPoolABI,
|
||||||
functionName: 'allTokens',
|
functionName: 'allTokens',
|
||||||
}).catch(() => [] as readonly `0x${string}`[]),
|
}).catch(() => [] as readonly `0x${string}`[]),
|
||||||
|
publicClient.readContract({
|
||||||
|
address: partyInfoAddress as `0x${string}`,
|
||||||
|
abi: IPartyInfoABI,
|
||||||
|
functionName: 'working',
|
||||||
|
args: [poolAddress],
|
||||||
|
}).catch(() => false),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
details.push({
|
// Only add pool if it's working
|
||||||
address: poolAddress,
|
if (isWorking) {
|
||||||
name: name as string,
|
details.push({
|
||||||
symbol: symbol as string,
|
address: poolAddress,
|
||||||
tokens: tokens as readonly `0x${string}`[],
|
name: name as string,
|
||||||
});
|
symbol: symbol as string,
|
||||||
|
tokens: tokens as readonly `0x${string}`[],
|
||||||
|
});
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error fetching pool details for', poolAddress, err);
|
console.error('Error fetching pool details for', poolAddress, err);
|
||||||
// Add pool with fallback values
|
// Skip pools that fail to load
|
||||||
details.push({
|
|
||||||
address: poolAddress,
|
|
||||||
name: 'Unknown Pool',
|
|
||||||
symbol: 'POOL',
|
|
||||||
tokens: [],
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -688,4 +693,74 @@ export function useLPTokenBalance(
|
|||||||
error,
|
error,
|
||||||
isReady: mounted,
|
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,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user