From f210de03a867715789c36987dee5e7babf3d99ff Mon Sep 17 00:00:00 2001 From: Matias Poblete <86752543+MattPoblete@users.noreply.github.com> Date: Fri, 30 Aug 2024 16:39:44 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7Refresh=20trade=20in=20protocol=20c?= =?UTF-8?q?hange?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Settings/ProtocolsSettings/index.tsx | 12 +++++++++++- src/hooks/useBestTrade.ts | 9 ++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/components/Settings/ProtocolsSettings/index.tsx b/src/components/Settings/ProtocolsSettings/index.tsx index 85fef28c..921f0077 100644 --- a/src/components/Settings/ProtocolsSettings/index.tsx +++ b/src/components/Settings/ProtocolsSettings/index.tsx @@ -6,6 +6,7 @@ import { AppContext } from 'contexts'; import { useRouterSDK } from 'functions/generateRoute'; import React, { useContext, useEffect, useState } from 'react' import { Box, styled, Switch, SwitchProps, Typography, useTheme } from 'soroswap-ui'; +import { useSWRConfig } from 'swr'; export const CustomSwitch = styled((props: SwitchProps) => ( @@ -64,6 +65,7 @@ const ProtocolsSettings = () => { const theme = useTheme(); const [isOpen, setIsOpen] = useState(false); const { protocolsStatus, setProtocolsStatus } = useContext(AppContext).Settings; + const { mutate } = useSWRConfig(); const switchProtocolValue = (key: string) => { const newProtocolsStatus = protocolsStatus.map((protocol) => { @@ -77,8 +79,16 @@ const ProtocolsSettings = () => { }); const hasTrueValue = newProtocolsStatus.some((protocol) => protocol.value); if (hasTrueValue) { - setProtocolsStatus(newProtocolsStatus); resetRouterSdkCache(); + setProtocolsStatus(newProtocolsStatus); + mutate( + (key: any) => { + console.log(key) + return true; + }, + undefined, + { revalidate: true }, + ); } else return; } diff --git a/src/hooks/useBestTrade.ts b/src/hooks/useBestTrade.ts index 210b57ee..e2ebca65 100644 --- a/src/hooks/useBestTrade.ts +++ b/src/hooks/useBestTrade.ts @@ -1,7 +1,8 @@ +import { AppContext } from 'contexts'; import { useRouterSDK } from 'functions/generateRoute'; import { hasDistribution } from 'helpers/aggregator'; import { CurrencyAmount, TokenType } from 'interfaces'; -import { useEffect, useMemo, useState } from 'react'; +import { useContext, useEffect, useMemo, useState } from 'react'; import { TradeType as SdkTradeType } from 'soroswap-router-sdk'; import { InterfaceTrade, QuoteState, TradeState, TradeType } from 'state/routing/types'; import useSWR from 'swr'; @@ -29,6 +30,7 @@ export function useBestTrade( resetRouterSdkCache: () => void; } { const { generateRoute, resetRouterSdkCache, maxHops } = useRouterSDK(); + const {protocolsStatus} = useContext(AppContext).Settings; /** * Custom hook that fetches the best trade based on the specified amount and trade type. * @@ -142,7 +144,7 @@ export function useBestTrade( } return baseTrade; - }, [expectedAmount, inputAmount, outputAmount, tradeType, data]); + }, [expectedAmount, inputAmount, outputAmount, tradeType, data, protocolsStatus]); /* If the pairAddress or the trades chenges, we upgrade the tradeResult @@ -154,7 +156,7 @@ export function useBestTrade( const myTradeResult = { state: state, trade: trade }; return myTradeResult; - }, [data, trade]); //should get the pair address and quotes + }, [data, trade, protocolsStatus]); //should get the pair address and quotes const skipFetch: boolean = false; @@ -195,6 +197,7 @@ export function useBestTrade( trade, isLoading, resetRouterSdkCache, + protocolsStatus ]); return bestTrade;