diff --git a/package-lock.json b/package-lock.json index 2abd4c19..0aaf3ba4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8702,9 +8702,9 @@ } }, "node_modules/caniuse-lite": { - "version": "1.0.30001559", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001559.tgz", - "integrity": "sha512-cPiMKZgqgkg5LY3/ntGeLFUpi6tzddBNS58A4tnTgQw1zON7u2sZMU7SzOeVH4tj20++9ggL+V6FDOFMTaFFYA==", + "version": "1.0.30001639", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001639.tgz", + "integrity": "sha512-eFHflNTBIlFwP2AIKaYuBQN/apnUoKNhBdza8ZnW/h2di4LCZ4xFqYlxUxo+LQ76KFI1PGcC1QDxMbxTZpSCAg==", "funding": [ { "type": "opencollective", diff --git a/src/context/Web3Context.js b/src/context/Web3Context.js index b0342576..a911e3e6 100644 --- a/src/context/Web3Context.js +++ b/src/context/Web3Context.js @@ -26,6 +26,7 @@ function Web3ContextProvider({ children }) { const [web3Error, setWeb3Error] = useState('') const [wallets, setWallets] = useState(null) const [isValidNetwork, setIsValidNetwork] = useState(false) + const [isValidNetworkForAlpha, setIsValidNetworkForAlpha] = useState(false) const [daiContract, setDaiContract] = useState(null) const [alphaContract, setAlphaContract] = useState(null) const [gammaPacksContract, setGammaPacksContract] = useState(null) @@ -114,6 +115,13 @@ function Web3ContextProvider({ children }) { ) }) + const isValidNetworkForAlpha = () => { + const network = NETWORKS['sepolia'] + return ( + network.config.chainId === chainIdHex + ) + } + if (isValidNetwork) { connectContracts(web3Provider.getSigner()) setIsValidNetwork(true) @@ -121,6 +129,15 @@ function Web3ContextProvider({ children }) { setIsValidNetwork(false) setWeb3Error('account_invalid_network') } + + if(isValidNetworkForAlpha()) { + connectContractsForAlpha(web3Provider.getSigner()) + setIsValidNetworkForAlpha(true) + } else { + setIsValidNetworkForAlpha(false) + setWeb3Error('account_invalid_network') + } + return [web3Provider, accountAddress] } catch (e) { console.error({ e }) @@ -145,6 +162,11 @@ function Web3ContextProvider({ children }) { return network ? network : null } + function getCurrentNetworkForAlpha() { + const network = NETWORKS['sepolia'].config.chainId === decToHex(chainId) + return network ? NETWORKS['sepolia'] : null + } + function connectContracts(_signer) { try { const _contracts = getCurrentNetwork().contracts @@ -239,10 +261,29 @@ function Web3ContextProvider({ children }) { } } + function connectContractsForAlpha (_signer) { + try { + const _contracts = getCurrentNetworkForAlpha().contracts + + const daiContractInstance = new ethers.Contract(_contracts.daiAddress, daiAbi.abi, _signer) + + const alphaContractInstance = new ethers.Contract( + _contracts.alphaAddress, + alphaAbi.abi, + _signer + ) + setDaiContract(daiContractInstance) + setAlphaContract(alphaContractInstance) + } catch (e) { + console.error({ e }) + } + } + async function disconnectWallet() { disconnect() setWallets(null) setIsValidNetwork(false) + setIsValidNetworkForAlpha(false) setWeb3Error('') } @@ -269,6 +310,7 @@ function Web3ContextProvider({ children }) { setWeb3Error('account_invalid_network') const _chanIdHex = decToHex(newChain) setIsValidNetwork(false) + setIsValidNetworkForAlpha(false) if (enabledNetworkChainIds.includes(_chanIdHex)) { const provider = new ethers.providers.Web3Provider(window.ethereum, 'any') @@ -276,6 +318,13 @@ function Web3ContextProvider({ children }) { connectContracts(signer) setIsValidNetwork(true) } + + if(_chanIdHex === NETWORKS['sepolia'].config.chainId) { + const provider = new ethers.providers.Web3Provider(window.ethereum, 'any') + const signer = provider.getSigner() + connectContractsForAlpha(signer) + setIsValidNetworkForAlpha(true) + } }) } }, []) //eslint-disable-line react-hooks/exhaustive-deps @@ -293,6 +342,7 @@ function Web3ContextProvider({ children }) { web3Error, isConnected, isValidNetwork, + isValidNetworkForAlpha, enabledNetworkNames, connectWallet, disconnectWallet, diff --git a/src/sections/Alpha/AlphaMain.jsx b/src/sections/Alpha/AlphaMain.jsx index 670c4c2e..fcc052f0 100644 --- a/src/sections/Alpha/AlphaMain.jsx +++ b/src/sections/Alpha/AlphaMain.jsx @@ -67,6 +67,7 @@ const AlphaMain = () => { connectWallet, isConnected, isValidNetwork, + isValidNetworkForAlpha, enabledNetworkNames } = useWeb3Context() const [showRules, setShowRules] = useState(false) @@ -76,7 +77,7 @@ const AlphaMain = () => { const fetchAlbums = async () => { try { - if (!walletAddress || !isValidNetwork || !alphaContract || !seasonNames) return + if (!walletAddress || !isValidNetworkForAlpha || !alphaContract || !seasonNames) return startLoading() let albumsArr = [] @@ -100,12 +101,12 @@ const AlphaMain = () => { useEffect(() => { fetchAlbums() - }, [isValidNetwork, albums]) //eslint-disable-line react-hooks/exhaustive-deps + }, [isValidNetworkForAlpha, albums]) //eslint-disable-line react-hooks/exhaustive-deps useEffect(() => { fetchSeasonData() setShowMain(false) - }, [walletAddress, isValidNetwork]) //eslint-disable-line react-hooks/exhaustive-deps + }, [walletAddress, isValidNetworkForAlpha]) //eslint-disable-line react-hooks/exhaustive-deps useEffect(() => { swiper = new Swiper('.swiper-container', { @@ -200,7 +201,7 @@ const AlphaMain = () => { const fetchSeasonData = async () => { try { - if (!walletAddress || !isValidNetwork || !alphaContract) return + if (!walletAddress || !isValidNetworkForAlpha || !alphaContract) return startLoading() let seasonData = await alphaContract.getSeasonData() @@ -629,10 +630,10 @@ const AlphaMain = () => { {t('connect_wallet')} )} - {isConnected && !isValidNetwork && ( + {isConnected && !isValidNetworkForAlpha && (