import type { Actions, Provider, ProviderConnectInfo, ProviderRpcError } from '@web3-react/types' import { Connector } from '@web3-react/types' function parseChainId(chainId: string) { return Number.parseInt(chainId, 16) } export class EIP1193 extends Connector { provider: Provider constructor(actions: Actions, provider: Provider, connectEagerly = true) { super(actions) this.provider = provider this.provider.on('connect', ({ chainId }: ProviderConnectInfo): void => { this.actions.update({ chainId: parseChainId(chainId) }) }) this.provider.on('disconnect', (error: ProviderRpcError): void => { this.actions.reportError(error) }) this.provider.on('chainChanged', (chainId: string): void => { this.actions.update({ chainId: parseChainId(chainId) }) }) this.provider.on('accountsChanged', (accounts: string[]): void => { this.actions.update({ accounts }) }) if (connectEagerly) { const cancelActivation = this.actions.startActivation() Promise.all([ this.provider.request({ method: 'eth_chainId' }) as Promise, this.provider.request({ method: 'eth_accounts' }) as Promise, ]) .then(([chainId, accounts]) => { this.actions.update({ chainId: parseChainId(chainId), accounts }) }) .catch((error) => { console.debug('Could not connect eagerly', error) cancelActivation() }) } } public async activate(): Promise { this.actions.startActivation() return Promise.all([ this.provider.request({ method: 'eth_chainId' }) as Promise, this.provider.request({ method: 'eth_requestAccounts' }) as Promise, ]) .then(([chainId, accounts]) => { this.actions.update({ chainId: parseChainId(chainId), accounts }) }) .catch((error: Error) => { this.actions.reportError(error) }) } }