-
Notifications
You must be signed in to change notification settings - Fork 2
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
Dynamic client side envs 2 #80
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
03179ea
Implement dynamic client side envs
JasonMHasperhoven fb644e1
Remove configConstants
JasonMHasperhoven 6e8bdf6
Fix lint issue
JasonMHasperhoven 5c197bb
docs: update readme with new env vars
conorsch a13e384
Add useEnv fetcher
JasonMHasperhoven 4a5cd3c
Refactor app to use fetchers
JasonMHasperhoven 99101c3
Remove ClientEnv type file
JasonMHasperhoven 6948ae3
Merge branch 'main' into dynamic-client-side-envs2
JasonMHasperhoven 11428fa
Fix merge conflict + view pool bug
JasonMHasperhoven a3eb862
Add defaults for client env
JasonMHasperhoven 726c0d0
Fix type import
JasonMHasperhoven 95871aa
Refactor fetchers to suffix Deprecated
JasonMHasperhoven 68a0a8e
Fix duplicate lpAssetView.tsx file
JasonMHasperhoven c9de74a
Fix lint issues
JasonMHasperhoven b014301
Fix lp asset view
JasonMHasperhoven 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
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.
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.
issue: it is tempting to use our own types, but we should do our best to use types from BufBuild that are more universally relied upon in the penumbra ecosystem. For instance, we shouldn't have components that accept a Token, but probably ones that accept a
ValueView
type