fix(dapp-v2): ensures thrown errors bubble to RPC result modal

Fixes #10
This commit is contained in:
Ben Kremer 2022-03-18 11:53:47 +01:00
parent 59b9eb5867
commit f1e69504a0
2 changed files with 19 additions and 14 deletions

View File

@ -26,17 +26,12 @@ import { useChainData } from "./ChainDataContext";
* Types * Types
*/ */
interface IFormattedRpcResponse { interface IFormattedRpcResponse {
method: string; method?: string;
address: string; address?: string;
valid: boolean; valid: boolean;
result: string; result: string;
} }
interface IRpcResult {
method: string;
valid: boolean;
}
type TRpcRequestCallback = (chainId: string, address: string) => Promise<void>; type TRpcRequestCallback = (chainId: string, address: string) => Promise<void>;
interface IContext { interface IContext {
@ -56,7 +51,7 @@ interface IContext {
testSignMessage: TRpcRequestCallback; testSignMessage: TRpcRequestCallback;
testSignTransaction: TRpcRequestCallback; testSignTransaction: TRpcRequestCallback;
}; };
rpcResult?: IRpcResult | null; rpcResult?: IFormattedRpcResponse | null;
isRpcRequestPending: boolean; isRpcRequestPending: boolean;
isTestnet: boolean; isTestnet: boolean;
setIsTestnet: (isTestnet: boolean) => void; setIsTestnet: (isTestnet: boolean) => void;
@ -72,7 +67,7 @@ export const JsonRpcContext = createContext<IContext>({} as IContext);
*/ */
export function JsonRpcContextProvider({ children }: { children: ReactNode | ReactNode[] }) { export function JsonRpcContextProvider({ children }: { children: ReactNode | ReactNode[] }) {
const [pending, setPending] = useState(false); const [pending, setPending] = useState(false);
const [result, setResult] = useState<IRpcResult | null>(); const [result, setResult] = useState<IFormattedRpcResponse | null>();
const [isTestnet, setIsTestnet] = useState(getLocalStorageTestnetFlag()); const [isTestnet, setIsTestnet] = useState(getLocalStorageTestnetFlag());
const { client, session, accounts, balances, solanaPublicKeys } = useWalletConnectClient(); const { client, session, accounts, balances, solanaPublicKeys } = useWalletConnectClient();
@ -93,9 +88,13 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
setPending(true); setPending(true);
const result = await rpcRequest(chainId, address); const result = await rpcRequest(chainId, address);
setResult(result); setResult(result);
} catch (err) { } catch (err: any) {
console.error(err); console.error("RPC request failed: ", err);
setResult(null); setResult({
address,
valid: false,
result: err?.message ?? err,
});
} finally { } finally {
setPending(false); setPending(false);
} }
@ -125,6 +124,7 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
setResult({ setResult({
method: "ping", method: "ping",
valid, valid,
result: valid ? "Ping succeeded" : "Ping failed",
}); });
} catch (e) { } catch (e) {
console.error(e); console.error(e);

View File

@ -12,8 +12,13 @@ export async function getGasPrice(chainId: string): Promise<string> {
export async function formatTestTransaction(account: string) { export async function formatTestTransaction(account: string) {
const [namespace, reference, address] = account.split(":"); const [namespace, reference, address] = account.split(":");
const chainId = `${namespace}:${reference}`; const chainId = `${namespace}:${reference}`;
// nonce
const _nonce = await apiGetAccountNonce(address, chainId); let _nonce;
try {
_nonce = await apiGetAccountNonce(address, chainId);
} catch (error) {
throw new Error(`Failed to fetch nonce for address ${address} on chain ${chainId}`);
}
const nonce = encoding.sanitizeHex(encoding.numberToHex(_nonce)); const nonce = encoding.sanitizeHex(encoding.numberToHex(_nonce));