-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: detect smart contracts for every evm chains (#7358)
* fix: detect smart contracts for every evm chains * fix: handle non evm trades properly * fix: handle smartcontractaddress return type properly * chore: trigger ci * fix: typo * feat: fix button not disabled * fix: disable if thorchain swap * fix: big brain kev review * fix: remove all disabled state * fix: move isLoading to isLoading --------- Co-authored-by: woody <[email protected]>
- Loading branch information
1 parent
0397564
commit 9f00c73
Showing
14 changed files
with
67 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 17 additions & 16 deletions
33
src/hooks/useIsSmartContractAddress/useIsSmartContractAddress.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,26 @@ | ||
import { useQuery } from '@tanstack/react-query' | ||
import type { ChainId } from '@shapeshiftoss/caip' | ||
import { isEvmChainId } from '@shapeshiftoss/chain-adapters' | ||
import { skipToken, useQuery } from '@tanstack/react-query' | ||
import { useMemo } from 'react' | ||
import { isSmartContractAddress } from 'lib/address/utils' | ||
|
||
export const useIsSmartContractAddress = (address: string) => { | ||
export const useIsSmartContractAddress = (address: string, chainId: ChainId) => { | ||
// Lowercase the address to ensure proper caching | ||
const userAddress = useMemo(() => address.toLowerCase(), [address]) | ||
|
||
const queryParams = useMemo(() => { | ||
return { | ||
queryKey: [ | ||
'isSmartContractAddress', | ||
{ | ||
userAddress, | ||
}, | ||
], | ||
queryFn: () => isSmartContractAddress(userAddress), | ||
enabled: Boolean(userAddress.length), | ||
} | ||
}, [userAddress]) | ||
|
||
const query = useQuery(queryParams) | ||
const query = useQuery({ | ||
queryKey: [ | ||
'isSmartContractAddress', | ||
{ | ||
userAddress, | ||
chainId, | ||
}, | ||
], | ||
queryFn: | ||
isEvmChainId(chainId) && Boolean(userAddress.length) | ||
? () => isSmartContractAddress(userAddress, chainId) | ||
: skipToken, | ||
}) | ||
|
||
return query | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
import type { ChainId } from '@shapeshiftoss/caip' | ||
import { isEvmChainId } from '@shapeshiftoss/chain-adapters' | ||
import { isAddress } from 'viem' | ||
import { getEthersProvider } from 'lib/ethersProviderSingleton' | ||
|
||
export const isEthAddress = (address: string): boolean => /^0x[0-9A-Fa-f]{40}$/.test(address) | ||
|
||
export const isSmartContractAddress = async (address: string): Promise<boolean> => { | ||
export const isSmartContractAddress = async ( | ||
address: string, | ||
chainId: ChainId, | ||
): Promise<boolean> => { | ||
if (!isAddress(address)) return false | ||
const bytecode = await getEthersProvider().getCode(address) | ||
if (!isEvmChainId(chainId)) return false | ||
const bytecode = await getEthersProvider(chainId).getCode(address) | ||
return bytecode !== '0x' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters