feat(VotingContract): make voting length configurable

This commit is contained in:
Patryk Osmaczko 2023-03-21 14:32:36 +01:00 committed by osmaczko
parent 910c0a41d6
commit f7579494bd
3 changed files with 14 additions and 10 deletions

View File

@ -13,9 +13,6 @@ contract VotingContract {
using ECDSA for bytes32;
using SafeMath for uint256;
uint256 private constant VOTING_LENGTH = 1000;
uint256 private constant TIME_BETWEEN_VOTING = 3600;
enum VoteType {
REMOVE,
ADD
@ -57,6 +54,9 @@ contract VotingContract {
Directory public directory;
IERC20 public token;
uint256 public votingLength;
uint256 public timeBetweenVoting;
VotingRoom[] public votingRooms;
mapping(bytes => uint256) public activeRoomIDByCommunityID;
mapping(bytes => uint256[]) private roomIDsByCommunityID;
@ -100,9 +100,11 @@ contract VotingContract {
return digest.recover(vote.r, vote.vs) == vote.voter;
}
constructor(IERC20 _address) {
constructor(IERC20 _address, uint256 _votingLength, uint256 _timeBetweenVoting) {
owner = msg.sender;
token = _address;
votingLength = _votingLength;
timeBetweenVoting = _timeBetweenVoting;
DOMAIN_SEPARATOR = hash(
EIP712Domain({
name: 'Voting Contract',
@ -180,7 +182,7 @@ contract VotingContract {
if (historyLength > 0) {
uint256 roomId = roomIDsByCommunityID[publicKey][historyLength - 1];
require(
block.timestamp.sub(_getVotingRoom(roomId).endAt) > TIME_BETWEEN_VOTING,
block.timestamp.sub(_getVotingRoom(roomId).endAt) > timeBetweenVoting,
'Community was in a vote recently'
);
}
@ -196,7 +198,7 @@ contract VotingContract {
votingRooms.push(
VotingRoom({
startBlock: block.number,
endAt: block.timestamp.add(VOTING_LENGTH),
endAt: block.timestamp.add(votingLength),
voteType: voteType,
finalized: false,
community: publicKey,

View File

@ -9,9 +9,9 @@ const hre = require("hardhat");
import { ERC20Mock, MultiCall } from '../abi';
import { BigNumber } from 'ethers';
async function deployVotingContract(tokenAddress: string) {
async function deployVotingContract(tokenAddress: string, votingLength, timeBetweenVoting) {
const contractFactory = await hre.ethers.getContractFactory('VotingContract');
const contract = await contractFactory.deploy(tokenAddress);
const contract = await contractFactory.deploy(tokenAddress, votingLength, timeBetweenVoting);
await contract.deployed();
console.log(`Voting contract deployed with address: ${contract.address}`);
@ -67,6 +67,8 @@ async function obtainTokenAddress(deployer, chainId): Promise<string> {
async function main() {
const [deployer] = await hre.ethers.getSigners();
const network = await hre.ethers.provider.getNetwork();
const votingLengthInSeconds = isTestNetwork(network.chainId) ? 2 * 60 : 30 * 24 * 3600 // 2 minutes or 30 days
const timeBetweenVotingInSeconds = isTestNetwork(network.chainId) ? 60 : 7 * 24 * 3600 // 1 minute or 7 days
console.log(`Deploying contracts on the network: ${network.name}(${network.chainId}), with the account: ${deployer.address}`);
@ -75,7 +77,7 @@ async function main() {
}
const tokenAddress = await obtainTokenAddress(deployer, network.chainId);
const votingContract = await deployVotingContract(tokenAddress);
const votingContract = await deployVotingContract(tokenAddress, votingLengthInSeconds, timeBetweenVotingInSeconds);
const directoryContract = await deployDirectoryContract(votingContract.address);
await votingContract.setDirectory(directoryContract.address)
}

View File

@ -99,7 +99,7 @@ async function fixture() {
await erc20Contract.transfer(thirdSigner.address, 10000)
const votingContractFactory = await ethers.getContractFactory('VotingContract')
const votingContract = await votingContractFactory.deploy(erc20Contract.address)
const votingContract = await votingContractFactory.deploy(erc20Contract.address, 1000, 3600)
const directoryContractFactory = await ethers.getContractFactory('Directory')
const directoryContract = await directoryContractFactory.deploy(votingContract.address)