diff --git a/dapps/react-dapp-v2/src/contexts/JsonRpcContext.tsx b/dapps/react-dapp-v2/src/contexts/JsonRpcContext.tsx index e32d6f4..7ea1d90 100644 --- a/dapps/react-dapp-v2/src/contexts/JsonRpcContext.tsx +++ b/dapps/react-dapp-v2/src/contexts/JsonRpcContext.tsx @@ -26,17 +26,12 @@ import { useChainData } from "./ChainDataContext"; * Types */ interface IFormattedRpcResponse { - method: string; - address: string; + method?: string; + address?: string; valid: boolean; result: string; } -interface IRpcResult { - method: string; - valid: boolean; -} - type TRpcRequestCallback = (chainId: string, address: string) => Promise; interface IContext { @@ -56,7 +51,7 @@ interface IContext { testSignMessage: TRpcRequestCallback; testSignTransaction: TRpcRequestCallback; }; - rpcResult?: IRpcResult | null; + rpcResult?: IFormattedRpcResponse | null; isRpcRequestPending: boolean; isTestnet: boolean; setIsTestnet: (isTestnet: boolean) => void; @@ -72,7 +67,7 @@ export const JsonRpcContext = createContext({} as IContext); */ export function JsonRpcContextProvider({ children }: { children: ReactNode | ReactNode[] }) { const [pending, setPending] = useState(false); - const [result, setResult] = useState(); + const [result, setResult] = useState(); const [isTestnet, setIsTestnet] = useState(getLocalStorageTestnetFlag()); const { client, session, accounts, balances, solanaPublicKeys } = useWalletConnectClient(); @@ -93,9 +88,13 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea setPending(true); const result = await rpcRequest(chainId, address); setResult(result); - } catch (err) { - console.error(err); - setResult(null); + } catch (err: any) { + console.error("RPC request failed: ", err); + setResult({ + address, + valid: false, + result: err?.message ?? err, + }); } finally { setPending(false); } @@ -125,6 +124,7 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea setResult({ method: "ping", valid, + result: valid ? "Ping succeeded" : "Ping failed", }); } catch (e) { console.error(e); diff --git a/dapps/react-dapp-v2/src/helpers/tx.ts b/dapps/react-dapp-v2/src/helpers/tx.ts index 99efde0..4f5fc5f 100644 --- a/dapps/react-dapp-v2/src/helpers/tx.ts +++ b/dapps/react-dapp-v2/src/helpers/tx.ts @@ -12,8 +12,13 @@ export async function getGasPrice(chainId: string): Promise { export async function formatTestTransaction(account: string) { const [namespace, reference, address] = account.split(":"); 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));