Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: address book undefined entry address #1338

Merged
merged 6 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/store/__tests__/addressBookSlice.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { addressBookSlice, setAddressBook, upsertAddressBookEntry, removeAddressBookEntry } from '../addressBookSlice'
import {
addressBookSlice,
setAddressBook,
upsertAddressBookEntry,
removeAddressBookEntry,
selectAddressBookByChain,
} from '../addressBookSlice'

const initialState = {
'1': { '0x0': 'Alice', '0x1': 'Bob' },
Expand Down Expand Up @@ -54,4 +60,21 @@ describe('addressBookSlice', () => {
'4': { '0x0': 'Charlie', '0x1': 'Dave' },
})
})

it('should not return entries with invalid address format', () => {
const initialState = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kudos for adding a test :)

'1': { '0x0': 'Alice', '0x1': 'Bob', '0x2': 'Fred' },
'5': {
'0x744aaf04ad770895ce469300771d2ca38463cfa0': 'unchecksummed',
'0x744aAF04AD770895Ce469300771D2CA38463cFa0': 'checksummed',
undefined: 'bug',
},
}

const expectedOutput = {
'0x744aAF04AD770895Ce469300771D2CA38463cFa0': 'checksummed',
}

expect(selectAddressBookByChain.resultFunc(initialState, '5')).toEqual(expectedOutput)
})
})
6 changes: 5 additions & 1 deletion src/store/addressBookSlice.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { createSelector, createSlice, type PayloadAction } from '@reduxjs/toolkit'
import { validateAddress } from '@/utils/validation'
import { pickBy } from 'lodash'
import type { RootState } from '.'

export type AddressBook = { [address: string]: string }
Expand Down Expand Up @@ -45,6 +47,8 @@ export const selectAllAddressBooks = (state: RootState): AddressBookState => {
export const selectAddressBookByChain = createSelector(
[selectAllAddressBooks, (_, chainId: string) => chainId],
(allAddressBooks, chainId): AddressBook => {
return chainId ? allAddressBooks[chainId] || {} : {}
const chainAddresses = allAddressBooks[chainId]
const validAddresses = pickBy(chainAddresses, (_, key) => validateAddress(key) === undefined)
return chainId ? validAddresses || {} : {}
},
)