0

Scrolling https://www.ethereum.org/token approximately half way down, I read this:

The next step is making the buy and sell functions:

function buy() payable returns (uint amount){
    amount = msg.value / buyPrice;                    // calculates the amount
    require(balanceOf[this] >= amount);               // 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
}

function sell(uint amount) returns (uint revenue){
    require(balanceOf[msg.sender] >= amount);         // 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;
    msg.sender.transfer(revenue);                     // sends ether to the seller: it's important to do this last to prevent recursion attacks
    Transfer(msg.sender, this, amount);               // executes an event reflecting on the change
    return revenue;                                   // ends function and returns
}

Why does the sell function take the input amount as an argument, whereas the buy function receives it inside msg.value?

I initially thought the reason might be that standard wallets (Mist, MyEtherWallet, Parity, etc), which see the payable keyword and detect this function as an "Ether Receiver", pass everything inside msg.

But it doesn't quite make sense to me that ethereum.org conforms to standard wallets (i.e., ethereum.org should be the one dictating the standard, not the one complying to it).

So what exactly am I missing here?

Thank you!

1

The code for selling requires the amount the tokens you want to sell. The buying part assumes the amount indirectly from the price and the ether sent. Because in any case, you need to send the ether equivalent to the amount you want, asking for the amount is redundant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.