solidity - Solidity 彩票智能合约,但我只能给第一个奖品。我想把它送给所有的赢家
问题描述
不能让它为所有获胜者提供奖品,只有第一个推杆正确组合的人。我认为 findWinners 函数有问题,但我找不到。如果我将地址 A 设为好的组合,然后 B 与一个不好的组合,最后 C 与一个好的组合,它只会给 A 奖励。
pragma solidity ^0.5.1;
contract Lottery {
Game[] ListGames;
// uint public nbGames = ListGames.length;
uint price = 100000;
uint[6] winnerGame;
// winnerGame = [1,2,3,4,5,6];
address payable[] ListWinners;
uint winnersPayed = 0;
uint expiredGames = 0;
struct Game {
address payable gamer;
uint[6] numbers;
uint date;
bool valid;
}
function setWinner(uint[6] memory _winnerGame) public {
winnerGame = _winnerGame;
}
function findWinners() public returns (address payable[] memory) {
uint size = ListGames.length;
bool win = true;
for (uint j = 0; j < size; j++) {
for (uint i=0; i<6 ; i++) {
if ((ListGames[j].numbers[i] != winnerGame[i]) || !ListGames[j].valid) {
win = false;
}
}
if (win) {
ListGames[j].valid = false;
expiredGames ++;
ListWinners.push(ListGames[j].gamer);
}
}
return ListWinners;
}
function payer() public returns (uint) {
uint nbWinners = ListWinners.length;
uint prize;
if (nbWinners - winnersPayed != 0) {
prize = address(this).balance / (nbWinners- winnersPayed);
for (uint i = 0; i < nbWinners; i++) {
if(ListWinners[i] != address(0)) {
ListWinners[i].transfer(prize);
delete ListWinners[i];
winnersPayed++;
require(ListWinners[i] == address(0));
}
}
}
return nbWinners;
}
modifier costs(uint amount) {
require(msg.value >= amount);
_;
}
function postGame(uint[6] memory _numbers) public payable costs(price) {
Game memory newGame = Game(msg.sender, _numbers, block. timestamp, true);
ListGames.push(newGame);
}
function numberValidGames() public view returns (uint) {
return ListGames.length - expiredGames;
}
function getWinnersList() public view returns (address payable [] memory) {
return ListWinners;
}
function getTime() public view returns (uint) {
return block.timestamp;
}
function getBalance() public view returns (uint) {
return address(this).balance;
}
}
解决方案
推荐阅读
- python - Wagtail PostgreSQL 搜索后端显示警告“MyModel.search_fields 包含不存在的字段”但功能齐全并按预期运行
- sql-server - 这两种说法有什么区别?
- javascript - 使用 node.js puppeteer 通过 VPN IP 连接
- php - 如何将 for 循环结果存储为数组?
- c++ - 如何显式调用运算符 < 的模板化重载
考虑以下用于重载
operator<<
for a的示例代码class A
:#include <iostream> class A { template <typename T> friend A &operator<<(A &a, const
- azure - Azure 函数中的 Cosmos DB 408 响应
- vba - 我找不到引用“父母”的正确方法
- python - 如何将字典列表中返回的python JSON字典,数据转换为SQL插入
- c++ - C ++如何确定互斥锁是否被单个线程不成比例地占用而阻塞其他线程
- java - 为什么我的代码不能运行“猜我的号码”游戏?