-
Notifications
You must be signed in to change notification settings - Fork 209
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
refactor: use useSWR in useUpdateUsdcBalances #2164
base: master
Are you sure you want to change the base?
Changes from 7 commits
9e7a0e9
8f68523
ac1c048
e30fc51
aade96d
cba873c
11bdc75
2aee6c9
8df7b96
8affdca
4fc016e
01d4679
05a144e
4f3c843
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,135 @@ | ||||||
import { useCallback, useMemo } from 'react' | ||||||
import { isAddress } from 'ethers/lib/utils.js' | ||||||
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 childChainUsdcAddressFetcher([ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we do something like this, also renaming the method? I see that it's easier to pass directly from SWR with an array, but it's just harder to read when calling the method. getChildUsdcAddress({
parentUsdcAddress,
parentChainId,
childChainId
)} nit: I don't think we need to usd |
||||||
_parentChainUsdcAddress, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we can do something about Maybe we don't need |
||||||
parentChainId, | ||||||
childChainId | ||||||
]: [Address, number, number]) { | ||||||
const { | ||||||
isEthereumMainnet: isParentEthereumMainnet, | ||||||
isSepolia: isParentSepolia | ||||||
} = isNetwork(parentChainId) | ||||||
|
||||||
if (isParentEthereumMainnet) { | ||||||
return CommonAddress.ArbitrumOne.USDC | ||||||
} | ||||||
|
||||||
if (isParentSepolia) { | ||||||
return CommonAddress.ArbitrumSepolia.USDC | ||||||
} | ||||||
|
||||||
const _parentChainProvider = getProviderForChainId(parentChainId) | ||||||
const _childChainProvider = getProviderForChainId(childChainId) | ||||||
|
||||||
return getL2ERC20Address({ | ||||||
erc20L1Address: _parentChainUsdcAddress, | ||||||
l1Provider: _parentChainProvider, | ||||||
l2Provider: _childChainProvider | ||||||
}) | ||||||
} | ||||||
|
||||||
export function useParentChainUsdcAddress() { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IF you decide to go with variable names without 'chain', can rename this to |
||||||
const [networks] = useNetworks() | ||||||
const { parentChain } = useNetworksRelationship(networks) | ||||||
|
||||||
return useMemo(() => { | ||||||
fionnachan marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
const { | ||||||
isEthereumMainnet: isParentEthereumMainnet, | ||||||
isSepolia: isParentSepolia, | ||||||
isArbitrumOne: isParentArbitrumOne, | ||||||
isArbitrumSepolia: isParentArbitrumSepolia | ||||||
} = isNetwork(parentChain.id) | ||||||
|
||||||
if (isParentEthereumMainnet) { | ||||||
return CommonAddress.Ethereum.USDC | ||||||
} | ||||||
|
||||||
if (isParentSepolia) { | ||||||
return CommonAddress.Sepolia.USDC | ||||||
} | ||||||
|
||||||
if (isParentArbitrumOne) { | ||||||
return CommonAddress.ArbitrumOne.USDC | ||||||
} | ||||||
|
||||||
if (isParentArbitrumSepolia) { | ||||||
return CommonAddress.ArbitrumSepolia.USDC | ||||||
} | ||||||
}, [parentChain.id]) | ||||||
} | ||||||
|
||||||
export function useUpdateUsdcBalances({ | ||||||
walletAddress | ||||||
}: { | ||||||
walletAddress: string | undefined | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit
Suggested change
|
||||||
}) { | ||||||
const [networks] = useNetworks() | ||||||
const { parentChain, childChain } = useNetworksRelationship(networks) | ||||||
|
||||||
const _walletAddress: Address | undefined = | ||||||
walletAddress && isAddress(walletAddress) ? walletAddress : undefined | ||||||
fionnachan marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
const { | ||||||
updateErc20ParentBalances: updateErc20ParentBalance, | ||||||
updateErc20ChildBalances: updateErc20ChildBalance | ||||||
} = useBalances({ | ||||||
parentWalletAddress: _walletAddress, | ||||||
childWalletAddress: _walletAddress | ||||||
}) | ||||||
|
||||||
const parentChainUsdcAddress = useParentChainUsdcAddress() | ||||||
|
||||||
// we don't have native USDC addresses for Orbit chains, we need to fetch it | ||||||
const { | ||||||
data: childChainUsdcAddress, | ||||||
error, // can be unbridged to Orbit chain so no address to be found | ||||||
fionnachan marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
isLoading | ||||||
} = useSWRImmutable( | ||||||
typeof parentChainUsdcAddress !== 'undefined' | ||||||
? [ | ||||||
parentChainUsdcAddress, | ||||||
parentChain.id, | ||||||
childChain.id, | ||||||
'fetchChildChainUsdcAddress' | ||||||
] | ||||||
: null, | ||||||
childChainUsdcAddressFetcher | ||||||
) | ||||||
|
||||||
const updateUsdcBalances = useCallback(() => { | ||||||
// USDC is not native for the selected networks, do nothing | ||||||
if (!parentChainUsdcAddress) { | ||||||
return | ||||||
} | ||||||
|
||||||
if (isLoading) { | ||||||
return | ||||||
} | ||||||
|
||||||
updateErc20ParentBalance([parentChainUsdcAddress.toLowerCase()]) | ||||||
|
||||||
if (childChainUsdcAddress) { | ||||||
updateErc20ChildBalance([childChainUsdcAddress.toLowerCase()]) | ||||||
} | ||||||
}, [ | ||||||
isLoading, | ||||||
childChainUsdcAddress, | ||||||
parentChainUsdcAddress, | ||||||
updateErc20ChildBalance, | ||||||
updateErc20ParentBalance | ||||||
]) | ||||||
|
||||||
return { | ||||||
updateUsdcBalances | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just thinking here, can we maybe call
updateUSDCBalances
inupdateTokenData
directly?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
supposedly, i think it requires some exploration and so i haven't created any tickets for it or dealt with it on this PR as it's out of scope