0

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.

1 Answer 1

3

In Ethereum calling a contract function is a transaction. And as in any transaction, you can also send an amount of ether when calling a contract function.

In this case msg.value reflects the amount of ether(actually its wei) sent when calling the Buy() function. The amount of coins you buy is specified in the code msg.value/buyPrice.

if (!msg.sender.send(revenue)) throw; is done to verify that the transaction executed successfully. The send() function sends ether from the address of who is calling it to the address specified as an argument. The send function returns false if it fails, so that's why we have the ! sign which is a negation operator. So what this line says is: "send the specified ether from my account to his account and if it fails throw".

Intrinsic gas too low error means that you need to add more gas to the transaction.

2
  • In buy(..) we subtracted the coin from contract balance and added it to Account's balance. So I can understand how coins are being transferred. But how ether is transferred from account to contract? What piece of code enables ether transfer? Commented Aug 3, 2016 at 9:24
  • 1
    @PrashantPrabhakarSingh It's protocol code that transfers ether, just like how you pay the miner for your transaction. Don't forget that when you invoke buy, you specify value (just like you can specify gas for your transaction) like this example.
    – eth
    Commented Aug 4, 2016 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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