-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate getting token balance for non-connected chain (#106)
- Loading branch information
Showing
11 changed files
with
79 additions
and
46 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './useTokenBalance'; | ||
export * from './useTokenBalanceForChain'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { assertAddressType } from '@snx-v3/assertAddressType'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { Network, useProviderForChain, useWallet } from '@snx-v3/useBlockchain'; | ||
import { Contract } from 'ethers'; | ||
|
||
import { abi, BalanceSchema } from './useTokenBalance'; | ||
|
||
export const useTokenBalanceForChain = (address?: string, network?: Network) => { | ||
const wallet = useWallet(); | ||
const provider = useProviderForChain(network); | ||
const tokenAddress = assertAddressType(address) ? address : undefined; | ||
return useQuery({ | ||
queryKey: [ | ||
`${network?.id}-${network?.preset}`, | ||
'TokenBalance', | ||
{ accountAddress: wallet?.address }, | ||
{ tokenAddress }, | ||
], | ||
queryFn: async () => { | ||
if (wallet?.address && tokenAddress && provider) { | ||
const contract = new Contract(tokenAddress, abi, provider); | ||
return BalanceSchema.parse(await contract.balanceOf(wallet?.address)); | ||
} | ||
}, | ||
enabled: Boolean(wallet?.address && tokenAddress && provider), | ||
refetchInterval: 5000, | ||
}); | ||
}; |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './useUSDProxy'; | ||
export * from './useUSDProxyForChain'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Contract } from '@ethersproject/contracts'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { Network, useProviderForChain } from '@snx-v3/useBlockchain'; | ||
import { importUSDProxy, USDProxyType } from '@synthetixio/v3-contracts'; | ||
|
||
export function useUSDProxyForChain(network?: Network) { | ||
const provider = useProviderForChain(network); | ||
const withSigner = false; | ||
return useQuery({ | ||
queryKey: [`${network?.id}-${network?.preset}`, 'USDProxy', { withSigner }], | ||
queryFn: async function () { | ||
if (network && provider) { | ||
const { address, abi } = await importUSDProxy(network.id, network.preset); | ||
return new Contract(address, abi, provider) as USDProxyType; | ||
} | ||
}, | ||
enabled: Boolean(network && provider), | ||
staleTime: Infinity, | ||
}); | ||
} |
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