As per my understanding Buy()
allows main contact to buy coins in exchange of ether.
If I am correct, the question is how? We don't pass any value to the function like how much we want to buy. I executed the function on Mist, function executed fine but I couldn't detect any changes in my account. So it confused me about what actually buy() does.
function buy() returns (uint amount){
amount = msg.value / buyPrice; // calculates the amount
if (balanceOf[this] < amount) throw; // checks if it has enough to sell
balanceOf[msg.sender] += amount; // adds the amount to buyer's balance
balanceOf[this] -= amount; // subtracts amount from seller's balance
Transfer(this, msg.sender, amount); // execute an event reflecting the change
return amount; // ends function and returns
}
Sell()
: Allows user to sell coins and buy ether in exchange.
But the code didn't execute, giving Intrinsic gas too low error
in Mist.
function sell(uint amount) returns (uint revenue){
if (balanceOf[msg.sender] < amount ) throw; // checks if the sender has enough to sell
balanceOf[this] += amount; // adds the amount to owner's balance
balanceOf[msg.sender] -= amount; // subtracts the amount from seller's balance
revenue = amount * sellPrice;
if (!msg.sender.send(revenue)) { // sends ether to the seller: it's important
throw; // to do this last to prevent recursion attacks
} else {
Transfer(msg.sender, this, amount); // executes an event reflecting on the change
return revenue; // ends function and returns
}
}
Also couldn't understand use of if (!msg.sender.send(revenue)) throw;
I am beginner to ethereum.Help me to understand the buy an d sell functions.