You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
I've asked for help in the Truffle Gitter before filing this issue.
Issue
I am currently building the frontend to interact with a contract. I have read that the "artifacts.require" function isn't meant to be used outside of a truffle testing environment. So be able to test my contract properly, I would like to have my testing enviroment work the exact way as my frontend. Therefore, I am setting up a test file without using artifact.require, so only importing a JSON file with require().
When using artifacts, I don't need to require web3 and pass a provider, I assume that is done automatically , and then I can deploy and call any function from the contract, like written below. I assume that the provider (and network ID) is fetched from truffle-config file depending on the network selected (truffle test --network XXX). All of this works well.
However, when doing it without artifact, I need to require web3 and inject a provider.
const MyContract = require('../build/contracts/myContract.json');
const Web3 = require('web3');
const provider = ????
const web3 = new Web3(provider);
contractInstance = await new web3.eth.Contract(myContract.abi).deploy({data: myContract.bytecode }).send({});
const result = await contractInstance.methods.myFunction().call();
When I manually add a provider in the test, adding the HDWalletProvider with the nemonic as done in the truffle-config gile where there is "???" written above) it works. However, I would like to have it working the same way as when using artifacts, getting the provider from truffle-config file depending on the network selected (with --network ...). How can I get the adequate provider from truffle-config depending on network parameter that I have entered when runing truffle test --network XXX?
Steps to Reproduce
Create any contract and it's corresponding test file. Then deploying it in the test without using artifacts (as show above). I can't find a way on how to get in the test the correct provider from the truffle-config file according to the user's input (truffle test --network XXX).
Expected Behavior
I would expect that there is a way to get the correct provider (and network ID) outputed by the truffle-config file when running the test command.
Actual Results
I can't get the correct provider and network ID in my test without using "artifacts.require".
Environment
Operating System:
Ethereum client:
Truffle version (truffle version): v5.3.9
node version (node --version): v.14.16.1
npm version (npm --version): 6.14.12
The text was updated successfully, but these errors were encountered:
I'm a little confused at what you're trying to do here. Are you simply trying to get the provider inside the test, or are you trying to manually extract the provider from the config?
If the former, well, the test environment should supply a web3, so you can get the provider as web3.currentProvider. If the latter... well, manually extracting it is a thing you could do, but if you insist on doing it manually, then there's not really anything for us to do there, is there?
I think I have now understood the problem. I was not aware that the test environment was already supplying the web3. Therefore, when not using artifacts.require I was providing my own web3 and that is where the problem was.
I just removed all the web3 lines in the second snippet of code and it worked well.
Ah, I see, I thought you were providing your own web3 purely as part of your insistence on doing things manually, rather than because you didn't know it was already provided. :) Glad we could help!
Issue
I am currently building the frontend to interact with a contract. I have read that the "artifacts.require" function isn't meant to be used outside of a truffle testing environment. So be able to test my contract properly, I would like to have my testing enviroment work the exact way as my frontend. Therefore, I am setting up a test file without using artifact.require, so only importing a JSON file with require().
When using artifacts, I don't need to require web3 and pass a provider, I assume that is done automatically , and then I can deploy and call any function from the contract, like written below. I assume that the provider (and network ID) is fetched from truffle-config file depending on the network selected (truffle test --network XXX). All of this works well.
However, when doing it without artifact, I need to require web3 and inject a provider.
When I manually add a provider in the test, adding the HDWalletProvider with the nemonic as done in the truffle-config gile where there is "???" written above) it works. However, I would like to have it working the same way as when using artifacts, getting the provider from truffle-config file depending on the network selected (with --network ...). How can I get the adequate provider from truffle-config depending on network parameter that I have entered when runing truffle test --network XXX?
Steps to Reproduce
Create any contract and it's corresponding test file. Then deploying it in the test without using artifacts (as show above). I can't find a way on how to get in the test the correct provider from the truffle-config file according to the user's input (truffle test --network XXX).
Expected Behavior
I would expect that there is a way to get the correct provider (and network ID) outputed by the truffle-config file when running the test command.
Actual Results
I can't get the correct provider and network ID in my test without using "artifacts.require".
Environment
truffle version
): v5.3.9node --version
): v.14.16.1npm --version
): 6.14.12The text was updated successfully, but these errors were encountered: