1

So briefly, when the user clicks a submit button from UI, it needs to pass a fixed price _price to pay() function (there are options to choose a fixed price on UI ex: 0.01, 0.02, 0.03). In the function, it checks if the msg.value is equal to the amount passed from the UI. Then the sender sends that price to the contract address and it adds up to the total contract balance. When I debug it with remix, it gives an error message when it hits require(_price == msg.value)

transact to Test.pay errored: VM error: revert. revert The transaction has been reverted to the initial state. Note: The constructor should be payable if you send value. Debug the transaction to get more information.

contract Test {

    function Test() {
        owner = msg.sender;
    }

    function pay(uint _price) payable returns (bool) {
        //...

        require(_price == msg.value); // Check ether sent is matching with the price
        address(this).transfer(msg.value); // Send price to the contract address
        balanceOfContract[address(this)] += msg.value; // Store price to current contract balance

        LogDeposit(msg.sender, msg.value);

        return true;
    }
}

I increased the gas limit just in case, but still giving me the error. Help is appreciated!

6
  • I guess I would assume that _price and msg.value are different. :-) Are you typing in the exact same number in the "value" field toward the top in Remix and in the field next to the "pay" button?
    – user19510
    Commented Jan 31, 2018 at 6:45
  • Oh, or is it possible the revert is on the following line? address(this).transfer(msg.value) doesn't make much sense. That has no effect, since it transfers ether from the contract to the contract, so there's no need for that line. Also, it will fail if you don't have a payable fallback function.
    – user19510
    Commented Jan 31, 2018 at 6:47
  • Thanks for your reply. I put the exact same number in the value field (ether selected) and field next to the pay button and pressed pay button in red. Still got stuck in that require line. I deleted address(this).transfer(msg.value)
    – bbusdriver
    Commented Jan 31, 2018 at 7:11
  • The basic code works fine for me in Remix. I added address owner; and deleted everything in pay except for the require and the return line. Then I deployed the contract using the JavaScript VM, entered 5 in both the "value" field (with the unit set to "wei") and in the field next to the red "pay" button. Then I pressed the "pay" button, and there was no error.
    – user19510
    Commented Jan 31, 2018 at 7:14
  • You are right! I changed ether to wei and it worked. Why ether is not working?
    – bbusdriver
    Commented Jan 31, 2018 at 7:45

1 Answer 1

1

Summarizing the findings in the comment thread:

The issue was that the _price passed in to the pay function was being specified in ether rather than in wei. msg.value is always in wei, so the require was failing.

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.