简体   繁体   English

传递函数无法如预期般可靠地工作

[英]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. 这里的sell()和buy()函数向合约发送/接收合约令牌,但是当调用具有几乎相同签名的sendTokens()函数时,它无法发送或发送0个令牌。 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. 但是sendTokens几乎没有相同的代码。

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. 也许Metamask和remix目前不支持这种类型的事务。 When I call this method from Web3js it worked. 当我从Web3js调用此方法时,它起作用了。 So couldn't find the exact reason but yes it worked at end. 因此找不到确切的原因,但是是的,它最终奏效了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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