lokesh
lokesh

Reputation: 123

msg.sender is not equal to owner

I am trying to write HealthCare contract by taking the reference from this Link. Where Doctor can only update the details of the Patient when Patient sends the address.

File: Patient.sol

pragma solidity ^0.4.21;

contract Patient {
    string public name = "";
    string public dateOfBirth;
    mapping (address => uint) public balances;

    function () payable{
        balances[msg.sender] = 40;
    }

    function getContractAddress() public view returns(address) {
        return address(this);
    }

    // ############### Get Functions Begin ######################
    function getName() external view returns (string) {
        return name;  // First_Name Last_Name
    }

    function getDateOfBirth() external view returns (string) {
        return dateOfBirth; // YYYYMMDD
    }
}

File: Doctor.sol

pragma solidity ^0.4.21;
import './Patient.sol';

contract Doctor{
    // the address of the owner (the patient)
    address public owner;
    string public name;
    string public dateOfBirth;
    string public patient_problem;

    modifier isOwnerPatient {
        require(msg.sender == owner);
        _;
    }
    // constructor that sets the owner to the address creating
    // this smart contract
    function Doctor() {
        owner =  msg.sender;
    }
    // allows physician to add an allergy
    function AddProblem(string _allergie) public isOwnerPatient {
        patient_problem =  _allergie;
    }
}

But when I am running the function AddProblem() of Doctor contract. It is not going inside the loop. It seems that the owner != msg.sender. I am using remix IDE and inputting Patient's contract Address into Doctors At Address input area.

Upvotes: 0

Views: 2011

Answers (2)

supakaity
supakaity

Reputation: 400

Loading a previously created contract.

The At Address field in remix allows you to treat an address as a contract of a certain type.

In this case you're telling it that the Patient contract is a Doctor, which is not correct.

But you have another problem...

The doctor contract doesn't make sense.

A Patient should have a Doctor and/or a Doctor should have multiple Patients.

Is it perhaps that the Doctor contract is misnamed?

Patient adding a Doctor

Regardless, the Doctor seems to want to treat it's creator as the patient address, which means that the Patient should have a method to create a Doctor contract somewhere inside it via for example:

address public myDoctor;
function createDoctor() public {
    myDoctor = new Doctor();
}

Then you could create a Patient, call the patient's createDoctor function.

Once you have called that, you can get the Patient's myDoctor address, and in remix, select the Doctor contract in remix, input the address you got from myDoctor into the Load contract from address field and click Add Address.

This should then mean you have a Patient and a Doctor visible in Remix, but then you'll need a method inside the Patient to call the AddProblem method inside it's myDoctor, since this will only be callable by the Patient and not by you.

Upvotes: 3

Adam Kipnis
Adam Kipnis

Reputation: 10971

You’re not using at for the correct reason. That field is used to get a reference to an already deployed contract The msg.sender will be the address that initiates the call into a function (after the reference to a deployed contract is established).

It looks like you’re trying to create a Patient contract, get the contract address, then create a Doctor contract using the address you retrieved. This will not work. If you want msg.sender in Doctor to be the address of the Patient contract, you would need to have the Doctor contract created inside Patient (new Doctor()).

Upvotes: 0

Related Questions