diff --git a/EthereumTransactionCall.sh b/EthereumTransactionCall.sh new file mode 100644 index 0000000..222a9f2 --- /dev/null +++ b/EthereumTransactionCall.sh @@ -0,0 +1,4 @@ +cast call 0x8f98B899F4135408Fe03228cE942Ad6BF8E40f22 \ + "working(address)" \ + 0x3EDE1eE859A72aEc85ff04d305B6Ffe89f2Cb4eb \ + --rpc-url https://eth-sepolia.g.alchemy.com/v2/demo diff --git a/src/hooks/usePartyPlanner.ts b/src/hooks/usePartyPlanner.ts index 95bfd66..6755ea4 100644 --- a/src/hooks/usePartyPlanner.ts +++ b/src/hooks/usePartyPlanner.ts @@ -580,7 +580,8 @@ export function useSwapMintAmounts( poolAddress: `0x${string}` | undefined, inputTokenIndex: number | undefined, maxAmountIn: bigint | undefined, - lpTokenPrice?: number // Market price of the LP token in decimal format + lpTokenPrice?: number, // Market price of the LP token in decimal format + inputTokenDecimals?: number // Decimals of the input token ) { const publicClient = usePublicClient(); const [mounted, setMounted] = useState(false); @@ -629,13 +630,42 @@ export function useSwapMintAmounts( args: [poolAddress, BigInt(inputTokenIndex), maxAmountIn], }) as readonly [bigint, bigint, bigint]; - // Calculate slippage if LP token price is provided - let calculatedSlippage: number | undefined; - if (lpTokenPrice !== undefined) { + // Fetch the market price if not provided + let marketPrice = lpTokenPrice; + if (marketPrice === undefined) { try { - // For swapMint: output is result[0] (amountInUsed), input is maxAmountIn, fee is result[2] - calculatedSlippage = calculateSlippage(lpTokenPrice, result[0], maxAmountIn, result[2]); - console.log('swapMint calculatedSlippage', calculatedSlippage); + console.log('input token index', inputTokenIndex) + // Get the pool price (price of the pool in terms of the input token) + const poolPriceInt128 = await publicClient.readContract({ + address: partyInfoAddress as `0x${string}`, + abi: IPartyInfoABI, + functionName: 'poolPrice', + args: [poolAddress, BigInt(inputTokenIndex)], + }) as bigint; + + // Convert Q64 format to decimal (price = priceValue / 2^64) + // poolPrice returns how much 1 LP token is worth in terms of the input token + marketPrice = Number(poolPriceInt128) / 2 ** 64; + console.log('swapMintAmounts fetched marketPrice (poolPrice)', marketPrice); + } catch (priceErr) { + console.error('Error fetching poolPrice:', priceErr); + } + } + + // Calculate slippage if market price is available + let calculatedSlippage: number | undefined; + if (marketPrice !== undefined) { + try { + // For swapMint: + // - marketPrice: how much 1 LP token is worth in input tokens (from poolPrice) + // - swapOutputAmount: LP tokens minted (result[2]) + // - swapInputAmount: input token amount used (maxAmountIn) + // - inFee: fee charged (result[1]) + console.log('LP minted', result[1]) + // Convert result[1] to token price using poolPrice: result[1] * poolPrice + const lpMintedInTokenPrice = BigInt(Math.floor(Number(result[1]) * marketPrice)); + calculatedSlippage = calculateSlippage(marketPrice, lpMintedInTokenPrice, result[0], result[2]); + console.log('🎯 swapMint calculatedSlippage: ' + calculatedSlippage.toFixed(4) + '%'); } catch (slippageErr) { console.error(`Error calculating slippage for swapMint:`, slippageErr); } diff --git a/src/hooks/usePartyPool.ts b/src/hooks/usePartyPool.ts index 696d222..382e7dc 100644 --- a/src/hooks/usePartyPool.ts +++ b/src/hooks/usePartyPool.ts @@ -29,7 +29,7 @@ export function calculateSlippage( const swapPrice = Number(swapOutputAmount) / (Number(swapInputAmount) - Number(swapFee)); // Calculate slippage percentage: ((swapPrice - marketPrice) / marketPrice) * 100 - const slippage = ((swapPrice - marketPrice) / marketPrice) * 100; + const slippage = ((marketPrice - swapPrice) / marketPrice) * 100; return slippage; }