Created
May 3, 2023 05:28
-
-
Save alonmuroch/cbdda9d5e595354739b4e79b581cc4bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.8.2 <0.9.0; | |
/** | |
* @title Voting | |
* @dev Store & retrieve value in a variable | |
* @custom:dev-run-script ./scripts/deploy_with_ethers.ts | |
*/ | |
contract Voting { | |
struct vote { | |
bool finalized; | |
uint slot; | |
uint votes; | |
} | |
mapping(bytes => mapping(address => bool)) public voted; | |
mapping(bytes => vote) public committeed; | |
mapping(address => uint) public balances; | |
uint staked; | |
uint slot; | |
bytes lastSnapshot; | |
/** | |
* @dev Store value in variable | |
* @param num value to store | |
*/ | |
function stake(uint256 num) public { | |
balances[msg.sender] = num; | |
staked += num; | |
} | |
/** | |
* @dev Return value | |
*/ | |
function voteOn(bytes calldata root) public { | |
require(committeed[root].finalized == false); | |
require(balances[msg.sender] > 0); | |
committeed[root].votes += balances[msg.sender]; | |
if (committeed[root].votes >= quorum()) { | |
committeed[root].slot = slot; | |
lastSnapshot = root; | |
slot++; | |
} | |
} | |
function getSlot() view public returns (uint) { | |
return slot; | |
} | |
function getLastSnapshot() view public returns (bytes memory) { | |
return lastSnapshot; | |
} | |
function quorum() pure internal returns (uint) { | |
return 75; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment