Skip to content

Commit

Permalink
remove provider updated event and handle errors in provider relay
Browse files Browse the repository at this point in the history
  • Loading branch information
jhesgodi committed Sep 3, 2024
1 parent 5ed7e3d commit c7db5ae
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PostMessageHandler, PostMessageHandlerEventType } from '@imtbl/checkout-sdk';
import { PostMessageHandler } from '@imtbl/checkout-sdk';
import {
Dispatch, ReactNode, useContext, useEffect,
} from 'react';
Expand All @@ -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;
Expand All @@ -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 (
<CheckoutContext.Provider value={values}>
{children}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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],
);
Expand Down Expand Up @@ -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],
);

Expand Down

0 comments on commit c7db5ae

Please sign in to comment.