Skip to content

Commit

Permalink
fix: set empty names as shortened address (5afe#3794)
Browse files Browse the repository at this point in the history
* fix: update empty names + fallback to address

* fix: shorten fallback

* fix: temp dispatch to fix empty names

* fix: move dispatch

* fix: add tests

* fix: add test

* fix: reduce truncation length

* fix: add return type
  • Loading branch information
iamacook authored Apr 20, 2022
1 parent 98476d0 commit 5b23152
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 108 deletions.
7 changes: 6 additions & 1 deletion src/logic/addressBook/hooks/useAddressBookSync.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect } from 'react'
import { useDispatch } from 'react-redux'
import { Dispatch } from 'src/logic/safe/store/actions/types.d'
import { addressBookSync } from 'src/logic/addressBook/store/actions'
import { addressBookFixEmptyNames, addressBookSync } from 'src/logic/addressBook/store/actions'
import { ADDRESS_BOOK_REDUCER_ID } from 'src/logic/addressBook/store/reducer'
import { AddressBookState } from 'src/logic/addressBook/model/addressBook'

Expand All @@ -27,6 +27,11 @@ const useAddressBookSync = (): void => {
window.removeEventListener('storage', onStorageUpdate)
}
}, [dispatch])

// Temporary fix that should be removed after a sufficient amount of time
useEffect(() => {
dispatch(addressBookFixEmptyNames())
}, [dispatch])
}

export default useAddressBookSync
304 changes: 199 additions & 105 deletions src/logic/addressBook/store/__tests__/addressBookReducer.test.ts
Original file line number Diff line number Diff line change
@@ -1,114 +1,208 @@
import { CHAIN_ID } from 'src/config/chain.d'
import { batchLoadEntries } from '../reducer'
import { addressBookFixEmptyNames } from '../actions'
import addressBookReducer, { batchLoadEntries } from '../reducer'

describe('Test AddressBook BatchLoadEntries Reducer', () => {
it('returns an addressbook array', () => {
const addressBookEntries = [
{
address: '0x4462527986c3fD47f498eF25B4D01e6AAD7aBcb2',
name: 'Entry 1',
chainId: CHAIN_ID.RINKEBY,
},
{
address: '0x918925e548C7208713a965A8cdA0287e5FF9d96F',
name: 'Entry 2',
chainId: CHAIN_ID.RINKEBY,
},
]
const currentState = []
const action = {
type: 'addressBook/import',
payload: addressBookEntries,
}
const newState = batchLoadEntries(currentState, action)
expect(newState).toStrictEqual(addressBookEntries)
})
describe('batchLoadEntries', () => {
it('returns an addressbook array', () => {
const addressBookEntries = [
{
address: '0x4462527986c3fD47f498eF25B4D01e6AAD7aBcb2',
name: 'Entry 1',
chainId: CHAIN_ID.RINKEBY,
},
{
address: '0x918925e548C7208713a965A8cdA0287e5FF9d96F',
name: 'Entry 2',
chainId: CHAIN_ID.RINKEBY,
},
]
const currentState = []
const action = {
type: 'addressBook/import',
payload: addressBookEntries,
}
const newState = batchLoadEntries(currentState, action)
expect(newState).toStrictEqual(addressBookEntries)
})

it('merges entries from different chains', () => {
const addressBookEntries = [
{
address: '0x4462527986c3fD47f498eF25B4D01e6AAD7aBcb2',
name: 'Entry 1',
chainId: CHAIN_ID.RINKEBY,
},
{
address: '0x918925e548C7208713a965A8cdA0287e5FF9d96F',
name: 'Entry 2',
chainId: CHAIN_ID.RINKEBY,
},
]
const initialState = [
{
address: '0x4462527986c3fD47f498eF25B4D01e6AAD7aBcb2',
name: 'Entry 1',
chainId: CHAIN_ID.VOLTA,
},
{
address: '0x918925e548C7208713a965A8cdA0287e5FF9d96F',
name: 'Entry 2',
chainId: CHAIN_ID.VOLTA,
},
]
const action = {
type: 'addressBook/import',
payload: addressBookEntries,
}
const newState = batchLoadEntries(initialState, action)
expect(newState).toStrictEqual(initialState.concat(addressBookEntries))
})
it('merges entries from different chains', () => {
const addressBookEntries = [
{
address: '0x4462527986c3fD47f498eF25B4D01e6AAD7aBcb2',
name: 'Entry 1',
chainId: CHAIN_ID.RINKEBY,
},
{
address: '0x918925e548C7208713a965A8cdA0287e5FF9d96F',
name: 'Entry 2',
chainId: CHAIN_ID.RINKEBY,
},
]
const initialState = [
{
address: '0x4462527986c3fD47f498eF25B4D01e6AAD7aBcb2',
name: 'Entry 1',
chainId: CHAIN_ID.VOLTA,
},
{
address: '0x918925e548C7208713a965A8cdA0287e5FF9d96F',
name: 'Entry 2',
chainId: CHAIN_ID.VOLTA,
},
]
const action = {
type: 'addressBook/import',
payload: addressBookEntries,
}
const newState = batchLoadEntries(initialState, action)
expect(newState).toStrictEqual(initialState.concat(addressBookEntries))
})

it('skips entries with wrong name format', () => {
const addressBookEntries = [
{
address: '0x4462527986c3fD47f498eF25B4D01e6AAD7aBcb2',
name: 'OWNER # 1',
chainId: CHAIN_ID.RINKEBY,
},
{
address: '0x918925e548C7208713a965A8cdA0287e5FF9d96F',
name: 'OWNER # 2',
chainId: CHAIN_ID.RINKEBY,
},
]
const initialState = []
const action = {
type: 'addressBook/import',
payload: addressBookEntries,
}
const newState = batchLoadEntries(initialState, action)
expect(newState).toStrictEqual(initialState)
})

it('replaces name when entries share address and chain', () => {
const addressBookEntries = [
{
address: '0x4462527986c3fD47f498eF25B4D01e6AAD7aBcb2',
name: 'NewEntry 1',
chainId: CHAIN_ID.RINKEBY,
},
{
address: '0x918925e548C7208713a965A8cdA0287e5FF9d96F',
name: 'New Entry 2',
chainId: CHAIN_ID.RINKEBY,
},
]
const initialState = [
{
address: '0x4462527986c3fD47f498eF25B4D01e6AAD7aBcb2',
name: 'Entry 1',
chainId: CHAIN_ID.RINKEBY,
},
{
address: '0x918925e548C7208713a965A8cdA0287e5FF9d96F',
name: 'Entry 2',
chainId: CHAIN_ID.RINKEBY,
},
]
const action = {
type: 'addressBook/import',
payload: addressBookEntries,
}
const newState = batchLoadEntries(initialState, action)
expect(newState).toStrictEqual(addressBookEntries)
})

it('skips entries with wrong name format', () => {
const addressBookEntries = [
{
address: '0x4462527986c3fD47f498eF25B4D01e6AAD7aBcb2',
name: 'OWNER # 1',
chainId: CHAIN_ID.RINKEBY,
},
{
address: '0x918925e548C7208713a965A8cdA0287e5FF9d96F',
name: 'OWNER # 2',
chainId: CHAIN_ID.RINKEBY,
},
]
const initialState = []
const action = {
type: 'addressBook/import',
payload: addressBookEntries,
}
const newState = batchLoadEntries(initialState, action)
expect(newState).toStrictEqual(initialState)
it('fixes empty names upon import', () => {
const addressBookEntries = [
{
address: '0x4462527986c3fD47f498eF25B4D01e6AAD7aBcb2',
name: '0x4462...Bcb2',
chainId: CHAIN_ID.RINKEBY,
},
{
address: '0x918925e548C7208713a965A8cdA0287e5FF9d96F',
name: '0x9189...d96F',
chainId: CHAIN_ID.RINKEBY,
},
]
const initialState = [
{
address: '0x4462527986c3fD47f498eF25B4D01e6AAD7aBcb2',
name: '',
chainId: CHAIN_ID.RINKEBY,
},
{
address: '0x918925e548C7208713a965A8cdA0287e5FF9d96F',
name: '',
chainId: CHAIN_ID.RINKEBY,
},
]
const action = {
type: 'addressBook/import',
payload: addressBookEntries,
}
const newState = batchLoadEntries(initialState, action)
expect(newState).toStrictEqual(addressBookEntries)
})
})

it('replaces name when entries share address and chain', () => {
const addressBookEntries = [
{
address: '0x4462527986c3fD47f498eF25B4D01e6AAD7aBcb2',
name: 'NewEntry 1',
chainId: CHAIN_ID.RINKEBY,
},
{
address: '0x918925e548C7208713a965A8cdA0287e5FF9d96F',
name: 'New Entry 2',
chainId: CHAIN_ID.RINKEBY,
},
]
const initialState = [
{
address: '0x4462527986c3fD47f498eF25B4D01e6AAD7aBcb2',
name: 'Entry 1',
chainId: CHAIN_ID.RINKEBY,
},
{
address: '0x918925e548C7208713a965A8cdA0287e5FF9d96F',
name: 'Entry 2',
chainId: CHAIN_ID.RINKEBY,
},
]
const action = {
type: 'addressBook/import',
payload: addressBookEntries,
}
const newState = batchLoadEntries(initialState, action)
expect(newState).toStrictEqual(addressBookEntries)
describe('addressBookFixEmptyNames', () => {
it('fixes empty names', () => {
const prevState = [
{
address: '0x4462527986c3fD47f498eF25B4D01e6AAD7aBcb2',
name: '',
chainId: CHAIN_ID.RINKEBY,
},
{
address: '0x918925e548C7208713a965A8cdA0287e5FF9d96F',
name: '',
chainId: CHAIN_ID.RINKEBY,
},
]

expect(addressBookReducer(prevState, addressBookFixEmptyNames())).toEqual([
{
address: '0x4462527986c3fD47f498eF25B4D01e6AAD7aBcb2',
name: '0x4462...Bcb2',
chainId: CHAIN_ID.RINKEBY,
},
{
address: '0x918925e548C7208713a965A8cdA0287e5FF9d96F',
name: '0x9189...d96F',
chainId: CHAIN_ID.RINKEBY,
},
])
})

it("doesn't 'fix' named contacts", () => {
const prevState = [
{
address: '0x4462527986c3fD47f498eF25B4D01e6AAD7aBcb2',
name: 'test',
chainId: CHAIN_ID.RINKEBY,
},
{
address: '0x918925e548C7208713a965A8cdA0287e5FF9d96F',
name: '',
chainId: CHAIN_ID.RINKEBY,
},
]

expect(addressBookReducer(prevState, addressBookFixEmptyNames())).toEqual([
{
address: '0x4462527986c3fD47f498eF25B4D01e6AAD7aBcb2',
name: 'test',
chainId: CHAIN_ID.RINKEBY,
},
{
address: '0x918925e548C7208713a965A8cdA0287e5FF9d96F',
name: '0x9189...d96F',
chainId: CHAIN_ID.RINKEBY,
},
])
})
})
})
2 changes: 2 additions & 0 deletions src/logic/addressBook/store/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ export enum ADDRESS_BOOK_ACTIONS {
IMPORT = 'addressBook/import',
SAFE_LOAD = 'addressBook/safeLoad',
SYNC = 'addressBook/sync',
FIX_EMPTY_NAMES = 'addressBook/fixEmptyNames',
}

export const addressBookAddOrUpdate = createAction<AddressBookEntry>(ADDRESS_BOOK_ACTIONS.ADD_OR_UPDATE)
export const addressBookRemove = createAction<AddressBookEntry>(ADDRESS_BOOK_ACTIONS.REMOVE)
export const addressBookSafeLoad = createAction<AddressBookState>(ADDRESS_BOOK_ACTIONS.SAFE_LOAD)
export const addressBookImport = createAction<AddressBookState>(ADDRESS_BOOK_ACTIONS.IMPORT)
export const addressBookSync = createAction<AddressBookState>(ADDRESS_BOOK_ACTIONS.SYNC)
export const addressBookFixEmptyNames = createAction(ADDRESS_BOOK_ACTIONS.FIX_EMPTY_NAMES)
Loading

0 comments on commit 5b23152

Please sign in to comment.