-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
01b7092
commit f1c2fb9
Showing
13 changed files
with
316 additions
and
108 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { isValidAddress } from '@ethereumjs/util'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { getProvider } from '@wagmi/core'; | ||
import { Address } from 'wagmi'; | ||
|
||
import { | ||
QueryConfig, | ||
QueryFunctionArgs, | ||
QueryFunctionResult, | ||
createQueryKey, | ||
} from '~/core/react-query'; | ||
import { getAssetMetadata } from '~/core/utils/assets'; | ||
|
||
// /////////////////////////////////////////////// | ||
// Query Types | ||
|
||
type AssetMetadataArgs = { | ||
assetAddress?: Address; | ||
chainId: number; | ||
}; | ||
|
||
// /////////////////////////////////////////////// | ||
// Query Key | ||
|
||
export const assetMetadataQueryKey = ({ | ||
assetAddress, | ||
chainId, | ||
}: AssetMetadataArgs) => | ||
createQueryKey( | ||
'assetMetadata', | ||
{ assetAddress, chainId }, | ||
{ persisterVersion: 1 }, | ||
); | ||
|
||
type AssetMetadataQueryKey = ReturnType<typeof assetMetadataQueryKey>; | ||
|
||
// /////////////////////////////////////////////// | ||
// Query Function | ||
|
||
async function assetMetadataQueryFunction({ | ||
queryKey: [{ assetAddress, chainId }], | ||
}: QueryFunctionArgs<typeof assetMetadataQueryKey>) { | ||
if (assetAddress && isValidAddress(assetAddress)) { | ||
const provider = getProvider({ chainId: Number(chainId) }); | ||
const metadata = await getAssetMetadata({ | ||
address: assetAddress, | ||
provider, | ||
}); | ||
return { | ||
address: assetAddress, | ||
symbol: metadata.symbol, | ||
decimals: metadata.decimals, | ||
name: metadata.name, | ||
}; | ||
} | ||
} | ||
|
||
type AssetMetadataResult = QueryFunctionResult< | ||
typeof assetMetadataQueryFunction | ||
>; | ||
|
||
// /////////////////////////////////////////////// | ||
// Query Hook | ||
|
||
export function useAssetMetadata( | ||
{ assetAddress, chainId }: AssetMetadataArgs, | ||
config: QueryConfig< | ||
AssetMetadataResult, | ||
Error, | ||
AssetMetadataResult, | ||
AssetMetadataQueryKey | ||
> = {}, | ||
) { | ||
return useQuery( | ||
assetMetadataQueryKey({ | ||
assetAddress, | ||
chainId, | ||
}), | ||
assetMetadataQueryFunction, | ||
{ | ||
...config, | ||
}, | ||
); | ||
} |
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
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
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
Oops, something went wrong.