3

It doesn't look like the contract is ever paid here. Is this a miscode or am I reading it incorrectly? Where does the exchange of ether occur?

function buy() payable returns (uint amount){
    amount = msg.value / buyPrice;            // calculates the amount of token
    if (balanceOf[this] < amount) throw;      // checks if it has enough token to sell
    balanceOf[msg.sender] += amount;          // adds the amount of token to buyer's balance
    balanceOf[this] -= amount;                // subtracts the amount of token from seller's balance
    Transfer(this, msg.sender, amount);       // execute an event reflecting the change
    return amount;                            // ends function and returns
}
3
  • would you please share the tuto Jul 5 '17 at 17:53
  • It is not clear what you are asking, and you have not provided any context to the submitted code snippet. Jul 5 '17 at 20:07
  • Apologies, it can be found here: ethereum.org/token
    – bGe
    Jul 5 '17 at 20:17
3

this buy function transfers to the buyer an amount of tokens against the received ethers. It gets payed because this function is payable so it receives ethers via msg.value and allocate to the buyer an equivalent amount of these tokens in balanceof[].

3
  • So payable automatically means that someone has transferred ether, which is recorded as msg.value? Does not need to be programmed?
    – bGe
    Jul 5 '17 at 20:17
  • it means that user can send ethers when calling this function. a contract is an account so it receives fund as the normal account does Jul 5 '17 at 21:08
  • but where in the code is the exchange of ether written?
    – bGe
    Jul 6 '17 at 16:22

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.