1
votes

Considering below code

mapping (address => uint256) public balanceOf;

event Transfer(address indexed from, address indexed to, uint256 value);

function _transfer(address _from, address _to, uint _value) internal {
    require(_to != 0x0);
    require(balanceOf[_from] >= _value);
    balanceOf[_from] -= _value;
    balanceOf[_to] += _value;
    Transfer(_from, _to, _value);
}

function sell(uint256 amount) public {
    require(this.balance >= amount * sellPrice);
    _transfer(msg.sender, this, amount);              
    msg.sender.transfer(amount * sellPrice);          
}

function sendTokens(address _to, uint value) public {
    _transfer(msg.sender,_to,value);
    msg.sender.transfer(value * sellPrice);         
}

function buy() payable public {
    uint amount = (msg.value / buyPrice) * 1 wei;       
    _transfer(this, msg.sender, amount);              
}

Here sell() and buy() function sends/receive tokens to/from contract but when function with almost same signature sendTokens() is called it is not able to send or are sending 0 tokens. Contract also has ethers and tokens. But it always consider tokens to send and receive. But sendTokens is not behaving same with almost same code.

Also I have an another question related to this. Every account has ethers and multiple tokens. Then how will it identify that here this specific tokens needs to be send or here ether is to be send and not any tokens.

Can anyone help. Thanks in advance.

1
The contract seems to be working as expected. What are your inputs, your expected results, and the results you're getting that you think are incorrect? - Adam Kipnis
Considering sendTokens() functions then I give an address in which I want to send tokens from my account. My account do have received tokens from contract. Also I give the number of tokens lets say 500 as I do have a balance of 1000 tokens. When I click for transaction it gives me that transaction successfull. When i see more details of that transaction that it gives me 0 token transfer from this address to that address. Also when I try to send transaction again it gives me gas limit exceeds 300000 limit. I use remix and metamask and ROPStEN network - Harsh
Does your contract address has ether? - Marcos Martínez

1 Answers

0
votes

Well there is no correction in the code. Perhaps Metamask and remix is not supporting this type of transaction at this moment. When I call this method from Web3js it worked. So couldn't find the exact reason but yes it worked at end.