![](/img/trans.png)
[英]Pick Winner Lottery test throws; Error: Transaction has been reverted by the EVM:
[英]how to get the address of a winner in lottery decentarlized lottery application
我在solidity中創建了一個彩票應用程序。 All things are working fine but I want to add this feature that when the winner is selected it should return me the address of winner as well.
我能為此做些什么? 任何幫助將不勝感激,謝謝。 這是我的可靠代碼。
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Lottery_Application {
address public manager;
address payable[] public participants;
constructor() {
manager=msg.sender;
}
receive() external payable {
require(msg.value>=1 ether);
participants.push(payable(msg.sender));
}
function getbalance() public view returns(uint) {
require(msg.sender==manager);
return address(this).balance;
}
function random() public view returns(uint) {
return uint(keccak256(abi.encodePacked(block.difficulty,block.timestamp,participants.length)));
}
function selectwinner() public returns(address) {
require(msg.sender==manager);
require(participants.length>=3);
uint r=random();
address payable winner;
uint index=r% participants.length;
winner=participants[index];
winner.transfer(getbalance());
winner=address(msg.sender);
return winner;
}
}
狀態改變函數不能返回任何東西。 你可以通過兩種方式做到這一點。
event WinnerSelected(address winner)
在您的獲勝者選擇函數結束時, emit WinnerSelected(winner)
。
address public recentWinner
在您的獲勝者選擇函數的末尾, recentWinner = winner
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.