Skip to content

Commit

Permalink
fix: check iframe
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Nov 5, 2024
1 parent 8f114ef commit f7786b3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
45 changes: 23 additions & 22 deletions src/components/Widgets/Widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { useConfig } from 'wagmi';
import { WidgetWrapper } from '.';
import type { WidgetProps } from './Widget.types';
import { refuelAllowChains, themeAllowChains } from './Widget.types';
import { isIframeEnvironment } from 'src/utils/iframe';

export function Widget({
starterVariant,
Expand Down Expand Up @@ -175,31 +176,31 @@ export function Widget({
maxPriceImpact: 0.4,
allowSwitchChain: !isMultisigSigner, // avoid routes requiring chain switch for multisig wallets
},
providers: isMultisigSigner
? ([
EVM({
getWalletClient: () => getWalletClient(wagmiConfig),
switchChain: async (chainId) => {
const chain = await switchChain(wagmiConfig, { chainId });
trackEvent({
category: TrackingCategory.Widget,
action: TrackingAction.SwitchChain,
label: 'switch-chain',
data: {
[TrackingEventParameter.ChainId]: chainId,
},
});
return getWalletClient(wagmiConfig, { chainId: chain.id });
},
multisig: multisigSdkConfig,
}),
] as any) // TODO: fix typing Eugene :pray:
: undefined,
providers:
isMultisigSigner && isIframeEnvironment()
? [
EVM({
getWalletClient: () => getWalletClient(wagmiConfig),
switchChain: async (chainId) => {
const chain = await switchChain(wagmiConfig, { chainId });
trackEvent({
category: TrackingCategory.Widget,
action: TrackingAction.SwitchChain,
label: 'switch-chain',
data: {
[TrackingEventParameter.ChainId]: chainId,
},
});
return getWalletClient(wagmiConfig, { chainId: chain.id });
},
multisig: multisigSdkConfig,
}),
]
: undefined,
},
buildUrl: true,
// insurance: true,
integrator: integratorStringByType,
tokens: tokens as any, // TODO: fix typing Eugene :pray:
tokens: tokens,
};
}, [
fromChain,
Expand Down
6 changes: 2 additions & 4 deletions src/hooks/useMultisig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { Connector } from 'wagmi';
import type { GatewayTransactionDetails } from '@safe-global/safe-apps-sdk';
import SafeAppsSDK, { TransactionStatus } from '@safe-global/safe-apps-sdk';
import { useEffect, useState } from 'react';
import { isIframeEnvironment } from 'src/utils/iframe';

const getIsSafeConnector = async (connector?: Connector): Promise<boolean> => {
let isSafeConnector = connector?.id === 'safe';
Expand All @@ -33,10 +34,7 @@ export const useMultisig = () => {
const [isSafeConnector, setIsSafeConnector] = useState(false);

const checkMultisigEnvironment = async () => {
// in Multisig env, window.parent is not equal to window
const isIframeEnvironment = window.parent !== window;

if (!isIframeEnvironment) {
if (!isIframeEnvironment()) {
return false;
}

Expand Down
6 changes: 6 additions & 0 deletions src/utils/iframe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const isIframeEnvironment = () => {
// in iframe env, window.parent is not equal to window
const anyWindow = typeof window !== 'undefined' ? (window as any) : undefined;
const isIframeEnvironment = anyWindow && anyWindow.parent !== anyWindow;
return isIframeEnvironment;
};

0 comments on commit f7786b3

Please sign in to comment.