How to manually create the instance of the contract in truffle?

0 votes

Say I have 2 contracts like this

A.sol
import './B.sol';
contract A {
    event BCreated(address addressOfB);
    function createB(){
        B b = new B();   
        BCreated(b);
    }
}


B.sol
contract B {    
    uint8 value = 5;
    function getValue() constant returns(uint8){
        return value;
    }
}

I am trying to write the test cases for these contracts. I can deploy the contract A using the migrations file and will get the instance of it.

But I am not sure about how to get the instance of contract B, after the contract is created using function createB()

Ok I can get the address of the contract B in events after calling function createB(), But not sure about the instance.

For this example, you can say that I can separately test contract B as it doesn't do much. But in the real case, I need to create an instance using the address coming from the event.

Here is the little bit of js code for truffle test file In this I have the address of B

var A = artifacts.require("./A.sol");
contract('A', (accounts) => {
    it("Value should be 5", async () => {
        let instanceOfA = await A.deployed()
        let resultTx = await instanceOfA.createB({ from: accounts[0] });
        console.log("Address of B: " + resultTx.logs[0].args.addressOfB);
        /**
         * How do I create the instance of B now?
         */
    })
})
Sep 24, 2018 in Blockchain by digger
• 26,550 points
62 views

1 answer to this question.

0 votes

You can try to do it like this

var A = artifacts.require("./A.sol");
var B = artifacts.require("./B.sol");
contract('A', (accounts) => {
    it("Value should be 5", async () => {
        let instanceOfA = await A.deployed()
        let resultTx = await instanceOfA.createB({ from: accounts[0] });
        console.log("Address of B: " + resultTx.logs[0].args.addressOfB);

        let instanceOfB = await B.at(resultTx.logs[0].args.addressOfB);

    })
})
answered Sep 24, 2018 by slayer
• 29,170 points

Related Questions In Blockchain

0 votes
1 answer

How do I send back ethers to the sender of the tokens in a smart contract?

Whenever a smart contract receives ether via ...READ MORE

answered May 30, 2018 in Blockchain by Perry
• 17,020 points
465 views
0 votes
1 answer

How to create a contract in truffle?

The command you are using to create ...READ MORE

answered Dec 11, 2018 in Blockchain by Omkar
• 67,660 points
31 views
0 votes
1 answer

How to check the data integrity logic in proof of work mining?

The proof of work is actually works ...READ MORE

answered May 7, 2018 in Blockchain by Johnathon
• 9,080 points
42 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
173 views
0 votes
1 answer
+3 votes
2 answers

How to run ethereumjs using Node.JS

You need to install testrpc globally on ...READ MORE

answered Mar 27, 2018 in Blockchain by ned_crew
• 1,620 points
162 views
0 votes
1 answer

How to store photos in Hyperledger smart contract?

To do this, you need to represent ...READ MORE

answered Jul 9, 2018 in Blockchain by slayer
• 29,170 points
82 views