Skip to content

Instantly share code, notes, and snippets.

@rekmarks
Last active May 7, 2020 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rekmarks/06999f88fe6ab0cd1d71ac7cd2b2ac93 to your computer and use it in GitHub Desktop.
Save rekmarks/06999f88fe6ab0cd1d71ac7cd2b2ac93 to your computer and use it in GitHub Desktop.
Ethereum Provider Initialization Event

New event dispatched on window: ethereum#initialized

Event name inspired by JSDoc @event tag: https://jsdoc.app/tags-event.html

if (window.ethereum) {

  handleEthereum()

} else {

  window.addEventListener(
    'ethereum#initialized',
    handleEthereum,
    { once: true }
  )

  // If the event is not dispatched by the end of the timeout,
  // the user probably doesn't have MetaMask installed.
  setTimeout(handleEthereum, 3000) // 3 seconds
}

function handleEthereum () {
  const { ethereum } = window
  if (ethereum && ethereum.isMetaMask) {
    console.log('Ethereum successfully detected!')
    // Do work...
  } else {
    console.log('Please install MetaMask!')
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment