[Ethereum] Automatic buying and selling of created tokens not working. What am I missing

ethersoliditytokens

Created token using example code from etherum.org for automatic buying and selling but can't buy or sell from another account. What am I missing?

https://ethereum.org/token#automatic-selling-and-buying

Update:

I have transferred tokens to the contract from the owner wallet. This works.
I have set buy and sell prices.

How does any customer buy tokens from me using their Ether? The contract as it is won't allow ether to be sent to it.

If I need a to add a "fallback function" to the contract how is this done? Such a function is not in the example code so have ethereum.org stuffed up?

Why does this part of the contract not function as a fallback? Is it as simple as sending text with the ether transaction?

/// @notice Buy tokens from contract by sending ether  
    function buy() payable public {  
        uint amount = msg.value / buyPrice;        // calculates the amount  
        _transfer(this, msg.sender, amount);       // makes the transfers  
    }

Best Answer

There are actually a few things you have to take care of:

  1. Make sure that there are some tokens at the token contract address. If there is not any, then noone is able to buy tokens, since every purchase is made not from the owner of the token but from the token contract address (this).

    If you have a look at the source code you linked you can see the line Transfer(this, msg.sender, amount) in the buy function. This means that the contract tries to send tokens from its own balance, but at the creation of the token contract, only the owner of the token contract possess tokens, not the contract itself. So just send some tokens from the owner of the contract to the token contract itself!

  2. Did you set buyPrice and sellPrice? If you did not, then they are 0, and division by zero will give you an exception at the buy().

Related Topic