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

Fix: ensure using this.provider as the context this #3721

Merged
merged 2 commits into from
Oct 5, 2020

Conversation

chendatony31
Copy link
Contributor

Description

The bug is brought in this PR: #3649

It may cause some problems when calling provider.request. As the context this maybe window sometimes, so we should make sure the context is the provider.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I have selected the correct base branch.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation.
  • My changes generate no new warnings.
  • Any dependent changes have been merged and published in downstream modules.
  • I ran npm run dtslint with success and extended the tests and types if necessary.
  • I ran npm run test:unit with success.
  • I ran npm run test:cov and my test cases cover all the lines and branches of the added code.
  • I ran npm run build and tested dist/web3.min.js in a browser.
  • I have tested my code on the live network.
  • I have checked the Deploy Preview and it looks correct.
  • I have updated the CHANGELOG.md file in the root folder.

@coveralls
Copy link

coveralls commented Sep 21, 2020

Coverage Status

Coverage remained the same at 80.373% when pulling 5e19a84 on chendatony31:1.x into 9b92aac on ethereum:1.x.

@chendatony31 chendatony31 changed the title fix: ensure using this.provider as the context this Fix: ensure using this.provider as the context this Sep 21, 2020
@ryanio
Copy link
Collaborator

ryanio commented Sep 22, 2020

Is it really necessary for the context to be provider? It could be, but I don't think it should be relied on like that. Could you adjust your provider code to bind to the scope as explicitly needed?

@chendatony31
Copy link
Contributor Author

Is it really necessary for the context to be provider? It could be, but I don't think it should be relied on like that. Could you adjust your provider code to bind to the scope as explicitly needed?

Yeah, We've got a problem, and now I have to detect the context whether is the window or the provider. it may work, but not cool.

More discussion may check here: #3649 (comment)

@GregTheGreek
Copy link
Contributor

This makes sense but will need to test it out later today and probably add a testcase for this

@Velenir
Copy link
Contributor

Velenir commented Sep 23, 2020

Here's a codesandbox with a reproduction of this issue with @walletconnect/web3-provider

Copy link
Contributor

@frankiebee frankiebee left a comment

Choose a reason for hiding this comment

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

this looks good to me, binding provider context is relatively harmless in this situation and good practice for this style code. it's a little strange though because callbackifiy should preserve the this but maybe due to the changes in the pr mentioned that has change.

Copy link

@vikmeup vikmeup left a comment

Choose a reason for hiding this comment

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

Did you consider to write a test to avoid such issues in the future?

@sebsobseb
Copy link

sebsobseb commented Oct 2, 2020

Any idea when this will get merged and released with tag 1.3.1 ?

@GregTheGreek GregTheGreek merged commit 4615f2d into web3:1.x Oct 5, 2020
@chendatony31 chendatony31 deleted the 1.x branch October 9, 2020 12:23
a0ngo added a commit to a0ngo/starkex-eth that referenced this pull request Aug 4, 2022
Update to resolve web3-core-requestmanager issue reported in web3/web3.js#3721

Assume a provider is passed to DydxClient, where the request function uses the 'this' keyword (for example to ensure initialization of the provider, perhaps fetching some data).
When calling functions, such as:
`client.eth.exchange.deposit(....)`

The flow will eventually reach:
'RequestManager.send'
In this, when calling the provider.request, we do not bind the provider, thus the value of 'this' is undefined, and any call to 'this' will result in an error due to undefined value.

The fix will add the '.bind(this.provider)' which is currently missing, and make the 'this' value to be defined to the provider initially provided to the client.
a0ngo added a commit to a0ngo/starkex-eth that referenced this pull request Aug 17, 2022
Update to resolve web3-core-requestmanager issue reported in web3/web3.js#3721

Assume a provider is passed to DydxClient, where the request function uses the 'this' keyword (for example to ensure initialization of the provider, perhaps fetching some data).
When calling functions, such as:
`client.eth.exchange.deposit(....)`

The flow will eventually reach:
'RequestManager.send'
In this, when calling the provider.request, we do not bind the provider, thus the value of 'this' is undefined, and any call to 'this' will result in an error due to undefined value.

The fix will add the '.bind(this.provider)' which is currently missing, and make the 'this' value to be defined to the provider initially provided to the client.
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.

None yet

8 participants