WIP: slipage for swapAmounts
This commit is contained in:
4
EthereumTransactionCall.sh
Normal file
4
EthereumTransactionCall.sh
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
cast call 0x8f98B899F4135408Fe03228cE942Ad6BF8E40f22 \
|
||||||
|
"working(address)" \
|
||||||
|
0x3EDE1eE859A72aEc85ff04d305B6Ffe89f2Cb4eb \
|
||||||
|
--rpc-url https://eth-sepolia.g.alchemy.com/v2/demo
|
||||||
@@ -580,7 +580,8 @@ export function useSwapMintAmounts(
|
|||||||
poolAddress: `0x${string}` | undefined,
|
poolAddress: `0x${string}` | undefined,
|
||||||
inputTokenIndex: number | undefined,
|
inputTokenIndex: number | undefined,
|
||||||
maxAmountIn: bigint | 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 publicClient = usePublicClient();
|
||||||
const [mounted, setMounted] = useState(false);
|
const [mounted, setMounted] = useState(false);
|
||||||
@@ -629,13 +630,42 @@ export function useSwapMintAmounts(
|
|||||||
args: [poolAddress, BigInt(inputTokenIndex), maxAmountIn],
|
args: [poolAddress, BigInt(inputTokenIndex), maxAmountIn],
|
||||||
}) as readonly [bigint, bigint, bigint];
|
}) as readonly [bigint, bigint, bigint];
|
||||||
|
|
||||||
// Calculate slippage if LP token price is provided
|
// Fetch the market price if not provided
|
||||||
let calculatedSlippage: number | undefined;
|
let marketPrice = lpTokenPrice;
|
||||||
if (lpTokenPrice !== undefined) {
|
if (marketPrice === undefined) {
|
||||||
try {
|
try {
|
||||||
// For swapMint: output is result[0] (amountInUsed), input is maxAmountIn, fee is result[2]
|
console.log('input token index', inputTokenIndex)
|
||||||
calculatedSlippage = calculateSlippage(lpTokenPrice, result[0], maxAmountIn, result[2]);
|
// Get the pool price (price of the pool in terms of the input token)
|
||||||
console.log('swapMint calculatedSlippage', calculatedSlippage);
|
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) {
|
} catch (slippageErr) {
|
||||||
console.error(`Error calculating slippage for swapMint:`, slippageErr);
|
console.error(`Error calculating slippage for swapMint:`, slippageErr);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ export function calculateSlippage(
|
|||||||
const swapPrice = Number(swapOutputAmount) / (Number(swapInputAmount) - Number(swapFee));
|
const swapPrice = Number(swapOutputAmount) / (Number(swapInputAmount) - Number(swapFee));
|
||||||
|
|
||||||
// Calculate slippage percentage: ((swapPrice - marketPrice) / marketPrice) * 100
|
// Calculate slippage percentage: ((swapPrice - marketPrice) / marketPrice) * 100
|
||||||
const slippage = ((swapPrice - marketPrice) / marketPrice) * 100;
|
const slippage = ((marketPrice - swapPrice) / marketPrice) * 100;
|
||||||
|
|
||||||
return slippage;
|
return slippage;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user