Skip to content

Commit

Permalink
chore: allow to debug eth provider requests
Browse files Browse the repository at this point in the history
  • Loading branch information
michalkvasnicak committed Dec 13, 2024
1 parent 46fff8a commit c8bb17a
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion packages/render/src/use-frame-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import type {
AddFrameResult,
EthProviderRequest,
FrameHost,
FrameLocationContext,
FrameLocationContextLauncher,
SetPrimaryButton,
} from "@farcaster/frame-sdk";
import type { WebView, WebViewProps } from "react-native-webview";
Expand Down Expand Up @@ -167,6 +169,12 @@ export type UseFrameAppOptions = {
* Frame client that is rendering the app
*/
client: FrameClientConfig;
/**
* Information about the context from which the frame was launched.
*
* @defaultValue launcher context
*/
location?: FrameLocationContext;
/**
* Either:
*
Expand Down Expand Up @@ -248,6 +256,12 @@ export type UseFrameAppOptions = {
onSendTransactionRequest?: OnSendTransactionRequestFunction;
onSignMessageRequest?: OnSignMessageRequestFunction;
onSignTypedDataRequest?: OnSignTypedDataRequestFunction;
/**
* Called when the frame app requests eth provider request.
*
* This is called only if debug mode is enabled and should be used only for debugging.
*/
onDebugEthProviderRequest?: (...args: Parameters<EthProviderRequest>) => void;
};

type UnregisterEndpointFunction = () => void;
Expand Down Expand Up @@ -276,12 +290,17 @@ export type UseFrameAppReturn =
status: "error";
};

const defaultLocation: FrameLocationContextLauncher = {
type: "launcher",
};

/**
* This hook is used to handle frames v2 apps.
*/
export function useFrameApp({
provider,
client,
location = defaultLocation,
farcasterSigner,
source,
fetchFn,
Expand All @@ -297,9 +316,12 @@ export function useFrameApp({
onSendTransactionRequest = defaultOnSendTransactionRequest,
onSignMessageRequest = defaultOnSignMessageRequest,
onSignTypedDataRequest = defaultOnSignTypedDataRequest,
onDebugEthProviderRequest,
}: UseFrameAppOptions): UseFrameAppReturn {
const providerRef = useFreshRef(provider);
const debugRef = useFreshRef(debug);
const clientRef = useFreshRef(client);
const locationRef = useFreshRef(location);
const readyRef = useFreshRef(onReady);
const closeRef = useFreshRef(onClose);
const onOpenUrlRef = useFreshRef(onOpenUrl);
Expand All @@ -311,6 +333,7 @@ export function useFrameApp({
const onSignMessageRequestRef = useFreshRef(onSignMessageRequest);
const onSignTypedDataRequestRef = useFreshRef(onSignTypedDataRequest);
const addFrameRequestsCacheRef = useFreshRef(addFrameRequestsCache);
const onDebugEthProviderRequestRef = useFreshRef(onDebugEthProviderRequest);
const frameResolutionState = useFetchFrameApp({
source,
fetchFn,
Expand Down Expand Up @@ -384,7 +407,11 @@ export function useFrameApp({
logDebugRef.current(
'@frames.js/render/unstable-use-frame-app: "context" getter called'
);
return { user: { fid: signer.fid }, client: clientRef.current };
return {
user: { fid: signer.fid },
client: clientRef.current,
location: locationRef.current,
};
},
openUrl(url) {
logDebugRef.current(
Expand Down Expand Up @@ -420,6 +447,10 @@ export function useFrameApp({
parameters
);

if (debugRef.current) {
onDebugEthProviderRequestRef.current?.(parameters);
}

let isApproved = true;

if (isSendTransactionRpcRequest(parameters)) {
Expand Down Expand Up @@ -544,9 +575,12 @@ export function useFrameApp({
onSignerNotApprovedRef,
closeRef,
clientRef,
locationRef,
onOpenUrlRef,
readyRef,
onPrimaryButtonSetRef,
debugRef,
onDebugEthProviderRequestRef,
onSendTransactionRequestRef,
onSignTypedDataRequestRef,
onSignMessageRequestRef,
Expand Down

0 comments on commit c8bb17a

Please sign in to comment.