diff --git a/src/constants/tokens.ts b/src/constants/tokens.ts index 31ded79413..d60390fd5c 100644 --- a/src/constants/tokens.ts +++ b/src/constants/tokens.ts @@ -90,7 +90,13 @@ export const MATIC_MAINNET = new Token( 'MATIC', 'Polygon Matic' ) -const MATIC_POLYGON = new Token(ChainId.POLYGON, '0x0000000000000000000000000000000000001010', 18, 'MATIC', 'Matic') +export const MATIC_POLYGON = new Token( + ChainId.POLYGON, + '0x0000000000000000000000000000000000001010', + 18, + 'MATIC', + 'Matic' +) export const DAI_POLYGON = new Token( ChainId.POLYGON, '0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063', @@ -143,13 +149,6 @@ export const WBTC_OPTIMISM = new Token( 'WBTC', 'Wrapped BTC' ) -const MATIC_POLYGON_MUMBAI = new Token( - ChainId.POLYGON_MUMBAI, - '0x0000000000000000000000000000000000001010', - 18, - 'MATIC', - 'Matic' -) export const WETH_POLYGON_MUMBAI = new Token( ChainId.POLYGON_MUMBAI, '0xa6fa4fb5f76172d178d61b04b0ecd319c5d1c0aa', @@ -359,14 +358,21 @@ export function isPolygon(chainId: number): chainId is ChainId.POLYGON | ChainId return chainId === ChainId.POLYGON_MUMBAI || chainId === ChainId.POLYGON } -function getPolygonNativeCurrency(chainId: number) { - switch (chainId) { - case ChainId.POLYGON: - return MATIC_POLYGON - case ChainId.POLYGON_MUMBAI: - return MATIC_POLYGON_MUMBAI - default: - throw new Error('Not polygon') +class PolygonNativeCurrency extends NativeCurrency { + equals(other: Currency): boolean { + return other.isNative && other.chainId === this.chainId + } + + get wrapped(): Token { + if (!isPolygon(this.chainId)) throw new Error('Not Polygon') + const wrapped = WRAPPED_NATIVE_CURRENCY[this.chainId] + invariant(wrapped instanceof Token) + return wrapped + } + + public constructor(chainId: number) { + if (!isPolygon(chainId)) throw new Error('Not Polygon') + super(chainId, 18, 'MATIC', 'Matic') } } @@ -433,7 +439,7 @@ export function nativeOnChain(chainId: number): NativeCurrency | Token { if (cachedNativeCurrency[chainId]) return cachedNativeCurrency[chainId] let nativeCurrency: NativeCurrency | Token if (isPolygon(chainId)) { - nativeCurrency = getPolygonNativeCurrency(chainId) + nativeCurrency = new PolygonNativeCurrency(chainId) } else if (isCelo(chainId)) { nativeCurrency = getCeloNativeCurrency(chainId) } else if (isBsc(chainId)) { diff --git a/src/utils/nativeTokens.ts b/src/utils/nativeTokens.ts index c16bb86c6c..4d9628ba18 100644 --- a/src/utils/nativeTokens.ts +++ b/src/utils/nativeTokens.ts @@ -1,4 +1,4 @@ -import { nativeOnChain } from 'constants/tokens' +import { MATIC_POLYGON, nativeOnChain } from 'constants/tokens' import { Chain } from 'graphql/data/__generated__/types-and-hooks' import { supportedChainIdFromGQLChain } from 'graphql/data/util' @@ -10,8 +10,11 @@ export function getNativeTokenDBAddress(chain: Chain): string | undefined { switch (chain) { // Celo & Polygon have precompiles for their native tokens case Chain.Celo: - case Chain.Polygon: return nativeOnChain(pageChainId).wrapped.address + case Chain.Polygon: + // Like Celo, native MATIC does have a ERC20 precompile, but we use WMATIC in routing/pools + // So instead of returning nativeOnChain().wrapped.address, should directly use the precompile address here + return MATIC_POLYGON.address case Chain.Ethereum: case Chain.Arbitrum: case Chain.EthereumGoerli: