trufflesuite / truffle

:warning: The Truffle Suite is being sunset. For information on ongoing support, migration options and FAQs, visit the Consensys blog. Thank you for all the support over the years.
https://consensys.io/blog/consensys-announces-the-sunset-of-truffle-and-ganache-and-new-hardhat?utm_source=github&utm_medium=referral&utm_campaign=2023_Sep_truffle-sunset-2023_announcement_
MIT License
14.02k stars 2.32k forks source link

Suggestion: Support @truffle/contract browser load without "we will stop injecting web3 in Q4 2020" warning #3288

Open wbt opened 4 years ago

wbt commented 4 years ago

Issue

When loading @truffle/contract for browser usage, the following warning is triggered:

MetaMask: We will stop injecting web3 in Q4 2020. Please see this article for more information: https://medium.com/metamask/no-longer-injecting-web3-js-4a899ad6e59e

Steps to Reproduce

Create the following HTML file:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Truffle-contract loading test</title>
</head>
<body>
    This is just a test to see what happens when loading truffle-contract.<br>
    If you open the console, you will probably see a MetaMask warning
    "We will stop injecting web3 in Q4 2020."
    <script src="js/truffle-contract.js"></script>
</body>
</html>

Set up a local web server (e.g. with Node/Express) to serve this file and /js/truffle-contract.js. Access the file in a Metamask-enabled browser and open the console.

Expected Behavior

It should be possible to load this Javascript into memory without doing anything, so that the TruffleContract object is available, but there is no MetaMask warning unless/until the script attempts to access an injected global web3. This permits truffle-contract to be loaded in parallel with other scripts in initial page-load, instead of requiring waiting until after initializing web3 with a provider.

Actual Results

TruffleContract is trying to use the globally injected web3 object at the time of loading, triggering that warning.

Environment

gnidan commented 4 years ago

Yep! Should definitely not have that warning. Thanks for the issue @wbt!

eggplantzzz commented 4 years ago

I don't think that this should cause a problem once MetaMask stops injecting web3 though the message is annoying. I believe this message comes from Truffle's own web3 dependency which checks to see if there is a global web3 around. In the future I think this will not find a global web3 and not use it.

It looks like this should be more of an issue for Drizzle I would think?

wbt commented 4 years ago

Why does Truffle check to see if there is a global web3 around just when loading the Javascript file? It should not be doing anything at that time.

eggplantzzz commented 4 years ago

@truffle/contract has a dependency on web3. At some point web3 checks to see if there is a global provider. If you follow the stacktrace in the console it will lead you to web3 code.

fainashalts commented 3 years ago

Looking into this, we don't believe there will be an issue on the Truffle side from removing the warning. There may be problems from the Drizzle side of things. Do you have any thoughts @cds-amal?

wbt commented 3 years ago

I think the best solution to this is that if Web3 is looking for a global provider, that is what should be changed...loading a library module should just define things and not do more.