[wip] adding swap amount conversion to the swam-form
This commit is contained in:
113
src/hooks/usePartyPool.ts
Normal file
113
src/hooks/usePartyPool.ts
Normal file
@@ -0,0 +1,113 @@
|
||||
'use client';
|
||||
|
||||
import { useState, useEffect } from 'react';
|
||||
import { usePublicClient } from 'wagmi';
|
||||
import IPartyPoolABI from '@/contracts/IPartyPoolABI';
|
||||
import type { AvailableToken } from './usePartyPlanner';
|
||||
|
||||
export interface SwapAmountResult {
|
||||
tokenAddress: `0x${string}`;
|
||||
tokenSymbol: string;
|
||||
amountIn: bigint;
|
||||
amountOut: bigint;
|
||||
fee: bigint;
|
||||
poolAddress: `0x${string}`;
|
||||
}
|
||||
|
||||
export function useSwapAmounts(
|
||||
availableTokens: AvailableToken[] | null,
|
||||
fromAmount: string,
|
||||
fromTokenDecimals: number
|
||||
) {
|
||||
const publicClient = usePublicClient();
|
||||
const [mounted, setMounted] = useState(false);
|
||||
const [swapAmounts, setSwapAmounts] = useState<SwapAmountResult[] | null>(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
setMounted(true);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!mounted || !availableTokens || !fromAmount || parseFloat(fromAmount) <= 0) {
|
||||
setSwapAmounts(null);
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const calculateSwapAmounts = async () => {
|
||||
if (!publicClient) {
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
setLoading(true);
|
||||
|
||||
// Parse the from amount to the token's decimals
|
||||
const amountInWei = BigInt(Math.floor(parseFloat(fromAmount) * Math.pow(10, fromTokenDecimals)));
|
||||
|
||||
// Use a very large limit price (essentially no limit) - user will replace later
|
||||
// int128 max is 2^127 - 1, but we'll use a reasonable large number
|
||||
const limitPrice = BigInt('170141183460469231731687303715884105727'); // max int128
|
||||
|
||||
const results: SwapAmountResult[] = [];
|
||||
|
||||
// Calculate swap amounts for each available token using their first swap route
|
||||
for (const token of availableTokens) {
|
||||
if (token.swapRoutes.length === 0) continue;
|
||||
|
||||
// Use the first swap route for now
|
||||
const route = token.swapRoutes[0];
|
||||
|
||||
try {
|
||||
const swapResult = await publicClient.readContract({
|
||||
address: route.poolAddress,
|
||||
abi: IPartyPoolABI,
|
||||
functionName: 'swapAmounts',
|
||||
args: [
|
||||
BigInt(route.inputTokenIndex),
|
||||
BigInt(route.outputTokenIndex),
|
||||
amountInWei,
|
||||
limitPrice,
|
||||
],
|
||||
}) as readonly [bigint, bigint, bigint];
|
||||
|
||||
const [amountIn, amountOut, fee] = swapResult;
|
||||
|
||||
results.push({
|
||||
tokenAddress: token.address,
|
||||
tokenSymbol: token.symbol,
|
||||
amountIn,
|
||||
amountOut,
|
||||
fee,
|
||||
poolAddress: route.poolAddress,
|
||||
});
|
||||
|
||||
console.log(`Swap ${token.symbol}:`, {
|
||||
amountIn: amountIn.toString(),
|
||||
amountOut: amountOut.toString(),
|
||||
fee: fee.toString(),
|
||||
pool: route.poolAddress,
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(`Error calculating swap for ${token.symbol}:`, err);
|
||||
}
|
||||
}
|
||||
|
||||
setSwapAmounts(results);
|
||||
} catch (err) {
|
||||
console.error('Error calculating swap amounts:', err);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
calculateSwapAmounts();
|
||||
}, [publicClient, mounted, availableTokens, fromAmount, fromTokenDecimals]);
|
||||
|
||||
return {
|
||||
swapAmounts,
|
||||
loading,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user