如何使用 Solidity 转移余额?
how can I transfer balance using 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 ethereum 平台上测试它时收到的错误消息是:
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;
函数 pickWinner() public 应付限制 {
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.
我正在使用 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 ethereum 平台上测试它时收到的错误消息是:
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;
函数 pickWinner() public 应付限制 {
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.