From 9cfad6f4276f2644b7cfbe4ba87b18b90092b205 Mon Sep 17 00:00:00 2001 From: Jonah Lin <57741810+linjonah@users.noreply.github.com> Date: Mon, 16 Oct 2023 09:59:55 -0700 Subject: [PATCH 1/7] refactor get address functionality to use viem and return checksummed address --- .../components/Portfolio/SearchBar.tsx | 18 +++++++++++++----- .../synapse-interface/utils/isValidAddress.tsx | 11 +++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/packages/synapse-interface/components/Portfolio/SearchBar.tsx b/packages/synapse-interface/components/Portfolio/SearchBar.tsx index f8b3ef66cc..cb74d84bca 100644 --- a/packages/synapse-interface/components/Portfolio/SearchBar.tsx +++ b/packages/synapse-interface/components/Portfolio/SearchBar.tsx @@ -13,7 +13,7 @@ import { initialState as portfolioInitialState, PortfolioState, } from '@/slices/portfolio/reducer' -import { isValidAddress } from '@/utils/isValidAddress' +import { isValidAddress, getValidAddress } from '@/utils/isValidAddress' import { shortenAddress } from '@/utils/shortenAddress' import { isTransactionHash } from '@/utils/validators' import { getTransactionHashExplorerLink } from './Transaction/components/TransactionExplorerLink' @@ -72,19 +72,27 @@ export const SearchBar = () => { return isTransactionHash(searchInput) }, [searchInput]) + const checkSearchInputIsAddress: Address | null = useMemo(() => { + return getValidAddress(searchInput) + }, [searchInput]) + + console.log('checkSearchInputIsAddress:', checkSearchInputIsAddress) + useEffect(() => { const masqueradeActive: boolean = Object.keys(searchedBalancesAndAllowances).length > 0 - if (searchInputIsAddress && !masqueradeActive) { + if (checkSearchInputIsAddress && !masqueradeActive) { dispatch( - fetchAndStoreSearchInputPortfolioBalances(searchInput as Address) + fetchAndStoreSearchInputPortfolioBalances( + checkSearchInputIsAddress as Address + ) ) } - if (masqueradeActive && searchInputIsAddress) { + if (masqueradeActive && checkSearchInputIsAddress) { clearSearchInput() } - }, [searchInputIsAddress, searchedBalancesAndAllowances]) + }, [checkSearchInputIsAddress, searchedBalancesAndAllowances]) useEffect(() => { if (searchInputIsTransactionHash) { diff --git a/packages/synapse-interface/utils/isValidAddress.tsx b/packages/synapse-interface/utils/isValidAddress.tsx index 91fee7b65f..123df36e79 100644 --- a/packages/synapse-interface/utils/isValidAddress.tsx +++ b/packages/synapse-interface/utils/isValidAddress.tsx @@ -1,4 +1,5 @@ import { getAddress } from '@ethersproject/address' +import { getAddress as getAddressViem, Address } from 'viem' export const isValidAddress = (address: string): boolean => { try { @@ -8,3 +9,13 @@ export const isValidAddress = (address: string): boolean => { return false } } + +export const getValidAddress = (address: string): Address | any => { + try { + const validatedAddress: Address = getAddressViem(address) + return validatedAddress + } catch (e: any) { + console.error('getValidAddress error: ', e) + return null + } +} From 97c81c7708133ba7df109af96d858ad15c4ae0c1 Mon Sep 17 00:00:00 2001 From: Jonah Lin <57741810+linjonah@users.noreply.github.com> Date: Mon, 16 Oct 2023 10:05:59 -0700 Subject: [PATCH 2/7] auto checksum address when querying from search input --- packages/synapse-interface/slices/transactions/updater.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/synapse-interface/slices/transactions/updater.tsx b/packages/synapse-interface/slices/transactions/updater.tsx index 5c4081d113..5f0d08943c 100644 --- a/packages/synapse-interface/slices/transactions/updater.tsx +++ b/packages/synapse-interface/slices/transactions/updater.tsx @@ -39,7 +39,7 @@ import { addPendingAwaitingCompletionTransaction, removePendingAwaitingCompletionTransaction, } from './actions' -import { isValidAddress } from '@/utils/isValidAddress' +import { isValidAddress, getValidAddress } from '@/utils/isValidAddress' import { checkTransactionsExist } from '@/components/Portfolio/Activity' const queryHistoricalTime: number = getTimeMinutesBeforeNow(oneMonthInMinutes) @@ -99,11 +99,11 @@ export default function Updater(): null { searchedBalancesAndAllowances )[0] as Address fetchUserHistoricalActivity({ - address: queriedAddress, + address: getValidAddress(queriedAddress), startTime: queryHistoricalTime, }) fetchUserPendingActivity({ - address: queriedAddress, + address: getValidAddress(queriedAddress), startTime: queryPendingTime, }) } From e9d3cb80a035a989125f1d91dd1df90caf56f793 Mon Sep 17 00:00:00 2001 From: Jonah Lin <57741810+linjonah@users.noreply.github.com> Date: Mon, 16 Oct 2023 10:08:23 -0700 Subject: [PATCH 3/7] rm previous check var --- .../synapse-interface/components/Portfolio/SearchBar.tsx | 6 ------ 1 file changed, 6 deletions(-) diff --git a/packages/synapse-interface/components/Portfolio/SearchBar.tsx b/packages/synapse-interface/components/Portfolio/SearchBar.tsx index cb74d84bca..a5e9d170af 100644 --- a/packages/synapse-interface/components/Portfolio/SearchBar.tsx +++ b/packages/synapse-interface/components/Portfolio/SearchBar.tsx @@ -64,10 +64,6 @@ export const SearchBar = () => { } }, [activeTab]) - const searchInputIsAddress: boolean = useMemo(() => { - return isValidAddress(searchInput) - }, [searchInput]) - const searchInputIsTransactionHash: boolean = useMemo(() => { return isTransactionHash(searchInput) }, [searchInput]) @@ -76,8 +72,6 @@ export const SearchBar = () => { return getValidAddress(searchInput) }, [searchInput]) - console.log('checkSearchInputIsAddress:', checkSearchInputIsAddress) - useEffect(() => { const masqueradeActive: boolean = Object.keys(searchedBalancesAndAllowances).length > 0 From 01dc66bd96022633eb057965fe3ef2ee725e2a7f Mon Sep 17 00:00:00 2001 From: Jonah Lin <57741810+linjonah@users.noreply.github.com> Date: Mon, 16 Oct 2023 10:15:49 -0700 Subject: [PATCH 4/7] Refactor isValidAddress to use viem --- .../synapse-interface/utils/isValidAddress.tsx | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/synapse-interface/utils/isValidAddress.tsx b/packages/synapse-interface/utils/isValidAddress.tsx index 123df36e79..9da1e5f895 100644 --- a/packages/synapse-interface/utils/isValidAddress.tsx +++ b/packages/synapse-interface/utils/isValidAddress.tsx @@ -1,20 +1,22 @@ -import { getAddress } from '@ethersproject/address' -import { getAddress as getAddressViem, Address } from 'viem' +import { getAddress, Address, InvalidAddressError } from 'viem' export const isValidAddress = (address: string): boolean => { try { const validatedAddress: string = getAddress(address) return true - } catch (e: any) { + } catch (e: InvalidAddressError | any) { + console.error('isValidAddress error: ', e) return false } } -export const getValidAddress = (address: string): Address | any => { +export const getValidAddress = ( + address: string +): Address | InvalidAddressError => { try { - const validatedAddress: Address = getAddressViem(address) + const validatedAddress: Address = getAddress(address) return validatedAddress - } catch (e: any) { + } catch (e: InvalidAddressError | any) { console.error('getValidAddress error: ', e) return null } From a113e3ba86c53dc0f73770fd80dda124dced16e8 Mon Sep 17 00:00:00 2001 From: Jonah Lin <57741810+linjonah@users.noreply.github.com> Date: Mon, 16 Oct 2023 10:16:17 -0700 Subject: [PATCH 5/7] ... --- packages/synapse-interface/utils/isValidAddress.tsx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/synapse-interface/utils/isValidAddress.tsx b/packages/synapse-interface/utils/isValidAddress.tsx index 9da1e5f895..f727ee3d7e 100644 --- a/packages/synapse-interface/utils/isValidAddress.tsx +++ b/packages/synapse-interface/utils/isValidAddress.tsx @@ -10,9 +10,7 @@ export const isValidAddress = (address: string): boolean => { } } -export const getValidAddress = ( - address: string -): Address | InvalidAddressError => { +export const getValidAddress = (address: string): Address | any => { try { const validatedAddress: Address = getAddress(address) return validatedAddress From 5aaa8e128a296ad02ac657d7fccdecb13701625b Mon Sep 17 00:00:00 2001 From: Jonah Lin <57741810+linjonah@users.noreply.github.com> Date: Mon, 16 Oct 2023 14:43:10 -0700 Subject: [PATCH 6/7] Update var naming more semantically --- .../components/Portfolio/SearchBar.tsx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/synapse-interface/components/Portfolio/SearchBar.tsx b/packages/synapse-interface/components/Portfolio/SearchBar.tsx index a5e9d170af..ef4b5bbff2 100644 --- a/packages/synapse-interface/components/Portfolio/SearchBar.tsx +++ b/packages/synapse-interface/components/Portfolio/SearchBar.tsx @@ -68,25 +68,25 @@ export const SearchBar = () => { return isTransactionHash(searchInput) }, [searchInput]) - const checkSearchInputIsAddress: Address | null = useMemo(() => { + const checksumValidAddress: Address | null = useMemo(() => { return getValidAddress(searchInput) }, [searchInput]) useEffect(() => { const masqueradeActive: boolean = Object.keys(searchedBalancesAndAllowances).length > 0 - if (checkSearchInputIsAddress && !masqueradeActive) { + if (checksumValidAddress && !masqueradeActive) { dispatch( fetchAndStoreSearchInputPortfolioBalances( - checkSearchInputIsAddress as Address + checksumValidAddress as Address ) ) } - if (masqueradeActive && checkSearchInputIsAddress) { + if (masqueradeActive && checksumValidAddress) { clearSearchInput() } - }, [checkSearchInputIsAddress, searchedBalancesAndAllowances]) + }, [checksumValidAddress, searchedBalancesAndAllowances]) useEffect(() => { if (searchInputIsTransactionHash) { From cc1afd071aaf7070ec732025c0b5ed7106ad0b41 Mon Sep 17 00:00:00 2001 From: Jonah Lin <57741810+linjonah@users.noreply.github.com> Date: Mon, 16 Oct 2023 14:47:04 -0700 Subject: [PATCH 7/7] clean --- packages/synapse-interface/slices/transactions/updater.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/synapse-interface/slices/transactions/updater.tsx b/packages/synapse-interface/slices/transactions/updater.tsx index 5f0d08943c..ffde69b491 100644 --- a/packages/synapse-interface/slices/transactions/updater.tsx +++ b/packages/synapse-interface/slices/transactions/updater.tsx @@ -39,8 +39,7 @@ import { addPendingAwaitingCompletionTransaction, removePendingAwaitingCompletionTransaction, } from './actions' -import { isValidAddress, getValidAddress } from '@/utils/isValidAddress' -import { checkTransactionsExist } from '@/components/Portfolio/Activity' +import { getValidAddress } from '@/utils/isValidAddress' const queryHistoricalTime: number = getTimeMinutesBeforeNow(oneMonthInMinutes) const queryPendingTime: number = getTimeMinutesBeforeNow(oneDayInMinutes)