Skip to content

Commit

Permalink
fix: fix public key fetching (a bit)
Browse files Browse the repository at this point in the history
  • Loading branch information
emccorson committed Feb 29, 2024
1 parent f18e24f commit e74bf73
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 30 deletions.
12 changes: 7 additions & 5 deletions apps/namada-interface/src/App/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable max-len */
import { AnimatePresence } from "framer-motion";
import { createBrowserHistory } from "history";
import { useAtom, useAtomValue, useSetAtom } from "jotai";
import { useAtomValue, useSetAtom } from "jotai";
import { loadable } from "jotai/utils";
import { useEffect, useState } from "react";
import { PersistGate } from "redux-persist/integration/react";
Expand Down Expand Up @@ -39,7 +39,7 @@ import { TopNavigation } from "./TopNavigation";

import { accountsAtom, balancesAtom } from "slices/accounts";
import { chainAtom } from "slices/chain";
import { minimumGasPriceAtom } from "slices/fees";
import { isRevealPkNeededAtom, minimumGasPriceAtom } from "slices/fees";

export const history = createBrowserHistory({ window });

Expand All @@ -65,9 +65,11 @@ const useOnChainChanged = (): void => {
const chain = useAtomValue(chainAtom);

const refreshMinimumGasPrice = useSetAtom(minimumGasPriceAtom);
const refreshPublicKeys = useSetAtom(isRevealPkNeededAtom);

useEffect(() => {
refreshMinimumGasPrice();
refreshPublicKeys();
}, [chain]);
};

Expand Down Expand Up @@ -105,10 +107,12 @@ const useOnAccountsChanged = (): void => {
const accountsLoadable = useAtomValue(loadable(accountsAtom));

const refreshBalances = useSetAtom(balancesAtom);
const refreshPublicKeys = useSetAtom(isRevealPkNeededAtom);

useEffect(() => {
if (accountsLoadable.state === "hasData") {
refreshBalances();
refreshPublicKeys();
}
}, [accountsLoadable]);
};
Expand All @@ -128,9 +132,7 @@ function App(): JSX.Element {
setColorMode((currentMode) => (currentMode === "dark" ? "light" : "dark"));
};

const [chain, refreshChain] = useAtom(chainAtom);
const refreshAccounts = useSetAtom(accountsAtom);
const refreshBalances = useSetAtom(balancesAtom);
const chain = useAtomValue(chainAtom);

const { connectedChains } = useAppSelector<SettingsState>(
(state) => state.settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ export const NamadaProposalsUpdatedHandler =
};

export const NamadaUpdatedBalancesHandler =
(dispatch: Dispatch<unknown>) => async () => {
(dispatch: Dispatch<unknown>, refreshBalances: () => void) => async () => {
dispatch(fetchBalances());
refreshBalances();
};

export const NamadaUpdatedStakingHandler =
Expand All @@ -67,7 +68,7 @@ export const NamadaTxStartedHandler =
};

export const NamadaTxCompletedHandler =
(dispatch: Dispatch<unknown>, refreshBalances: () => Promise<void>) =>
(dispatch: Dispatch<unknown>, refreshPublicKeys: () => void) =>
async (event: CustomEventInit) => {
const { msgId, txType, success, payload } = event.detail;
if (!success) {
Expand All @@ -81,7 +82,7 @@ export const NamadaTxCompletedHandler =
error: payload || "",
})
);
refreshBalances();
refreshPublicKeys();
};

export const NamadaConnectionRevokedHandler =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import { useSetAtom } from "jotai";
import { accountsAtom, balancesAtom } from "slices/accounts";
import { chainAtom } from "slices/chain";
import { isRevealPkNeededAtom } from "slices/fees";
import { namadaExtensionConnectedAtom } from "slices/settings";

export const ExtensionEventsContext = createContext({});
Expand All @@ -35,6 +36,7 @@ export const ExtensionEventsProvider: React.FC = (props): JSX.Element => {
const refreshAccounts = useSetAtom(accountsAtom);
const refreshChain = useSetAtom(chainAtom);
const refreshBalances = useSetAtom(balancesAtom);
const refreshPublicKeys = useSetAtom(isRevealPkNeededAtom);
const setNamadaExtensionConnected = useSetAtom(namadaExtensionConnectedAtom);

// Instantiate handlers:
Expand All @@ -50,10 +52,13 @@ export const ExtensionEventsProvider: React.FC = (props): JSX.Element => {
);
const namadaTxStartedHandler = NamadaTxStartedHandler(dispatch);
const namadaTxCompletedHandler = NamadaTxCompletedHandler(
dispatch,
refreshPublicKeys
);
const namadaUpdatedBalancesHandler = NamadaUpdatedBalancesHandler(
dispatch,
refreshBalances
);
const namadaUpdatedBalancesHandler = NamadaUpdatedBalancesHandler(dispatch);
const namadaUpdatedStakingHandler = NamadaUpdatedStakingHandler(dispatch);
const namadaProposalsUpdatedHandler = NamadaProposalsUpdatedHandler(dispatch);
const namadaConnectionRevokedHandler = NamadaConnectionRevokedHandler(
Expand Down
60 changes: 39 additions & 21 deletions apps/namada-interface/src/slices/fees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,45 @@ const minimumGasPriceAtom = (() => {
);
})();

const isRevealPkNeededAtom = atom(async (get) => {
const accounts = await get(accountsAtom);
const transparentAccounts = accounts.filter((account) => !account.isShielded);

const { rpc } = get(chainAtom);
const query = new Query(rpc);

const map: Record<string, string | null> = {};
await Promise.all(
transparentAccounts.map(async ({ address }) => {
const publicKey = await query.query_public_key(address);
map[address] = publicKey;
})
);
const isRevealPkNeededAtom = (() => {
type RevealPkNeededMap = { [address: string]: boolean };

return (address: string): boolean => {
if (!(address in map)) {
throw new Error("address not found in public key map");
}
return map[address] === null;
};
});
const base = atom(new Promise<RevealPkNeededMap>(() => {}));

return atom(
async (get) => {
const map = await get(base);

return (address: string): boolean => {
if (!(address in map)) {
throw new Error("address not found in public key map");
}
return map[address];
};
},
(get, set) =>
set(
base,
(async (): Promise<RevealPkNeededMap> => {
const accounts = await get(accountsAtom);
const transparentAccounts = accounts.filter(
(account) => !account.isShielded
);

const { rpc } = get(chainAtom);
const query = new Query(rpc);

const entries = await Promise.all(
transparentAccounts.map(async ({ address }) => {
const publicKey = await query.query_public_key(address);
return [address, !publicKey];
})
);

return Object.fromEntries(entries);
})()
)
);
})();

export { isRevealPkNeededAtom, minimumGasPriceAtom };

0 comments on commit e74bf73

Please sign in to comment.