From a6584731ba5c569f76f7c7ea5b936cb130379cd5 Mon Sep 17 00:00:00 2001 From: Nguyen Van Viet Date: Tue, 17 Dec 2024 14:19:05 +0700 Subject: [PATCH] fix: dont persist permit data (#2566) --- src/components/SwapForm/SwapModal/index.tsx | 2 +- src/hooks/Trades.ts | 3 +- src/hooks/usePermit.ts | 4 +- src/pages/PartnerSwap/index.tsx | 3 +- src/pages/SwapV3/PopulatedSwapForm.tsx | 4 +- src/state/swap/actions.ts | 10 +++++ src/state/swap/hooks.ts | 10 +++++ src/state/swap/reducer.ts | 50 +++++++++++++++++++++ src/state/transactions/updater.tsx | 2 +- src/state/user/actions.ts | 10 ----- src/state/user/hooks.tsx | 9 ---- src/state/user/reducer.ts | 49 -------------------- 12 files changed, 80 insertions(+), 76 deletions(-) diff --git a/src/components/SwapForm/SwapModal/index.tsx b/src/components/SwapForm/SwapModal/index.tsx index 3ebd8e082a..d5d2d45837 100644 --- a/src/components/SwapForm/SwapModal/index.tsx +++ b/src/components/SwapForm/SwapModal/index.tsx @@ -12,7 +12,7 @@ import { TransactionSubmittedContent, } from 'components/TransactionConfirmationModal' import { useActiveWeb3React } from 'hooks' -import { permitError } from 'state/user/actions' +import { permitError } from 'state/swap/actions' import { captureSwapError } from 'utils/sentry' import ConfirmSwapModalContent from './ConfirmSwapModalContent' diff --git a/src/hooks/Trades.ts b/src/hooks/Trades.ts index a7a169eb0d..3ab1c6a5ce 100644 --- a/src/hooks/Trades.ts +++ b/src/hooks/Trades.ts @@ -10,8 +10,9 @@ import { useAllCurrencyCombinations } from 'hooks/useAllCurrencyCombinations' import useDebounce from 'hooks/useDebounce' import { AppState } from 'state' import { useAllDexes, useExcludeDexes } from 'state/customizeDexes/hooks' +import { usePermitData } from 'state/swap/hooks' import { useAllTransactions } from 'state/transactions/hooks' -import { usePermitData, useUserSlippageTolerance } from 'state/user/hooks' +import { useUserSlippageTolerance } from 'state/user/hooks' import { isAddress } from 'utils' import { Aggregator } from 'utils/aggregator' diff --git a/src/hooks/usePermit.ts b/src/hooks/usePermit.ts index 41076db291..8667d47e9c 100644 --- a/src/hooks/usePermit.ts +++ b/src/hooks/usePermit.ts @@ -13,8 +13,8 @@ import { useActiveWeb3React, useWeb3React } from 'hooks' import { useNotify } from 'state/application/hooks' import { WrappedTokenInfo } from 'state/lists/wrappedTokenInfo' import { useSingleCallResult } from 'state/multicall/hooks' -import { permitUpdate } from 'state/user/actions' -import { usePermitData } from 'state/user/hooks' +import { permitUpdate } from 'state/swap/actions' +import { usePermitData } from 'state/swap/hooks' import { friendlyError } from 'utils/errorMessage' import { useReadingContract } from './useContract' diff --git a/src/pages/PartnerSwap/index.tsx b/src/pages/PartnerSwap/index.tsx index a38c8bb762..ba3d43733f 100644 --- a/src/pages/PartnerSwap/index.tsx +++ b/src/pages/PartnerSwap/index.tsx @@ -32,7 +32,8 @@ import { TAB, isSettingTab } from 'pages/SwapV3' import Header from 'pages/SwapV3/Header' import Updater from 'state/customizeDexes/updater' import { Field } from 'state/swap/actions' -import { useDegenModeManager, usePermitData, useUserSlippageTolerance, useUserTransactionTTL } from 'state/user/hooks' +import { usePermitData } from 'state/swap/hooks' +import { useDegenModeManager, useUserSlippageTolerance, useUserTransactionTTL } from 'state/user/hooks' import { useCurrencyBalances } from 'state/wallet/hooks' import { TransactionFlowState } from 'types/TransactionFlowState' import { DetailedRouteSummary } from 'types/route' diff --git a/src/pages/SwapV3/PopulatedSwapForm.tsx b/src/pages/SwapV3/PopulatedSwapForm.tsx index d3c85f540b..82e069313a 100644 --- a/src/pages/SwapV3/PopulatedSwapForm.tsx +++ b/src/pages/SwapV3/PopulatedSwapForm.tsx @@ -5,8 +5,8 @@ import { useLocation, useSearchParams } from 'react-router-dom' import SwapForm, { SwapFormProps } from 'components/SwapForm' import { APP_PATHS } from 'constants/index' import { Field } from 'state/swap/actions' -import { useInputCurrency, useOutputCurrency, useSwapActionHandlers } from 'state/swap/hooks' -import { useDegenModeManager, usePermitData, useUserSlippageTolerance, useUserTransactionTTL } from 'state/user/hooks' +import { useInputCurrency, useOutputCurrency, usePermitData, useSwapActionHandlers } from 'state/swap/hooks' +import { useDegenModeManager, useUserSlippageTolerance, useUserTransactionTTL } from 'state/user/hooks' import { useCurrencyBalances } from 'state/wallet/hooks' import { DetailedRouteSummary } from 'types/route' diff --git a/src/state/swap/actions.ts b/src/state/swap/actions.ts index c1bc061a60..473bb2f9ca 100644 --- a/src/state/swap/actions.ts +++ b/src/state/swap/actions.ts @@ -22,3 +22,13 @@ export const replaceSwapState = createAction<{ export const setRecipient = createAction<{ recipient: string | null }>('swap/setRecipient') export const setTrendingSoonShowed = createAction('swap/setTrendingSoonShowed') export const setTrade = createAction<{ trade: Aggregator | undefined }>('swap/setTrade') +export const permitUpdate = createAction<{ + chainId: number + address: string + rawSignature: string + deadline: number + value: string + account: string +}>('swap/permitUpdate') +export const revokePermit = createAction<{ chainId: number; address: string; account: string }>('swap/revokePermit') +export const permitError = createAction<{ chainId: number; address: string; account: string }>('swap/permitError') diff --git a/src/state/swap/hooks.ts b/src/state/swap/hooks.ts index 383ce300c3..6464c6ae6f 100644 --- a/src/state/swap/hooks.ts +++ b/src/state/swap/hooks.ts @@ -9,6 +9,7 @@ import { CORRELATED_COINS_ADDRESS, DEFAULT_OUTPUT_TOKEN_BY_CHAIN, NativeCurrenci import { useActiveWeb3React } from 'hooks' import { useAllTokens, useCurrencyV2, useStableCoins } from 'hooks/Tokens' import { NETWORKS_INFO } from 'hooks/useChainsConfig' +import { useAppSelector } from 'state/hooks' import { AppDispatch, AppState } from 'state/index' import { Field, resetSelectCurrency, setRecipient, setTrade, typeInput } from 'state/swap/actions' import { SwapState } from 'state/swap/reducer' @@ -325,3 +326,12 @@ export const useSwitchPairToLimitOrder = () => { [networkInfo, inputCurrencyId, outputCurrencyId, navigate], ) } + +export const usePermitData: ( + address?: string, +) => { rawSignature?: string; deadline?: number; value?: string; errorCount?: number } | null = address => { + const { chainId, account } = useActiveWeb3React() + const permitData = useAppSelector(state => state.swap.permitData) + + return address && account && permitData ? permitData[account]?.[chainId]?.[address] : null +} diff --git a/src/state/swap/reducer.ts b/src/state/swap/reducer.ts index ea106b08d8..1e4b85ca1c 100644 --- a/src/state/swap/reducer.ts +++ b/src/state/swap/reducer.ts @@ -4,8 +4,11 @@ import { Aggregator } from 'utils/aggregator' import { Field, + permitError, + permitUpdate, replaceSwapState, resetSelectCurrency, + revokePermit, setRecipient, setTrade, setTrendingSoonShowed, @@ -35,6 +38,19 @@ export interface SwapState { readonly txHash: string | undefined readonly isSelectTokenManually: boolean + + permitData?: { + [account: string]: { + [chainId: number]: { + [address: string]: { + rawSignature?: string + deadline?: number + value?: string + errorCount?: number + } | null + } + } + } } // const { search, pathname } = window.location @@ -44,6 +60,7 @@ export interface SwapState { const initialState: SwapState = { independentField: Field.INPUT, + permitData: {}, typedValue: '1', // [Field.INPUT]: { // currencyId: inputCurrency?.toString() || '', @@ -137,5 +154,38 @@ export default createReducer(initialState, builder => }) .addCase(setTrade, (state, { payload: { trade } }) => { state.trade = trade + }) + .addCase(permitUpdate, (state, { payload: { chainId, address, rawSignature, deadline, value, account } }) => { + if (!state.permitData) state.permitData = {} + if (!state.permitData[account]) state.permitData[account] = {} + if (!state.permitData[account][chainId]) state.permitData[account][chainId] = {} + + state.permitData[account][chainId][address] = { + rawSignature, + deadline, + value, + errorCount: state.permitData[account][chainId][address]?.errorCount || 0, + } + }) + .addCase(revokePermit, (state, { payload: { chainId, address, account } }) => { + if ( + !state.permitData || + !state.permitData[account] || + !state.permitData[account][chainId] || + !state.permitData[account][chainId][address] + ) + return + + state.permitData[account][chainId][address] = null + }) + .addCase(permitError, (state, { payload: { chainId, address, account } }) => { + if (!state.permitData?.[account]?.[chainId]?.[address]) return + const { errorCount } = state.permitData[account][chainId][address] || {} + state.permitData[account][chainId][address] = { + rawSignature: undefined, + deadline: undefined, + value: undefined, + errorCount: (errorCount || 0) + 1, + } }), ) diff --git a/src/state/transactions/updater.tsx b/src/state/transactions/updater.tsx index de77cd9660..d03db81cec 100644 --- a/src/state/transactions/updater.tsx +++ b/src/state/transactions/updater.tsx @@ -12,7 +12,7 @@ import { useActiveWeb3React, useWeb3React } from 'hooks' import useMixpanel, { MIXPANEL_TYPE, NEED_CHECK_SUBGRAPH_TRANSACTION_TYPES } from 'hooks/useMixpanel' import { useBlockNumber, useKyberSwapConfig, useTransactionNotify } from 'state/application/hooks' import { AppDispatch, AppState } from 'state/index' -import { revokePermit } from 'state/user/actions' +import { revokePermit } from 'state/swap/actions' import { findTx } from 'utils' import { checkedTransaction, finalizeTransaction, modifyTransaction, removeTx, replaceTx } from './actions' diff --git a/src/state/user/actions.ts b/src/state/user/actions.ts index 32d388d13c..82bfa6f584 100644 --- a/src/state/user/actions.ts +++ b/src/state/user/actions.ts @@ -55,16 +55,6 @@ export const updateTokenAnalysisSettings = createAction('user/updateToke export const updateAcceptedTermVersion = createAction('user/updateAcceptedTermVersion') export const changeViewMode = createAction('user/changeViewMode') export const toggleHolidayMode = createAction('user/toggleHolidayMode') -export const permitUpdate = createAction<{ - chainId: number - address: string - rawSignature: string - deadline: number - value: string - account: string -}>('user/permitUpdate') -export const revokePermit = createAction<{ chainId: number; address: string; account: string }>('user/revokePermit') -export const permitError = createAction<{ chainId: number; address: string; account: string }>('user/permitError') export const pinSlippageControl = createAction('user/pinSlippageControl') export const toggleMyEarningChart = createAction('user/toggleMyEarningChart') diff --git a/src/state/user/hooks.tsx b/src/state/user/hooks.tsx index db874c4ad2..c615a2aa30 100644 --- a/src/state/user/hooks.tsx +++ b/src/state/user/hooks.tsx @@ -507,15 +507,6 @@ export const useSlippageSettingByPage = () => { } } -export const usePermitData: ( - address?: string, -) => { rawSignature?: string; deadline?: number; value?: string; errorCount?: number } | null = address => { - const { chainId, account } = useActiveWeb3React() - const permitData = useAppSelector(state => state.user.permitData) - - return address && account && permitData ? permitData[account]?.[chainId]?.[address] : null -} - export const useShowMyEarningChart: () => [boolean, () => void] = () => { const dispatch = useAppDispatch() diff --git a/src/state/user/reducer.ts b/src/state/user/reducer.ts index d04c52fc57..8a6994c2ac 100644 --- a/src/state/user/reducer.ts +++ b/src/state/user/reducer.ts @@ -17,12 +17,9 @@ import { addSerializedPair, addSerializedToken, changeViewMode, - permitError, - permitUpdate, pinSlippageControl, removeSerializedPair, removeSerializedToken, - revokePermit, setCrossChainSetting, setPaymentToken, toggleFavoriteToken, @@ -108,19 +105,6 @@ export interface UserState { viewMode: VIEW_MODE paymentToken: Token | null holidayMode: boolean - permitData: { - [account: string]: { - [chainId: number]: { - [address: string]: { - rawSignature?: string - deadline?: number - value?: string - errorCount?: number - } | null - } - } - } - isSlippageControlPinned: boolean crossChain: CrossChainSetting @@ -157,7 +141,6 @@ const initialState: UserState = { acceptedTermVersion: null, viewMode: VIEW_MODE.GRID, holidayMode: true, - permitData: {}, isSlippageControlPinned: true, crossChain: CROSS_CHAIN_SETTING_DEFAULT, myEarningChart: true, @@ -295,38 +278,6 @@ export default createReducer(initialState, builder => const oldMode = state.holidayMode state.holidayMode = !oldMode }) - .addCase(permitUpdate, (state, { payload: { chainId, address, rawSignature, deadline, value, account } }) => { - if (!state.permitData) state.permitData = {} - if (!state.permitData[account]) state.permitData[account] = {} - if (!state.permitData[account][chainId]) state.permitData[account][chainId] = {} - - state.permitData[account][chainId][address] = { - rawSignature, - deadline, - value, - errorCount: state.permitData[account][chainId][address]?.errorCount || 0, - } - }) - .addCase(revokePermit, (state, { payload: { chainId, address, account } }) => { - if ( - !state.permitData[account] || - !state.permitData[account][chainId] || - !state.permitData[account][chainId][address] - ) - return - - state.permitData[account][chainId][address] = null - }) - .addCase(permitError, (state, { payload: { chainId, address, account } }) => { - if (!state.permitData?.[account]?.[chainId]?.[address]) return - const { errorCount } = state.permitData[account][chainId][address] || {} - state.permitData[account][chainId][address] = { - rawSignature: undefined, - deadline: undefined, - value: undefined, - errorCount: (errorCount || 0) + 1, - } - }) .addCase(pinSlippageControl, (state, { payload }) => { state.isSlippageControlPinned = payload })