i am developing a rental application using truffle with angular 5 for which i need to start a new contract everytime a new tenant rents a house from owner. For this purpose i have deployed a contractFactory using truffle. Following is my contractFactory

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

contract contractFactory {
  address[] public contracts;

  function getContractCount() public constant returns(uint) {
    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);
    return c;

every time i need a new contract between owner and tenant i call newContract function of this contractFacotry, but the problem is that this contractFactory is always returning same address for every contract. Here is my angular service which makes use of web3 to call this contractFacotry function.

import { Injectable } from '@angular/core';
import * as Web3 from 'web3';
import {Observable} from 'rxjs/Observable';
import { fromPromise } from 'rxjs/observable/fromPromise';
import {observable} from "rxjs/symbol/observable";
import {reject} from "q";

const FactoryArtifacts = require('/home/work/angularplustruffle/angular4-truffle-starter-dapp/build/contracts/contractFactory.json');
const contract = require('truffle-contract');
const RentalArtifacts = require('/home/work/angularplustruffle/angular4-truffle-starter-dapp/build/contracts/Rental.json');

declare let require: any;
declare let window: any;

export class ContractsService {
  ContractFactory = contract(FactoryArtifacts);
  Rental = contract(RentalArtifacts);
  private web3: any;
  public acc_no: any = 5;

  constructor() {
    if (typeof window.web3 !== 'undefined') {
      // Use Mist/MetaMask's provider
      this.web3 = new Web3(window.web3.currentProvider);

      /* if (this.web3.version.network !== '4') {
        alert('Please connect to the Rinkeby network');
      } */
    } else {
        'Please use a dapp browser like mist or MetaMask plugin for chrome'
  public deployRentalContract(rent, security_deposit, house, owner, tenant): Observable<any> {
    let meta;
      return Observable.create( observer => {
          .then( instance => {
              meta = instance;
              return meta.newContract(rent, security_deposit, house, owner, tenant);
          .then(value => {
            console.log("value is");
          .catch(e => {

i have tried calling getContractCount() function from contract Factory but it is also returning zero all the time. any idea what is wrong here?

1 Answer 1


You need to remove the word constant from this line.

function newContract(uint _rent, uint _security_deposit, string _house, address _owner, address _tenant) public constant returns(address) {

constant is hard-wiring the idea that it's a read-only (dry-run, what if) function that cannot update the state.

Hope it helps.

  • by removing constant i am getting arror "Error: invalid address" Commented Apr 11, 2018 at 23:37
  • 1
    You are probably getting a transaction hash from the signed transaction instead an address from the return. The corrected contract may necessitate some changes on the client side such as the way you discover the new contract address after the transaction is actually mined. Commented Apr 12, 2018 at 2:46
  • d0 i need to make the solidity function payable? this is one solution i got by googling it. When i tried it, it returned the transaction detail but the to address was again same when called for more than one time. Commented Apr 12, 2018 at 7:25
  • can you please have a look at this? ethereum.stackexchange.com/questions/45502/… Commented Apr 12, 2018 at 19:55
  • Zack beat me to it. I'm glad you're all sorted out now. Thanks for the accept ;-) Commented Apr 13, 2018 at 0:13

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.