0

i have a contract which has a function that sends ether to an account address. here is the contract code

pragma solidity ^0.4.8;

contract Rental {
    struct PaidRent {
        uint id;
        uint value;
    }
    PaidRent[] public paidrents;
    event RentPaid(bool Status);
    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(uint _rent) public payable returns(bool status) {
        if(owner.send(_rent)){
          RentPaid(true);
          return true;
        } else {
          return false;
        }
    }

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



}

and in my angular's service i am first transferring some amount to contract address and then calling this function in following way

paymyRent(contractInstance, rent) {
    this.contractevent = contractInstance.RentPaid();
    this.contractevent.watch(function (error, result) {
      if (!error) {
        console.log(result.args);
      } else {
        console.log(error);
      }
    });
    console.log(contractInstance.getowner());
    contractInstance.payRent(rent, {from: this.web3.eth.accounts[0],
      gas:1000000, value: this.web3.toWei(0.01, "ether")});
  } 

but this function nevers sends the ether to owner's address and neither it emits the event. Other functions of contract such as getowner(), getTenant(), getHouse() are working fine except this payRent() function.

EDIT

enter image description here

1

msg.value contains the sent amount

According to the web3, you're sending 0.01 ether, but passing in a value of rent.

Then you're trying to send rent to the owner, but only 0.1 ether will be available because that's all you've sent.

I think you want something more along the lines of:

Solidity function:

function payRent() public payable returns(bool status) {
    require(msg.value == rent);

    if(owner.send(msg.value)){
      RentPaid(true);
      return true;
    } else {
      return false;
    }
}

Web3 code:

contractInstance.payRent({from: this.web3.eth.accounts[0],
  gas:1000000, value: this.web3.toWei(rent, "ether")});

Another issue

Also you have a problem that if the .send() fails, the funds will be stuck in the contract and not returned to the renter.

You probably want .transfer() instead which will cause the transaction to fail instead and return the funds to the sender.

Improved solidity example:

function payRent() public payable returns(bool status) {
    require(msg.value == rent);
    RentPaid(true);
    owner.transfer(msg.value);
    return true;
}
  • thank your for your answer. i have made changes you suggested but still no luck. moreover i have pasted screenshot of transaction at my contract's address. there is no trace of transaction sending money out of contract. – Shoaib Iqbal Apr 18 '18 at 18:29

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.