Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use correct source address for usdc on Orbit #2324

Merged
merged 9 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Image from 'next/image'
import { useAccount } from 'wagmi'
import { AutoSizer, List, ListRowProps } from 'react-virtualized'
import { twMerge } from 'tailwind-merge'
import useSWRImmutable from 'swr/immutable'

import { useAppState } from '../../state'
import {
Expand Down Expand Up @@ -34,10 +35,11 @@ import { TokenRow } from './TokenRow'
import { useNetworks } from '../../hooks/useNetworks'
import { useNetworksRelationship } from '../../hooks/useNetworksRelationship'
import { Switch } from '../common/atoms/Switch'
import { useSelectedToken } from '../../hooks/useSelectedToken'
import { getUsdcToken, useSelectedToken } from '../../hooks/useSelectedToken'
import { useBalances } from '../../hooks/useBalances'
import { useSetInputAmount } from '../../hooks/TransferPanel/useSetInputAmount'
import { addressesEqual } from '../../util/AddressUtils'
import { getProviderForChainId } from '@/token-bridge-sdk/utils'

export const ARB_ONE_NATIVE_USDC_TOKEN = {
...ArbOneNativeUSDC,
Expand Down Expand Up @@ -187,6 +189,8 @@ function TokensPanel({
const nativeCurrency = useNativeCurrency({ provider: childChainProvider })

const {
isEthereumMainnet: isParentChainEthereumMainnet,
isSepolia: isParentChainSepolia,
isArbitrumOne: isParentChainArbitrumOne,
isArbitrumSepolia: isParentChainArbitrumSepolia
} = isNetwork(parentChain.id)
Expand Down Expand Up @@ -241,6 +245,43 @@ function TokensPanel({
]
)

const usdcParentAddress = useMemo(() => {
if (isParentChainEthereumMainnet) {
return CommonAddress.Ethereum.USDC
}
if (isParentChainSepolia) {
return CommonAddress.Sepolia.USDC
}
if (isParentChainArbitrumOne) {
return CommonAddress.ArbitrumOne.USDC
}
if (isParentChainArbitrumSepolia) {
return CommonAddress.ArbitrumSepolia.USDC
}
}, [
isParentChainEthereumMainnet,
isParentChainSepolia,
isParentChainArbitrumOne,
isParentChainArbitrumSepolia
])

const { data: usdcToken = null } = useSWRImmutable(
usdcParentAddress
? ([
usdcParentAddress,
parentChain.id,
childChain.id,
'token_search_usdc_token'
] as const)
: null,
([_usdcParentAddress, _parentChainId, _childChainId]) =>
getUsdcToken({
tokenAddress: _usdcParentAddress,
parentProvider: getProviderForChainId(_parentChainId),
childProvider: getProviderForChainId(_childChainId)
})
)

const tokensToShow = useMemo(() => {
const tokenSearch = newToken.trim().toLowerCase()
const tokenAddresses = [
Expand Down Expand Up @@ -430,10 +471,11 @@ function TokensPanel({
const address = tokensToShow[virtualizedProps.index]
let token: ERC20BridgeToken | null = null

if (isTokenArbitrumOneNativeUSDC(address)) {
token = ARB_ONE_NATIVE_USDC_TOKEN
} else if (isTokenArbitrumSepoliaNativeUSDC(address)) {
token = ARB_SEPOLIA_NATIVE_USDC_TOKEN
if (
isTokenArbitrumOneNativeUSDC(address) ||
isTokenArbitrumSepoliaNativeUSDC(address)
) {
token = usdcToken
} else if (address) {
token = tokensFromLists[address] || tokensFromUser[address] || null
}
Expand All @@ -457,7 +499,7 @@ function TokensPanel({
/>
)
},
[tokensToShow, tokensFromLists, tokensFromUser, onTokenSelected]
[tokensToShow, tokensFromLists, tokensFromUser, onTokenSelected, usdcToken]
)

const AddButton = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
isTokenArbitrumOneNativeUSDC,
isTokenArbitrumSepoliaNativeUSDC
} from '../util/TokenUtils'
import { isNetwork } from '../util/networks'

/**
* Balance of the child chain's native currency or ERC20 token
Expand All @@ -20,6 +21,9 @@ export function useBalanceOnSourceChain(
const { address: walletAddress } = useAccount()
const [networks] = useNetworks()
const { isDepositMode } = useNetworksRelationship(networks)
const { isOrbitChain: isSourceOrbitChain } = isNetwork(
networks.sourceChain.id
)

const {
erc20: [erc20SourceChainBalances]
Expand Down Expand Up @@ -47,7 +51,10 @@ export function useBalanceOnSourceChain(
isTokenArbitrumOneNativeUSDC(tokenAddressLowercased) ||
isTokenArbitrumSepoliaNativeUSDC(tokenAddressLowercased)
) {
return erc20SourceChainBalances[tokenAddressLowercased] ?? constants.Zero
// because we read parent chain address, make sure we don't read Orbit chain's address if it's the source chain
if (!isSourceOrbitChain) {
return erc20SourceChainBalances[tokenAddressLowercased] ?? constants.Zero
}
}

const tokenChildChainAddress = token.l2Address?.toLowerCase()
Expand Down
2 changes: 1 addition & 1 deletion packages/arb-token-bridge-ui/src/hooks/useSelectedToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function sanitizeTokenAddress(tokenAddress: string | null): string | undefined {
return undefined
}

async function getUsdcToken({
export async function getUsdcToken({
tokenAddress,
parentProvider,
childProvider
Expand Down
Loading