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

refactor: use useSWR in useUpdateUsdcBalances #2164

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

fionnachan
Copy link
Member

@fionnachan fionnachan commented Dec 24, 2024

We don't need to fetch the USDC address on the child chain every time we want to update the USDC balances because it will never change

Closes FS-1082

@cla-bot cla-bot bot added the cla-signed label Dec 24, 2024
Copy link

vercel bot commented Dec 24, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
arbitrum-token-bridge ✅ Ready (Inspect) Visit Preview Jan 8, 2025 2:38pm

Base automatically changed from move-balance-updater-syncer-to-hook to master December 27, 2024 13:33
@fionnachan fionnachan marked this pull request as ready for review January 3, 2025 17:38
import { useBalances } from '../useBalances'
import { getProviderForChainId } from '@/token-bridge-sdk/utils'

export async function childChainUsdcAddressFetcher([
Copy link
Contributor

Choose a reason for hiding this comment

The 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 chain in variable names, it's implied. We've been using it without chain on the bridge already and everywhere in the sdk. Might wanna follow the same structure.

})
}

export function useParentChainUsdcAddress() {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 useParentUsdcAddress. Otherwise keep consistent.

export function useUpdateUsdcBalances({
walletAddress
}: {
walletAddress: string | undefined
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit

Suggested change
walletAddress: string | undefined
walletAddress: Address | undefined

getL2ERC20Address: jest.fn()
}))

const xaiTestnetChainId = 37714555429
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you could cast all of these as ChainId here and won't have to do it every time later

])

expect(result).toEqual(CommonAddress.ArbitrumOne.USDC)
expect(result).not.toEqual(CommonAddress.ArbitrumOne['USDC.e'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we don't need to check for USDC.e, if USDC passed then USDC.e must fail (unless we change ArbitrumOne['USDC.e'] to the same value)

import { getProviderForChainId } from '@/token-bridge-sdk/utils'

export async function childChainUsdcAddressFetcher([
_parentChainUsdcAddress,
Copy link
Contributor

@brtkx brtkx Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can do something about _parentChainUsdcAddress, it should not be required for non-Orbit chains, so we should be able to just call the method without specifying parent USDC address and rely on chain ID only.

Maybe we don't need useParentChainUsdcAddress, we could just make it a util method and use it here as well?

const [networks] = useNetworks()
const { parentChain } = useNetworksRelationship(networks)

return useMemo(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a string, We don't need useMemo here

// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We never return this error

updateUsdcBalances()
return
}

latestTokenBridge?.current?.token?.updateTokenData(selectedToken.address)
Copy link
Contributor

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 in updateTokenData directly?

Copy link
Member Author

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

Comment on lines 80 to 81
const _walletAddress: Address | undefined =
walletAddress && isAddress(walletAddress) ? walletAddress : undefined
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: I don't think we need to type it here?

Also, can we reuse walletAddress directly? Can we have invalid address here?

Comment on lines 17 to 19
jest.mock('../useNetworks', () => ({
useNetworks: jest.fn()
}))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we need to mock useNetworks maybe it's a sign we can pass networks as a parameter to useUpdateUsdcBalances?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d rather just mock it in the tests and call useNetworks in useUpdateUsdcBalances

I don’t think we should design the function to facilitate test writing

Comment on lines 33 to 55
const xaiTestnet = orbitTestnets[xaiTestnetChainId]

if (!xaiTestnet) {
throw new Error(`Could not find Xai Testnet in the Orbit chains list.`)
}

registerCustomArbitrumNetwork(xaiTestnet)

const polterTestnet = orbitTestnets[polterTestnetChainId]

if (!polterTestnet) {
throw new Error(`Could not find Polter Testnet in the Orbit chains list.`)
}

registerCustomArbitrumNetwork(polterTestnet)

const geistMainnet = orbitMainnets[geistMainnetChainId]

if (!geistMainnet) {
throw new Error(`Could not find Geist Mainnet in the Orbit chains list.`)
}

registerCustomArbitrumNetwork(geistMainnet)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can create an helper for that? We have the same pattern in isDepositMode and networks test

chrstph-dvx
chrstph-dvx previously approved these changes Jan 7, 2025
@@ -4,6 +4,7 @@ import { twMerge } from 'tailwind-merge'
import { utils } from 'ethers'
import { Chain, useAccount } from 'wagmi'
import { useMedia } from 'react-use'
import { isAddress } from 'ethers/lib/utils.js'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { isAddress } from 'ethers/lib/utils.js'
import { isAddress } from 'ethers/lib/utils

chrstph-dvx
chrstph-dvx previously approved these changes Jan 7, 2025
brtkx
brtkx previously approved these changes Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants