extension-provider icon indicating copy to clipboard operation
extension-provider copied to clipboard

How to receive events?

Open Jack-Works opened this issue 5 years ago • 5 comments

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?

Jack-Works avatar Sep 01 '20 06:09 Jack-Works

@danfinlay Many thx

Tedko avatar Sep 01 '20 06:09 Tedko

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) => {

})

danfinlay avatar Sep 24 '20 18:09 danfinlay

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 avatar Feb 03 '22 09:02 tong0x

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

tong0x avatar Feb 03 '22 10:02 tong0x

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

huosanghuakai avatar May 27 '22 09:05 huosanghuakai