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

Prevent creating transactions with zero input/output because of tradi… #1315

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
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
36 changes: 33 additions & 3 deletions src/modules/swap/utils/swap-router-sdk-adapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,28 @@ import {
Trade,
getAllowedRoutePairsCombinations as originalGetAllowedRoutePairsCombinations,
useRoutePairsCombinations as originalUseRoutePairsCombinations,
useTradeWithSlippageTolerance as originalUseTradeWithSlippageTolerance
useTradeWithSlippageTolerance as originalUseTradeWithSlippageTolerance,
DexTypeEnum
} from 'swap-router-sdk';
import { RoutePair } from 'swap-router-sdk/dist/interface/route-pair.interface';
import { WhitelistedPair } from 'swap-router-sdk/dist/interface/whitelisted-pair.interface';

import { MAX_HOPS_COUNT } from '@config/constants';
import { getTokenIdFromSlug, getTokenSlug, isExist, isTezosToken } from '@shared/helpers';
import {
getTokenIdFromSlug,
getTokenSlug,
isExist,
isGreaterThanZero,
isLastElementIndex,
isTezosToken
} from '@shared/helpers';
import { Nullable, Optional, Token } from '@shared/types';

import { useRoutePairs } from '../providers/route-pairs-provider';

const FALLBACK_TRADE: Trade = [];
const FALLBACK_TOKEN_ID = 0;
const MIN_VALID_SWAP_AMOUNT = 1;

export const getSwapRouterSdkTokenSlug = (token: Token) =>
getTokenSlug({
Expand Down Expand Up @@ -67,6 +76,14 @@ export const getAllowedRoutePairsCombinations = (
MAX_HOPS_COUNT
);

const getCorrectedTokenAmountWithSlippage = (
tokenAmountWithSlippage: BigNumber,
tokenAmountWithoutSlippage: BigNumber
) =>
isGreaterThanZero(tokenAmountWithoutSlippage) && tokenAmountWithSlippage.isZero()
? new BigNumber(MIN_VALID_SWAP_AMOUNT)
: tokenAmountWithSlippage;

export const useTradeWithSlippageTolerance = (
inputAmount: Nullable<BigNumber>,
bestTrade: Nullable<Trade>,
Expand All @@ -77,6 +94,19 @@ export const useTradeWithSlippageTolerance = (
bestTrade ?? FALLBACK_TRADE,
tradingSlippage.toNumber()
);
const correctedValue = originalValue.map((operation, index) => {
const { aTokenAmount: aTokenAmountWithSlippage, bTokenAmount: bTokenAmountWithSlippage, dexType } = operation;
const { aTokenAmount: aTokenAmountWithoutSlippage, bTokenAmount: bTokenAmountWithoutSlippage } = bestTrade![index];

return {
...operation,
aTokenAmount: getCorrectedTokenAmountWithSlippage(aTokenAmountWithSlippage, aTokenAmountWithoutSlippage),
bTokenAmount:
isLastElementIndex(index, originalValue) && dexType === DexTypeEnum.QuipuSwapV3
? bTokenAmountWithSlippage
: getCorrectedTokenAmountWithSlippage(bTokenAmountWithSlippage, bTokenAmountWithoutSlippage)
};
});

return bestTrade && originalValue;
return bestTrade && correctedValue;
};