Skip to content

Commit

Permalink
Merge pull request #368 from P4-Games/alpha-fine-tunning
Browse files Browse the repository at this point in the history
[feat] changed alpha correct network to only sepolia
  • Loading branch information
dappsar authored Jul 4, 2024
2 parents 82366cb + 347bf66 commit 643107d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions src/context/Web3Context.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -114,13 +115,29 @@ function Web3ContextProvider({ children }) {
)
})

const isValidNetworkForAlpha = () => {
const network = NETWORKS['sepolia']
return (
network.config.chainId === chainIdHex
)
}

if (isValidNetwork) {
connectContracts(web3Provider.getSigner())
setIsValidNetwork(true)
} else {
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 })
Expand All @@ -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
Expand Down Expand Up @@ -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('')
}

Expand All @@ -269,13 +310,21 @@ 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')
const signer = provider.getSigner()
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
Expand All @@ -293,6 +342,7 @@ function Web3ContextProvider({ children }) {
web3Error,
isConnected,
isValidNetwork,
isValidNetworkForAlpha,
enabledNetworkNames,
connectWallet,
disconnectWallet,
Expand Down
17 changes: 9 additions & 8 deletions src/sections/Alpha/AlphaMain.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const AlphaMain = () => {
connectWallet,
isConnected,
isValidNetwork,
isValidNetworkForAlpha,
enabledNetworkNames
} = useWeb3Context()
const [showRules, setShowRules] = useState(false)
Expand All @@ -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 = []
Expand All @@ -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', {
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -629,10 +630,10 @@ const AlphaMain = () => {
{t('connect_wallet')}
</button>
)}
{isConnected && !isValidNetwork && (
{isConnected && !isValidNetworkForAlpha && (
<div className='invalid__network__div'>
<span className='invalid__network'>
{t('account_invalid_network').replace('{NETWORKS}', enabledNetworkNames)}
{t('account_invalid_network').replace('{NETWORKS}', 'sepolia')}
</span>
</div>
)}
Expand All @@ -650,11 +651,11 @@ const AlphaMain = () => {
return (
<div className='alpha_main'>
<div className='alpha'>
{(!isConnected || !isValidNetwork) && <NotConnected />}
{(!isConnected || !isValidNetworkForAlpha) && <NotConnected />}

{showRules && <Rules type='alpha' setShowRules={setShowRules} />}

{isConnected && isValidNetwork && alphaContract && seasonNames && (
{isConnected && isValidNetworkForAlpha && alphaContract && seasonNames && (
<div
className={
showMain
Expand Down

0 comments on commit 643107d

Please sign in to comment.