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

Using the helpers outside of Truffle (i.e. remove assumption of global web3) #35

Closed
cnasikas opened this issue May 2, 2019 · 12 comments · Fixed by #38
Closed

Using the helpers outside of Truffle (i.e. remove assumption of global web3) #35

cnasikas opened this issue May 2, 2019 · 12 comments · Fixed by #38

Comments

@cnasikas
Copy link

cnasikas commented May 2, 2019

I am trying to const oz = require('openzeppelin-test-helpers') but I am getting the following error:

/path/to/project/node_modules/openzeppelin-test-helpers/src/setup.js:2
const BN = web3.utils.BN;
           ^

ReferenceError: web3 is not defined

Why web3 is not imported inside setup.js ? Am I missing something obvious ?

@cnasikas
Copy link
Author

cnasikas commented May 2, 2019

Oh I see... You assume that we run truffle test and web3 is provided by truffle, right ?

I want to use the library as a standalone library. There are a lot of useful functions that can be used outside a testing environment. For example in a project I am working at the moment we have a solidity grader where students submit their solutions. The functionality provided by openzeppelin-test-helpers can be used by the grader to do various checks (same as testing) and decide whether the solution is correct or not.

@nventuro
Copy link
Contributor

nventuro commented May 2, 2019

Hello @cnasikas, thanks for reporting this!

I've considered supporting a standalone environment where either the user supplies web3 or helpers provides it (since there are so many incompatible versions out there), but there are some extra dependencies on truffle, particularly when it comes to decoding events (though this is something we could do ourselves to decouple that).

Is there any particular reason why you don't want/cannot use truffle? If I'm not mistaken, you can also run truffle exec on a script, and it will run with the full truffle environment, providing access to web3, the contract artifacts, etc. (though you will need to set up ganache by yourself, if using a local blockchain).

@dionyziz
Copy link

dionyziz commented May 2, 2019

@nventuro Thank you for your feedback. I am working with @cnasikas on this grader. We do not want to run it from within a bash script. Rather, our grader runs in a JS environment and we want to capture potential errors from within Javascript, without resorting to using shell()-like commands.

@nventuro
Copy link
Contributor

nventuro commented May 2, 2019

@dionyziz my bad for not being clear enough, truffle exec runs a JS script, not bash.

@dionyziz
Copy link

dionyziz commented May 2, 2019

@nventuro I'm not sure how we can use this. This grader is fired up from within a server-side express web service which has its own complex environment and is a long-running process behind an nginx. Could you point us to the right direction?

@frangio
Copy link
Contributor

frangio commented May 2, 2019

We should 100% support using this library outside of a Truffle project. I wasn't aware of this limitation.

@nventuro What parts depend on it?

@frangio frangio changed the title ReferenceError: web3 is not defined Using the helpers outside of Truffle (i.e. remove assumption of global web3) May 2, 2019
@cnasikas
Copy link
Author

cnasikas commented May 3, 2019

@nventuro @frangio Thank you for your feedback. I also think that the library should be supported outside Truffle if possible. There are a lot of use cases where the provided functionality by the library is needed. Think of ethernaut for example.

Thanks a lot!

@nventuro
Copy link
Contributor

nventuro commented May 3, 2019

@cnasikas @dionyziz would you expect the helpers to include web3, or for the user to provide it? The first approach has the advantage of us knowing which web3 version we're using, but we'd have to provide a way to set the web3 provider, which is a non-issue in the second one.

@frangio what if the require returned a function that receives a web3 instance, and we internally check for its version (web3.version)? Usage would look like

const { expectEvent, send, shouldFail } = require('openzeppelin-test-helpers')(web3);

@cnasikas
Copy link
Author

cnasikas commented May 4, 2019

@nventuro I think it is better the openzeppelin to provide web3, to have fine control on versions, and provide at the same time a way to set the provider.

For example:

const oz = require('openzeppelin-test-helpers')
const provider = new oz.Web3.providers.WebsocketProvider(provider)
const { expectEvent, send, shouldFail } = oz.init(provider)

Reading the code I saw that some modules do not need a provider and depend only on web-utils. Maybe that modules can be exported right away without the need of setting a provider.

Thanks a lot for your effort and time putting into this.

@frangio
Copy link
Contributor

frangio commented May 9, 2019

@cnasikas @dionyziz We're adding support for manually injecting a Web3 instance in #38, for situations like yours. It will allow you to write:

require('openzeppelin-test-helpers/configure')({ web3: ... });

Try it out if you'd like and let us know what you think of this!

@cnasikas
Copy link
Author

@frangio Thanks a lot! That was fast :). I will try it as soon as possible!

@cnasikas
Copy link
Author

I test it and for the moment everything works fine! Thanks a lot!

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 a pull request may close this issue.

4 participants