Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 61 additions & 83 deletions components/entry/Entry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,92 +27,14 @@ export default function Entry(props: Props) {

const [playMusic, setPlayMusic] = useState(false)
const [playSound, setPlaySound] = useState(false)
const [nameOfChain, setNameOfChain] = useState('Binance Smart Chain')
const [nameOfChain, setNameOfChain] = useState('Harmony Mainnet')
const [openModalAddWallet, setOpenModalAddWallet] = useState(false)
//deploy cloudfare 2
useEffect(() => {
try {
const connectWallet = async () => {
if (window.ethereum) {
const chainId = window?.ethereum?.chainId
setNameOfChain(chainName[chainId] || '')
if (
chainId === '0x63564c40' ||
chainId === '0x6357d2e0' ||
chainId === '0x61'
) {
window.ethereum
.request({ method: 'eth_requestAccounts' })
.then(() => {
checkIsConnected(true)
})
.catch(() => {
checkIsConnected(false)
})
} else {
window.ethereum
.request({ method: 'eth_requestAccounts' })
.then(() => {
checkIsConnected(true)
window.ethereum
.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: '0x63564c40' }],
})
.then(() => {
checkIsConnected(true)
})
.catch((error) => {
window.ethereum.request({
method: 'wallet_addEthereumChain',
params: [
{
chainId: '0x63564c40',
chainName: 'Harmony Mainnet',
rpcUrls: ['https://api.harmony.one'],
nativeCurrency: {
name: 'ONE',
symbol: 'ONE',
decimals: 18,
},
},
],
})
})
})
.catch(() => {
checkIsConnected(false)
})
}
}
}
connectWallet()
checkTokenWasAdded()
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
const subscribeChainChanged = window.ethereum.on(
'chainChanged',
async () => {
setTimeout(async () => {
await connectWallet()
}, 1000)
}
)
const subscribeWalletChanged = window.ethereum.on(
'accountsChanged',
async () => {
setTimeout(async () => {
await checkTokenWasAdded()
}, 1000)
}
)
return () => {
if (typeof subscribeChainChanged === 'function') {
subscribeChainChanged()
}
if (typeof subscribeWalletChanged === 'function') {
subscribeWalletChanged()
}
}
} catch (error: unknown) {
setOpenModalAddWallet(true)
}
Expand Down Expand Up @@ -144,6 +66,61 @@ export default function Entry(props: Props) {
}
}
}

const connectWallet = async () => {
if (window.ethereum) {
const chainId = window?.ethereum?.chainId
setNameOfChain(chainName[chainId] || '')
if (
chainId === '0x63564c40' ||
chainId === '0x6357d2e0' ||
chainId === '0x61'
) {
window.ethereum
.request({ method: 'eth_requestAccounts' })
.then(() => {
checkIsConnected(true)
})
.catch(() => {
checkIsConnected(false)
})
} else {
window.ethereum
.request({ method: 'eth_requestAccounts' })
.then(() => {
// checkIsConnected(true)
window.ethereum
.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: '0x63564c40' }],
})
.then(() => {
checkIsConnected(true)
})
.catch((error) => {
window.ethereum.request({
method: 'wallet_addEthereumChain',
params: [
{
chainId: '0x63564c40',
chainName: 'Harmony Mainnet',
rpcUrls: ['https://api.harmony.one'],
nativeCurrency: {
name: 'ONE',
symbol: 'ONE',
decimals: 18,
},
},
],
})
})
})
.catch(() => {
checkIsConnected(false)
})
}
}
}
const onCloseModal = () => {
setOpenModalAddWallet(false)
}
Expand All @@ -161,14 +138,15 @@ export default function Entry(props: Props) {
setPlayMusic(!playMusic)
}
}

const switchWallet = () => {
connectWallet()
}
return (
<div className={styles.main}>
<img src={'/images/common/gameLogo.png'} alt={'logo'} />
<Link href={'/'} passHref>
<Button
style={_styles.buttonStyle}
// onClick={connectWallet}
>
<Button style={_styles.buttonStyle} onClick={switchWallet}>
<Text style={_styles.buttonText}>PLAY</Text>
</Button>
</Link>
Expand Down
1 change: 0 additions & 1 deletion components/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export default function Layout({ children, home }) {
const [connected, setConnected] = useState(false)

const checkIsConnected = useCallback((status) => {
console.log(`Connected change from ${connected} to ${status}`)
setConnected(status)
}, [])

Expand Down