Commit d989c61d authored by Ian Lapham's avatar Ian Lapham Committed by GitHub

fix: update type on wrapped native currency (#3454)

* update wrapped native currency type

* fix build error on base check

* update type on mapping to permit undefined values

* undo unneeded changes

* update filter check
parent 5dd80597
...@@ -157,7 +157,7 @@ export function getPriceOrderingFromPositionForUI(position?: Position): { ...@@ -157,7 +157,7 @@ export function getPriceOrderingFromPositionForUI(position?: Position): {
// if token1 is an ETH-/BTC-stable asset, set it as the base token // if token1 is an ETH-/BTC-stable asset, set it as the base token
const bases = [...Object.values(WRAPPED_NATIVE_CURRENCY), WBTC] const bases = [...Object.values(WRAPPED_NATIVE_CURRENCY), WBTC]
if (bases.some((base) => base.equals(token1))) { if (bases.some((base) => base && base.equals(token1))) {
return { return {
priceLower: position.token0PriceUpper.invert(), priceLower: position.token0PriceUpper.invert(),
priceUpper: position.token0PriceLower.invert(), priceUpper: position.token0PriceLower.invert(),
......
...@@ -44,7 +44,9 @@ type ChainCurrencyList = { ...@@ -44,7 +44,9 @@ type ChainCurrencyList = {
} }
const WRAPPED_NATIVE_CURRENCIES_ONLY: ChainTokenList = Object.fromEntries( const WRAPPED_NATIVE_CURRENCIES_ONLY: ChainTokenList = Object.fromEntries(
Object.entries(WRAPPED_NATIVE_CURRENCY).map(([key, value]) => [key, [value]]) Object.entries(WRAPPED_NATIVE_CURRENCY)
.map(([key, value]) => [key, [value]])
.filter(Boolean)
) )
// used to construct intermediary pairs for trading // used to construct intermediary pairs for trading
...@@ -96,7 +98,7 @@ export const ADDITIONAL_BASES: { [chainId: number]: { [tokenAddress: string]: To ...@@ -96,7 +98,7 @@ export const ADDITIONAL_BASES: { [chainId: number]: { [tokenAddress: string]: To
*/ */
export const CUSTOM_BASES: { [chainId: number]: { [tokenAddress: string]: Token[] } } = { export const CUSTOM_BASES: { [chainId: number]: { [tokenAddress: string]: Token[] } } = {
[SupportedChainId.MAINNET]: { [SupportedChainId.MAINNET]: {
[AMPL.address]: [DAI, WRAPPED_NATIVE_CURRENCY[SupportedChainId.MAINNET]], [AMPL.address]: [DAI, WRAPPED_NATIVE_CURRENCY[SupportedChainId.MAINNET] as Token],
}, },
} }
...@@ -110,29 +112,35 @@ export const COMMON_BASES: ChainCurrencyList = { ...@@ -110,29 +112,35 @@ export const COMMON_BASES: ChainCurrencyList = {
USDC_MAINNET, USDC_MAINNET,
USDT, USDT,
WBTC, WBTC,
WRAPPED_NATIVE_CURRENCY[SupportedChainId.MAINNET], WRAPPED_NATIVE_CURRENCY[SupportedChainId.MAINNET] as Token,
], ],
[SupportedChainId.ROPSTEN]: [ [SupportedChainId.ROPSTEN]: [
nativeOnChain(SupportedChainId.ROPSTEN), nativeOnChain(SupportedChainId.ROPSTEN),
WRAPPED_NATIVE_CURRENCY[SupportedChainId.ROPSTEN], WRAPPED_NATIVE_CURRENCY[SupportedChainId.ROPSTEN] as Token,
], ],
[SupportedChainId.RINKEBY]: [ [SupportedChainId.RINKEBY]: [
nativeOnChain(SupportedChainId.RINKEBY), nativeOnChain(SupportedChainId.RINKEBY),
WRAPPED_NATIVE_CURRENCY[SupportedChainId.RINKEBY], WRAPPED_NATIVE_CURRENCY[SupportedChainId.RINKEBY] as Token,
],
[SupportedChainId.GOERLI]: [
nativeOnChain(SupportedChainId.GOERLI),
WRAPPED_NATIVE_CURRENCY[SupportedChainId.GOERLI] as Token,
],
[SupportedChainId.KOVAN]: [
nativeOnChain(SupportedChainId.KOVAN),
WRAPPED_NATIVE_CURRENCY[SupportedChainId.KOVAN] as Token,
], ],
[SupportedChainId.GOERLI]: [nativeOnChain(SupportedChainId.GOERLI), WRAPPED_NATIVE_CURRENCY[SupportedChainId.GOERLI]],
[SupportedChainId.KOVAN]: [nativeOnChain(SupportedChainId.KOVAN), WRAPPED_NATIVE_CURRENCY[SupportedChainId.KOVAN]],
[SupportedChainId.ARBITRUM_ONE]: [ [SupportedChainId.ARBITRUM_ONE]: [
nativeOnChain(SupportedChainId.ARBITRUM_ONE), nativeOnChain(SupportedChainId.ARBITRUM_ONE),
DAI_ARBITRUM_ONE, DAI_ARBITRUM_ONE,
USDC_ARBITRUM, USDC_ARBITRUM,
USDT_ARBITRUM_ONE, USDT_ARBITRUM_ONE,
WBTC_ARBITRUM_ONE, WBTC_ARBITRUM_ONE,
WRAPPED_NATIVE_CURRENCY[SupportedChainId.ARBITRUM_ONE], WRAPPED_NATIVE_CURRENCY[SupportedChainId.ARBITRUM_ONE] as Token,
], ],
[SupportedChainId.ARBITRUM_RINKEBY]: [ [SupportedChainId.ARBITRUM_RINKEBY]: [
nativeOnChain(SupportedChainId.ARBITRUM_RINKEBY), nativeOnChain(SupportedChainId.ARBITRUM_RINKEBY),
WRAPPED_NATIVE_CURRENCY[SupportedChainId.ARBITRUM_RINKEBY], WRAPPED_NATIVE_CURRENCY[SupportedChainId.ARBITRUM_RINKEBY] as Token,
], ],
[SupportedChainId.OPTIMISM]: [ [SupportedChainId.OPTIMISM]: [
nativeOnChain(SupportedChainId.OPTIMISM), nativeOnChain(SupportedChainId.OPTIMISM),
...@@ -152,7 +160,7 @@ export const COMMON_BASES: ChainCurrencyList = { ...@@ -152,7 +160,7 @@ export const COMMON_BASES: ChainCurrencyList = {
], ],
[SupportedChainId.POLYGON_MUMBAI]: [ [SupportedChainId.POLYGON_MUMBAI]: [
nativeOnChain(SupportedChainId.POLYGON_MUMBAI), nativeOnChain(SupportedChainId.POLYGON_MUMBAI),
WRAPPED_NATIVE_CURRENCY[SupportedChainId.POLYGON_MUMBAI], WRAPPED_NATIVE_CURRENCY[SupportedChainId.POLYGON_MUMBAI] as Token,
WETH_POLYGON_MUMBAI, WETH_POLYGON_MUMBAI,
], ],
} }
......
...@@ -12,6 +12,7 @@ import { ...@@ -12,6 +12,7 @@ import {
USDC_RINKEBY, USDC_RINKEBY,
USDC_ROPSTEN, USDC_ROPSTEN,
} from '@uniswap/smart-order-router' } from '@uniswap/smart-order-router'
import invariant from 'tiny-invariant'
import { UNI_ADDRESS } from './addresses' import { UNI_ADDRESS } from './addresses'
import { SupportedChainId } from './chains' import { SupportedChainId } from './chains'
...@@ -208,8 +209,8 @@ export const UNI: { [chainId: number]: Token } = { ...@@ -208,8 +209,8 @@ export const UNI: { [chainId: number]: Token } = {
[SupportedChainId.KOVAN]: new Token(SupportedChainId.KOVAN, UNI_ADDRESS[42], 18, 'UNI', 'Uniswap'), [SupportedChainId.KOVAN]: new Token(SupportedChainId.KOVAN, UNI_ADDRESS[42], 18, 'UNI', 'Uniswap'),
} }
export const WRAPPED_NATIVE_CURRENCY: { [chainId: number]: Token } = { export const WRAPPED_NATIVE_CURRENCY: { [chainId: number]: Token | undefined } = {
...WETH9, ...(WETH9 as Record<SupportedChainId, Token>),
[SupportedChainId.OPTIMISM]: new Token( [SupportedChainId.OPTIMISM]: new Token(
SupportedChainId.OPTIMISM, SupportedChainId.OPTIMISM,
'0x4200000000000000000000000000000000000006', '0x4200000000000000000000000000000000000006',
...@@ -265,7 +266,9 @@ class MaticNativeCurrency extends NativeCurrency { ...@@ -265,7 +266,9 @@ class MaticNativeCurrency extends NativeCurrency {
get wrapped(): Token { get wrapped(): Token {
if (!isMatic(this.chainId)) throw new Error('Not matic') if (!isMatic(this.chainId)) throw new Error('Not matic')
return WRAPPED_NATIVE_CURRENCY[this.chainId] const wrapped = WRAPPED_NATIVE_CURRENCY[this.chainId]
invariant(wrapped instanceof Token)
return wrapped
} }
public constructor(chainId: number) { public constructor(chainId: number) {
...@@ -276,7 +279,8 @@ class MaticNativeCurrency extends NativeCurrency { ...@@ -276,7 +279,8 @@ class MaticNativeCurrency extends NativeCurrency {
export class ExtendedEther extends Ether { export class ExtendedEther extends Ether {
public get wrapped(): Token { public get wrapped(): Token {
if (this.chainId in WRAPPED_NATIVE_CURRENCY) return WRAPPED_NATIVE_CURRENCY[this.chainId] const wrapped = WRAPPED_NATIVE_CURRENCY[this.chainId]
if (wrapped) return wrapped
throw new Error('Unsupported chain ID') throw new Error('Unsupported chain ID')
} }
......
...@@ -55,11 +55,10 @@ export default function useWrapCallback(): UseWrapCallbackReturns { ...@@ -55,11 +55,10 @@ export default function useWrapCallback(): UseWrapCallbackReturns {
if (!inputCurrency || !outputCurrency || !chainId) { if (!inputCurrency || !outputCurrency || !chainId) {
return WrapType.NOT_APPLICABLE return WrapType.NOT_APPLICABLE
} }
const wrappedNativeCurrency = WRAPPED_NATIVE_CURRENCY[chainId] if (inputCurrency.isNative && WRAPPED_NATIVE_CURRENCY[chainId]?.equals(outputCurrency)) {
if (inputCurrency.isNative && wrappedNativeCurrency.equals(outputCurrency)) {
return WrapType.WRAP return WrapType.WRAP
} }
if (wrappedNativeCurrency.equals(inputCurrency) && outputCurrency.isNative) { if (WRAPPED_NATIVE_CURRENCY[chainId]?.equals(inputCurrency) && outputCurrency.isNative) {
return WrapType.UNWRAP return WrapType.UNWRAP
} }
return WrapType.NOT_APPLICABLE return WrapType.NOT_APPLICABLE
......
...@@ -54,15 +54,18 @@ export default function AddLiquidity({ ...@@ -54,15 +54,18 @@ export default function AddLiquidity({
history, history,
}: RouteComponentProps<{ currencyIdA?: string; currencyIdB?: string }>) { }: RouteComponentProps<{ currencyIdA?: string; currencyIdB?: string }>) {
const { account, chainId, library } = useActiveWeb3React() const { account, chainId, library } = useActiveWeb3React()
const theme = useContext(ThemeContext) const theme = useContext(ThemeContext)
const currencyA = useCurrency(currencyIdA) const currencyA = useCurrency(currencyIdA)
const currencyB = useCurrency(currencyIdB) const currencyB = useCurrency(currencyIdB)
const wrappedNativeCurrency = chainId ? WRAPPED_NATIVE_CURRENCY[chainId] : undefined
const oneCurrencyIsWETH = Boolean( const oneCurrencyIsWETH = Boolean(
chainId && chainId &&
((currencyA && currencyA.equals(WRAPPED_NATIVE_CURRENCY[chainId])) || wrappedNativeCurrency &&
(currencyB && currencyB.equals(WRAPPED_NATIVE_CURRENCY[chainId]))) ((currencyA && currencyA.equals(wrappedNativeCurrency)) || (currencyB && currencyB.equals(wrappedNativeCurrency)))
) )
const toggleWalletModal = useWalletModalToggle() // toggle wallet when disconnected const toggleWalletModal = useWalletModalToggle() // toggle wallet when disconnected
......
...@@ -592,10 +592,10 @@ function V2PairMigration({ ...@@ -592,10 +592,10 @@ function V2PairMigration({
<ThemedText.Black fontSize={12}> <ThemedText.Black fontSize={12}>
<Trans> <Trans>
At least {formatCurrencyAmount(refund0, 4)}{' '} At least {formatCurrencyAmount(refund0, 4)}{' '}
{token0.equals(WRAPPED_NATIVE_CURRENCY[chainId]) ? 'ETH' : token0.symbol} and{' '} {chainId && WRAPPED_NATIVE_CURRENCY[chainId]?.equals(token0) ? 'ETH' : token0.symbol} and{' '}
{formatCurrencyAmount(refund1, 4)}{' '} {formatCurrencyAmount(refund1, 4)}{' '}
{token1.equals(WRAPPED_NATIVE_CURRENCY[chainId]) ? 'ETH' : token1.symbol} will be refunded to your {chainId && WRAPPED_NATIVE_CURRENCY[chainId]?.equals(token1) ? 'ETH' : token1.symbol} will be
wallet due to selected price range. refunded to your wallet due to selected price range.
</Trans> </Trans>
</ThemedText.Black> </ThemedText.Black>
) : null} ) : null}
......
...@@ -266,8 +266,8 @@ function Remove({ tokenId }: { tokenId: BigNumber }) { ...@@ -266,8 +266,8 @@ function Remove({ tokenId }: { tokenId: BigNumber }) {
liquidityValue1?.currency && liquidityValue1?.currency &&
(liquidityValue0.currency.isNative || (liquidityValue0.currency.isNative ||
liquidityValue1.currency.isNative || liquidityValue1.currency.isNative ||
liquidityValue0.currency.wrapped.equals(WRAPPED_NATIVE_CURRENCY[liquidityValue0.currency.chainId]) || WRAPPED_NATIVE_CURRENCY[liquidityValue0.currency.chainId]?.equals(liquidityValue0.currency.wrapped) ||
liquidityValue1.currency.wrapped.equals(WRAPPED_NATIVE_CURRENCY[liquidityValue1.currency.chainId])) WRAPPED_NATIVE_CURRENCY[liquidityValue1.currency.chainId]?.equals(liquidityValue1.currency.wrapped))
) )
return ( return (
<AutoColumn> <AutoColumn>
......
...@@ -386,10 +386,12 @@ export default function RemoveLiquidity({ ...@@ -386,10 +386,12 @@ export default function RemoveLiquidity({
) )
const oneCurrencyIsETH = currencyA?.isNative || currencyB?.isNative const oneCurrencyIsETH = currencyA?.isNative || currencyB?.isNative
const oneCurrencyIsWETH = Boolean( const oneCurrencyIsWETH = Boolean(
chainId && chainId &&
WRAPPED_NATIVE_CURRENCY[chainId] && WRAPPED_NATIVE_CURRENCY[chainId] &&
(currencyA?.equals(WRAPPED_NATIVE_CURRENCY[chainId]) || currencyB?.equals(WRAPPED_NATIVE_CURRENCY[chainId])) ((currencyA && WRAPPED_NATIVE_CURRENCY[chainId]?.equals(currencyA)) ||
(currencyB && WRAPPED_NATIVE_CURRENCY[chainId]?.equals(currencyB)))
) )
const handleSelectCurrencyA = useCallback( const handleSelectCurrencyA = useCallback(
...@@ -533,16 +535,24 @@ export default function RemoveLiquidity({ ...@@ -533,16 +535,24 @@ export default function RemoveLiquidity({
{oneCurrencyIsETH ? ( {oneCurrencyIsETH ? (
<StyledInternalLink <StyledInternalLink
to={`/remove/v2/${ to={`/remove/v2/${
currencyA?.isNative ? WRAPPED_NATIVE_CURRENCY[chainId].address : currencyIdA currencyA?.isNative && chainId && WRAPPED_NATIVE_CURRENCY[chainId]
}/${currencyB?.isNative ? WRAPPED_NATIVE_CURRENCY[chainId].address : currencyIdB}`} ? WRAPPED_NATIVE_CURRENCY[chainId]?.address
: currencyIdA
}/${
currencyB?.isNative && chainId && WRAPPED_NATIVE_CURRENCY[chainId]
? WRAPPED_NATIVE_CURRENCY[chainId]?.address
: currencyIdB
}`}
> >
Receive WETH Receive WETH
</StyledInternalLink> </StyledInternalLink>
) : oneCurrencyIsWETH ? ( ) : oneCurrencyIsWETH ? (
<StyledInternalLink <StyledInternalLink
to={`/remove/v2/${ to={`/remove/v2/${
currencyA?.equals(WRAPPED_NATIVE_CURRENCY[chainId]) ? 'ETH' : currencyIdA currencyA && WRAPPED_NATIVE_CURRENCY[chainId]?.equals(currencyA) ? 'ETH' : currencyIdA
}/${currencyB?.equals(WRAPPED_NATIVE_CURRENCY[chainId]) ? 'ETH' : currencyIdB}`} }/${
currencyB && WRAPPED_NATIVE_CURRENCY[chainId]?.equals(currencyB) ? 'ETH' : currencyIdB
}`}
> >
Receive ETH Receive ETH
</StyledInternalLink> </StyledInternalLink>
......
...@@ -3,6 +3,7 @@ import { Trans } from '@lingui/macro' ...@@ -3,6 +3,7 @@ import { Trans } from '@lingui/macro'
import StakingRewardsJson from '@uniswap/liquidity-staker/build/StakingRewards.json' import StakingRewardsJson from '@uniswap/liquidity-staker/build/StakingRewards.json'
import { CurrencyAmount, Token } from '@uniswap/sdk-core' import { CurrencyAmount, Token } from '@uniswap/sdk-core'
import { Pair } from '@uniswap/v2-sdk' import { Pair } from '@uniswap/v2-sdk'
import { SupportedChainId } from 'constants/chains'
import useActiveWeb3React from 'hooks/useActiveWeb3React' import useActiveWeb3React from 'hooks/useActiveWeb3React'
import useCurrentBlockTimestamp from 'hooks/useCurrentBlockTimestamp' import useCurrentBlockTimestamp from 'hooks/useCurrentBlockTimestamp'
import JSBI from 'jsbi' import JSBI from 'jsbi'
...@@ -27,19 +28,19 @@ export const STAKING_REWARDS_INFO: { ...@@ -27,19 +28,19 @@ export const STAKING_REWARDS_INFO: {
} = { } = {
1: [ 1: [
{ {
tokens: [WRAPPED_NATIVE_CURRENCY[1], DAI], tokens: [WRAPPED_NATIVE_CURRENCY[SupportedChainId.MAINNET] as Token, DAI],
stakingRewardAddress: '0xa1484C3aa22a66C62b77E0AE78E15258bd0cB711', stakingRewardAddress: '0xa1484C3aa22a66C62b77E0AE78E15258bd0cB711',
}, },
{ {
tokens: [WRAPPED_NATIVE_CURRENCY[1], USDC_MAINNET], tokens: [WRAPPED_NATIVE_CURRENCY[SupportedChainId.MAINNET] as Token, USDC_MAINNET],
stakingRewardAddress: '0x7FBa4B8Dc5E7616e59622806932DBea72537A56b', stakingRewardAddress: '0x7FBa4B8Dc5E7616e59622806932DBea72537A56b',
}, },
{ {
tokens: [WRAPPED_NATIVE_CURRENCY[1], USDT], tokens: [WRAPPED_NATIVE_CURRENCY[SupportedChainId.MAINNET] as Token, USDT],
stakingRewardAddress: '0x6C3e4cb2E96B01F4b866965A91ed4437839A121a', stakingRewardAddress: '0x6C3e4cb2E96B01F4b866965A91ed4437839A121a',
}, },
{ {
tokens: [WRAPPED_NATIVE_CURRENCY[1], WBTC], tokens: [WRAPPED_NATIVE_CURRENCY[SupportedChainId.MAINNET] as Token, WBTC],
stakingRewardAddress: '0xCA35e32e7926b96A9988f61d510E038108d8068e', stakingRewardAddress: '0xCA35e32e7926b96A9988f61d510E038108d8068e',
}, },
], ],
......
...@@ -6,7 +6,7 @@ import { supportedChainId } from './supportedChainId' ...@@ -6,7 +6,7 @@ import { supportedChainId } from './supportedChainId'
export function unwrappedToken(currency: Currency): Currency { export function unwrappedToken(currency: Currency): Currency {
if (currency.isNative) return currency if (currency.isNative) return currency
const formattedChainId = supportedChainId(currency.chainId) const formattedChainId = supportedChainId(currency.chainId)
if (formattedChainId && currency.equals(WRAPPED_NATIVE_CURRENCY[formattedChainId])) if (formattedChainId && WRAPPED_NATIVE_CURRENCY[formattedChainId]?.equals(currency))
return nativeOnChain(currency.chainId) return nativeOnChain(currency.chainId)
return currency return currency
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment