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/masquerade checksum #1455

Merged
merged 7 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 11 additions & 9 deletions packages/synapse-interface/components/Portfolio/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -64,27 +64,29 @@ export const SearchBar = () => {
}
}, [activeTab])

const searchInputIsAddress: boolean = useMemo(() => {
return isValidAddress(searchInput)
}, [searchInput])

const searchInputIsTransactionHash: boolean = useMemo(() => {
return isTransactionHash(searchInput)
}, [searchInput])

const checksumValidAddress: Address | null = useMemo(() => {
return getValidAddress(searchInput)
}, [searchInput])

useEffect(() => {
const masqueradeActive: boolean =
Object.keys(searchedBalancesAndAllowances).length > 0
if (searchInputIsAddress && !masqueradeActive) {
if (checksumValidAddress && !masqueradeActive) {
dispatch(
fetchAndStoreSearchInputPortfolioBalances(searchInput as Address)
fetchAndStoreSearchInputPortfolioBalances(
checksumValidAddress as Address
)
)
}

if (masqueradeActive && searchInputIsAddress) {
if (masqueradeActive && checksumValidAddress) {
clearSearchInput()
}
}, [searchInputIsAddress, searchedBalancesAndAllowances])
}, [checksumValidAddress, searchedBalancesAndAllowances])

useEffect(() => {
if (searchInputIsTransactionHash) {
Expand Down
7 changes: 3 additions & 4 deletions packages/synapse-interface/slices/transactions/updater.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ import {
addPendingAwaitingCompletionTransaction,
removePendingAwaitingCompletionTransaction,
} from './actions'
import { isValidAddress } from '@/utils/isValidAddress'
import { checkTransactionsExist } from '@/components/Portfolio/Activity'
import { getValidAddress } from '@/utils/isValidAddress'

const queryHistoricalTime: number = getTimeMinutesBeforeNow(oneMonthInMinutes)
const queryPendingTime: number = getTimeMinutesBeforeNow(oneDayInMinutes)
Expand Down Expand Up @@ -99,11 +98,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,
})
Comment on lines 98 to 107
Copy link
Contributor

Choose a reason for hiding this comment

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

The getValidAddress function is used to validate the queriedAddress before it is used in the fetchUserHistoricalActivity and fetchUserPendingActivity functions. This ensures that the address is valid and checksummed before querying, improving the reliability of the system.

-        fetchUserHistoricalActivity({
-         address: queriedAddress,
-         startTime: queryHistoricalTime,
-       })
-       fetchUserPendingActivity({
-         address: queriedAddress,
-         startTime: queryPendingTime,
-       })
+        fetchUserHistoricalActivity({
+         address: getValidAddress(queriedAddress),
+         startTime: queryHistoricalTime,
+       })
+       fetchUserPendingActivity({
+         address: getValidAddress(queriedAddress),
+         startTime: queryPendingTime,
+       })

}
Expand Down
15 changes: 13 additions & 2 deletions packages/synapse-interface/utils/isValidAddress.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { getAddress } from '@ethersproject/address'
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 => {
try {
const validatedAddress: Address = getAddress(address)
return validatedAddress
} catch (e: InvalidAddressError | any) {
console.error('getValidAddress error: ', e)
return null
}
}
Loading