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,740 points
2,756 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,370 points

Related Questions In Blockchain

0 votes
1 answer

How to create an instance of contract in Truffle console?

Hi, @There, I would suggest you go through ...READ MORE

answered Jun 17, 2020 in Blockchain by Gitika
• 65,770 points
2,028 views
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,100 points
3,618 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
• 69,220 points
1,651 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
1,939 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,465 views
+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,610 points
1,452 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,370 points
858 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP