0

I have my contract and people can buy my tokens via web3 and MetaMask. Now when I try to execute sell() function throw an exception and always Etherscan says Failed.

I set to sell and buy prices on my contract.

Here is my sell function:

/// @notice Sell `amount` tokens to contract
/// @param amount amount of tokens to be sold
function sell(uint256 amount) public {
    require(address(this).balance >= amount * sellPrice);      // checks if the contract has enough ether to buy
    _transfer(msg.sender, this, amount);              // makes the transfers
    msg.sender.transfer(amount * sellPrice);          // sends ether to the seller. It's important to do this last to avoid recursion attacks
}

I don't know what more I have to do. Does someone have any idea to help me?

Full code: https://pastebin.com/eBYC77GV.

Etherscan Report: https://etherscan.io/tx/0x1213ca9540b8b7c0bd34f09dac906906772416a31e3b01559d0c0a3c05582a19

TylerH
  • 20,799
  • 66
  • 75
  • 101
Francisco
  • 163
  • 1
  • 5
  • 18
  • 1
    Not sure why you were down-voted. Maybe try asking over at https://ethereum.stackexchange.com ? – supakaity Apr 01 '18 at 15:15
  • 1
    I would need to see more of the state to help out. Usually, when I see similar questions posted, the error ends up being a misunderstanding of which address owns the balance. It looks like you're either intending for the contract address to hold the initial supply of tokens (which it doesn't...the deployer of the contract will hold the balance and `buy` will fail), or you're calling `mintToken` after deployment and using the contract address as the target (in which case, both `buy` and `sell` succeed). The previous transactions and address of who issued those transactions are important. – Adam Kipnis Apr 01 '18 at 17:17
  • Can you send me the etherscan links? It may be you never set the sellprice. – James Apr 02 '18 at 17:08
  • I think this can't work. You can't make caller to send funds to `this`. Caller should send it. In that case `sell(uint256 amount)` should be `payable`. – Ferit Apr 02 '18 at 21:45
  • @AdamKipnis I am making the transactions from another address than contract creation. – Francisco Apr 03 '18 at 19:22
  • @James Hi, here is: https://etherscan.io/tx/0x1213ca9540b8b7c0bd34f09dac906906772416a31e3b01559d0c0a3c05582a19 – Francisco Apr 03 '18 at 19:22
  • @norganna I am not sure too. I will try there. – Francisco Apr 03 '18 at 19:24
  • Can you please send me the contract code? Do you own the contract? Have you called the sell price function to make sure it is not zero? – James Apr 03 '18 at 19:24
  • @James Contract code:https://pastebin.com/eBYC77GV. I'm the owner of the contract. Sell price is not zero. – Francisco Apr 03 '18 at 19:37
  • You need to call the function setPrices() – James Apr 03 '18 at 19:40
  • @James I did that. With setPrices() I set buy price and I can buy without problems. – Francisco Apr 03 '18 at 19:51
  • The way I got it to work was: As address A, I deployed the contract, called `setPrices`, then minted tokens using the contract address as the target. From there, I was able to switch to address B and do both buy and sell. You need to make sure your balance mapping is what you expect before buying/selling. Like @James is pointing out, the order of operations before you call sell is important and you can’t debug without having that info. – Adam Kipnis Apr 03 '18 at 19:56

0 Answers0