removing limit price from swap. updating slippage warnings and updating ABIs
This commit is contained in:
@@ -397,9 +397,9 @@ export function SwapForm() {
|
|||||||
Math.abs(swapAmounts[0].calculatedSlippage) > currentSlippage
|
Math.abs(swapAmounts[0].calculatedSlippage) > currentSlippage
|
||||||
) && (
|
) && (
|
||||||
<div className="px-4 py-3 bg-yellow-500/10 border border-yellow-500/20 rounded-lg">
|
<div className="px-4 py-3 bg-yellow-500/10 border border-yellow-500/20 rounded-lg">
|
||||||
<p className="text-sm text-yellow-600 dark:text-yellow-500 font-medium">⚠️ High Slippage Warning</p>
|
<p className="text-sm text-yellow-600 dark:text-yellow-500 font-medium">⚠️ Slippage Exceeds Your Tolerance</p>
|
||||||
<p className="text-xs text-yellow-600/80 dark:text-yellow-500/80 mt-1">
|
<p className="text-xs text-yellow-600/80 dark:text-yellow-500/80 mt-1">
|
||||||
The estimated slippage for this swap is {Math.abs(swapAmounts[0].calculatedSlippage).toFixed(2)}%. You may lose money due to low liquidity in this pool.
|
The estimated slippage for this swap is {Math.abs(swapAmounts[0].calculatedSlippage).toFixed(2)}%, which exceeds your maximum slippage setting of {currentSlippage}%. This swap may result in less favorable pricing than expected due to low liquidity.
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
@@ -493,7 +493,7 @@ export function SwapForm() {
|
|||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<p className="text-xs text-muted-foreground mt-2">
|
<p className="text-xs text-muted-foreground mt-2">
|
||||||
Your transaction will revert if the price changes unfavorably by more than this percentage.
|
You will be warned if the slippage exceeds this setting, and you can choose whether to proceed with the trade.
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -183,8 +183,8 @@ const IPartyInfoABI = [
|
|||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "",
|
"name": "",
|
||||||
"type": "int128",
|
"type": "uint256",
|
||||||
"internalType": "int128"
|
"internalType": "uint256"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"stateMutability": "view"
|
"stateMutability": "view"
|
||||||
|
|||||||
@@ -415,6 +415,19 @@ const IPartyPoolABI = [
|
|||||||
],
|
],
|
||||||
"stateMutability": "payable"
|
"stateMutability": "payable"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "function",
|
||||||
|
"name": "mintImpl",
|
||||||
|
"inputs": [],
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "",
|
||||||
|
"type": "address",
|
||||||
|
"internalType": "address"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"stateMutability": "view"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "function",
|
"type": "function",
|
||||||
"name": "name",
|
"name": "name",
|
||||||
@@ -605,6 +618,19 @@ const IPartyPoolABI = [
|
|||||||
],
|
],
|
||||||
"stateMutability": "view"
|
"stateMutability": "view"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "function",
|
||||||
|
"name": "swapImpl",
|
||||||
|
"inputs": [],
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "",
|
||||||
|
"type": "address",
|
||||||
|
"internalType": "address"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"stateMutability": "view"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "function",
|
"type": "function",
|
||||||
"name": "swapMint",
|
"name": "swapMint",
|
||||||
|
|||||||
@@ -120,18 +120,6 @@ export function useSwapAmounts(
|
|||||||
setLoading(true);
|
setLoading(true);
|
||||||
|
|
||||||
const amountInTokenUnits = parseUnits(fromAmount, fromTokenDecimals);
|
const amountInTokenUnits = parseUnits(fromAmount, fromTokenDecimals);
|
||||||
// Calculate limit price based on slippage tolerance
|
|
||||||
// limitPrice in Q96 format = Q96 * (100 + slippage%) / 100
|
|
||||||
// This represents the maximum acceptable price ratio (1 + slippage%)
|
|
||||||
const slippageBasisPoints = BigInt(Math.floor(slippagePercent * 100)); // Convert to basis points (0.5% = 50)
|
|
||||||
const limitPrice = (Q96 * (10000n + slippageBasisPoints)) / 10000n;
|
|
||||||
|
|
||||||
console.log('Limit Price Calculation:', {
|
|
||||||
slippagePercent,
|
|
||||||
slippageBasisPoints: slippageBasisPoints.toString(),
|
|
||||||
limitPriceQ96: limitPrice.toString(),
|
|
||||||
Q96: Q96.toString(),
|
|
||||||
});
|
|
||||||
|
|
||||||
const results: SwapAmountResult[] = [];
|
const results: SwapAmountResult[] = [];
|
||||||
const chainId = await publicClient.getChainId();
|
const chainId = await publicClient.getChainId();
|
||||||
@@ -146,8 +134,8 @@ export function useSwapAmounts(
|
|||||||
// Evaluate ALL routes for this token
|
// Evaluate ALL routes for this token
|
||||||
for (const route of token.swapRoutes) {
|
for (const route of token.swapRoutes) {
|
||||||
try {
|
try {
|
||||||
// Get swap amounts
|
// Get swap amounts with NO LIMIT
|
||||||
const swapResult = await publicClient.readContract({
|
const [swapInputAmount, swapOutputAmount, inFee] = await publicClient.readContract({
|
||||||
address: route.poolAddress,
|
address: route.poolAddress,
|
||||||
abi: IPartyPoolABI,
|
abi: IPartyPoolABI,
|
||||||
functionName: 'swapAmounts',
|
functionName: 'swapAmounts',
|
||||||
@@ -155,12 +143,10 @@ export function useSwapAmounts(
|
|||||||
BigInt(route.inputTokenIndex),
|
BigInt(route.inputTokenIndex),
|
||||||
BigInt(route.outputTokenIndex),
|
BigInt(route.outputTokenIndex),
|
||||||
amountInTokenUnits,
|
amountInTokenUnits,
|
||||||
limitPrice,
|
0n, // NO LIMIT
|
||||||
],
|
],
|
||||||
}) as readonly [bigint, bigint, bigint];
|
}) as readonly [bigint, bigint, bigint];
|
||||||
|
|
||||||
const [amountIn, amountOut, fee] = swapResult;
|
|
||||||
|
|
||||||
// Get kappa for this pool
|
// Get kappa for this pool
|
||||||
const kappa = await publicClient.readContract({
|
const kappa = await publicClient.readContract({
|
||||||
address: route.poolAddress,
|
address: route.poolAddress,
|
||||||
@@ -172,19 +158,6 @@ export function useSwapAmounts(
|
|||||||
let calculatedSlippage: number | undefined;
|
let calculatedSlippage: number | undefined;
|
||||||
if (partyInfoAddress) {
|
if (partyInfoAddress) {
|
||||||
try {
|
try {
|
||||||
// Get swap amounts with NO LIMIT (0 means no price limit)
|
|
||||||
const [swapInputAmount, swapOutputAmount, inFee] = await publicClient.readContract({
|
|
||||||
address: route.poolAddress,
|
|
||||||
abi: IPartyPoolABI,
|
|
||||||
functionName: 'swapAmounts',
|
|
||||||
args: [
|
|
||||||
BigInt(route.inputTokenIndex),
|
|
||||||
BigInt(route.outputTokenIndex),
|
|
||||||
amountInTokenUnits,
|
|
||||||
0n, // NO LIMIT
|
|
||||||
],
|
|
||||||
}) as readonly [bigint, bigint, bigint];
|
|
||||||
|
|
||||||
// Get the current market price from PoolInfo
|
// Get the current market price from PoolInfo
|
||||||
const priceInt128 = await publicClient.readContract({
|
const priceInt128 = await publicClient.readContract({
|
||||||
address: partyInfoAddress,
|
address: partyInfoAddress,
|
||||||
@@ -193,7 +166,6 @@ export function useSwapAmounts(
|
|||||||
args: [route.poolAddress, BigInt(route.inputTokenIndex), BigInt(route.outputTokenIndex)],
|
args: [route.poolAddress, BigInt(route.inputTokenIndex), BigInt(route.outputTokenIndex)],
|
||||||
}) as bigint;
|
}) as bigint;
|
||||||
|
|
||||||
|
|
||||||
// Convert Q128 format to decimal (price = priceValue / 2^128)
|
// Convert Q128 format to decimal (price = priceValue / 2^128)
|
||||||
// Then apply decimal conversion: 10^18 / 10^outputTokenDecimals
|
// Then apply decimal conversion: 10^18 / 10^outputTokenDecimals
|
||||||
const priceQ128 = Number(priceInt128) / 2 ** 128;
|
const priceQ128 = Number(priceInt128) / 2 ** 128;
|
||||||
@@ -214,9 +186,9 @@ export function useSwapAmounts(
|
|||||||
routeResults.push({
|
routeResults.push({
|
||||||
tokenAddress: token.address,
|
tokenAddress: token.address,
|
||||||
tokenSymbol: token.symbol,
|
tokenSymbol: token.symbol,
|
||||||
amountIn,
|
amountIn: swapInputAmount,
|
||||||
amountOut,
|
amountOut: swapOutputAmount,
|
||||||
fee,
|
fee: inFee,
|
||||||
poolAddress: route.poolAddress,
|
poolAddress: route.poolAddress,
|
||||||
kappa,
|
kappa,
|
||||||
inputTokenIndex: route.inputTokenIndex,
|
inputTokenIndex: route.inputTokenIndex,
|
||||||
|
|||||||
Reference in New Issue
Block a user