0

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  
    }
  • what's the problem you're having? any error message? – nyusternie Oct 12 '17 at 10:01
  • My contract had a buy() function but it won't accept ether. This is copied straight from the example provided by ethereum.org – Wesley Warren Oct 13 '17 at 20:46
  • Make sure you are sending enough gas with the buy() transaction. 200,000 should be a safe amount (the remainder/unused gas will be returned back to your wallet). – nyusternie Oct 19 '17 at 23:45
1

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().

  • Thanks, great help! Brilliant stuff!. I had set prices but the contract itself did not have any tokens. When I try to send Ether to the contract it stills shows a warning of a failed transaction error message. The "contract won't allow this transaction to be executed" for some reason. Please help me just a bit more if you can? – Wesley Warren Oct 12 '17 at 12:01
  • Do not send Ether to the token contract since it does not implement the fallback function, therefore the Ether transfer will always fail. Rather transfer tokens from the owner of the token contract to the token contract address. So you should do something like Token.transfer(Token Contract Address, amount). This transaction should be sent from the token owner account! Good luck! – István András Seres Oct 12 '17 at 14:15
  • Ok, Understood. I have transferred tokens to the contract from the owner contract. This works. How does any customer buy tokens from me using their Ether then? How do I add a "fallback function" to the contract? Why does this part of the contract not function as a fallback? Is it as simple as sending text with the ether? /// @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 } – Wesley Warren Oct 13 '17 at 6:51
  • 1) Buying tokens from the contract: they'll only need to call the buy() function and send some Ether along with this function call (transaction if you prefer). Note that only the buy function is 'payable' , so this is the only function which is able to accept Ether, all the rest will throw if you send Ether to it. 2) You can not add any function to your contract once it is deployed! The blockchain is immutable, therefore also your smart contract code! But you could redeploy a different token contract with some added functions if you'd like... – István András Seres Oct 13 '17 at 7:20
  • Yes understood, i would deploy a new contract as required to test with. But my existing contract had a buy() function. Why does it not accept ether? – Wesley Warren Oct 13 '17 at 20:45

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.