0

I am developing a movie renting smart contract where the owner can add new movies, clients can search movies and pay for the movies they select. Adding and searching is working to my liking.

The problem: I want to develop the pay function as such- where it takes one argument( the title of the movie) and clients has to pay the exact amount set by the owner, he can not pay less then the price.

For example: owner add a movie: title-titanic,price-10 eth. When client use the pay function he put the title titanic and pay 10 eth. If he tries to pay less or more the transaction will not be successful.

Here is the code:

pragma solidity 0.8.7;

// SPDX-License-Identifier: MIT   

contract Movie{
   address public owner;  

 struct Move{

   uint year;

   uint price;
}
 
   mapping (string => Move ) public movieInfo;
   mapping(uint => Move) amount;

   constructor()  payable{
      owner= msg.sender;
   }

   function addMovie(string memory _title, uint _year, uint _price) public {
      require( msg.sender==owner);

      movieInfo[_title]= Move(_year, _price);
   }

  

   function pay(string memory _title) public payable{
         
   
    }



   function totalFunds() public view returns(uint){
      return address(this).balance;
   }

    
}

1 Answer 1

1

If you want your smart contract function to accept payment it should have the payable function modifier and check the value of the message sent to your smart contract using the msg.value pre-defined property as defined here, so the code of the function should look like this to check a specific amount sent:

{

   ... // previous code

    function pay(string memory _title) public payable {
        // Get a reference to the Movie from storage
        Movie storage requested_movie = movieInfo[_title];
    
        // Check if price sent is valid, else revert the transaction
        require(msg.value == requested_movie.price, "The price received is invalid");

        // rest of your logic
    }

   ... // following code
}

To enhance your error handling and make it easier for you to returns meaningful error without having to store the error message on the blockchain (to pay less fees when deploying your smart contract), you could use error and revert with a NatSpec comment as explained in this part of the Solidity documentation:

{

    ... // previous code

    /// The price paid was incorrect.
    error IncorrectPrice(uint correctPrice);

    function pay(string memory _title) public payable {
        // Get a reference to the Movie from storage
        Movie storage requested_movie = movieInfo[_title];
    
        // Revert if the paid price is incorrect
        if (msg.value != requested_movie.price)
            revert InvalidPrice(requested_movie.price);

        // rest of your logic
    }

   ... // following code
}
1
  • keep up the good work man.
    – learner
    Commented Mar 30, 2022 at 23:11

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.