slippage for swaps
This commit is contained in:
@@ -99,6 +99,8 @@ export interface SwapRoute {
|
||||
poolAddress: `0x${string}`;
|
||||
inputTokenIndex: number;
|
||||
outputTokenIndex: number;
|
||||
inputTokenDecimal: number;
|
||||
outputTokenDecimal: number;
|
||||
}
|
||||
|
||||
export interface AvailableToken {
|
||||
@@ -234,11 +236,29 @@ export function useGetPoolsByToken(tokenAddress: `0x${string}` | undefined, offs
|
||||
functionName: 'symbol',
|
||||
}).catch(() => null);
|
||||
|
||||
const inputTokenDecimal = await publicClient.readContract({
|
||||
address: tokenAddress,
|
||||
abi: ERC20ABI,
|
||||
functionName: 'decimals',
|
||||
}).catch(() => null);
|
||||
|
||||
const outputTokenDecimal = await publicClient.readContract({
|
||||
address: outputTokenAddress,
|
||||
abi: ERC20ABI,
|
||||
functionName: 'decimals',
|
||||
}).catch(() => null);
|
||||
|
||||
// Skip tokens with the same symbol as the selected token
|
||||
if (!outputTokenSymbol || outputTokenSymbol === selectedTokenSymbol) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip tokens if decimals failed to load
|
||||
if (inputTokenDecimal === null || outputTokenDecimal === null) {
|
||||
console.error(`Failed to load decimals for token ${outputTokenAddress} or ${tokenAddress}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Create or update the available token entry
|
||||
const tokenKey = outputTokenAddress.toLowerCase();
|
||||
if (!tokenRoutesMap.has(tokenKey)) {
|
||||
@@ -254,6 +274,8 @@ export function useGetPoolsByToken(tokenAddress: `0x${string}` | undefined, offs
|
||||
poolAddress,
|
||||
inputTokenIndex,
|
||||
outputTokenIndex,
|
||||
inputTokenDecimal,
|
||||
outputTokenDecimal,
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
@@ -505,6 +527,8 @@ export function useGetAllPools(offset: number = 0, limit: number = 100) {
|
||||
|
||||
// Calculate TVL (approximate by getting first token balance and multiplying by 3)
|
||||
const tokenBalance = Number(balance) / Math.pow(10, decimals);
|
||||
|
||||
console.log('tokenBalance', tokenBalance);
|
||||
const approximateTVL = tokenBalance * 3;
|
||||
tvlStr = formatTVL(approximateTVL);
|
||||
}
|
||||
|
||||
@@ -21,13 +21,12 @@ const Q96 = 1n << 96n;
|
||||
*/
|
||||
export function calculateSlippage(
|
||||
marketPrice: number,
|
||||
swapOutputAmount: bigint,
|
||||
swapInputAmount: bigint,
|
||||
swapFee: bigint
|
||||
swapOutputAmount: number,
|
||||
swapInputAmount: number,
|
||||
swapFee: number
|
||||
): number {
|
||||
// Calculate actual swap price with decimal correction
|
||||
const swapPrice = Number(swapOutputAmount) / (Number(swapInputAmount) - Number(swapFee));
|
||||
|
||||
const swapPrice = swapOutputAmount / ((swapInputAmount) - (swapFee));
|
||||
// Calculate slippage percentage: ((swapPrice - marketPrice) / marketPrice) * 100
|
||||
const slippage = ((marketPrice - swapPrice) / marketPrice) * 100;
|
||||
|
||||
@@ -194,11 +193,18 @@ export function useSwapAmounts(
|
||||
args: [route.poolAddress, BigInt(route.inputTokenIndex), BigInt(route.outputTokenIndex)],
|
||||
}) as bigint;
|
||||
|
||||
// Convert Q64 format to decimal (price = priceValue / 2^64)
|
||||
const marketPrice = Number(priceInt128) / 2 ** 64;
|
||||
|
||||
// Convert Q128 format to decimal (price = priceValue / 2^128)
|
||||
// Then apply decimal conversion: 10^18 / 10^outputTokenDecimals
|
||||
const priceQ128 = Number(priceInt128) / 2 ** 128;
|
||||
const decimalAdjustment = 10 ** Math.abs(route.outputTokenDecimal - route.inputTokenDecimal);
|
||||
const marketPrice = priceQ128 / decimalAdjustment;
|
||||
const swapOutputAmountDecimal = Number(swapOutputAmount) / 10 ** route.outputTokenDecimal;
|
||||
const swapInputAmountDecimal = Number(swapInputAmount) / 10 ** route.inputTokenDecimal;
|
||||
const swapFeeDecimal = Number(inFee) / 10 ** route.inputTokenDecimal;
|
||||
// Calculate slippage using the reusable function
|
||||
calculatedSlippage = calculateSlippage(marketPrice, swapOutputAmount, swapInputAmount, inFee);
|
||||
|
||||
calculatedSlippage = calculateSlippage(marketPrice, swapOutputAmountDecimal, swapInputAmountDecimal, swapFeeDecimal);
|
||||
console.log('calculatedSlippage', calculatedSlippage)
|
||||
} catch (slippageErr) {
|
||||
console.error(`Error calculating slippage for ${token.symbol}:`, slippageErr);
|
||||
|
||||
Reference in New Issue
Block a user