fix: use pool reserves ratio for liquidity calculation instead of swap quotes

This commit is contained in:
2026-02-06 14:17:23 +03:00
parent dcb5a2c313
commit 0a2e38cb27
+76 -125
View File
@@ -177,45 +177,50 @@ export const AddLiquidityModal: React.FC<AddLiquidityModalProps> = ({
fetchAssetHubBalances(); fetchAssetHubBalances();
}, [assetHubApi, isAssetHubReady, selectedAccount, isOpen, asset0, asset1]); }, [assetHubApi, isAssetHubReady, selectedAccount, isOpen, asset0, asset1]);
// Fetch minimum deposit requirements from runtime and pool reserves // Note: Minimum deposits are calculated in the fetchPoolReserves effect below
// based on the actual pool ratio and asset minBalances
// Helper to convert asset ID to XCM Location format
const formatAssetLocation = (id: number) => {
if (id === -1) {
// Native token from relay chain
return { parents: 1, interior: 'Here' };
}
// Asset on Asset Hub
return { parents: 0, interior: { X2: [{ PalletInstance: 50 }, { GeneralIndex: id }] } };
};
// Fetch current pool reserves and calculate ratio + minimums
useEffect(() => { useEffect(() => {
if (!assetHubApi || !isAssetHubReady || !isOpen) return; if (!assetHubApi || !isAssetHubReady || !isOpen) return;
const fetchMinimumBalances = async () => { const fetchPoolReserves = async () => {
try { try {
// Default minimums // First, get asset minBalances from chain
let minBalance0 = 0.1; let asset0MinBalance = 0.1; // default for native
let minBalance1 = 0.1; let asset1MinBalance = 0.1; // default
// Query asset minBalance for non-native tokens
if (asset0 >= 0) {
const assetDetails0 = await assetHubApi.query.assets.asset(asset0);
if (assetDetails0.isSome) {
const details0 = assetDetails0.unwrap().toJSON() as AssetDetails;
const minBalance0Raw = details0.minBalance || '0';
const fetchedMin0 = Number(minBalance0Raw) / Math.pow(10, asset0Decimals);
minBalance0 = Math.max(fetchedMin0, 0.1);
}
}
if (asset1 >= 0) { if (asset1 >= 0) {
const assetDetails1 = await assetHubApi.query.assets.asset(asset1); try {
if (assetDetails1.isSome) { const assetDetails = await assetHubApi.query.assets.asset(asset1);
const details1 = assetDetails1.unwrap().toJSON() as AssetDetails; if (assetDetails.isSome) {
const minBalance1Raw = details1.minBalance || '0'; const details = assetDetails.unwrap().toJSON() as AssetDetails;
const fetchedMin1 = Number(minBalance1Raw) / Math.pow(10, asset1Decimals); const minBalRaw = details.minBalance || '0';
minBalance1 = Math.max(fetchedMin1, 0.1); asset1MinBalance = Math.max(Number(minBalRaw) / Math.pow(10, asset1Decimals), 0.1);
if (import.meta.env.DEV) console.log(`Asset ${asset1} minBalance:`, asset1MinBalance);
}
} catch (e) {
if (import.meta.env.DEV) console.log('Could not fetch asset1 minBalance');
} }
} }
// Get pool reserves to calculate ratio-based minimums const asset0Location = formatAssetLocation(asset0);
const asset0Location = asset0 === -1 const asset1Location = formatAssetLocation(asset1);
? { parents: 1, interior: 'Here' } const poolKey = [asset0Location, asset1Location];
: { parents: 0, interior: { X2: [{ PalletInstance: 50 }, { GeneralIndex: asset0 }] } }; const poolInfo = await assetHubApi.query.assetConversion.pools(poolKey);
const asset1Location = asset1 === -1
? { parents: 1, interior: 'Here' }
: { parents: 0, interior: { X2: [{ PalletInstance: 50 }, { GeneralIndex: asset1 }] } };
if (poolInfo.isSome) {
// Pool exists - get reserves for exact ratio
try { try {
const reserves = await assetHubApi.call.assetConversionApi.getReserves( const reserves = await assetHubApi.call.assetConversionApi.getReserves(
asset0Location, asset0Location,
@@ -228,117 +233,63 @@ export const AddLiquidityModal: React.FC<AddLiquidityModalProps> = ({
const reserve1 = Number(reserve1Raw.toString()) / Math.pow(10, asset1Decimals); const reserve1 = Number(reserve1Raw.toString()) / Math.pow(10, asset1Decimals);
if (reserve0 > 0 && reserve1 > 0) { if (reserve0 > 0 && reserve1 > 0) {
// Calculate minimums based on pool ratio // Use exact reserve ratio for liquidity (no slippage/fees)
// If asset1 min is 0.1 DOT and ratio is 1:3, then asset0 min should be 0.3 HEZ const ratio = reserve1 / reserve0; // asset1 per asset0
const ratioBasedMin0 = minBalance1 * (reserve0 / reserve1); setCurrentPrice(ratio);
const ratioBasedMin1 = minBalance0 * (reserve1 / reserve0); setIsPoolEmpty(false);
minBalance0 = Math.max(minBalance0, ratioBasedMin0); // Calculate minimums based on ratio
minBalance1 = Math.max(minBalance1, ratioBasedMin1); // Pool ratio: 3 HEZ = 1 DOT, so ratio = 1/3 = 0.333
// If DOT min is 0.1, then HEZ min = 0.1 / 0.333 = 0.3 HEZ
const requiredAsset0Min = asset1MinBalance / ratio;
const finalMin0 = Math.max(0.1, requiredAsset0Min);
const finalMin1 = asset1MinBalance;
setMinDeposit0(finalMin0);
setMinDeposit1(finalMin1);
if (import.meta.env.DEV) { if (import.meta.env.DEV) {
console.log('📊 Pool reserves:', { reserve0, reserve1 }); console.log('📊 Pool reserves:', { reserve0, reserve1 });
console.log('📊 Ratio-based minimums:', { ratioBasedMin0, ratioBasedMin1 }); console.log('📊 Ratio (asset1/asset0):', ratio, `(1 ${asset0Name} = ${ratio.toFixed(4)} ${asset1Name})`);
console.log('📊 Final minimums:', { [asset0Name]: finalMin0, [asset1Name]: finalMin1 });
} }
} else {
setIsPoolEmpty(true);
setCurrentPrice(null);
setMinDeposit0(0.1);
setMinDeposit1(asset1MinBalance);
} }
} else {
setIsPoolEmpty(true);
setCurrentPrice(null);
setMinDeposit0(0.1);
setMinDeposit1(asset1MinBalance);
if (import.meta.env.DEV) console.log('Pool exists but no reserves');
} }
} catch (reserveErr) { } catch (reserveErr) {
if (import.meta.env.DEV) console.log('Could not fetch reserves, using default minimums'); if (import.meta.env.DEV) console.error('Error getting reserves:', reserveErr);
setIsPoolEmpty(true);
setCurrentPrice(null);
setMinDeposit0(0.1);
setMinDeposit1(asset1MinBalance);
}
} else {
setCurrentPrice(null);
setIsPoolEmpty(true);
setMinDeposit0(0.1);
setMinDeposit1(asset1MinBalance);
if (import.meta.env.DEV) console.log('Pool does not exist yet');
} }
if (import.meta.env.DEV) console.log('📊 Final minimum deposits:', {
asset0: asset0Name, minBalance0,
asset1: asset1Name, minBalance1
});
setMinDeposit0(minBalance0);
setMinDeposit1(minBalance1);
} catch (err) { } catch (err) {
if (import.meta.env.DEV) console.error('Error fetching minimum balances:', err); if (import.meta.env.DEV) console.error('Error fetching pool:', err);
setCurrentPrice(null);
setIsPoolEmpty(true);
} }
}; };
fetchMinimumBalances(); fetchPoolReserves();
}, [assetHubApi, isAssetHubReady, isOpen, asset0, asset1, asset0Decimals, asset1Decimals, asset0Name, asset1Name]); }, [assetHubApi, isAssetHubReady, isOpen, asset0, asset1, asset0Decimals, asset1Decimals, asset0Name, asset1Name]);
// Helper to convert asset ID to XCM Location format
const formatAssetLocation = (id: number) => {
if (id === -1) {
// Native token from relay chain
return { parents: 1, interior: 'Here' };
}
// Asset on Asset Hub
return { parents: 0, interior: { X2: [{ PalletInstance: 50 }, { GeneralIndex: id }] } };
};
// Fetch current pool price and reserves
useEffect(() => {
if (!assetHubApi || !isAssetHubReady || !isOpen) return;
const fetchPoolPrice = async () => {
try {
// Use XCM Location format for pool queries (required for native token)
const asset0Location = formatAssetLocation(asset0);
const asset1Location = formatAssetLocation(asset1);
const poolKey = [asset0Location, asset1Location];
const poolInfo = await assetHubApi.query.assetConversion.pools(poolKey);
if (poolInfo.isSome) {
// Pool exists - try to get reserves via runtime API
try {
// Use quotePriceExactTokensForTokens to get the exchange rate
// This gives us the output amount for 1 unit of input
const oneUnit = BigInt(Math.pow(10, asset0Decimals)); // 1 token in smallest units
const quote = await assetHubApi.call.assetConversionApi.quotePriceExactTokensForTokens(
asset0Location,
asset1Location,
oneUnit.toString(),
true // include fee
);
if (quote && !quote.isNone) {
const outputAmount = Number(quote.unwrap().toString()) / Math.pow(10, asset1Decimals);
const inputAmount = 1; // We queried for 1 token
const price = outputAmount / inputAmount;
if (price > 0) {
setCurrentPrice(price);
setIsPoolEmpty(false);
if (import.meta.env.DEV) console.log('Pool price from runtime API:', price);
} else {
setIsPoolEmpty(true);
setCurrentPrice(null);
}
} else {
// Quote returned nothing - pool might be empty
setIsPoolEmpty(true);
setCurrentPrice(null);
if (import.meta.env.DEV) console.log('Pool exists but no quote available - may be empty');
}
} catch (quoteErr) {
if (import.meta.env.DEV) console.error('Error getting quote:', quoteErr);
// Pool exists but couldn't get quote - allow manual input
setIsPoolEmpty(true);
setCurrentPrice(null);
}
} else {
// Pool doesn't exist yet
setCurrentPrice(null);
setIsPoolEmpty(true);
if (import.meta.env.DEV) console.log('Pool does not exist yet - manual input allowed');
}
} catch (err) {
if (import.meta.env.DEV) console.error('Error fetching pool price:', err);
// On error, assume pool is empty to allow manual input
setCurrentPrice(null);
setIsPoolEmpty(true);
}
};
fetchPoolPrice();
}, [assetHubApi, isAssetHubReady, isOpen, asset0, asset1, asset0Decimals, asset1Decimals]);
// Auto-calculate asset1 amount based on asset0 input (only if pool has liquidity) // Auto-calculate asset1 amount based on asset0 input (only if pool has liquidity)
useEffect(() => { useEffect(() => {
if (!isPoolEmpty && amount0 && currentPrice) { if (!isPoolEmpty && amount0 && currentPrice) {