Skip to content

Commit

Permalink
Merge pull request #583 from soroswap/main
Browse files Browse the repository at this point in the history
fix path calculation
  • Loading branch information
esteblock authored Dec 6, 2024
2 parents ee53adc + f0369dc commit ac866ab
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
3 changes: 1 addition & 2 deletions src/functions/fromExactOutputGetExpectedInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
19 changes: 8 additions & 11 deletions src/functions/generateRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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,
Expand All @@ -168,7 +166,6 @@ export const useRouterSDK = () => {
throw error;
}
}

if (!factory) throw new Error('Factory address not found');
const currencyAmount = fromAddressAndAmountToCurrencyAmount(
amountAsset.currency.contract,
Expand Down
27 changes: 20 additions & 7 deletions src/functions/getExpectedAmount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit ac866ab

Please sign in to comment.