diff --git a/examples/react/src/App.tsx b/examples/react/src/App.tsx index ba5c7704..366909c6 100644 --- a/examples/react/src/App.tsx +++ b/examples/react/src/App.tsx @@ -7,7 +7,7 @@ import { KitCheckoutProvider } from '@0xsequence/kit-checkout' import Homepage from './components/Homepage' import { WagmiConfig, createConfig, configureChains } from 'wagmi' import { publicProvider } from 'wagmi/providers/public' -import { mainnet, polygon } from 'wagmi/chains' +import { mainnet, polygon, arbitrumGoerli } from 'wagmi/chains' import '@0xsequence/design-system/styles.css' @@ -37,23 +37,23 @@ function App() { // logoUrlDarkMode: 'sw-logo-white.svg', // logoUrlLightMode: 'sw-logo-black.svg', }, - displayedAssets: [ - // Matic token - { - contractAddress: ethers.constants.AddressZero, - chainId: 137, - }, - // USDC token - { - contractAddress: '0x2791bca1f2de4661ed88a30c99a7a9449aa84174', - chainId: 137, - }, - // skyweaver collectibles - { - contractAddress: '0x631998e91476da5b870d741192fc5cbc55f5a52e', - chainId: 137 - } - ], + // displayedAssets: [ + // // Matic token + // { + // contractAddress: ethers.constants.AddressZero, + // chainId: 137, + // }, + // // USDC token + // { + // contractAddress: '0x2791bca1f2de4661ed88a30c99a7a9449aa84174', + // chainId: 137, + // }, + // // skyweaver collectibles + // { + // contractAddress: '0x631998e91476da5b870d741192fc5cbc55f5a52e', + // chainId: 137 + // } + // ], } return ( diff --git a/examples/react/src/components/Footer.tsx b/examples/react/src/components/Footer.tsx index 727793ff..99898c77 100644 --- a/examples/react/src/components/Footer.tsx +++ b/examples/react/src/components/Footer.tsx @@ -28,9 +28,10 @@ export const Footer = () => { const Socials = () => { return ( - {socialLinks.map(socialLink => { + {socialLinks.map((socialLink, index) => { return ( window.open(socialLink.url)} > diff --git a/packages/checkout/src/views/CheckoutSelection/component/OrderSummaryItem.tsx b/packages/checkout/src/views/CheckoutSelection/component/OrderSummaryItem.tsx index cacf0ca5..c0fb1d12 100644 --- a/packages/checkout/src/views/CheckoutSelection/component/OrderSummaryItem.tsx +++ b/packages/checkout/src/views/CheckoutSelection/component/OrderSummaryItem.tsx @@ -1,8 +1,8 @@ import React from 'react' import { ethers } from 'ethers' +import { useNetwork } from 'wagmi' import { Box, Card, Image, Text } from '@0xsequence/design-system' import { getNativeTokenInfoByChainId } from '@0xsequence/kit' - import { CoinIcon } from '../../../shared/components/CoinIcon' import { Skeleton } from '../../../shared/components/Skeleton' @@ -22,6 +22,7 @@ export const OrderSummaryItem = ({ quantityRaw, chainId, }: OrderSummaryItem) => { + const { chains = [] } = useNetwork() const { data: tokenMetadata, isLoading: isTokenMetadataLoading, @@ -47,7 +48,7 @@ export const OrderSummaryItem = ({ ) } - const nativeTokenInfo = getNativeTokenInfoByChainId(chainId) + const nativeTokenInfo = getNativeTokenInfoByChainId(chainId, chains) const { name = 'unknown', image, decimals = 0 } = tokenMetadata || {} const { diff --git a/packages/checkout/src/views/CheckoutSelection/index.tsx b/packages/checkout/src/views/CheckoutSelection/index.tsx index bfc0966f..46d00a60 100644 --- a/packages/checkout/src/views/CheckoutSelection/index.tsx +++ b/packages/checkout/src/views/CheckoutSelection/index.tsx @@ -12,7 +12,7 @@ import { import { getNativeTokenInfoByChainId } from '@0xsequence/kit' -import { useAccount } from 'wagmi' +import { useAccount, useNetwork } from 'wagmi' import { OrderSummaryItem } from './component/OrderSummaryItem' @@ -32,6 +32,7 @@ import { import * as styles from './styles.css' export const CheckoutSelection = () => { + const { chains = [] } = useNetwork() const { setNavigation } = useNavigation() const { closeCheckout, settings } = useCheckoutModal() const { address: accountAddress } = useAccount() @@ -60,7 +61,7 @@ export const CheckoutSelection = () => { const isLoading = (contractInfoLoading || balancesLoading) && cryptoCheckoutSettings const isNativeToken = compareAddress(cryptoCheckoutSettings?.coinQuantity?.contractAddress || '', ethers.constants.AddressZero) - const nativeTokenInfo = getNativeTokenInfoByChainId(cryptoCheckoutSettings?.chainId || 1) + const nativeTokenInfo = getNativeTokenInfoByChainId(cryptoCheckoutSettings?.chainId || 1, chains) const coinDecimals = isNativeToken ? nativeTokenInfo.decimals : contractInfoData?.decimals || 0 const coinSymbol = isNativeToken ? nativeTokenInfo.symbol : contractInfoData?.symbol || 'COIN' diff --git a/packages/kit/src/utils/tokens.ts b/packages/kit/src/utils/tokens.ts index 8de012ff..52baf9ac 100644 --- a/packages/kit/src/utils/tokens.ts +++ b/packages/kit/src/utils/tokens.ts @@ -1,3 +1,5 @@ +import { Chain } from 'wagmi' + export interface NativeTokenInfo { name: string, symbol: string, @@ -99,6 +101,24 @@ export const getChainIdList = () => { return Object.keys(nativeTokenInfos).map(chainId => parseInt(chainId)) } -export const getNativeTokenInfoByChainId = (chainId: number) => { - return nativeTokenInfos[chainId] || nativeTokenInfos[1] + +export const defaultNativeTokenInfo = (chainId: number, wagmiChains: Chain[]) => { + const foundChain = wagmiChains.find(chain => chain.id === chainId) + + if (foundChain) { + return { + name: foundChain.name, + symbol: foundChain.nativeCurrency.symbol, + decimals: foundChain.nativeCurrency.decimals, + logoURI: nativeTokenInfos[1].logoURI, + blockExplorerName: foundChain.blockExplorers?.default.name, + blockExplorerUrl: foundChain.blockExplorers?.default.url + } + } + + return +} + +export const getNativeTokenInfoByChainId = (chainId: number, wagmiChains: Chain[]) => { + return nativeTokenInfos[chainId] || defaultNativeTokenInfo(chainId, wagmiChains) || nativeTokenInfos[1] } \ No newline at end of file diff --git a/packages/wallet/src/shared/NetworkBadge.tsx b/packages/wallet/src/shared/NetworkBadge.tsx index 3c13ef62..0a76074f 100644 --- a/packages/wallet/src/shared/NetworkBadge.tsx +++ b/packages/wallet/src/shared/NetworkBadge.tsx @@ -1,6 +1,7 @@ import React from 'react' import { Box, Image, Text } from '@0xsequence/design-system' import { getNativeTokenInfoByChainId, getChainColor, getChainBGColor } from '@0xsequence/kit' +import { useNetwork } from 'wagmi' import { getNetworkConfigAndClients, @@ -14,8 +15,9 @@ interface NetworkBadgeProps { export const NetworkBadge = ({ chainId }: NetworkBadgeProps) => { + const { chains = [] } = useNetwork() const { network } = getNetworkConfigAndClients(chainId) - const nativeTokenInfo = getNativeTokenInfoByChainId(chainId) + const nativeTokenInfo = getNativeTokenInfoByChainId(chainId, chains) const chainColor = getChainColor(chainId) const chainBGColor = getChainBGColor(chainId) diff --git a/packages/wallet/src/shared/SendItemInfo.tsx b/packages/wallet/src/shared/SendItemInfo.tsx index bc930ce9..3ec51ff8 100644 --- a/packages/wallet/src/shared/SendItemInfo.tsx +++ b/packages/wallet/src/shared/SendItemInfo.tsx @@ -3,6 +3,7 @@ import { Box, Image, Text, vars } from '@0xsequence/design-system' import React from 'react' import { Skeleton } from './Skeleton' import { ethers } from 'ethers' +import { useNetwork } from 'wagmi' import { getNativeTokenInfoByChainId } from '@0xsequence/kit' @@ -50,10 +51,11 @@ export const SendItemInfo = ({ chainId, showSquareImage, }: SendItemInfoProps) => { + const { chains = [] } = useNetwork() const { fiatCurrency } = useSettings() const formattedBalance = ethers.utils.formatUnits(balance, decimals) const balanceDisplayed = formatDisplay(formattedBalance) - const nativeTokenInfo = getNativeTokenInfoByChainId(chainId) + const nativeTokenInfo = getNativeTokenInfoByChainId(chainId, chains) return ( diff --git a/packages/wallet/src/shared/TransactionHistoryList/TransactionHistoryItem.tsx b/packages/wallet/src/shared/TransactionHistoryList/TransactionHistoryItem.tsx index f7000b2e..254ab92f 100644 --- a/packages/wallet/src/shared/TransactionHistoryList/TransactionHistoryItem.tsx +++ b/packages/wallet/src/shared/TransactionHistoryList/TransactionHistoryItem.tsx @@ -13,6 +13,7 @@ import { vars } from '@0xsequence/design-system' import dayjs from 'dayjs' +import { useNetwork } from 'wagmi' import * as sharedStyles from '../../shared/styles.css' import { Skeleton } from '../../shared/Skeleton' @@ -35,6 +36,7 @@ interface TransactionHistoryItemProps { export const TransactionHistoryItem = ({ transaction }: TransactionHistoryItemProps) => { + const { chains = [] } = useNetwork() const { fiatCurrency } = useSettings() const { setNavigation } = useNavigation() @@ -74,7 +76,7 @@ export const TransactionHistoryItem = ({ const { transfers } = transaction - const nativeTokenInfo = getNativeTokenInfoByChainId(transaction.chainId) + const nativeTokenInfo = getNativeTokenInfoByChainId(transaction.chainId, chains) const getTransactionIconByType = (transferType: TxnTransferType) => { switch(transferType) { @@ -162,7 +164,7 @@ export const TransactionHistoryItem = ({ {amounts.map((amount, index) => { - const nativeTokenInfo = getNativeTokenInfoByChainId(transaction.chainId) + const nativeTokenInfo = getNativeTokenInfoByChainId(transaction.chainId, chains) const isNativeToken = compareAddress(transfer.contractAddress, ethers.constants.AddressZero) const isCollectible = transfer.contractInfo?.type === 'ERC721' || transfer.contractInfo?.type === 'ERC1155' let decimals diff --git a/packages/wallet/src/views/CoinDetails/index.tsx b/packages/wallet/src/views/CoinDetails/index.tsx index dc857a6e..5c9d957a 100644 --- a/packages/wallet/src/views/CoinDetails/index.tsx +++ b/packages/wallet/src/views/CoinDetails/index.tsx @@ -3,7 +3,7 @@ import { ethers } from 'ethers' import { Box, Button, Image, SendIcon, Text, vars } from '@0xsequence/design-system' import { getNativeTokenInfoByChainId } from '@0xsequence/kit' -import { useAccount } from 'wagmi' +import { useAccount, useNetwork } from 'wagmi' import { CoinDetailsSkeleton } from './Skeleton' @@ -35,6 +35,7 @@ export const CoinDetails = ({ contractAddress, chainId }: CoinDetailsProps) => { + const { chains = [] } = useNetwork() const { setNavigation } = useNavigation() const { fiatCurrency, hideUnlistedTokens } = useSettings() @@ -82,10 +83,10 @@ export const CoinDetails = ({ } const isNativeToken = compareAddress(contractAddress, ethers.constants.AddressZero) - const logo = isNativeToken ? getNativeTokenInfoByChainId(chainId).logoURI : dataCoinBalance?.contractInfo?.logoURI - const symbol = isNativeToken ? getNativeTokenInfoByChainId(chainId).symbol : dataCoinBalance?.contractInfo?.symbol - const name = isNativeToken ? getNativeTokenInfoByChainId(chainId).name : dataCoinBalance?.contractInfo?.name - const decimals = isNativeToken ? getNativeTokenInfoByChainId(chainId).decimals : dataCoinBalance?.contractInfo?.decimals + const logo = isNativeToken ? getNativeTokenInfoByChainId(chainId, chains).logoURI : dataCoinBalance?.contractInfo?.logoURI + const symbol = isNativeToken ? getNativeTokenInfoByChainId(chainId, chains).symbol : dataCoinBalance?.contractInfo?.symbol + const name = isNativeToken ? getNativeTokenInfoByChainId(chainId, chains).name : dataCoinBalance?.contractInfo?.name + const decimals = isNativeToken ? getNativeTokenInfoByChainId(chainId, chains).decimals : dataCoinBalance?.contractInfo?.decimals const formattedBalance = ethers.utils.formatUnits(dataCoinBalance?.balance || '0', decimals) const balanceDisplayed = formatDisplay(formattedBalance) diff --git a/packages/wallet/src/views/CollectibleDetails/index.tsx b/packages/wallet/src/views/CollectibleDetails/index.tsx index 5dce6780..65153e8e 100644 --- a/packages/wallet/src/views/CollectibleDetails/index.tsx +++ b/packages/wallet/src/views/CollectibleDetails/index.tsx @@ -1,6 +1,6 @@ import React from 'react' import { ethers } from 'ethers' -import { useAccount } from 'wagmi' +import { useAccount, useNetwork } from 'wagmi' import { Box, Button, Image, SendIcon, Text, vars } from '@0xsequence/design-system' import { getNativeTokenInfoByChainId } from '@0xsequence/kit' @@ -35,6 +35,7 @@ export const CollectibleDetails = ({ chainId, tokenId, }: CollectibleDetailsProps) => { + const { chains = [] } = useNetwork() const { address: accountAddress } = useAccount() const { fiatCurrency } = useSettings() const { setNavigation } = useNavigation() @@ -95,7 +96,7 @@ export const CollectibleDetails = ({ }) } - const nativeTokenInfo = getNativeTokenInfoByChainId(chainId) + const nativeTokenInfo = getNativeTokenInfoByChainId(chainId, chains) const collectionLogo = dataCollectibleBalance?.contractInfo?.logoURI const collectionName = dataCollectibleBalance?.contractInfo?.name || 'Unknown Collection' diff --git a/packages/wallet/src/views/Home/components/AssetSummary/CoinTile/index.tsx b/packages/wallet/src/views/Home/components/AssetSummary/CoinTile/index.tsx index 870616e3..8b87ce98 100644 --- a/packages/wallet/src/views/Home/components/AssetSummary/CoinTile/index.tsx +++ b/packages/wallet/src/views/Home/components/AssetSummary/CoinTile/index.tsx @@ -1,5 +1,6 @@ import React from 'react' import { ethers } from 'ethers' +import { useNetwork } from 'wagmi' import { Box, Text } from '@0xsequence/design-system' import { TokenBalance } from '@0xsequence/indexer' @@ -27,9 +28,10 @@ interface CoinTileProps { export const CoinTile = ({ balance }: CoinTileProps) => { + const { chains = [] } = useNetwork() const { fiatCurrency } = useSettings() const isNativeToken = compareAddress(balance.contractAddress, ethers.constants.AddressZero) - const nativeTokenInfo = getNativeTokenInfoByChainId(balance.chainId) + const nativeTokenInfo = getNativeTokenInfoByChainId(balance.chainId, chains) const { data: dataCoinPrices = [], isLoading: isLoadingCoinPrice } = useCoinPrices({ tokens: [{ diff --git a/packages/wallet/src/views/Receive.tsx b/packages/wallet/src/views/Receive.tsx index 1842ef19..8faaad8e 100644 --- a/packages/wallet/src/views/Receive.tsx +++ b/packages/wallet/src/views/Receive.tsx @@ -9,10 +9,10 @@ import { getNativeTokenInfoByChainId } from '@0xsequence/kit' export const Receive = () => { const { address } = useAccount() - const { chain } = useNetwork() + const { chain, chains = [] } = useNetwork() const [isCopied, setCopied] = useState(false) - const nativeTokenInfo = getNativeTokenInfoByChainId(chain?.id || 1) + const nativeTokenInfo = getNativeTokenInfoByChainId(chain?.id || 1, chains) useEffect(() => { if (isCopied) { diff --git a/packages/wallet/src/views/Search/SearchWallet.tsx b/packages/wallet/src/views/Search/SearchWallet.tsx index bda1f379..7730568e 100644 --- a/packages/wallet/src/views/Search/SearchWallet.tsx +++ b/packages/wallet/src/views/Search/SearchWallet.tsx @@ -3,7 +3,7 @@ import { ethers } from 'ethers' import { Box, SearchIcon, Text, TextInput, vars } from '@0xsequence/design-system' import { getNativeTokenInfoByChainId } from '@0xsequence/kit' import Fuse from 'fuse.js' -import { useAccount } from 'wagmi' +import { useAccount, useNetwork } from 'wagmi' import { BalanceItem } from './components/BalanceItem' import { WalletLink } from './components/WalletLink' @@ -19,6 +19,7 @@ import { import { compareAddress, computeBalanceFiat } from '../../utils' export const SearchWallet = () => { + const { chains = [] } = useNetwork() const { fiatCurrency, hideUnlistedTokens, selectedNetworks } = useSettings() const [search, setSearch] = useState('') const { address: accountAddress } = useAccount() @@ -72,7 +73,7 @@ export const SearchWallet = () => { const indexedCoinBalances: IndexedData[] = coinBalances.map((balance, index) => { if (compareAddress(balance.contractAddress, ethers.constants.AddressZero)) { - const nativeTokenInfo = getNativeTokenInfoByChainId(balance.chainId) + const nativeTokenInfo = getNativeTokenInfoByChainId(balance.chainId, chains) return { index, diff --git a/packages/wallet/src/views/Search/SearchWalletViewAll.tsx b/packages/wallet/src/views/Search/SearchWalletViewAll.tsx index 0d936568..9515d48b 100644 --- a/packages/wallet/src/views/Search/SearchWalletViewAll.tsx +++ b/packages/wallet/src/views/Search/SearchWalletViewAll.tsx @@ -13,7 +13,7 @@ import { import { getNativeTokenInfoByChainId } from '@0xsequence/kit' import { BalanceItem } from './components/BalanceItem' import Fuse from 'fuse.js' -import { useAccount } from 'wagmi' +import { useAccount, useNetwork } from 'wagmi' import { Skeleton } from '../../shared/Skeleton' import { SCROLLBAR_WIDTH } from '../../constants' @@ -32,6 +32,7 @@ interface SearchWalletViewAllProps { export const SearchWalletViewAll = ({ defaultTab }: SearchWalletViewAllProps) => { + const { chains = [] } = useNetwork() const { fiatCurrency, hideUnlistedTokens, selectedNetworks } = useSettings() const [search, setSearch] = useState('') const [selectedTab, setSelectedTab] = useState(defaultTab) @@ -93,7 +94,7 @@ export const SearchWalletViewAll = ({ const indexedCoinBalances: IndexedData[] = coinBalances.map((balance, index) => { if (compareAddress(balance.contractAddress, ethers.constants.AddressZero)) { - const nativeTokenInfo = getNativeTokenInfoByChainId(balance.chainId) + const nativeTokenInfo = getNativeTokenInfoByChainId(balance.chainId, chains) return { index, diff --git a/packages/wallet/src/views/Search/components/BalanceItem.tsx b/packages/wallet/src/views/Search/components/BalanceItem.tsx index f6d6df54..ea5cfdbf 100644 --- a/packages/wallet/src/views/Search/components/BalanceItem.tsx +++ b/packages/wallet/src/views/Search/components/BalanceItem.tsx @@ -1,5 +1,6 @@ import React from 'react' import { ethers } from 'ethers' +import { useNetwork } from 'wagmi' import { Box, Image, Text, ChevronRightIcon } from '@0xsequence/design-system' import { TokenBalance } from '@0xsequence/indexer' import { getNativeTokenInfoByChainId } from '@0xsequence/kit' @@ -20,9 +21,10 @@ interface BalanceItemProps { export const BalanceItem = ({ balance }: BalanceItemProps) => { + const { chains = [] } = useNetwork() const { setNavigation } = useNavigation() const isNativeToken = compareAddress(balance.contractAddress, ethers.constants.AddressZero) - const nativeTokenInfo = getNativeTokenInfoByChainId(balance.chainId) + const nativeTokenInfo = getNativeTokenInfoByChainId(balance.chainId, chains) const logoURI = isNativeToken ? nativeTokenInfo.logoURI : balance?.contractInfo?.logoURI const tokenName = isNativeToken ? nativeTokenInfo.name : balance?.contractInfo?.name || 'Unknown' diff --git a/packages/wallet/src/views/SendCoin.tsx b/packages/wallet/src/views/SendCoin.tsx index 9ae91939..40a98ddd 100644 --- a/packages/wallet/src/views/SendCoin.tsx +++ b/packages/wallet/src/views/SendCoin.tsx @@ -18,7 +18,8 @@ import { useAccount, useChainId, useSwitchNetwork, - useWalletClient + useWalletClient, + useNetwork } from 'wagmi' import { SendItemInfo } from '../shared/SendItemInfo' @@ -49,6 +50,7 @@ export const SendCoin = ({ chainId, contractAddress }: SendCoinProps) => { + const { chains = [] } = useNetwork() const connectedChainId = useChainId() const { address: accountAddress = '', connector } = useAccount() /* @ts-ignore-next-line */ @@ -67,7 +69,7 @@ export const SendCoin = ({ chainIds: [chainId], contractAddress, }, { hideUnlistedTokens: false }) - const nativeTokenInfo = getNativeTokenInfoByChainId(chainId) + const nativeTokenInfo = getNativeTokenInfoByChainId(chainId, chains) const tokenBalance = balances.find(b => b.contractAddress === contractAddress) const { data: coinPrices = [], diff --git a/packages/wallet/src/views/SendCollectible.tsx b/packages/wallet/src/views/SendCollectible.tsx index a35b9bef..718f6b28 100644 --- a/packages/wallet/src/views/SendCollectible.tsx +++ b/packages/wallet/src/views/SendCollectible.tsx @@ -16,7 +16,7 @@ import { } from '@0xsequence/design-system' import { getNativeTokenInfoByChainId } from '@0xsequence/kit' import { TokenBalance } from '@0xsequence/indexer' -import { useAccount, useChainId, useSwitchNetwork, useWalletClient } from 'wagmi' +import { useAccount, useChainId, useSwitchNetwork, useWalletClient, useNetwork } from 'wagmi' import { SendItemInfo } from '../shared/SendItemInfo' import { ERC_1155_ABI, ERC_721_ABI } from '../constants' @@ -44,6 +44,7 @@ export const SendCollectible = ({ contractAddress, tokenId }: SendCollectibleProps) => { + const { chains = [] } = useNetwork() const connectedChainId = useChainId() const { address: accountAddress = '', connector } = useAccount() /* @ts-ignore-next-line */ @@ -62,7 +63,7 @@ export const SendCollectible = ({ collectionAddress: contractAddress, tokenId, }) - const nativeTokenInfo = getNativeTokenInfoByChainId(chainId) + const nativeTokenInfo = getNativeTokenInfoByChainId(chainId, chains) const isLoading = isLoadingBalances diff --git a/packages/wallet/src/views/Settings/Network.tsx b/packages/wallet/src/views/Settings/Network.tsx index 48ded30e..f5e22b6e 100644 --- a/packages/wallet/src/views/Settings/Network.tsx +++ b/packages/wallet/src/views/Settings/Network.tsx @@ -10,7 +10,7 @@ import { useSettings } from '../../hooks' export const SettingsNetwork = () => { const { selectedNetworks, setSelectedNetworks } = useSettings() - const { chains } = useNetwork() + const { chains = [] } = useNetwork() const onClickNetwork = (chainId: number) => { @@ -35,7 +35,7 @@ export const SettingsNetwork = () => { gap="2" > {chains.map((chain) => { - const networkInfo = getNativeTokenInfoByChainId(chain.id) + const networkInfo = getNativeTokenInfoByChainId(chain.id, chains) return ( { + const { chains = [] } = useNetwork() const { fiatCurrency } = useSettings() const coins: Token[] = [] @@ -89,7 +91,7 @@ export const TransactionDetails = ({ const arePricesLoading = (coins.length > 0 && coinPricesIsLoading) || (collectibles.length > 0 && collectiblePricesIsLoading) || isLoadingConversionRate - const nativeTokenInfo = getNativeTokenInfoByChainId(transaction.chainId) + const nativeTokenInfo = getNativeTokenInfoByChainId(transaction.chainId, chains) const date = dayjs(transaction.timestamp).format('ddd MMM DD YYYY, h:m:s a')