Identify the Safe web3 provider

This commit is contained in:
Germán Martínez 2019-05-16 20:07:33 +02:00
parent a5db2d4ed6
commit bbc738d29c
2 changed files with 24 additions and 8 deletions

View File

@ -12,6 +12,7 @@ export const ETHEREUM_NETWORK = {
}
export const WALLET_PROVIDER = {
SAFE: 'SAFE',
METAMASK: 'METAMASK',
PARITY: 'PARITY',
REMOTE: 'REMOTE',
@ -36,12 +37,23 @@ export const openTxInEtherScan = (tx: string, network: string) => `https://${net
export const getEtherScanLink = (address: string, network: string) => `https://${network}.etherscan.io/address/${address}`
let web3
export const getWeb3 = () => web3 || new Web3(window.web3.currentProvider)
export const getWeb3 = () => web3
const isMetamask: Function = (web3Provider): boolean => {
const isMetamaskConstructor = web3Provider.currentProvider.constructor.name === 'MetamaskInpageProvider'
const getProviderName: Function = (web3Provider): boolean => {
let name
return isMetamaskConstructor || web3Provider.currentProvider.isMetaMask
switch (web3Provider.currentProvider.constructor.name) {
case 'SafeWeb3Provider':
name = WALLET_PROVIDER.SAFE
break
case 'MetamaskInpageProvider':
name = WALLET_PROVIDER.METAMASK
break
default:
name = 'UNKNOWN'
}
return name
}
const getAccountFrom: Function = async (web3Provider): Promise<string | null> => {
@ -76,7 +88,7 @@ export const getProviderInfo: Function = async (): Promise<ProviderProps> => {
web3 = new Web3(web3Provider)
const name = isMetamask(web3) ? WALLET_PROVIDER.METAMASK : 'UNKNOWN'
const name = getProviderName(web3)
const account = await getAccountFrom(web3)
const network = await getNetworkIdFrom(web3)

View File

@ -9,15 +9,19 @@ const FrameDecorator = story => <div className={styles.frame}>{story()}</div>
storiesOf('Routes /welcome', module)
.addDecorator(FrameDecorator)
.add('Welcome with Gnosis Safe connected', () => {
const provider = select('Status by Provider', ['', 'UNKNOWN', 'SAFE', 'METAMASK', 'PARITY'], 'SAFE')
return <Component provider={provider} fetchProvider={() => {}} />
})
.add('Welcome with Metamask connected', () => {
const provider = select('Status by Provider', ['', 'UNKNOWN', 'METAMASK', 'PARITY'], 'METAMASK')
const provider = select('Status by Provider', ['', 'UNKNOWN', 'SAFE', 'METAMASK', 'PARITY'], 'METAMASK')
return <Component provider={provider} fetchProvider={() => {}} />
})
.add('Welcome with unknown wallet', () => {
const provider = select('Status by Provider', ['', 'UNKNOWN', 'METAMASK', 'PARITY'], 'UNKNOWN')
const provider = select('Status by Provider', ['', 'UNKNOWN', 'SAFE', 'METAMASK', 'PARITY'], 'UNKNOWN')
return <Component provider={provider} fetchProvider={() => {}} />
})
.add('Welcome without wallet connected', () => {
const provider = select('Status by Provider', ['', 'UNKNOWN', 'METAMASK', 'PARITY'], '')
const provider = select('Status by Provider', ['', 'UNKNOWN', 'SAFE', 'METAMASK', 'PARITY'], '')
return <Component provider={provider} fetchProvider={() => {}} />
})