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
*/
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<void>;
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<IContext>({} as IContext);
*/
export function JsonRpcContextProvider({ children }: { children: ReactNode | ReactNode[] }) {
const [pending, setPending] = useState(false);
const [result, setResult] = useState<IRpcResult | null>();
const [result, setResult] = useState<IFormattedRpcResponse | null>();
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);

View File

@ -12,8 +12,13 @@ export async function getGasPrice(chainId: string): Promise<string> {
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));