Skip to content

Commit

Permalink
Adding missing provider value
Browse files Browse the repository at this point in the history
  • Loading branch information
hide-on-bush-x committed Jan 11, 2023
1 parent 3bd9c2f commit 5c20544
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
5 changes: 5 additions & 0 deletions src/common/helpers/provider/masa-context.tsx
Expand Up @@ -51,6 +51,8 @@ export interface MasaShape {
wallet: string;
endDate: string;
} | null;
missingProvider?: boolean;
setMissingProvider?: (value: boolean) => void;
}

export const MasaContextProvider = ({
Expand All @@ -62,6 +64,7 @@ export const MasaContextProvider = ({
}: MasaContextProviderProps) => {
const [masaInstance, setMasaInstance] = useState<Masa | null>(null);
const [provider, setProvider] = useState<any>(null);
const [missingProvider, setMissingProvider] = useState<boolean>();

const [isModalOpen, setModalOpen] = useState(false);

Expand Down Expand Up @@ -300,6 +303,8 @@ export const MasaContextProvider = ({
logginLoading,
allowedForAllowlist,
allowlistInfo,
missingProvider,
setMissingProvider,
};

return (
Expand Down
33 changes: 22 additions & 11 deletions src/common/helpers/provider/use-metamask.ts
Expand Up @@ -2,22 +2,33 @@ import { ethers } from 'ethers';
import { useCallback, useEffect, useMemo } from 'react';
import { useMasa } from './use-masa';

const DEFAULT_RPC = 'https://rpc.ankr.com/eth_goerli';

export const useMetamask = ({ disable }: { disable?: boolean }) => {
const { setProvider, masa } = useMasa();
const { setProvider, setMissingProvider, masa } = useMasa();

//@ts-ignore
const provider = useMemo(() => {
return typeof window !== 'undefined'
? //@ts-ignore
window?.ethereum
? //@ts-ignore
new ethers.providers.Web3Provider(window?.ethereum)
: new ethers.providers.JsonRpcProvider(DEFAULT_RPC)
: null;
if (typeof window !== 'undefined') {
//@ts-ignore
if (typeof window?.ethereum !== 'undefined') {
//@ts-ignore
return new ethers.providers.Web3Provider(window?.ethereum);
} else {
return null;
}
} else {
return null;
}
}, []);

useEffect(() => {
if (setMissingProvider) {
if (provider) {
setMissingProvider(false);
} else {
setMissingProvider(true);
}
}
}, [provider, setMissingProvider]);

const accountChangedHandler = useCallback(
async (newAccount) => {
if (setProvider) setProvider(newAccount);
Expand Down

0 comments on commit 5c20544

Please sign in to comment.