0

In this I have set modifier (msg.sender == sellerAddress) which is not valid. But in this scenario, I want to allow only and only sellerAddress to take ether from my contract. How can I allow only seller address to do this. As well as which kind of inputs I have to enter while executing contract?

contract ContractDemo {
    address public buyerAddress;
    address public sellerAddress;
    uint public price; //price or charge for delivering parcel

    modifier onlyBuyer() {
        require(msg.sender == buyerAddress);
        _;
    }

    modifier onlySeller() {
        require(msg.sender == sellerAddress);
        _;
    }

    constructor(address payable _sellerAddress) public payable {
        buyerAddress = msg.sender; 
        sellerAddress = _sellerAddress;
        price = msg.value;
    }

    function deliveredParcel(address payable _seller) onlySeller public payable returns(bool) {
        require(msg.value == price);
        _seller.transfer(msg.value); 
        return true;
    }
}
2
  • As per my understanding of your code, function deliveredParcel(address _buyer)onlySeller public payable returns( bool){ require(msg.value == price); _seller.transfer(msg.value); return true; } Can you explain your problem statement detail Commented Jan 8, 2019 at 10:25
  • The modifier is not working because some conditions are not possible to execute. Also inputs given by me is not correct, that is the exact problem.
    – Rudrika
    Commented Jan 8, 2019 at 11:07

1 Answer 1

1

Call the deliverParcel function using the seller address then the modifier will work fine. The function will be like

function deliveredParcel()onlySeller public payable returns( bool) {
    require(msg.value == price);
    sellerAddress.transfer(msg.value); 
    return true;
   }

OR Change the modifier to this

modifier onlySeller(address seller){
    require(seller == sellerAddress);
    _;
}

Respectively the function will look like this

function deliveredParcel(address payable _seller)onlySeller(_seller) public payable returns( bool) {
    require(msg.value == price);
    _seller.transfer(msg.value); 
    return true;
   }
1
  • Thank you so much.... It worked for my contract. Using some modifications in modifier onlySeller(){}.
    – Rudrika
    Commented Jan 8, 2019 at 11:09

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.