Skip to content

Commit

Permalink
fix: dont persist permit data (#2566)
Browse files Browse the repository at this point in the history
  • Loading branch information
viet-nv authored Dec 17, 2024
1 parent f7ada75 commit a658473
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 76 deletions.
2 changes: 1 addition & 1 deletion src/components/SwapForm/SwapModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/Trades.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
4 changes: 2 additions & 2 deletions src/hooks/usePermit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
3 changes: 2 additions & 1 deletion src/pages/PartnerSwap/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions src/pages/SwapV3/PopulatedSwapForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
10 changes: 10 additions & 0 deletions src/state/swap/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
10 changes: 10 additions & 0 deletions src/state/swap/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
}
50 changes: 50 additions & 0 deletions src/state/swap/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import { Aggregator } from 'utils/aggregator'

import {
Field,
permitError,
permitUpdate,
replaceSwapState,
resetSelectCurrency,
revokePermit,
setRecipient,
setTrade,
setTrendingSoonShowed,
Expand Down Expand Up @@ -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
Expand All @@ -44,6 +60,7 @@ export interface SwapState {

const initialState: SwapState = {
independentField: Field.INPUT,
permitData: {},
typedValue: '1',
// [Field.INPUT]: {
// currencyId: inputCurrency?.toString() || '',
Expand Down Expand Up @@ -137,5 +154,38 @@ export default createReducer<SwapState>(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,
}
}),
)
2 changes: 1 addition & 1 deletion src/state/transactions/updater.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
10 changes: 0 additions & 10 deletions src/state/user/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,6 @@ export const updateTokenAnalysisSettings = createAction<string>('user/updateToke
export const updateAcceptedTermVersion = createAction<number | null>('user/updateAcceptedTermVersion')
export const changeViewMode = createAction<VIEW_MODE>('user/changeViewMode')
export const toggleHolidayMode = createAction<void>('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<boolean>('user/pinSlippageControl')
export const toggleMyEarningChart = createAction<void>('user/toggleMyEarningChart')

Expand Down
9 changes: 0 additions & 9 deletions src/state/user/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
49 changes: 0 additions & 49 deletions src/state/user/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ import {
addSerializedPair,
addSerializedToken,
changeViewMode,
permitError,
permitUpdate,
pinSlippageControl,
removeSerializedPair,
removeSerializedToken,
revokePermit,
setCrossChainSetting,
setPaymentToken,
toggleFavoriteToken,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
})
Expand Down

0 comments on commit a658473

Please sign in to comment.