Avoid constant creation of new web3 object (#1171)

* Avoid constant creation of new web3 object

* rollback web3 version, small type fixes in ConnectButton

* Use web3 instance in getProviderInfo

Co-authored-by: Mikhail Mikheev <mmvsha73@gmail.com>
This commit is contained in:
Daniel Sanchez 2020-08-03 11:29:17 +02:00 committed by GitHub
parent 86cbd94b0c
commit f374315a5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 19 additions and 15 deletions

View File

@ -56,7 +56,7 @@ export const onboard = Onboard({
],
})
export const onboardUser = async () => {
export const onboardUser = async (): Promise<boolean> => {
// before calling walletSelect you want to check if web3 has been instantiated
// which indicates that a wallet has already been selected
// and web3 has been instantiated with that provider
@ -65,7 +65,7 @@ export const onboardUser = async () => {
return walletSelected && onboard.walletCheck()
}
const ConnectButton = (props) => (
const ConnectButton = (props): React.ReactElement => (
<Button
color="primary"
minWidth={140}

View File

@ -95,14 +95,10 @@ const isSmartContractWallet = async (web3Provider: Web3, account: string): Promi
return contractCode.replace(EMPTY_DATA, '').replace(/0/g, '') !== ''
}
export const getProviderInfo = async (
web3Provider: string | Provider,
providerName = 'Wallet',
): Promise<ProviderProps> => {
web3 = new Web3(web3Provider)
const account = await getAccountFrom(web3)
const network = await getNetworkIdFrom(web3)
const smartContractWallet = await isSmartContractWallet(web3, account)
export const getProviderInfo = async (web3Instance: Web3, providerName = 'Wallet'): Promise<ProviderProps> => {
const account = await getAccountFrom(web3Instance)
const network = await getNetworkIdFrom(web3Instance)
const smartContractWallet = await isSmartContractWallet(web3Instance, account)
const hardwareWallet = isHardwareWallet(providerName)
const available = account !== null

View File

@ -51,7 +51,7 @@ const handleProviderNotification = (provider, dispatch) => {
export default (providerName: string) => async (dispatch: Dispatch): Promise<void> => {
const web3 = getWeb3()
const providerInfo = await getProviderInfo(web3.currentProvider, providerName)
const providerInfo = await getProviderInfo(web3, providerName)
await handleProviderNotification(providerInfo, dispatch)
processProviderResponse(dispatch, providerInfo)
}

View File

@ -37,7 +37,7 @@ const providerWatcherMware = (store) => (next) => async (action) => {
watcherInterval = setInterval(async () => {
const web3 = getWeb3()
const providerInfo = await getProviderInfo(web3.currentProvider)
const providerInfo = await getProviderInfo(web3)
const networkChanged = currentProviderProps.network !== providerInfo.network

View File

@ -72,7 +72,8 @@ export const aMinedSafe = async (
threshold = 1,
name = 'Safe Name',
) => {
const provider = await getProviderInfo(window.web3?.currentProvider || 'ws://localhost:8545')
const web3 = getWeb3()
const provider = await getProviderInfo(web3)
const walletRecord = makeProvider(provider)
store.dispatch(addProvider(walletRecord))

View File

@ -31,7 +31,8 @@ afterAll(() => {
})
const renderOpenSafeForm = async (localStore) => {
const provider = await getProviderInfo(window.web3.currentProvider)
const web3 = getWeb3()
const provider = await getProviderInfo(web3)
const walletRecord = makeProvider(provider)
localStore.dispatch(addProvider(walletRecord))

View File

@ -29,7 +29,8 @@ afterAll(() => {
})
const renderLoadSafe = async (localStore) => {
const provider = await getProviderInfo(window.web3.currentProvider || 'ws://localhost:8545')
const web3 = getWeb3()
const provider = await getProviderInfo(web3)
const walletRecord = makeProvider(provider)
localStore.dispatch(addProvider(walletRecord))

View File

@ -2327,6 +2327,11 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.17.28.tgz#0e36d718a29355ee51cec83b42d921299200f6d9"
integrity sha512-dzjES1Egb4c1a89C7lKwQh8pwjYmlOAG9dW1pBgxEk57tMrLnssOfEthz8kdkNaBd7lIqQx7APm5+mZ619IiCQ==
"@types/node@^10.12.18":
version "10.17.28"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.17.28.tgz#0e36d718a29355ee51cec83b42d921299200f6d9"
integrity sha512-dzjES1Egb4c1a89C7lKwQh8pwjYmlOAG9dW1pBgxEk57tMrLnssOfEthz8kdkNaBd7lIqQx7APm5+mZ619IiCQ==
"@types/node@^10.3.2":
version "10.17.27"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.17.27.tgz#391cb391c75646c8ad2a7b6ed3bbcee52d1bdf19"