0

I am trying to figure out how do we access Contract2's instance in Truffle's test if it was deployed by Contract1 in Truffle Develop? Can't seem to find a question that described my issue exactly so sorry in advance if there is one already!

I have a BallotFactory contract that generates a new Ballot contract as below.

contract BallotFactory {
address[] public deployedBallotsAddress;

function createBallot(
    string memory _title,
    string memory _description,
    string memory _option1,
    string memory _option2
) public {
    address newBallotAddress = address(
        new Ballot(_title, _description, _option1, _option2, msg.sender)
    ); 
    deployedBallotsAddress.push(newBallotAddress);
}

function getDeployedBallots() public view returns (address[] memory) {
    return deployedBallotsAddress;
}

I migrated the BallotFactory contract.

const BallotFactory = artifacts.require("BallotFactory");
module.exports = function (deployer) {
  deployer.deploy(BallotFactory);
};

In my Truffle test's beforeEach:

beforeEach(async () => {
factoryInstance = await BallotFactory.new({ from: accounts[0] });

await factoryInstance.createBallot(
  "President",
  "Vote for Alice or Bob",
  "Alice",
  "Bob",
  {
    from: accounts[0],
  }
);

[ballotAddress] = await factoryInstance.getDeployedBallots();

console.log(ballotAddress);

ballotInstance = await Ballot.deployed(ballotAddress); });

console.log(ballotAddress) returns as address which I guess means that BallotFactory successfully deployed a new Ballot contract. But when I try to access the deployed ballot instance in the last line, I get the error: "Ballot has not been deployed to detected network (network/artifact mismatch)". There are functions like voting on the Ballot contract that needs testing later in the 'it' hence the need to access it.

Testing is done on Truffle Develop without ganache.

Thanks!

1

Assuming that you have this:

const Ballot = artifacts.require("Ballot");

Change this:

ballotInstance = await Ballot.deployed(ballotAddress);

To this:

ballotInstance = Ballot.at(ballotAddress);
| improve this answer | |
  • Oh that did it, thank you so much! Sorry goodvibration, do you mind if I ask if which documentation I should refer to find functions and methods like this one? Is it web3js or truffle specific? I tried looking through the docs but couldn't find .at(). – seansing May 26 at 14:22
  • @SeanSing: Huh, very good question. Within a Truffle test, you have a global object named web3, which depends on the Web3 version that your Truffle version relies on. For example, Truffle 4.x relies on Web3 0.x, while Truffle 5.x relies on Web3 1.x. On top of that, as you've noted, some of these functions are not even part of Web3 documentation, because they are some sort of wrappers of web3 functions, which are implemented by Truffle. For example, the global object named artifacts. Specifically for your question about the at function, see my next comment below... – goodvibration May 26 at 14:37
  • @SeanSing: As far as I understand, Truffle's artifacts.require is a wrapper around web3.eth.contract (on web3 0.x) and around web3.eth.Contract (on web3 1.x). It returns an object which describes your contract. On this object, you can call new(arg1, arg2, arg3), thus deploying a new instance of your contract, or you can use at(address) in order to get a "pointer" to an already deployed contract. On the web3 layer, the at function exists only on version 0.x as far as I know (see the official documentation). – goodvibration May 26 at 14:42
  • @SeanSing: On Truffle 5.x, which uses web3 1.x, I am not sure whether or not the at function is still viable. If it is, then it obviously doesn't rely on web3's at function, since it is no longer available in that version of web3. – goodvibration May 26 at 14:47
  • To conclude this, here are the links that I always use: Web3 v0.x, Web3 v1.x, Truffle. – goodvibration May 26 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.