如何解决如何使用Solidity转移余额?
我正在使用Solidity开发彩票智能合约(教程)。无效的代码段涉及以下“随机”获胜者的选择:
pragma solidity ^0.4.17;
function pickWinner() public restricted {
uint index = random() % players.length;
players[index].transfer(this.balance);
players = new address[](0);
}
我在remix以太坊平台上对其进行测试时收到的错误消息是:
transact to Lottery.pickWinner errored: VM error: revert. revert The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.
如果看起来需要付款才能将奖金发送回赢家,那么如何将其包括在代码中?我已经尝试过使用msg.value,如下所示:
msg.value = 0.0001 ether;
players[index].transfer(this.balance);
,我仍然收到错误消息。如何解决? 谢谢。
解决方法
当您使用转移功能时,您需要将以太币转移到玩家帐户。为此,您必须在调用不属于函数内部的函数时传递一定数量的以太币。
-
首先使功能付清。
pragma solidity ^ 0.4.17;
function pickWinner()公用应付款限制{
uint index = random() % players.length; players[index].transfer(this.balance); players = new address[](0);
}
-
现在在价值文本框中传递所需数量的以太币,并开始交易。
See image to know where you have to pass ether value while calling functions.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。