Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix path calculation #583

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading