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);
contracts.push(c);
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;
@Injectable()
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);
this.ContractFactory.setProvider(window.web3.currentProvider);
this.Rental.setProvider(window.web3.currentProvider);
/* if (this.web3.version.network !== '4') {
alert('Please connect to the Rinkeby network');
} */
} else {
console.warn(
'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 => {
this.ContractFactory
.deployed()
.then( instance => {
meta = instance;
return meta.newContract(rent, security_deposit, house, owner, tenant);
})
.then(value => {
console.log("value is");
console.log(value);
observer.next(value);
observer.complete();
})
.catch(e => {
console.log(e);
observer.error(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?