File size: 3,991 Bytes
f998fcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// This contract is part of Zellic’s smart contract dataset, which is a collection of publicly available contract code gathered as of March 2023.

pragma solidity ^0.8.4;
/*

import "https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/interfaces/VRFCoordinatorV2Interface.sol";

import "https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/VRFConsumerBaseV2.sol";

*/

import "./VRFCoordinatorV2Interface.sol";
import "./VRFConsumerBaseV2.sol";


interface Stats {
    function incrementTOTAL() external  ;
    function incrementWON(address user) external  ;
    function incrementLOST(address user) external  ;
    function incrementPLAYED(address user) external  ;
}

contract Lottery is VRFConsumerBaseV2 {

  Stats public stats ; 
  
    
  VRFCoordinatorV2Interface COORDINATOR;

  // Your subscription ID.
  uint64 s_subscriptionId;


  address vrfCoordinator =0x271682DEB8C4E0901D1a1550aD2e64D568E69909 ;

  bytes32 keyHash = 0xff8dedfbfa60af186cf3c830acbc32c05aae823045ae5ea7da1e45fbfaba4f92;

  uint32 callbackGasLimit = 1000000;

  uint16 requestConfirmations = 3;

  uint32 numWords =  1;

  uint256[] public s_randomWords;
  uint256 public s_requestId;
  address s_owner;
  

  mapping(uint256 => address) public ticket ;
  mapping(address => bool) public bought;
  mapping(uint256 => address) public boughtTRACK;
  uint256 bvar ; 
  uint256 public ticketTRACK = 0; 
  uint256 weiVal = 200000000000000000; 
  uint256 maxp = 9;
  address owner =0xE6F747501498903758BDF7AE648188E86f508Ef6 ;
  address tax_payee = 0x70e19EFAeF64eBD5Bb4E8a081ca1fe4a455ecFE0;

  bool allowed_tobuy = true;
  
  constructor(uint64 subscriptionId) VRFConsumerBaseV2(vrfCoordinator) {
    COORDINATOR = VRFCoordinatorV2Interface(vrfCoordinator);
    s_owner = msg.sender;
    s_subscriptionId = subscriptionId;
    stats = Stats(0x2C81b71efc52fEF5EC30c9c531E7c74222b8c2aa);
    
  }
  
  modifier OnlyOwner(){
    require(msg.sender == owner);
    _;
  }
  
  function editVal(uint256 newv, uint256 maxpn) public OnlyOwner{
    require(maxpn <=10);
    weiVal = newv;
    maxp = maxpn-1;
    uint256 a = 1;
    while(a <= ticketTRACK){
      ticket[a] = 0x0000000000000000000000000000000000000000;
      a = a+1;
    }
    ticketTRACK = 0; 
    uint256 b = 1; 
    while (b <= bvar){
      address addyb = boughtTRACK[b];
      bought[addyb] = false;
      b = b+1;
    }
    bvar = 0 ; 
    allowed_tobuy = true;
  }
  

  function buyTicket() public payable{
      require(ticketTRACK <= maxp );
      require(bought[msg.sender]==false);
      require(msg.value == weiVal);
      require(allowed_tobuy == true);
      ticket[ticketTRACK+1] = msg.sender;
      ticketTRACK = ticketTRACK+1;
      bought[msg.sender] = true;
      bvar = bvar +1;
      boughtTRACK[bvar] = msg.sender;
      stats.incrementTOTAL();
      stats.incrementPLAYED(msg.sender);
  }
  
  function requestRandomWords() external OnlyOwner {
    s_requestId = COORDINATOR.requestRandomWords(
      keyHash,
      s_subscriptionId,
      requestConfirmations,
      callbackGasLimit,
      numWords
    );
    
  }

  function fulfillRandomWords  (
    uint256 s_requestId, 
    uint256[] memory randomWords
  ) internal override {
    uint256 s_randomRange = (randomWords[0] % maxp+1) + 1;
    settleLotto(s_randomRange);
  }
  

  function settleLotto(uint256 num) internal{
    address winner = ticket[num];
    uint256 balance = address(this).balance;
    uint256 tax = balance/10;
    uint256 total = balance-tax;
    payable(owner).transfer(tax);
    payable(winner).transfer(total);
    stats.incrementWON(winner);
    allowed_tobuy = false;
  }

  function refund() public OnlyOwner {
    uint256 a = 1;
    while(a <= ticketTRACK){
      payable(ticket[a]).transfer(weiVal);
      a = a+1;
    }
    allowed_tobuy = false;
  }

}