Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Issue when using truffle testing environment without using artifacts #4219

Closed
1 task
albert-llimos opened this issue Aug 3, 2021 · 3 comments
Closed
1 task

Comments

@albert-llimos
Copy link

albert-llimos commented Aug 3, 2021


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.

const MyContract = artifacts.require("myContract");
contractInstance = await MyContract.new();
const result = await contractInstance.myFunction();

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
@haltman-at
Copy link
Contributor

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?

@albert-llimos
Copy link
Author

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.

Thank you for the help!

@haltman-at
Copy link
Contributor

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!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants