Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

first step to future-proof web3 init #584

Closed
wants to merge 3 commits into from
Closed

Conversation

lsaether
Copy link
Contributor

Following https://medium.com/metamask/https-medium-com-metamask-breaking-change-injecting-web3-7722797916a8

Will have to manage the async initialization of web3.

@lsaether lsaether requested a review from e00dan August 28, 2018 08:04
@lsaether
Copy link
Contributor Author

fixes #595

@lsaether lsaether requested a review from kosecki123 August 28, 2018 10:14
@kosecki123
Copy link
Contributor

Can we switch the async option today?

@lsaether
Copy link
Contributor Author

lsaether commented Aug 28, 2018

@kosecki123 At this time it appears that the PR has yet to be merged MetaMask/metamask-extension#4703


window.postMessage({ type: 'ETHEREUM_PROVIDER_REQUEST' }, '*');

// If the MetaMask message is not received we set web3 to the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not as simple as that I believe. In reality message will be received asynchronously and by the time it will be received, web3 will be set to fallback provider (L319). This should be somehow promisified or separated through some checks from using L319 fallback provider.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea correct. I'm not sure what the best way to handle the async connection to MetaMask is though. This code just assumes it will replace the fallback web3 with MetaMask web3 when it is approved. We could have all the read-only operations available using back-up provider in the same way we have now and just wait to allow scheduling until MetaMask is unlocked.

With this code, if the message is received async, will the web3 object update and refresh the page?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First, we should promisify web3
so, L325 would be something like this.web3 = await web3

@adibas03
Copy link
Member

adibas03 commented Aug 29, 2018

@lsaether @bagaric
Consider

	this.web3HTTP = new Web3(new Web3.providers.HttpProvider(process.env.HTTP_PROVIDER));
	if (!web3) {
		this.promiseWeb3 = new Promise( (resolve, reject) => {
		  if (typeof window.web3 !== 'undefined') {
			// Current MetaMask flow
			this.connectedToMetaMask = true;
			resolve(new Web3(window.web3.currentProvider));
		  } else {
			// Future MetaMask flow
			window.addEventListener('message', ({ data }) => {
			  if (data && data.type && data.type === 'ETHEREUM_PROVIDER_SUCCESS') {
				this.connectedToMetaMask = true;
				resolve(new Web3(window.ethereum));
			  }
			});

			window.postMessage({ type: 'ETHEREUM_PROVIDER_REQUEST' }, '*');

			// If the MetaMask message is not received we set web3 to the
			// HTTP provider.
			setTimeout( () => {
				if (!this.connectedToMetaMask) {
					resolve(null)
					this.connectedToMetaMask = false;
				}
			}, 2000)
		  }
	  });
	  waitOnWeb3();
    }
	...
	
	waitOnWeb3 async () {
		const web3 = await this.promiseWeb3;
		this.web3 = web3;
		Object.assign(this, web3);
	}

With this,
isWeb3 = this.web3 !== null

@e00dan e00dan closed this Nov 21, 2018
@e00dan e00dan deleted the fix/future-proof-web3 branch November 21, 2018 13:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants