Skip to content

Commit

Permalink
fix chain-id
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentine1898 committed Jun 13, 2024
1 parent b60e273 commit 109c34d
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand All @@ -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<string | undefined>();
const [chainId, setChainId] = useState<string>();
const [grpcEndpointInput, setGrpcEndpointInput] = useState<string>('');
const [rpcError, setRpcError] = useState<string>();
const [isSubmitButtonEnabled, setIsSubmitButtonEnabled] = useState(false);
Expand Down
12 changes: 6 additions & 6 deletions apps/extension/src/shared/components/numeraires-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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));
};
Expand All @@ -20,6 +21,7 @@ const useNumerairesSelector = (state: AllSlices) => {
selectedNumeraires: state.numeraires.selectedNumeraires,
selectNumeraire: state.numeraires.selectNumeraire,
saveNumeraires: state.numeraires.saveNumeraires,
networkChainId: state.network.chainId,
};
};

Expand All @@ -31,11 +33,9 @@ export const NumeraireForm = ({
onSuccess: () => void | Promise<void>;
}) => {
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);

Expand Down
8 changes: 8 additions & 0 deletions apps/extension/src/state/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { AllSlices, SliceCreator } from '.';
export interface NetworkSlice {
grpcEndpoint: string | undefined;
fullSyncHeight?: number;
chainId?: string;
setGRPCEndpoint: (endpoint: string) => Promise<void>;
setChainId: (chainId: string) => void;
}

export const createNetworkSlice =
Expand All @@ -14,13 +16,19 @@ export const createNetworkSlice =
return {
grpcEndpoint: undefined,
fullSyncHeight: undefined,
chainId: undefined,
setGRPCEndpoint: async (endpoint: string) => {
set(state => {
state.network.grpcEndpoint = endpoint;
});

await local.set('grpcEndpoint', endpoint);
},
setChainId: (chainId: string) => {
set(state => {
state.network.chainId = chainId;
});
},
};
};

Expand Down
14 changes: 14 additions & 0 deletions apps/extension/src/state/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -120,6 +121,19 @@ function syncLocal(changes: Record<string, chrome.storage.StorageChange>, set: S
}),
);
}

if (changes['params']) {
const stored = changes['params'].newValue as
| StorageItem<LocalStorageState['params']>
| undefined;
set(
produce((state: AllSlices) => {
state.network.chainId = stored?.value
? AppParameters.fromJsonString(stored.value).chainId
: state.network.chainId;
}),
);
}
}

function syncSession(changes: Record<string, chrome.storage.StorageChange>, set: Setter) {
Expand Down
4 changes: 2 additions & 2 deletions apps/extension/src/storage/types.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -23,6 +23,6 @@ export interface LocalStorageState {
passwordKeyPrint: KeyPrintJson | undefined;
fullSyncHeight: number | undefined;
knownSites: OriginRecord[];
params: Jsonified<AppParameters> | undefined;
params: Stringified<AppParameters> | undefined;
numeraires: Stringified<AssetId>[];
}
2 changes: 1 addition & 1 deletion apps/extension/src/wallet-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion packages/storage/src/indexed-db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions packages/storage/src/indexed-db/indexed-db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
Expand All @@ -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,
}),
Expand Down

0 comments on commit 109c34d

Please sign in to comment.