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!
_price
andmsg.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?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.require
line. I deletedaddress(this).transfer(msg.value)
address owner;
and deleted everything inpay
except for therequire
and thereturn
line. Then I deployed the contract using the JavaScript VM, entered5
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.