adding actual amounts and fees to confirmation pages for swap, stake and unstake

This commit is contained in:
2025-11-05 14:31:56 -04:00
parent 9707c4892b
commit 0a518d31f9
5 changed files with 332 additions and 27 deletions

View File

@@ -2,6 +2,7 @@
import { useState, useEffect, useCallback } from 'react';
import { usePublicClient, useWalletClient } from 'wagmi';
import { decodeEventLog } from 'viem';
import IPartyPoolABI from '@/contracts/IPartyPoolABI';
import chainInfo from '../../../lmsr-amm/liqp-deployments.json';
import type { AvailableToken } from './usePartyPlanner';
@@ -189,6 +190,27 @@ export interface GasEstimate {
estimatedCostUsd: string;
}
export interface ActualSwapAmounts {
amountIn: bigint;
amountOut: bigint;
lpFee: bigint;
protocolFee: bigint;
}
export interface ActualSwapMintAmounts {
amountInUsed: bigint;
lpMinted: bigint;
lpFee: bigint;
protocolFee: bigint;
}
export interface ActualBurnSwapAmounts {
amountIn: bigint;
amountOut: bigint;
lpFee: bigint;
protocolFee: bigint;
}
export function useSwap() {
const { data: walletClient } = useWalletClient();
const publicClient = usePublicClient();
@@ -338,7 +360,46 @@ export function useSwap() {
const receipt = await publicClient.waitForTransactionReceipt({ hash });
console.log('✅ Swap transaction confirmed:', receipt);
return receipt;
// Parse the Swap event from the receipt logs
let actualSwapAmounts: ActualSwapAmounts | null = null;
for (const log of receipt.logs) {
try {
const decodedLog = decodeEventLog({
abi: IPartyPoolABI,
data: log.data,
topics: log.topics,
});
if (decodedLog.eventName === 'Swap') {
const { amountIn, amountOut, lpFee, protocolFee } = decodedLog.args as {
amountIn: bigint;
amountOut: bigint;
lpFee: bigint;
protocolFee: bigint;
};
actualSwapAmounts = {
amountIn,
amountOut,
lpFee,
protocolFee,
};
console.log('📊 Actual swap amounts from event:', {
amountIn: amountIn.toString(),
amountOut: amountOut.toString(),
lpFee: lpFee.toString(),
protocolFee: protocolFee.toString(),
});
break;
}
} catch (err) {
// Skip logs that don't match our ABI
continue;
}
}
return { receipt, actualSwapAmounts };
} catch (err) {
const errorMessage = err instanceof Error ? err.message : 'Swap failed';
setSwapError(errorMessage);
@@ -446,7 +507,46 @@ export function useSwapMint() {
const receipt = await publicClient.waitForTransactionReceipt({ hash });
console.log('✅ SwapMint transaction confirmed:', receipt);
return receipt;
// Parse the SwapMint event from the receipt logs
let actualSwapMintAmounts: ActualSwapMintAmounts | null = null;
for (const log of receipt.logs) {
try {
const decodedLog = decodeEventLog({
abi: IPartyPoolABI,
data: log.data,
topics: log.topics,
});
if (decodedLog.eventName === 'SwapMint') {
const { amountIn, amountOut, lpFee, protocolFee } = decodedLog.args as {
amountIn: bigint;
amountOut: bigint;
lpFee: bigint;
protocolFee: bigint;
};
actualSwapMintAmounts = {
amountInUsed: amountIn,
lpMinted: amountOut,
lpFee,
protocolFee,
};
console.log('📊 Actual swap mint amounts from event:', {
amountInUsed: amountIn.toString(),
lpMinted: amountOut.toString(),
lpFee: lpFee.toString(),
protocolFee: protocolFee.toString(),
});
break;
}
} catch (err) {
// Skip logs that don't match our ABI
continue;
}
}
return { receipt, actualSwapMintAmounts };
} catch (err) {
const errorMessage = err instanceof Error ? err.message : 'SwapMint failed';
setSwapMintError(errorMessage);
@@ -553,7 +653,46 @@ export function useBurnSwap() {
const receipt = await publicClient.waitForTransactionReceipt({ hash });
console.log('✅ BurnSwap transaction confirmed:', receipt);
return receipt;
// Parse the BurnSwap event from the receipt logs
let actualBurnSwapAmounts: ActualBurnSwapAmounts | null = null;
for (const log of receipt.logs) {
try {
const decodedLog = decodeEventLog({
abi: IPartyPoolABI,
data: log.data,
topics: log.topics,
});
if (decodedLog.eventName === 'BurnSwap') {
const { amountIn, amountOut, lpFee, protocolFee } = decodedLog.args as {
amountIn: bigint;
amountOut: bigint;
lpFee: bigint;
protocolFee: bigint;
};
actualBurnSwapAmounts = {
amountIn,
amountOut,
lpFee,
protocolFee,
};
console.log('📊 Actual burn swap amounts from event:', {
amountIn: amountIn.toString(),
amountOut: amountOut.toString(),
lpFee: lpFee.toString(),
protocolFee: protocolFee.toString(),
});
break;
}
} catch (err) {
// Skip logs that don't match our ABI
continue;
}
}
return { receipt, actualBurnSwapAmounts };
} catch (err) {
const errorMessage = err instanceof Error ? err.message : 'BurnSwap failed';
setBurnSwapError(errorMessage);