Big picture: How do I get multiple contract instances from a single sol file using truffle-contract?

I have two Contracts in my .sol file (it's more of a contract factory)

const artifacts = require('../build/contracts/Contract.json');
const contract = require('truffle-contract');
const MyContract = contract(artifacts);

How do create an instance of the 2nd contract using truffle-contract? when i'm testing in mocha i can do

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

but for some reason in truffle exec i get

TypeError: artifacts.require is not a function

Assuming you are using truffle javascript testing and you have a good understanding of mocha. Then you can easily create more instances of the same contract. So your syntax to create a new instance of a truffle contract should be.

 beforeEach(async () => {
    let SecondContract = await Contract.new()

Alternatively, perhaps you are asking a much simpler question that I imagined. When truffle artifacts are created for a single .sol file containing multiple contracts (such as in inherhitance), multiple artifacts are created (check the folder build/contracts/), try the following code snippet.

const artifacts = require('../build/contracts/Contract.json');
const SecondContract = require('../build/contracts/SecondContract.json')
const contract = require('truffle-contract');
const MyContract = contract(artifacts);
const myCoolContract = contract(SecondContract)

Perhaps you are missing a callback which is why truffle exec is not working as expected.

See https://truffleframework.com/docs/truffle/getting-started/writing-external-scripts

  • Oh my... i didn't notice that there was another json for the other contract. Thank you very much.
    – Kirby
    Oct 28 '18 at 23:53

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.