From 5cc078bab3115eade4ba9320b84eb0d9664d6c22 Mon Sep 17 00:00:00 2001 From: chopan123 Date: Thu, 5 Dec 2024 18:02:28 -0300 Subject: [PATCH] fix calculations, fix path --- .../fromExactOutputGetExpectedInput.tsx | 3 +-- src/functions/generateRoute.ts | 19 ++++++------- src/functions/getExpectedAmount.tsx | 27 ++++++++++++++----- 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/functions/fromExactOutputGetExpectedInput.tsx b/src/functions/fromExactOutputGetExpectedInput.tsx index a0719365..3b3635d8 100644 --- a/src/functions/fromExactOutputGetExpectedInput.tsx +++ b/src/functions/fromExactOutputGetExpectedInput.tsx @@ -9,8 +9,7 @@ export default function fromExactOutputGetExpectedInput( reserve0: BigNumber | undefined, reserve1: BigNumber | undefined, ): BigNumber { - if (!amountIn || !reserve0 || !reserve1) return BigNumber(0) - + if (!amountIn || !reserve0 || !reserve1) return BigNumber(0); return getExpectedAmountFromReserves(amountIn, reserve0, reserve1); } diff --git a/src/functions/generateRoute.ts b/src/functions/generateRoute.ts index 4c3f610f..4d2e2ca4 100644 --- a/src/functions/generateRoute.ts +++ b/src/functions/generateRoute.ts @@ -136,7 +136,7 @@ export const useRouterSDK = () => { } // Get amountOut or amountIn from reserves and tradeType - let outputAmount = await getExpectedAmount( + let quoteAmount = await getExpectedAmount( amountAsset.currency, quoteAsset, new BigNumber(amount), @@ -145,20 +145,18 @@ export const useRouterSDK = () => { ); // Convert from lumens to stroops (multiply by 10^7) - outputAmount = outputAmount.integerValue(); - - const quoteCurrencyAmount = CurrencyAmount.fromRawAmount(fromAddressToToken(quoteAsset.contract), outputAmount.toString()); + quoteAmount = quoteAmount.integerValue(); return { amountCurrency: amountAsset, - quoteCurrency: CurrencyAmount.fromRawAmount(fromAddressToToken(quoteAsset.contract), outputAmount.toString()), + quoteCurrency: CurrencyAmount.fromRawAmount(fromAddressToToken(quoteAsset.contract), quoteAmount.toString()), tradeType, trade: { - amountIn: tradeType === TradeType.EXACT_INPUT ? amount : outputAmount.toString(), - amountInMax: tradeType === TradeType.EXACT_OUTPUT ? outputAmount.toString() : undefined, - amountOut: tradeType === TradeType.EXACT_INPUT ? outputAmount.toString() : amount, - amountOutMin: tradeType === TradeType.EXACT_INPUT ? outputAmount.toString() : undefined, - path: [amountAsset.currency.contract, quoteAsset.contract], + amountIn: tradeType === TradeType.EXACT_INPUT ? amount : quoteAmount.toString(), + amountInMax: tradeType === TradeType.EXACT_OUTPUT ? quoteAmount.toString() : undefined, + amountOut: tradeType === TradeType.EXACT_INPUT ? quoteAmount.toString() : amount, + amountOutMin: tradeType === TradeType.EXACT_INPUT ? quoteAmount.toString() : undefined, + path: tradeType === TradeType.EXACT_INPUT ? [amountAsset.currency.contract, quoteAsset.contract] : [quoteAsset.contract, amountAsset.currency.contract], }, priceImpact: new Percent('0'), platform: PlatformType.ROUTER, @@ -168,7 +166,6 @@ export const useRouterSDK = () => { throw error; } } - if (!factory) throw new Error('Factory address not found'); const currencyAmount = fromAddressAndAmountToCurrencyAmount( amountAsset.currency.contract, diff --git a/src/functions/getExpectedAmount.tsx b/src/functions/getExpectedAmount.tsx index a822302a..4260099c 100644 --- a/src/functions/getExpectedAmount.tsx +++ b/src/functions/getExpectedAmount.tsx @@ -32,23 +32,36 @@ export async function getExpectedAmount( ); const reserves = customReserves ?? (await reservesBNWithTokens(pairAddress, sorobanContext)); + if (!reserves || !reserves.reserve0 || !reserves.reserve1) { + throw new Error('Reserves not found or invalid'); + } + + let reserveIn: BigNumber, reserveOut: BigNumber; + if (currencyIn.contract === reserves.token0) { + reserveIn = reserves.reserve0; + reserveOut = reserves.reserve1; + } else if (currencyIn.contract === reserves.token1) { + reserveIn = reserves.reserve1; + reserveOut = reserves.reserve0; + } else { + throw new Error('CurrencyIn does not match any token in reserves'); + } + let expectedOutput; if (tradeType === TradeType.EXACT_INPUT) { expectedOutput = fromExactInputGetExpectedOutput( amountIn, - reserves.reserve0, - reserves.reserve1, + reserveIn, + reserveOut, + currencyIn.decimals ?? 7, ); } else { - expectedOutput = fromExactOutputGetExpectedInput( - amountIn, - reserves.reserve0, - reserves.reserve1, - ); + expectedOutput = fromExactOutputGetExpectedInput(amountIn, reserveOut, reserveIn); } return expectedOutput; } catch (error) { + console.error('Error in getExpectedAmount:', error); return BigNumber(0); } }