-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement dynamic client side envs * Remove configConstants * Fix lint issue * docs: update readme with new env vars * Add useEnv fetcher * Refactor app to use fetchers * Remove ClientEnv type file * Fix merge conflict + view pool bug * Add defaults for client env * Fix type import * Refactor fetchers to suffix Deprecated * Fix duplicate lpAssetView.tsx file * Fix lint issues * Fix lp asset view --------- Co-authored-by: Conor Schaefer <[email protected]>
- Loading branch information
1 parent
bdf9ab6
commit df94a11
Showing
27 changed files
with
298 additions
and
234 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,5 @@ | ||
PENUMBRA_GRPC_ENDPOINT= | ||
PENUMBRA_INDEXER_ENDPOINT= | ||
PENUMBRA_INDEXER_CA_CERT= | ||
PENUMBRA_CHAIN_ID= | ||
PENUMBRA_CUILOA_URL= |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,13 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { ClientEnv } from '@/utils/env/types'; | ||
|
||
export const useEnv = () => { | ||
return useQuery({ | ||
queryKey: ['clientEnv'], | ||
queryFn: async (): Promise<ClientEnv> => { | ||
const res = await fetch('/api/env'); | ||
return (await res.json()) as ClientEnv; | ||
}, | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { ChainRegistryClient, Registry } from '@penumbra-labs/registry'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { useEnv } from './env'; | ||
|
||
export const chainRegistryClient = new ChainRegistryClient(); | ||
|
||
export const useRegistry = () => { | ||
const { data: env, isLoading: isEnvLoading, error: envError } = useEnv(); | ||
|
||
const { | ||
data: registry, | ||
isLoading: isRegistryLoading, | ||
error: registryError, | ||
} = useQuery({ | ||
queryKey: ['penumbraRegistry', env], | ||
queryFn: async (): Promise<Registry> => { | ||
const chainId = env?.PENUMBRA_CHAIN_ID; | ||
if (!chainId) { | ||
throw new Error('chain id not available to query registry'); | ||
} | ||
return chainRegistryClient.remote.get(chainId); | ||
}, | ||
staleTime: Infinity, | ||
enabled: Boolean(env), | ||
}); | ||
|
||
return { | ||
data: registry, | ||
isLoading: isEnvLoading || isRegistryLoading, | ||
error: envError ?? registryError, | ||
}; | ||
}; |
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,80 @@ | ||
import { useRegistry } from './registry'; | ||
import { AssetId, Metadata } from '@penumbra-zone/protobuf/penumbra/core/asset/v1/asset_pb'; | ||
import { decimalsFromDenomUnits, imagePathFromAssetImages } from '@/old/utils/token/tokenFetch'; | ||
import { uint8ArrayToBase64, base64ToUint8Array } from '@/old/utils/math/base64'; | ||
import { Token } from '@/old/utils/types/token'; | ||
|
||
export const useTokenAssets = () => { | ||
const { data: registry, isLoading: isRegistryLoading, error: registryError } = useRegistry(); | ||
const data: Metadata[] = registry?.getAllAssets() ?? []; | ||
|
||
return { | ||
data, | ||
isLoading: isRegistryLoading, | ||
error: registryError, | ||
}; | ||
}; | ||
|
||
export const useTokenAsset = (tokenId: Uint8Array | string) => { | ||
const { data: registry, isLoading: isRegistryLoading, error: registryError } = useRegistry(); | ||
|
||
const assetId: AssetId = new AssetId(); | ||
assetId.inner = typeof tokenId !== 'string' ? tokenId : base64ToUint8Array(tokenId); | ||
const tokenMetadata = registry?.getMetadata(assetId); | ||
|
||
return { | ||
data: tokenMetadata, | ||
isLoading: isRegistryLoading, | ||
error: registryError, | ||
}; | ||
}; | ||
|
||
export const useTokenAssetsDeprecated = () => { | ||
const { data: registry, isLoading: isRegistryLoading, error: registryError } = useRegistry(); | ||
const assets: Metadata[] = registry?.getAllAssets() ?? []; | ||
|
||
const tokenAssets = assets | ||
.filter(asset => asset.penumbraAssetId && !asset.display.startsWith('delegation_')) | ||
.map(asset => { | ||
const displayParts = asset.display.split('/'); | ||
return { | ||
decimals: decimalsFromDenomUnits(asset.denomUnits), | ||
display: displayParts[displayParts.length - 1] ?? '', | ||
symbol: asset.symbol, | ||
inner: asset.penumbraAssetId?.inner && uint8ArrayToBase64(asset.penumbraAssetId.inner), | ||
imagePath: imagePathFromAssetImages(asset.images), | ||
}; | ||
}) as Token[]; | ||
|
||
return { | ||
data: tokenAssets, | ||
isLoading: isRegistryLoading, | ||
error: registryError, | ||
}; | ||
}; | ||
|
||
export const useTokenAssetDeprecated = (tokenId: Uint8Array | string) => { | ||
const { data: registry, isLoading: isRegistryLoading, error: registryError } = useRegistry(); | ||
|
||
let tokenAsset = undefined; | ||
if (registry) { | ||
const assetId: AssetId = new AssetId(); | ||
assetId.inner = typeof tokenId !== 'string' ? tokenId : base64ToUint8Array(tokenId); | ||
const tokenMetadata = registry.getMetadata(assetId); | ||
|
||
const displayParts = tokenMetadata.display.split('/'); | ||
tokenAsset = { | ||
decimals: decimalsFromDenomUnits(tokenMetadata.denomUnits), | ||
display: displayParts[displayParts.length - 1] ?? '', | ||
symbol: tokenMetadata.symbol, | ||
inner: typeof tokenId !== 'string' ? uint8ArrayToBase64(tokenId) : tokenId, | ||
imagePath: imagePathFromAssetImages(tokenMetadata.images), | ||
}; | ||
} | ||
|
||
return { | ||
data: tokenAsset, | ||
isLoading: isRegistryLoading, | ||
error: registryError, | ||
}; | ||
}; |
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.