From 109c34d3495e73a5aebe9f2cce7a02c53ae906e7 Mon Sep 17 00:00:00 2001 From: valentine Date: Thu, 13 Jun 2024 14:48:26 +0300 Subject: [PATCH] fix chain-id --- .../grpc-endpoint-form/use-grpc-endpoint-form.ts | 7 +++++-- .../src/shared/components/numeraires-form.tsx | 12 ++++++------ apps/extension/src/state/network.ts | 8 ++++++++ apps/extension/src/state/persist.ts | 14 ++++++++++++++ apps/extension/src/storage/types.ts | 4 ++-- apps/extension/src/wallet-services.ts | 2 +- packages/storage/src/indexed-db/index.ts | 2 +- packages/storage/src/indexed-db/indexed-db.test.ts | 4 ++-- 8 files changed, 39 insertions(+), 14 deletions(-) diff --git a/apps/extension/src/shared/components/grpc-endpoint-form/use-grpc-endpoint-form.ts b/apps/extension/src/shared/components/grpc-endpoint-form/use-grpc-endpoint-form.ts index a234cf32..2b06bf6a 100644 --- a/apps/extension/src/shared/components/grpc-endpoint-form/use-grpc-endpoint-form.ts +++ b/apps/extension/src/shared/components/grpc-endpoint-form/use-grpc-endpoint-form.ts @@ -12,7 +12,9 @@ import { isValidUrl } from '../../utils/is-valid-url'; const useSaveGrpcEndpointSelector = (state: AllSlices) => ({ grpcEndpoint: state.network.grpcEndpoint, + chainId: state.network.chainId, setGrpcEndpoint: state.network.setGRPCEndpoint, + setChainId: state.network.setChainId, }); const getRpcsFromRegistry = () => { @@ -25,10 +27,11 @@ export const useGrpcEndpointForm = () => { const grpcEndpoints = useMemo(() => getRpcsFromRegistry(), []); // Get the rpc set in storage (if present) - const { grpcEndpoint, setGrpcEndpoint } = useStoreShallow(useSaveGrpcEndpointSelector); + const { grpcEndpoint, chainId, setGrpcEndpoint, setChainId } = useStoreShallow( + useSaveGrpcEndpointSelector, + ); const [originalChainId, setOriginalChainId] = useState(); - const [chainId, setChainId] = useState(); const [grpcEndpointInput, setGrpcEndpointInput] = useState(''); const [rpcError, setRpcError] = useState(); const [isSubmitButtonEnabled, setIsSubmitButtonEnabled] = useState(false); diff --git a/apps/extension/src/shared/components/numeraires-form.tsx b/apps/extension/src/shared/components/numeraires-form.tsx index ce7dc6a5..4cd842f9 100644 --- a/apps/extension/src/shared/components/numeraires-form.tsx +++ b/apps/extension/src/shared/components/numeraires-form.tsx @@ -9,8 +9,9 @@ import { getAssetId } from '@penumbra-zone/getters/metadata'; import { Button } from '@penumbra-zone/ui/components/ui/button'; import { Metadata } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/asset/v1/asset_pb'; -const getNumeraireFromRegistry = (chainId: string): Metadata[] => { +const getNumeraireFromRegistry = (chainId?: string): Metadata[] => { const registryClient = new ChainRegistryClient(); + if (!chainId) return []; const registry = registryClient.get(chainId); return registry.numeraires.map(n => registry.getMetadata(n)); }; @@ -20,6 +21,7 @@ const useNumerairesSelector = (state: AllSlices) => { selectedNumeraires: state.numeraires.selectedNumeraires, selectNumeraire: state.numeraires.selectNumeraire, saveNumeraires: state.numeraires.saveNumeraires, + networkChainId: state.network.chainId, }; }; @@ -31,11 +33,9 @@ export const NumeraireForm = ({ onSuccess: () => void | Promise; }) => { const { chainId } = useChainIdQuery(); - const { selectedNumeraires, selectNumeraire, saveNumeraires } = useStore(useNumerairesSelector); - const numeraires = useMemo( - () => getNumeraireFromRegistry(chainId ?? 'penumbra-testnet-deimos-8'), - [chainId], - ); + const { selectedNumeraires, selectNumeraire, saveNumeraires, networkChainId } = + useStore(useNumerairesSelector); + const numeraires = useMemo(() => getNumeraireFromRegistry(chainId ?? networkChainId), [chainId]); const [loading, setLoading] = useState(false); diff --git a/apps/extension/src/state/network.ts b/apps/extension/src/state/network.ts index f67b819e..db6ebee9 100644 --- a/apps/extension/src/state/network.ts +++ b/apps/extension/src/state/network.ts @@ -5,7 +5,9 @@ import { AllSlices, SliceCreator } from '.'; export interface NetworkSlice { grpcEndpoint: string | undefined; fullSyncHeight?: number; + chainId?: string; setGRPCEndpoint: (endpoint: string) => Promise; + setChainId: (chainId: string) => void; } export const createNetworkSlice = @@ -14,6 +16,7 @@ export const createNetworkSlice = return { grpcEndpoint: undefined, fullSyncHeight: undefined, + chainId: undefined, setGRPCEndpoint: async (endpoint: string) => { set(state => { state.network.grpcEndpoint = endpoint; @@ -21,6 +24,11 @@ export const createNetworkSlice = await local.set('grpcEndpoint', endpoint); }, + setChainId: (chainId: string) => { + set(state => { + state.network.chainId = chainId; + }); + }, }; }; diff --git a/apps/extension/src/state/persist.ts b/apps/extension/src/state/persist.ts index 2daf7a3b..7046d0b2 100644 --- a/apps/extension/src/state/persist.ts +++ b/apps/extension/src/state/persist.ts @@ -7,6 +7,7 @@ import { LocalStorageState } from '../storage/types'; import { sessionExtStorage, SessionStorageState } from '../storage/session'; import { StorageItem } from '../storage/base'; import { walletsFromJson } from '@penumbra-zone/types/wallet'; +import { AppParameters } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/app/v1/app_pb'; export type Middleware = < T, @@ -120,6 +121,19 @@ function syncLocal(changes: Record, set: S }), ); } + + if (changes['params']) { + const stored = changes['params'].newValue as + | StorageItem + | undefined; + set( + produce((state: AllSlices) => { + state.network.chainId = stored?.value + ? AppParameters.fromJsonString(stored.value).chainId + : state.network.chainId; + }), + ); + } } function syncSession(changes: Record, set: Setter) { diff --git a/apps/extension/src/storage/types.ts b/apps/extension/src/storage/types.ts index 2f1d88f7..1df3b80a 100644 --- a/apps/extension/src/storage/types.ts +++ b/apps/extension/src/storage/types.ts @@ -1,6 +1,6 @@ import { AppParameters } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/app/v1/app_pb'; import { KeyPrintJson } from '@penumbra-zone/crypto-web/encryption'; -import { Jsonified, Stringified } from '@penumbra-zone/types/jsonified'; +import { Stringified } from '@penumbra-zone/types/jsonified'; import { UserChoice } from '@penumbra-zone/types/user-choice'; import { WalletJson } from '@penumbra-zone/types/wallet'; import { AssetId } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/asset/v1/asset_pb'; @@ -23,6 +23,6 @@ export interface LocalStorageState { passwordKeyPrint: KeyPrintJson | undefined; fullSyncHeight: number | undefined; knownSites: OriginRecord[]; - params: Jsonified | undefined; + params: Stringified | undefined; numeraires: Stringified[]; } diff --git a/apps/extension/src/wallet-services.ts b/apps/extension/src/wallet-services.ts index 29151298..11ae64b9 100644 --- a/apps/extension/src/wallet-services.ts +++ b/apps/extension/src/wallet-services.ts @@ -41,7 +41,7 @@ export const startWalletServices = async () => { const getChainId = async (baseUrl: string) => { const localChainId = await localExtStorage .get('params') - .then(json => json && AppParameters.fromJson(json).chainId); + .then(json => json && AppParameters.fromJsonString(json).chainId); if (localChainId) return localChainId; diff --git a/packages/storage/src/indexed-db/index.ts b/packages/storage/src/indexed-db/index.ts index 82d2cc17..ea21ac78 100644 --- a/packages/storage/src/indexed-db/index.ts +++ b/packages/storage/src/indexed-db/index.ts @@ -676,7 +676,7 @@ export class IndexedDb implements IndexedDbInterface { while (cursor) { const price = EstimatedPrice.fromJson(cursor.value); if (!price.numeraire?.equals(this.stakingTokenAssetId)) { - cursor.delete(); + await cursor.delete(); } cursor = await cursor.continue(); } diff --git a/packages/storage/src/indexed-db/indexed-db.test.ts b/packages/storage/src/indexed-db/indexed-db.test.ts index f2200919..47f57c35 100644 --- a/packages/storage/src/indexed-db/indexed-db.test.ts +++ b/packages/storage/src/indexed-db/indexed-db.test.ts @@ -609,7 +609,7 @@ describe('IndexedDb', () => { await expect(db.getPricesForAsset(delegationMetadataA, 50n)).resolves.toEqual([ new EstimatedPrice({ pricedAsset: delegationMetadataA.penumbraAssetId!, - numeraire: numeraireAssetId, + numeraire: stakingAssetId, numerairePerUnit: 1.23, asOfHeight: 50n, }), @@ -625,7 +625,7 @@ describe('IndexedDb', () => { await expect(db.getPricesForAsset(delegationMetadataA, 241n)).resolves.toEqual([ new EstimatedPrice({ pricedAsset: delegationMetadataA.penumbraAssetId!, - numeraire: numeraireAssetId, + numeraire: stakingAssetId, numerairePerUnit: 1.23, asOfHeight: 50n, }),