MetaMask / extension-provider

A module for allowing a WebExtension to access the web3 provider from an installed MetaMask instance
MIT License
67 stars 28 forks source link

How to receive events? #11

Closed Jack-Works closed 2 days ago

Jack-Works commented 3 years ago
ethereum.on('accountsChanged', handler: (accounts: Array<string>) => void);

https://docs.metamask.io/guide/ethereum-provider.html#accountschanged

But there is no ethereum object injected by MetaMask in the extension environment, how can I receive this kind of event?

Tedko commented 3 years ago

@danfinlay Many thx

danfinlay commented 3 years ago

Were you able to figure this out? Per the readme, you should be able to subscribe to events by constructing an ethereum provider like this:

const createMetaMaskProvider = require('metamask-extension-provider')

const provider = createMetaMaskProvider()

provider.on('accountsChanged', (accounts) => {

})
tong0x commented 2 years ago

Hi, I am trying to use the event listener "accountsChanged" as @danfinlay mentioned, but I'm not able to receive any events. I am subscribing to events by constructing an Ethereum provider like above in the frontend portion of my extension. Would you have any clue as to why the event might not be firing?

tong0x commented 2 years ago

Main goal of this is to ensure my own extension is updated to using the currently selected account address on Metamask

huosanghuakai commented 2 years ago

There are only four events (error,message,connect,disconnect)now, really hope you can add all evtnts to 'metamask-extension-provider'