Skip to content

Calculate price impact for tokens without price #223

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
32 changes: 31 additions & 1 deletion src/components/Aggregator/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,26 @@ export function AggregatorContainer({ tokenList, sandwichList }) {
toToken: finalSelectedToToken?.address,
fromToken: finalSelectedFromToken?.address
});

const { data: halfAmountRoutes } = useGetRoutes({
chain: selectedChain?.value,
from: finalSelectedFromToken?.value,
to: finalSelectedToToken?.value,
amount: BigNumber(amountWithDecimals).div(2).toFixed(0),
disabledAdapters: adaptersNames.filter((name) => name !== aggregator),
extra: {
gasPriceData,
userAddress: address || ethers.constants.AddressZero,
amount: debouncedAmount / 2,
fromToken: finalSelectedFromToken,
toToken: finalSelectedToToken,
slippage,
isPrivacyEnabled,
amountOut: amountOutWithDecimals
},
enabled: !!aggregator
});

const { gasTokenPrice = 0, toTokenPrice, fromTokenPrice } = tokenPrices || {};

// format routes
Expand Down Expand Up @@ -650,6 +670,13 @@ export function AggregatorContainer({ tokenList, sandwichList }) {

const priceImpactRoute = selectedRoute ? fillRoute(selectedRoute) : null;

const hasLinearPriceImpact =
selectedRoute && (halfAmountRoutes || []).length
? BigNumber(selectedRoute?.price?.amountReturned)
.div(BigNumber((halfAmountRoutes || [])[0]?.price?.amountReturned))
.lt(1.5)
: false;

const selectedRoutesPriceImpact =
fromTokenPrice &&
toTokenPrice &&
Expand All @@ -662,7 +689,9 @@ export function AggregatorContainer({ tokenList, sandwichList }) {
: null;

const hasPriceImapct =
selectedRoutesPriceImpact === null || Number(selectedRoutesPriceImpact) > PRICE_IMPACT_WARNING_THRESHOLD;
selectedRoutesPriceImpact === null ||
Number(selectedRoutesPriceImpact) > PRICE_IMPACT_WARNING_THRESHOLD ||
hasLinearPriceImpact;
const hasMaxPriceImpact = selectedRoutesPriceImpact !== null && Number(selectedRoutesPriceImpact) > 30;

const insufficientBalance =
Expand Down Expand Up @@ -1067,6 +1096,7 @@ export function AggregatorContainer({ tokenList, sandwichList }) {
amount={selectedRoute?.amountIn}
slippage={slippage}
isPriceImpactNotKnown={isPriceImpactNotKnown}
hasLinearPriceImpact={hasLinearPriceImpact}
/>
<Box display={['none', 'none', 'flex', 'flex']} flexDirection="column" gap="4px">
{warnings}
Expand Down
18 changes: 17 additions & 1 deletion src/components/PriceImpact/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface IPriceImpact {
amountReturnedInSelectedRoute?: string;
slippage?: string;
isPriceImpactNotKnown?: boolean;
hasLinearPriceImpact?: boolean;
}

const NoPriceImpactAlert = ({ tokens }) => {
Expand All @@ -43,6 +44,18 @@ const NoPriceImpactAlert = ({ tokens }) => {
);
};

const LinearPriceImpact = () => {
return (
<Alert status="warning" borderRadius="0.375rem" py="8px">
<AlertIcon />
Price impact is high.
<Text display={['none', 'none', 'contents', 'contents']}>
Please be very careful when checking the swap cause you could lose money
</Text>
</Alert>
);
};

export function PriceImpact({
isLoading,
fromToken,
Expand All @@ -53,7 +66,8 @@ export function PriceImpact({
amountReturnedInSelectedRoute,
selectedRoutesPriceImpact,
slippage,
isPriceImpactNotKnown = false
isPriceImpactNotKnown = false,
hasLinearPriceImpact = false
}: IPriceImpact) {
const breakpoint = useBreakpoint();
const [priceOrder, setPriceOrder] = useState(1);
Expand All @@ -66,6 +80,8 @@ export function PriceImpact({
!toTokenPrice || Number.isNaN(Number(toTokenPrice)) ? toToken.symbol : null
].filter(Boolean);

if (isPriceImpactNotKnown && hasLinearPriceImpact) return <LinearPriceImpact />;

if (tokensWithoutPrice.length > 0) return <NoPriceImpactAlert tokens={tokensWithoutPrice} />;

if (!amount || Number.isNaN(Number(amount)) || !amountReturnedInSelectedRoute) {
Expand Down
7 changes: 5 additions & 2 deletions src/queries/useGetRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface IGetListRoutesProps {
extra?: any;
disabledAdapters?: Array<string>;
customRefetchInterval?: number;
enabled?: boolean;
}

export interface IRoute {
Expand Down Expand Up @@ -127,7 +128,8 @@ export function useGetRoutes({
amount,
extra = {},
disabledAdapters = [],
customRefetchInterval
customRefetchInterval,
enabled = true
}: IGetListRoutesProps) {
const res = useQueries({
queries: adapters
Expand All @@ -138,7 +140,8 @@ export function useGetRoutes({
queryFn: () => getAdapterRoutes({ adapter, chain, from, to, amount, extra }),
refetchInterval: customRefetchInterval || REFETCH_INTERVAL,
refetchOnWindowFocus: false,
refetchIntervalInBackground: false
refetchIntervalInBackground: false,
enabled
};
})
});
Expand Down