Reputation: 1725
I am working on a lottery smart contract (tutorial) using Solidity. The code snippet that is not working regards the picking of a "random" winner below:
pragma solidity ^0.4.17;
function pickWinner() public restricted {
uint index = random() % players.length;
players[index].transfer(this.balance);
players = new address[](0);
}
The error message, that I receive, when i ma testing it on the remix ethereum platform is:
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.
If, as it seems, a payment is needed to send the winnings back to the winner, then how can this be included in the code? I have tried using msg.value as follows:
msg.value = 0.0001 ether;
players[index].transfer(this.balance);
and I still receive an error message. How can this be fixed? Thank you.
Upvotes: 1
Views: 2865
Reputation: 26
As you are using transfer function its clear you need to transfer ether to the player account. And to do so you must pass the amount of ether while calling the function not inside it.
First make function payable.
pragma solidity ^0.4.17;
function pickWinner() public payable restricted {
uint index = random() % players.length;
players[index].transfer(this.balance);
players = new address[](0);
}
Now pass the requirement amount of ether in value textbox and initiate the transaction.
See image to know where you have to pass ether value while calling functions.
Upvotes: 1