summaryrefslogtreecommitdiff
path: root/Year_3/Blockchain/RandomMoney.sol
blob: 2a5def413a4df5fb589a1249c77aa88be9b97ce3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// SPDX-License-Identifier: MIT

pragma solidity >0.7 <0.9;

contract RandomMoney {
    uint public total;
    uint public constant minFee = 10;
    mapping(address => uint) public amounts;
    address[] private members;


    function play() public payable returns (uint) {
        require(msg.value >= minFee, "Value is less than the required minimum");


        amounts[msg.sender] += msg.value;

        if (amounts[msg.sender] == msg.value) {
            members.push(msg.sender);
        }

        total += msg.value;

        return total;
    }

    function pay() public payable {
        address payable account = payable(randomAddress());
        account.transfer(total);
        total = 0;
    }

    function randomAddress() internal view returns (address) {
        uint8 num = uint8(
            uint256(
                keccak256(abi.encode(block.timestamp, block.difficulty))
            ) % members.length
        );

        return members[num];
    }
}