首页 > 解决方案 > 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;
    }
}

标签: solidity

解决方案


推荐阅读