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;
    }
}
  • 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 – Jitendra Kumar. Balla Jan 8 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 Jan 8 at 11:07
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;
   }
  • Thank you so much.... It worked for my contract. Using some modifications in modifier onlySeller(){}. – Rudrika Jan 8 at 11:09

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.