1

There is my solidity

function buyTokens(uint256 _numberOfTokens) public payable {

    // Require that value is equal
    require(msg.value == multiply(_numberOfTokens, tokenPrice));

    // Require token contract has enough tokens
    require(tokenContract.balanceOf(this) >= _numberOfTokens);

    // Require transfer is successfull
    require(tokenContract.transfer(msg.sender, _numberOfTokens));

    // Keep track tokensSold
    tokensSold += _numberOfTokens;

    // track Sell event
    emit Sell(msg.sender, _numberOfTokens);
}

function sellTokens(uint256 _numberOfTokens) public payable {

    // Reuqire that value is equal
    require(msg.value == multiply(_numberOfTokens, tokenPrice));

    require(tokenContract.transferFrom(msg.sender,this, _numberOfTokens));

    tokensSold -= _numberOfTokens;

    uint amountEther = multiply(_numberOfTokens, tokenPrice)/(10**18);

    uint amountEtherToSend = address(this).balance - amountEther;

    msg.sender.transfer(amountEtherToSend);

    emit Sell(msg.sender, amountEther);
}

For ex 1 ABC token = 0.01 ETH. If I send 0.1 ETH I will send to sender 10 ABC token. It works fine Now we have 0.1 ETH in our wallet. That seller sells 5 tokens but seller receive all ETH from wallet. Can you help me fix sellTokens Function ?

closed as too broad by Ismael, Briomkez, shane, flygoing, Nicolas Massart Dec 3 '18 at 10:04

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

1

The error in your code is here:

uint amountEther = multiply(_numberOfTokens, tokenPrice)/(10**18);

uint amountEtherToSend = address(this).balance - amountEther;

msg.sender.transfer(amountEtherToSend);

Lets make up some values to show why. Let's assume:

  • _numberOfTokens is 5
  • tokenPrice is 1016 Wei = .01 ETH
  • address(this).balance is 1017 wei = .1 ETH

First you calculate amountEther, which using these values becomes .05, but since it is a uint, it will become zero.

Then you calculate amountEtherToSend which takes the whole balance of your account, and subtracts zero, which means it is the whole balance of your account, .1 ETH.

Then you send the whole balance of your account...

A fixed version of your function would be:

function sellTokens(uint256 _numberOfTokens) public {

    require(tokenContract.transferFrom(msg.sender,this, _numberOfTokens));

    tokensSold -= _numberOfTokens;

    uint amountToSend = multiply(_numberOfTokens, tokenPrice);

    msg.sender.transfer(amountToSend);

    emit Sell(msg.sender, amountToSend);
}

Note I also removed the payable and the require(msg.value == multiply(_numberOfTokens, tokenPrice)); since these don't really make sense for this function. You are making it so that for a user to sell their tokens, they need to send the contract ether, which doesn't sound right.

  • tokenPrice = 10000000000000000; tokenCount = 5; unit amountToSend = 50000000000000000; but when call this function doest not ether. – Nizomjon Hajiev Nov 30 '18 at 5:07
  • I do not understand what you are asking me, can you clarify? – Shawn Tabrizi Nov 30 '18 at 5:54
  • When I changed sellTokens with yours does not change token seller's account and as well as our wallet balance – Nizomjon Hajiev Nov 30 '18 at 6:10
  • is it depends on decimals ???? – Nizomjon Hajiev Nov 30 '18 at 6:39
0

You can't use transferFrom() method to transfer tokens unless sender called approve() method prior.

  • Can you help me to write approve() method. Because I am completely new. – Nizomjon Hajiev Nov 30 '18 at 13:41

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