replace ensureOnce with simpleMemoize when getting master copies because connect provider might change and we have to use the new instance

This commit is contained in:
mmv 2019-10-25 16:52:49 +04:00
parent e8d54b6228
commit 8c1465c740
3 changed files with 16 additions and 10 deletions

View File

@ -3,7 +3,7 @@ import contract from 'truffle-contract'
import ProxyFactorySol from '@gnosis.pm/safe-contracts/build/contracts/ProxyFactory.json'
import GnosisSafeSol from '@gnosis.pm/safe-contracts/build/contracts/GnosisSafe.json'
import SafeProxy from '@gnosis.pm/safe-contracts/build/contracts/Proxy.json'
import { ensureOnce } from '~/utils/singleton'
import { simpleMemoize } from '~/components/forms/validator'
import { getWeb3 } from '~/logic/wallets/getWeb3'
import { calculateGasOf, calculateGasPrice } from '~/logic/wallets/ethTransactions'
import { ZERO_ADDRESS } from '~/logic/wallets/ethAddresses'
@ -27,8 +27,8 @@ const createProxyFactoryContract = (web3: any) => {
return proxyFactory
}
export const getGnosisSafeContract = ensureOnce(createGnosisSafeContract)
const getCreateProxyFactoryContract = ensureOnce(createProxyFactoryContract)
export const getGnosisSafeContract = simpleMemoize(createGnosisSafeContract)
const getCreateProxyFactoryContract = simpleMemoize(createProxyFactoryContract)
const instanciateMasterCopies = async () => {
const web3 = getWeb3()
@ -55,7 +55,7 @@ const createMasterCopies = async () => {
safeMaster = await GnosisSafe.new({ from: userAccount, gas: '7000000' })
}
export const initContracts = ensureOnce(process.env.NODE_ENV === 'test' ? createMasterCopies : instanciateMasterCopies)
export const initContracts = process.env.NODE_ENV === 'test' ? createMasterCopies : instanciateMasterCopies
export const getSafeMasterContract = async () => {
await initContracts()
@ -106,7 +106,7 @@ const cleanByteCodeMetadata = (bytecode: string): string => {
return bytecode.substring(0, bytecode.lastIndexOf(metaData))
}
export const validateProxy = async (safeAddress: string): boolean => {
export const validateProxy = async (safeAddress: string): Promise<boolean> => {
// https://solidity.readthedocs.io/en/latest/metadata.html#usage-for-source-code-verification
const web3 = getWeb3()
const code = await web3.eth.getCode(safeAddress)

View File

@ -56,18 +56,23 @@ export const getEtherScanLink = (type: 'address' | 'tx', value: string) => {
}etherscan.io/${type}/${value}`
}
let web3
export const getWeb3 = () => web3 || (window.web3 && new Web3(window.web3.currentProvider)) || (window.ethereum && new Web3(window.ethereum))
const getInfuraUrl = () => {
const isMainnet = process.env.REACT_APP_NETWORK === 'mainnet'
return `https://${isMainnet ? '' : 'rinkeby.'}infura.io:443/v3/${process.env.REACT_APP_INFURA_TOKEN}`
}
export const web3RO = new Web3(new Web3.providers.HttpProvider(getInfuraUrl()))
let web3 = web3RO
export const getWeb3 = () => web3
// With some wallets from web3connect you have to use their provider instance only for signing
// And our own one to fetch data
export const web3RO = new Web3(new Web3.providers.HttpProvider(getInfuraUrl()))
export const resetWeb3 = () => {
web3 = web3RO
}
const getProviderName: Function = (web3Provider): string => {
let name

View File

@ -2,7 +2,7 @@
import { createAction } from 'redux-actions'
import type { Dispatch as ReduxDispatch } from 'redux'
import { NOTIFICATIONS, showSnackbar } from '~/logic/notifications'
import { getWeb3 } from '~/logic/wallets/getWeb3'
import { getWeb3, resetWeb3 } from '~/logic/wallets/getWeb3'
export const REMOVE_PROVIDER = 'REMOVE_PROVIDER'
@ -17,5 +17,6 @@ export default (enqueueSnackbar: Function, closeSnackbar: Function) => (dispatch
web3.currentProvider.close()
}
resetWeb3()
dispatch(removeProvider())
}