-
Notifications
You must be signed in to change notification settings - Fork 12.2k
/
Copy pathdecentralized
39 lines (31 loc) · 1.1 KB
/
decentralized
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract DecentralizedAlgorithm {
struct Algorithm {
string name;
string code; // Pseudocode or reference to the algorithm
uint256 votes;
}
Algorithm[] public algorithms;
mapping(address => uint256) public userVotes;
event AlgorithmAdded(string name, string code);
event Voted(address indexed user, uint256 algorithmId);
function addAlgorithm(string memory _name, string memory _code) public {
algorithms.push(Algorithm({
name: _name,
code: _code,
votes: 0
}));
emit AlgorithmAdded(_name, _code);
}
function vote(uint256 _algorithmId) public {
require(_algorithmId < algorithms.length, "Invalid algorithm ID");
require(userVotes[msg.sender] == 0, "User has already voted");
algorithms[_algorithmId].votes += 1;
userVotes[msg.sender] = _algorithmId;
emit Voted(msg.sender, _algorithmId);
}
function getAlgorithms() public view returns (Algorithm[] memory) {
return algorithms;
}
}