Commit aee4df10 authored by eddie's avatar eddie Committed by GitHub

fix: change default tx deadline to 10m (#7451)

* fix: change deadline to 10m

* test: add unit tests

* fix: improve unit tests
parent 82a19498
...@@ -5,8 +5,8 @@ export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000' ...@@ -5,8 +5,8 @@ export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'
// TODO(WEB-1984): Convert the deadline to minutes and remove unecessary conversions from // TODO(WEB-1984): Convert the deadline to minutes and remove unecessary conversions from
// seconds to minutes in the codebase. // seconds to minutes in the codebase.
// 30 minutes, denominated in seconds // 10 minutes, denominated in seconds
export const DEFAULT_DEADLINE_FROM_NOW = 60 * 30 export const DEFAULT_DEADLINE_FROM_NOW = 60 * 10
export const L2_DEADLINE_FROM_NOW = 60 * 5 export const L2_DEADLINE_FROM_NOW = 60 * 5
// transaction popup dismisal amounts // transaction popup dismisal amounts
......
...@@ -13,7 +13,7 @@ const defaultState = { ...@@ -13,7 +13,7 @@ const defaultState = {
user: {}, user: {},
_persist: { _persist: {
rehydrated: true, rehydrated: true,
version: 0, version: 1,
}, },
application: { application: {
chainId: null, chainId: null,
......
...@@ -2,19 +2,21 @@ import { createMigrate, MigrationManifest, PersistedState, PersistMigrate } from ...@@ -2,19 +2,21 @@ import { createMigrate, MigrationManifest, PersistedState, PersistMigrate } from
import { MigrationConfig } from 'redux-persist/es/createMigrate' import { MigrationConfig } from 'redux-persist/es/createMigrate'
import { migration0 } from './migrations/0' import { migration0 } from './migrations/0'
import { migration1 } from './migrations/1'
import { legacyLocalStorageMigration } from './migrations/legacy' import { legacyLocalStorageMigration } from './migrations/legacy'
/** /**
* These run once per state re-hydration when a version mismatch is detected. * These run once per state re-hydration when a version mismatch is detected.
* Keep them as lightweight as possible. * Keep them as lightweight as possible.
* *
* Migration functions should not assume that any value exists in localStorage previously, * Migration functions should not assume that any value exists in the persisted data previously,
* because a user may be visiting the site for the first time or have cleared their localStorage. * because a user may be visiting the site for the first time or have cleared their data.
*/ */
// The target version number is the key // The target version number is the key
export const migrations: MigrationManifest = { export const migrations: MigrationManifest = {
0: migration0, 0: migration0,
1: migration1,
} }
// We use a custom migration function for the initial state, because redux-persist // We use a custom migration function for the initial state, because redux-persist
......
import { createMigrate } from 'redux-persist'
import { RouterPreference } from 'state/routing/types'
import { SlippageTolerance } from 'state/user/types'
import { migration1, PersistAppStateV1 } from './1'
const previousState: PersistAppStateV1 = {
user: {
userLocale: null,
userRouterPreference: RouterPreference.API,
userHideClosedPositions: false,
userSlippageTolerance: SlippageTolerance.Auto,
userSlippageToleranceHasBeenMigratedToAuto: true,
userDeadline: 1800,
tokens: {},
pairs: {},
timestamp: Date.now(),
hideBaseWalletBanner: false,
},
_persist: {
version: 0,
rehydrated: true,
},
}
describe('migration to v1', () => {
it('should migrate the default deadline', async () => {
const migrator = createMigrate(
{
1: migration1,
},
{ debug: false }
)
const result: any = await migrator(previousState, 1)
expect(result?.user?.userDeadline).toEqual(600)
expect(result?._persist.version).toEqual(1)
expect(result?.user?.userLocale).toEqual(null)
expect(result?.user?.userRouterPreference).toEqual(RouterPreference.API)
expect(result?.user?.userHideClosedPositions).toEqual(false)
expect(result?.user?.userSlippageTolerance).toEqual(SlippageTolerance.Auto)
expect(result?.user?.userSlippageToleranceHasBeenMigratedToAuto).toEqual(true)
expect(result?.user?.tokens).toEqual({})
expect(result?.user?.pairs).toEqual({})
expect(result?.user?.timestamp).toEqual(previousState.user?.timestamp)
expect(result?.user?.hideBaseWalletBanner).toEqual(false)
})
it('should not migrate a non-default value', async () => {
const migrator = createMigrate(
{
1: migration1,
},
{ debug: false }
)
const result: any = await migrator(
{
...previousState,
user: {
...previousState.user,
userDeadline: 300,
},
} as PersistAppStateV1,
1
)
expect(result?.user?.userDeadline).toEqual(300)
})
it('should not migrate if user state is not set', async () => {
const migrator = createMigrate(
{
1: migration1,
},
{ debug: false }
)
const result: any = await migrator(
{
...previousState,
user: undefined,
} as PersistAppStateV1,
1
)
expect(result?.user?.userDeadline).toBeUndefined()
})
})
import { DEFAULT_DEADLINE_FROM_NOW } from 'constants/misc'
import { PersistState } from 'redux-persist'
import { UserState } from 'state/user/reducer'
export type PersistAppStateV1 = {
_persist: PersistState
} & { user?: UserState }
/**
* Migration to change the default user deadline from 30 minutes to 10 minutes.
* We only migrate if the saved deadline is the old default.
*/
export const migration1 = (state: PersistAppStateV1 | undefined) => {
if (state?.user && state.user?.userDeadline === 1800) {
return {
...state,
user: {
...state.user,
userDeadline: DEFAULT_DEADLINE_FROM_NOW,
},
_persist: {
...state._persist,
version: 1,
},
}
}
return state
}
...@@ -44,7 +44,7 @@ export type AppState = ReturnType<typeof appReducer> ...@@ -44,7 +44,7 @@ export type AppState = ReturnType<typeof appReducer>
const persistConfig: PersistConfig<AppState> = { const persistConfig: PersistConfig<AppState> = {
key: 'interface', key: 'interface',
version: 0, // see migrations.ts for more details about this version version: 1, // see migrations.ts for more details about this version
storage: localForage.createInstance({ storage: localForage.createInstance({
name: 'redux', name: 'redux',
}), }),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment