-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeFairLoto.sol
More file actions
115 lines (88 loc) · 4.24 KB
/
Copy paththeFairLoto.sol
File metadata and controls
115 lines (88 loc) · 4.24 KB
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract theFairLoto {
struct Commitment {
uint256 deposit;
uint256 timestamp;
bool claimed;
}
bytes32[] public commitments; // Store all commitments
uint256 public totalDeposits;
uint256 public randomResult;
bool public randomNumberGenerated;
address public owner;
uint256 public constant TTL = 30 days;
event CommitmentSubmitted(address indexed user, bytes32 commitment);
constructor() {
owner = msg.sender;
}
// Function to submit a commitment
function submitCommitment(bytes32 _commitment) public payable {
require(msg.value == 1 ether, "You must deposit exactly 1 ETH");
commitments.push(_commitment); // Store commitment in array
totalDeposits += msg.value;
emit CommitmentSubmitted(msg.sender, _commitment);
}
// Function to get the number of commitments
function getCommitmentCount() public view returns (uint256) {
return commitments.length;
}
// Random number generation after TTL has expired
function generateRandomNumber() public {
require(block.timestamp > block.timestamp + TTL, "Too early to generate random number");
require(!randomNumberGenerated, "Random number already generated");
uint256 commitmentCount = commitments.length;
require(commitmentCount > 0, "No commitments made");
// Generate a random index based on the number of participants
randomResult = uint256(keccak256(abi.encodePacked(block.timestamp, block.difficulty))) % 10000; // Range 1-10000
randomNumberGenerated = true;
}
// Function to get the winning number
function getWinningNumber() public view returns (uint256) {
require(randomNumberGenerated, "Random number has not been generated yet");
return randomResult;
}
// Verify zk-SNARK proof and allow winner to claim the prize
function submitProof(
uint256[8] calldata proof, // zk-SNARK proof elements
uint256[1] calldata input // zk-SNARK input (the commitment hash)
) public {
require(randomNumberGenerated, "Random number has not been generated yet");
// Verify zk-SNARK proof
bool valid = verifyProof(proof, input);
require(valid, "Invalid proof");
require(WinnerClaimedFlag, "Time of claimed winner isn't finish yet");
// If the proof is valid, the participant's commitment was based on the winning number
// This participant can claim their portion of the prize
Commitment storage commitment = commitments[bytes32(input[0])];
require(commitment.deposit > 0, "No commitment found");
require(!commitment.claimed, "Commitment already claimed");
// Transfer the prize (split among valid winners)
uint256 numberOfWinners = getNumberOfValidWinners();
uint256 share = totalDeposits / numberOfWinners;
commitment.claimed = true;
payable(msg.sender).transfer(share);
}
bool WinnerClaimedFlag = false;
// Function to calculate the number of valid winners
function getNumberOfValidWinners() internal view returns (uint256) onlyOwner {
// we limit this function to- 1 day afte the TTL- meaning
uint256 winnerCount = 0;
// Loop through all commitments and count the ones related to the winning number
for (uint256 i = 0; i < commitments.length; i++) {
if (isValidCommitment(commitments[i])) { // This function would check if the commitment is related to the winning number
winnerCount++;
}
}
// only going to be true once the prizepool(submission to the commitments is over and the winner was declared
if (timeOfPrizePool + block.timestamp) {
WinnerClaimedFlag = true
}
return winnerCount;
}
// zk-SNARKs or a different off-chain verification mechanism to ensure only valid commitments are considered
function isValidCommitment(bytes32 commitment) internal view returns (bool) {
// Logic to determine if the commitment corresponds to the winning number
// This can involve comparing with zk-SNARK proofs or other cryptographic methods
}
}