2

I have a contract deployed with truffle to a testrpc local testnet. This contract has has a function that deploys a new contract.

This new contract I can check the address in the testrpc console similar to this:

  Transaction: 0x85a7f17406536dd9618c6647d7d0595a2cee7e7e065ffc08a99e58aa5473ab71
  Contract created: 0x1104c5adf4476aec333ee687c725eacc8d417a7c
  Gas usage: 2642382
  Block Number: 7
  Block Time: Mon Feb 26 2018 19:27:25 GMT+0900 (JST)

But how can I use this new contract in the truffle console to interact with? The json file of the contract (the ABI etc) was created in truffle's build directory, but because the contract is not migrated I cannot use this command:

MyNewContract.deployed().then(_app => { app = _app })
app.showMeTheMoney()

Does anyone how, in the truffle console, I can interact with contracts deployed through other contracts?

2 Answers 2

1

You could manually modify the contract file in the build directory to include the address, but it will get overwritten eventually, so it's not a good way to do this.

Moreover deployed() is meant to be used in the sense of a singleton. If your contract is not a singleton then you shouldn't query it via deployed().

If you have the following constellation: contract A (singleton) instantiates contract B then you can store the reference(s) to B in A and query it from JavaScript.

Example

contract A {
    B[] public arrayOfBs;
    function create() returns (B) {
        B b = new B();
        arrayOfBs.push(b);
        return b;
    }
}

deployer.deploy(A);
...
A.deployed()
    .then(a => {return a.arrayOfBs(idx)})
    .then(bAddr => {return B.at(b)})
    .then(b => {b.showMeTheMoney()})

If B should be a singleton, then you need to change your migration script and deploy it like this.

deployer.deploy(A); 
deployer.deploy(B);

Then you'll be able to query it via B.deployed().

5
  • Is there any truffle function with which j can obtain an instance of a custom deployed contract?
    – mesqueeb
    Commented Feb 26, 2018 at 12:20
  • Yes. Just like in web3, you can use Contract.at(addr).
    – ivicaa
    Commented Feb 26, 2018 at 12:30
  • But you mean that it's required to save the reference in contract A? Can that saved reference also be in an array of references? Could you give a small example how to save the reference of contract B in A, since B will be a lot and A only one.
    – mesqueeb
    Commented Feb 26, 2018 at 12:33
  • I've updated the answer. If you can sketch your contracts, maybe I can give you a better example.
    – ivicaa
    Commented Feb 26, 2018 at 12:43
  • Dear @ivicaa Thanks for the great answer. I tried implementing your answer, but I ran into another problem. I have opened a new question here: ethereum.stackexchange.com/questions/41023/… It would be really cool if you could have a look!
    – mesqueeb
    Commented Feb 27, 2018 at 6:15
4

Does anyone how, in the truffle console, I can interact with contracts deployed through other contracts?

To see the list of contracts already deployed on the Truffle Develop network, this command works:

truffle(develop)> networks [--clean]

Network: develop (id: 4447)
  Migrations: 0xfb88de099e13c3ed21f80a7a1e49f8caecf10df6
  MyNewContract: 0xf204a4ef082f5c04bb89f7d5e6568b796096735a

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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