Skip to content

Instantly share code, notes, and snippets.

@lex-world
Created February 20, 2022 08:28
Show Gist options
  • Save lex-world/06b95274c7e2914e60363a0f9eb3baf4 to your computer and use it in GitHub Desktop.
Save lex-world/06b95274c7e2914e60363a0f9eb3baf4 to your computer and use it in GitHub Desktop.
import detectEthereumProvider from '@metamask/detect-provider'
const provider = await detectEthereumProvider()
if (provider) {
console.log('Ethereum successfully detected!')
// From now on, this should always be true:
// provider === window.ethereum
// Access the decentralized web!
// Legacy providers may only have ethereum.sendAsync
const chainId = await provider.request({
method: 'eth_chainId'
})
} else {
// if the provider is not detected, detectEthereumProvider resolves to null
console.error('Please install MetaMask!', error)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment