I'm trying to create a simple smart contract to learn solidity and how ethereum works.
From what I understand, using the modify payable on a method will make it accept a value. We then deduct from the sender and add that somewhere else, in this code I'm trying to send it to the owner of the contract.
contract AcceptEth {
address public owner;
uint public bal;
uint public price;
mapping (address => uint) balance;
function AcceptEth() {
// set owner as the address of the one who created the contract
owner = msg.sender;
// set the price to 2 ether
price = 2 ether;
}
function accept() payable returns(bool success) {
// deduct 2 ether from the one person who executed the contract
balance[msg.sender] -= price;
// send 2 ether to the owner of this contract
balance[owner] += price;
return true;
}
}
When I interact with this contract through remix, I get an error of "VM Exception while processing transaction: out of gas" it creates a transaction and the gas price was 21000000000 and the value was 0.00 ETH when I'm trying to get 2 ether from anyone who executes this method.
What's wrong with the code? Alternatively I can add a a variable for one to input the value they want to send, along with a withdraw method, right? but for the sake of learning, I wanted to keep it simple. but even this code feels a bit simple and feels like something is missing.