Skip to content

Commit

Permalink
fix(widget): ignore selected eip6963 provider when in widget (#5009)
Browse files Browse the repository at this point in the history
  • Loading branch information
shoom3301 authored Oct 18, 2024
1 parent 02855f1 commit 3f8446b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
7 changes: 6 additions & 1 deletion apps/cowswap-frontend/src/cow-react/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,13 @@ function Main() {

function Web3ProviderInstance({ children }: { children: ReactNode }) {
const selectedWallet = useAppSelector((state) => state.user.selectedWallet)
const { standaloneMode } = useInjectedWidgetParams()

return <Web3Provider selectedWallet={selectedWallet}>{children}</Web3Provider>
return (
<Web3Provider standaloneMode={standaloneMode} selectedWallet={selectedWallet}>
{children}
</Web3Provider>
)
}

function Toasts() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async function connect(connector: Connector) {
}
}

export function useEagerlyConnect(selectedWallet: ConnectionType | undefined) {
export function useEagerlyConnect(selectedWallet: ConnectionType | undefined, standaloneMode?: boolean) {
const [tryConnectEip6963Provider, setTryConnectEip6963Provider] = useState(false)
const eagerlyConnectInitRef = useRef(false)
const selectedEip6963ProviderInfo = useSelectedEip6963ProviderInfo()
Expand Down Expand Up @@ -75,6 +75,8 @@ export function useEagerlyConnect(selectedWallet: ConnectionType | undefined) {
* Activate the selected eip6963 provider
*/
useEffect(() => {
// Ignore remembered eip6963 provider if the app is in widget dapp mode
if (isInjectedWidget() && !standaloneMode) return
if (!selectedWallet || !tryConnectEip6963Provider) return

const connection = getWeb3ReactConnection(selectedWallet)
Expand All @@ -90,5 +92,5 @@ export function useEagerlyConnect(selectedWallet: ConnectionType | undefined) {
setTryConnectEip6963Provider(false)
connect(connector)
}
}, [selectedEip6963ProviderInfo, selectedWallet, setEip6963Provider, tryConnectEip6963Provider])
}, [standaloneMode, selectedEip6963ProviderInfo, selectedWallet, setEip6963Provider, tryConnectEip6963Provider])
}
7 changes: 4 additions & 3 deletions libs/wallet/src/web3-react/Web3Provider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import { Web3ReactConnection } from '../types'
interface Web3ProviderProps {
children: ReactNode
selectedWallet: ConnectionType | undefined
standaloneMode?: boolean
}

export function Web3Provider({ children, selectedWallet }: Web3ProviderProps) {
useEagerlyConnect(selectedWallet)
export function Web3Provider({ children, selectedWallet, standaloneMode }: Web3ProviderProps) {
useEagerlyConnect(selectedWallet, standaloneMode)

const connections = useOrderedConnections(selectedWallet)
const connectors: [Connector, Web3ReactHooks][] = connections
Expand All @@ -25,7 +26,7 @@ export function Web3Provider({ children, selectedWallet }: Web3ProviderProps) {

const key = useMemo(
() => connections.map(({ type }: Web3ReactConnection) => getConnectionName(type)).join('-'),
[connections]
[connections],
)

return (
Expand Down

0 comments on commit 3f8446b

Please sign in to comment.