exeos
Version:
A simple, maximally extensible, dependency minimized framework for building modern Ethereum dApps
168 lines (144 loc) • 5.24 kB
text/typescript
import { AbstractConnectorArguments, ConnectorUpdate } from '@web3-react/types'
import { AbstractConnector } from '@web3-react/abstract-connector'
import warning from 'tiny-warning'
import { SendReturnResult, SendReturn, Send } from './types'
function parseSendReturn(sendReturn: SendReturnResult | SendReturn): any {
return sendReturn.hasOwnProperty('result') ? sendReturn.result : sendReturn
}
export class NoEthereumProviderError extends Error {
public constructor() {
super()
this.name = this.constructor.name
this.message = 'No Ethereum provider was found on window.ethereum.'
}
}
export class UserRejectedRequestError extends Error {
public constructor() {
super()
this.name = this.constructor.name
this.message = 'The user rejected the request.'
}
}
export class InjectedConnector extends AbstractConnector {
constructor(kwargs: AbstractConnectorArguments) {
super(kwargs)
this.handleNetworkChanged = this.handleNetworkChanged.bind(this)
this.handleChainChanged = this.handleChainChanged.bind(this)
this.handleAccountsChanged = this.handleAccountsChanged.bind(this)
this.handleClose = this.handleClose.bind(this)
}
private handleChainChanged(chainId: string | number): void {
if (__DEV__) {
console.log("Handling 'chainChanged' event with payload", chainId)
}
this.emitUpdate({ chainId })
}
private handleAccountsChanged(accounts: string[]): void {
if (__DEV__) {
console.log("Handling 'accountsChanged' event with payload", accounts)
}
if (accounts.length === 0) {
this.emitDeactivate()
} else {
this.emitUpdate({ account: accounts[0] })
}
}
private handleClose(code: number, reason: string): void {
if (__DEV__) {
console.log("Handling 'close' event with payload", code, reason)
}
this.emitDeactivate()
}
private handleNetworkChanged(networkId: string | number): void {
if (__DEV__) {
console.log("Handling 'networkChanged' event with payload", networkId)
}
this.emitUpdate({ chainId: networkId })
}
public async activate(): Promise<ConnectorUpdate> {
if (!window.ethereum) {
throw new NoEthereumProviderError()
}
if (window.ethereum.on) {
window.ethereum.on('chainChanged', this.handleChainChanged)
window.ethereum.on('accountsChanged', this.handleAccountsChanged)
window.ethereum.on('close', this.handleClose)
window.ethereum.on('networkChanged', this.handleNetworkChanged)
}
if ((window.ethereum as any).isMetaMask) {
;(window.ethereum as any).autoRefreshOnNetworkChange = false
}
let account
try {
account = await (window.ethereum.send as Send)('eth_requestAccounts').then(
sendReturn => parseSendReturn(sendReturn)[0]
)
} catch (error) {
if ((error as any).code === 4001) {
throw new UserRejectedRequestError()
}
warning(false, 'eth_requestAccounts was unsuccessful, falling back to enable')
account = await window.ethereum.enable().then(sendReturn => parseSendReturn(sendReturn)[0])
}
return { provider: window.ethereum, account }
}
public async getProvider(): Promise<any> {
return window.ethereum
}
public async getChainId(): Promise<number | string> {
if (!window.ethereum) {
throw new NoEthereumProviderError()
}
try {
return await (window.ethereum.send as Send)('eth_chainId').then(parseSendReturn)
} catch {
warning(false, 'eth_chainId was unsuccessful, falling back to net_version')
try {
return await (window.ethereum.send as Send)('net_version').then(parseSendReturn)
} catch {
warning(false, 'net_version was unsuccessful, falling back to static properties')
return (
(window.ethereum as any).chainId ||
(window.ethereum as any).netVersion ||
(window.ethereum as any).networkVersion ||
(window.ethereum as any)._chainId
)
}
}
}
public async getAccount(): Promise<null | string> {
if (!window.ethereum) {
throw new NoEthereumProviderError()
}
try {
return (window.ethereum.send as Send)('eth_accounts').then(({ result: accounts }): string => accounts[0])
} catch {
warning(false, 'eth_accounts was unsuccessful, falling back to enable')
return window.ethereum.enable().then(sendReturn => parseSendReturn(sendReturn)[0])
}
}
public deactivate() {
if (window.ethereum && window.ethereum.removeListener) {
window.ethereum.removeListener('chainChanged', this.handleChainChanged)
window.ethereum.removeListener('accountsChanged', this.handleAccountsChanged)
window.ethereum.removeListener('close', this.handleClose)
window.ethereum.removeListener('networkChanged', this.handleNetworkChanged)
}
}
public async isAuthorized(): Promise<boolean> {
if (!window.ethereum) {
return false
}
try {
return await (window.ethereum.send as Send)('eth_accounts').then(sendReturn => {
if (parseSendReturn(sendReturn).length > 0) {
return true
} else {
return false
}
})
} catch {
return false
}
}
}