From c7db5aeb69d8162e1c51b787a7d8991b2b20687d Mon Sep 17 00:00:00 2001 From: Jhonatan Gonzalez Date: Wed, 4 Sep 2024 08:54:25 +1000 Subject: [PATCH] remove provider updated event and handle errors in provider relay --- .../context/CheckoutContextProvider.tsx | 19 +------- .../checkout/hooks/useProviderRelay.ts | 45 ++++++++++++------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/packages/checkout/widgets-lib/src/widgets/checkout/context/CheckoutContextProvider.tsx b/packages/checkout/widgets-lib/src/widgets/checkout/context/CheckoutContextProvider.tsx index c9ceb5abde..56f4ade91d 100644 --- a/packages/checkout/widgets-lib/src/widgets/checkout/context/CheckoutContextProvider.tsx +++ b/packages/checkout/widgets-lib/src/widgets/checkout/context/CheckoutContextProvider.tsx @@ -1,4 +1,4 @@ -import { PostMessageHandler, PostMessageHandlerEventType } from '@imtbl/checkout-sdk'; +import { PostMessageHandler } from '@imtbl/checkout-sdk'; import { Dispatch, ReactNode, useContext, useEffect, } from 'react'; @@ -21,13 +21,7 @@ export function CheckoutContextProvider({ children, }: CheckoutContextProviderProps) { const { checkoutState, checkoutDispatch } = values; - const { - checkout, - provider, - iframeContentWindow, - postMessageHandler, - iframeURL, - } = checkoutState; + const { checkout, iframeContentWindow, iframeURL } = checkoutState; useEffect(() => { if (!iframeContentWindow || !checkout || !iframeURL) return; @@ -50,15 +44,6 @@ export function CheckoutContextProvider({ }); }, [iframeContentWindow, checkout, iframeURL]); - useEffect(() => { - if (!provider || !postMessageHandler) return; - - postMessageHandler.send(PostMessageHandlerEventType.PROVIDER_UPDATED, { - isMetamask: provider.provider.isMetaMask, - isPassport: (provider.provider as any)?.isPassport, - }); - }, [provider, postMessageHandler]); - return ( {children} diff --git a/packages/checkout/widgets-lib/src/widgets/checkout/hooks/useProviderRelay.ts b/packages/checkout/widgets-lib/src/widgets/checkout/hooks/useProviderRelay.ts index 1e5d33f703..ba458c6e02 100644 --- a/packages/checkout/widgets-lib/src/widgets/checkout/hooks/useProviderRelay.ts +++ b/packages/checkout/widgets-lib/src/widgets/checkout/hooks/useProviderRelay.ts @@ -36,27 +36,35 @@ export function useProviderRelay() { */ const execute = useCallback( async (payload: ProviderRelayPayload, executeProvider: Web3Provider) => { - if (!executeProvider?.provider.request) { - throw new Error("Provider only supports 'request' method"); - } - if (!postMessageHandler) { throw new Error( 'Provider can execute request because PostMessageHandler is not initialized', ); } - const { id, params, method } = payload.jsonRpcRequestMessage; + if (!executeProvider?.provider.request) { + throw new Error("Provider only supports 'request' method"); + } + + try { + const { id, params, method } = payload.jsonRpcRequestMessage; - // Execute the request - const result = await executeProvider.provider.request({ method, params }); - const formattedResponse = { id, result, jsonrpc: '2.0' }; + // Execute the request + const result = await executeProvider.provider.request({ method, params }); + const formattedResponse = { id, result, jsonrpc: '2.0' }; - // Send the response using the postMessageHandler - postMessageHandler.send(PostMessageHandlerEventType.PROVIDER_RELAY, { - response: formattedResponse, - eip6963Info: payload.eip6963Info, - }); + // Send the response using the postMessageHandler + postMessageHandler.send(PostMessageHandlerEventType.PROVIDER_RELAY, { + response: formattedResponse, + eip6963Info: payload.eip6963Info, + }); + } catch (error: any) { + // Send the error using the postMessageHandler + postMessageHandler.send(PostMessageHandlerEventType.PROVIDER_RELAY, { + response: { id: payload.jsonRpcRequestMessage.id, error: error.message, jsonrpc: '2.0' }, + eip6963Info: payload.eip6963Info, + }); + } }, [postMessageHandler], ); @@ -107,9 +115,16 @@ export function useProviderRelay() { throw new Error('Provider is not defined'); } - await execute(providerRelayPayload, currentProvider); + try { + await execute(providerRelayPayload, currentProvider); + } catch (error: any) { + // Send the error using the postMessageHandler + postMessageHandler.send(PostMessageHandlerEventType.PROVIDER_RELAY, { + response: { id: providerRelayPayload.jsonRpcRequestMessage.id, error: error.message, jsonrpc: '2.0' }, + eip6963Info: providerRelayPayload.eip6963Info, + }); + } }, - [provider, postMessageHandler, checkout, execute], );