fix: update default tokens on unsupported networks (#3470)

* update default tokens on unsupported networks

* update supported network with hook

* update defaults

* fix on default input token
This commit is contained in:
Ian Lapham 2022-03-08 20:35:23 -05:00 committed by GitHub
parent 1aa042c5ef
commit e569dc2152
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -7,6 +7,8 @@ import useNativeCurrency from 'lib/hooks/useNativeCurrency'
import { Field, Swap, swapAtom } from 'lib/state/swap' import { Field, Swap, swapAtom } from 'lib/state/swap'
import { useCallback, useLayoutEffect, useState } from 'react' import { useCallback, useLayoutEffect, useState } from 'react'
import useOnSupportedNetwork from '../useOnSupportedNetwork'
export type DefaultAddress = string | { [chainId: number]: string | 'NATIVE' } | 'NATIVE' export type DefaultAddress = string | { [chainId: number]: string | 'NATIVE' } | 'NATIVE'
export interface TokenDefaults { export interface TokenDefaults {
@ -27,7 +29,11 @@ function useDefaultToken(
address = defaultAddress[chainId] address = defaultAddress[chainId]
} }
const token = useToken(address) const token = useToken(address)
if (chainId && address === 'NATIVE') {
const onSupportedNetwork = useOnSupportedNetwork()
// Only use native currency if chain ID is in supported chains. ExtendedEther will error otherwise.
if (chainId && address === 'NATIVE' && onSupportedNetwork) {
return nativeOnChain(chainId) return nativeOnChain(chainId)
} }
return token ?? undefined return token ?? undefined
@ -41,12 +47,13 @@ export default function useSyncTokenDefaults({
}: TokenDefaults) { }: TokenDefaults) {
const updateSwap = useUpdateAtom(swapAtom) const updateSwap = useUpdateAtom(swapAtom)
const { chainId } = useActiveWeb3React() const { chainId } = useActiveWeb3React()
const onSupportedNetwork = useOnSupportedNetwork()
const nativeCurrency = useNativeCurrency() const nativeCurrency = useNativeCurrency()
const defaultOutputToken = useDefaultToken(defaultOutputTokenAddress, chainId) const defaultOutputToken = useDefaultToken(defaultOutputTokenAddress, chainId)
const defaultInputToken = const defaultInputToken =
useDefaultToken(defaultInputTokenAddress, chainId) ?? useDefaultToken(defaultInputTokenAddress, chainId) ??
// Default the input token to the native currency if it is not the output token. // Default the input token to the native currency if it is not the output token.
(defaultOutputToken === nativeCurrency ? nativeCurrency : undefined) (defaultOutputToken !== nativeCurrency && onSupportedNetwork ? nativeCurrency : undefined)
const setToDefaults = useCallback(() => { const setToDefaults = useCallback(() => {
const defaultSwapState: Swap = { const defaultSwapState: Swap = {