-
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
Open
fionnachan
wants to merge
11
commits into
master
Choose a base branch
from
refactor-useUpdateUsdcBalances
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9e7a0e9
refactor: make balance updater a hook
fionnachan 8f68523
refactor: add useSWR to grab token address in useUpdateUsdcBalances
fionnachan ac1c048
Merge remote-tracking branch 'origin/master' into refactor-useUpdateU…
fionnachan e30fc51
change name
fionnachan aade96d
change name
fionnachan cba873c
only update usdc when selected
fionnachan 11bdc75
add tests
fionnachan 2aee6c9
rename vars
fionnachan 8df7b96
address review comments
fionnachan 8affdca
remove isAddress .js from import path
fionnachan 4fc016e
remove useless lines
fionnachan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...ges/arb-token-bridge-ui/src/components/TransactionHistory/TransactionHistorySearchBar.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ages/arb-token-bridge-ui/src/components/TransferPanel/hooks/useDestinationAddressError.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/arb-token-bridge-ui/src/components/common/AddCustomChain.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 0 additions & 88 deletions
88
packages/arb-token-bridge-ui/src/hooks/CCTP/useUpdateUSDCBalances.ts
This file was deleted.
Oops, something went wrong.
127 changes: 127 additions & 0 deletions
127
packages/arb-token-bridge-ui/src/hooks/CCTP/useUpdateUsdcBalances.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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