Skip to content

Commit

Permalink
Merge branch 'master' of github.com:OffchainLabs/arbitrum-token-bridg…
Browse files Browse the repository at this point in the history
…e into token-query-params
  • Loading branch information
brtkx committed Jan 21, 2025
2 parents 78b0edc + ec72890 commit 1dcc1e2
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 111 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { create } from 'zustand'
import { isAddress } from 'ethers/lib/utils.js'
import { isAddress } from 'ethers/lib/utils'
import { Address, useAccount } from 'wagmi'
import { useCallback, useEffect } from 'react'
import { MagnifyingGlassIcon } from '@heroicons/react/24/outline'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
isTokenArbitrumOneNativeUSDC,
isTokenArbitrumSepoliaNativeUSDC,
isTokenArbitrumOneUSDCe,
getL2ERC20Address,
isTokenNativeUSDC
} from '../../util/TokenUtils'
import { Button } from '../common/Button'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ArrowsUpDownIcon, ArrowDownIcon } from '@heroicons/react/24/outline'
import { twMerge } from 'tailwind-merge'
import { utils } from 'ethers'
import { Chain, useAccount } from 'wagmi'
import { isAddress } from 'ethers/lib/utils'

import { getExplorerUrl } from '../../util/networks'
import { ExternalLink } from '../common/ExternalLink'
Expand All @@ -14,7 +15,7 @@ import {
isTokenSepoliaUSDC,
isTokenMainnetUSDC
} from '../../util/TokenUtils'
import { useUpdateUSDCBalances } from '../../hooks/CCTP/useUpdateUSDCBalances'
import { useUpdateUsdcBalances } from '../../hooks/CCTP/useUpdateUsdcBalances'
import { useNativeCurrency } from '../../hooks/useNativeCurrency'
import { useNetworks } from '../../hooks/useNetworks'
import { useNetworksRelationship } from '../../hooks/useNetworksRelationship'
Expand Down Expand Up @@ -199,8 +200,12 @@ export function TransferPanelMain() {

const { updateErc20ParentBalances, updateErc20ChildBalances } = useBalances()

const { updateUSDCBalances } = useUpdateUSDCBalances({
walletAddress: destinationAddressOrWalletAddress
const { updateUsdcBalances } = useUpdateUsdcBalances({
walletAddress:
destinationAddressOrWalletAddress &&
isAddress(destinationAddressOrWalletAddress)
? destinationAddressOrWalletAddress
: undefined
})

useEffect(() => {
Expand All @@ -225,7 +230,7 @@ export function TransferPanelMain() {
isTokenArbitrumOneNativeUSDC(selectedToken.address) ||
isTokenArbitrumSepoliaNativeUSDC(selectedToken.address))
) {
updateUSDCBalances()
updateUsdcBalances()
return
}

Expand All @@ -238,7 +243,7 @@ export function TransferPanelMain() {
updateErc20ParentBalances,
updateErc20ChildBalances,
destinationAddressOrWalletAddress,
updateUSDCBalances,
updateUsdcBalances,
isTeleportMode
])

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useMemo } from 'react'
import useSWRImmutable from 'swr/immutable'
import { isAddress } from 'ethers/lib/utils.js'
import { isAddress } from 'ethers/lib/utils'

import { DestinationAddressErrors } from '../AdvancedSettings'
import { addressIsDenylisted } from '../../../util/AddressUtils'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState } from 'react'
import { isAddress } from 'ethers/lib/utils.js'
import { isAddress } from 'ethers/lib/utils'
import { Popover } from '@headlessui/react'
import { registerCustomArbitrumNetwork } from '@arbitrum/sdk'
import { StaticJsonRpcProvider } from '@ethersproject/providers'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { useInterval, useLatest } from 'react-use'
import { useAccount } from 'wagmi'

import { useAppState } from '../../state'
import { useUpdateUSDCBalances } from '../../hooks/CCTP/useUpdateUSDCBalances'
import { useSelectedToken } from '../../hooks/useSelectedToken'
import { useUpdateUsdcBalances } from '../../hooks/CCTP/useUpdateUsdcBalances'
import { isTokenNativeUSDC } from '../../util/TokenUtils'

// Updates all balances periodically
export function useBalanceUpdater() {
Expand All @@ -14,14 +15,17 @@ export function useBalanceUpdater() {
const { address: walletAddress } = useAccount()
const latestTokenBridge = useLatest(arbTokenBridge)

const { updateUSDCBalances } = useUpdateUSDCBalances({
const { updateUsdcBalances } = useUpdateUsdcBalances({
walletAddress
})

useInterval(() => {
updateUSDCBalances()

if (selectedToken) {
if (isTokenNativeUSDC(selectedToken.address)) {
updateUsdcBalances()
return
}

latestTokenBridge?.current?.token?.updateTokenData(selectedToken.address)
}
}, 10000)
Expand Down

This file was deleted.

127 changes: 127 additions & 0 deletions packages/arb-token-bridge-ui/src/hooks/CCTP/useUpdateUsdcBalances.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { useCallback } from 'react'
import { Address } from 'wagmi'
import useSWRImmutable from 'swr/immutable'

import { CommonAddress } from '../../util/CommonAddressUtils'
import { getL2ERC20Address } from '../../util/TokenUtils'
import { useNetworks } from '../useNetworks'
import { useNetworksRelationship } from '../useNetworksRelationship'
import { isNetwork } from '../../util/networks'
import { useBalances } from '../useBalances'
import { getProviderForChainId } from '@/token-bridge-sdk/utils'

export async function getChildUsdcAddress({
parentChainId,
childChainId
}: {
parentChainId: number
childChainId: number
}) {
const {
isEthereumMainnet: isParentEthereumMainnet,
isSepolia: isParentSepolia
} = isNetwork(parentChainId)

if (isParentEthereumMainnet) {
return CommonAddress.ArbitrumOne.USDC
}

if (isParentSepolia) {
return CommonAddress.ArbitrumSepolia.USDC
}

const parentUsdcAddress = getParentUsdcAddress(parentChainId)
const parentProvider = getProviderForChainId(parentChainId)
const childProvider = getProviderForChainId(childChainId)

if (!parentUsdcAddress) {
return
}

return getL2ERC20Address({
erc20L1Address: parentUsdcAddress,
l1Provider: parentProvider,
l2Provider: childProvider
})
}

export function getParentUsdcAddress(parentChainId: number) {
const {
isEthereumMainnet: isParentEthereumMainnet,
isSepolia: isParentSepolia,
isArbitrumOne: isParentArbitrumOne,
isArbitrumSepolia: isParentArbitrumSepolia
} = isNetwork(parentChainId)

if (isParentEthereumMainnet) {
return CommonAddress.Ethereum.USDC
}

if (isParentSepolia) {
return CommonAddress.Sepolia.USDC
}

if (isParentArbitrumOne) {
return CommonAddress.ArbitrumOne.USDC
}

if (isParentArbitrumSepolia) {
return CommonAddress.ArbitrumSepolia.USDC
}
}

export function useUpdateUsdcBalances({
walletAddress
}: {
walletAddress: Address | undefined
}) {
const [networks] = useNetworks()
const { parentChain, childChain } = useNetworksRelationship(networks)

const {
updateErc20ParentBalances: updateErc20ParentBalance,
updateErc20ChildBalances: updateErc20ChildBalance
} = useBalances({
parentWalletAddress: walletAddress,
childWalletAddress: walletAddress
})

// we don't have native USDC addresses for Orbit chains, we need to fetch it
const { data: childUsdcAddress, isLoading } = useSWRImmutable(
[parentChain.id, childChain.id, 'getChildUsdcAddress'],
([parentChainId, childChainId]) =>
getChildUsdcAddress({
parentChainId,
childChainId
})
)

const updateUsdcBalances = useCallback(() => {
const parentUsdcAddress = getParentUsdcAddress(parentChain.id)

// USDC is not native for the selected networks, do nothing
if (!parentUsdcAddress) {
return
}

if (isLoading) {
return
}

updateErc20ParentBalance([parentUsdcAddress.toLowerCase()])

if (childUsdcAddress) {
updateErc20ChildBalance([childUsdcAddress.toLowerCase()])
}
}, [
isLoading,
childUsdcAddress,
parentChain.id,
updateErc20ChildBalance,
updateErc20ParentBalance
])

return {
updateUsdcBalances
}
}
Loading

0 comments on commit 1dcc1e2

Please sign in to comment.