0

i have contract factory which looks like following

pragma solidity ^0.4.8;
import './RentalContract.sol';


contract contractFactory {
  address[] public contracts;


  function getContractCount() public constant returns(uint contractCount) {
    return contracts.length;
  }

  function newContract(uint _rent, uint _security_deposit, string _house, address _owner, address _tenant)
  public constant returns(address) {
    Rental c = new Rental(_rent, _security_deposit, _house, _owner, _tenant);
    contracts.push(c);
    return c;
  }
}

this contract has a function called newContract which creates an object of another contract called RentalContract and returns the address of the newly created object. Now i want to interact with this newly created instance of Rental contract through this address using web3js. How can I achieve this?

EDIT

here is my rental contract

pragma solidity ^0.4.8;

contract Rental {
    struct PaidRent {
        uint id;
        uint value;
    }
    PaidRent[] public paidrents;

    uint public createdTimestamp;
    uint public rent;
    uint public security_deposit;
    string public house;
    address public owner;
    address public tenant;

    enum State {Created, Started, Terminated}
    State public state;
    function Rental(uint _rent, uint _security_deposit, string _house, address _owner, address _tenant) {
        createdTimestamp = block.timestamp;
        rent = _rent;
        security_deposit = _security_deposit;
        house = _house;
        owner = _owner;
        tenant = _tenant;
    }
    modifier require(bool _condition) {
        if(!_condition) throw;
        _;
    }
    modifier ownerOnly() {
        if(msg.sender != owner) throw;
        _;
    }
    modifier TenantOnly() {
        if(msg.sender != tenant) throw;
        _;
    }
    modifier inState(State _state){
        if(_state != state) throw;
        _;
    }

    function getPaidRents() internal returns (PaidRent[]) {
        return paidrents;
    }

    function getHouse() constant returns (string) {
        return house;
    }
    function getowner() constant returns (address) {
        return owner;
    }
    function getTenant() constant returns (address) {
        return tenant;
    }
    function getRent() constant returns (uint) {
        return rent;
    }
    function getDeposit() constant returns (uint) {
        return security_deposit;
    }
    function getContractCreated() constant returns (uint) {
        return createdTimestamp;
    }
    function getContractAddress() constant returns (address) {
        return this;
    }
    function getState() returns (State) {
        return state;
    }
    function CollectEth() payable  {}

    function payRent() TenantOnly inState(State.Started) require(msg.value == rent) {
        owner.transfer(msg.value);
        paidrents.push(PaidRent({
            id : paidrents.length + 1,
            value : msg.value
        }));
    }


    function terminateContract(address deposit_to) inState(State.Started) require(this.balance == security_deposit) {
        deposit_to.transfer(security_deposit);
        state = State.Terminated;
    }



}

i am able to create a new instance of this rental contract thorugh contractFactory's function called newContract. i can interact with this new rental contract by var c2 = Rental.at('0x0109b453ea7734c2c32c3fc73b80de42d6e81f8e') on truffle console. but i don't know how to do this in my angular's service using web3js. if i try to use it in the same way as truffle console it says cannot find name Rental.

  • @Adam Kipnis can you please look into this? – Shoaib Iqbal Apr 9 '18 at 22:00
1

The ABI for the newly created contract is the same as the ABI for RentalContrat. The other thing you will need is the address. You may need to implement a function to read the array of addresses. Once you have that, you can follow any of the web3 tutorials available or just to follow the examples for web3 in readthedocs.

I hope this helps

EDIT: https://dappsforbeginners.wordpress.com/tutorials/interactions-between-contracts/

EDTIT after your EDIT:

You have the Rental contract and therefore you have the ABI. Assuming you have injected web3 you can now do:

var Rental = web3.eth.contract(ABI);
var rentalInstance = Rental.at('0x0109b453ea7734c2c32c3fc73b80de42d6e81f8e')

I am assuming the reason it was not finding Rental was that the ABI was not defined.

I hope this helps you.

  • thank you Jaime for the answer, but newly created contract is actually RentalContract. I think you meant "The ABI for the newly created contract is the same as the ABI for ContractFactory". – Shoaib Iqbal Apr 10 '18 at 4:27
  • no, the contract that you are creating is defined in RentalContract.sol and is called Rental, Right? as you are importing it, so we agree that the new contract is RentalContract then the ABI is the ABI of Rental contract not ContractFactory. You are not creating a copy of contractfactory but a copy of Rental. – Jaime Apr 10 '18 at 5:43
  • oh, i see. It's clear now. can you please look at the edit i made in the origional question – Shoaib Iqbal Apr 10 '18 at 6:00
  • I took a look and updated the answer. Let me know how it goes. – Jaime Apr 10 '18 at 6:20

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.