简体   繁体   中英

Transfer function not working as expected in solidity

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM