From e7b3ea37f6d63c6ed990a00e82b0bad7f8688ba7 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Mon, 16 Nov 2020 13:13:39 +0200 Subject: [PATCH] Voting contract init --- .../concordia-contracts/contracts/Forum.sol | 23 +++-- .../contracts/Migrations.sol | 5 +- .../concordia-contracts/contracts/Voting.sol | 90 +++++++++++++++++++ .../migrations/2_deploy_contracts.js | 3 +- .../concordia-contracts/truffle-config.js | 2 +- 5 files changed, 110 insertions(+), 13 deletions(-) create mode 100644 packages/concordia-contracts/contracts/Voting.sol diff --git a/packages/concordia-contracts/contracts/Forum.sol b/packages/concordia-contracts/contracts/Forum.sol index 13ad2ea..ae6ec07 100644 --- a/packages/concordia-contracts/contracts/Forum.sol +++ b/packages/concordia-contracts/contracts/Forum.sol @@ -1,5 +1,5 @@ //SPDX-License-Identifier: MIT -pragma solidity 0.7.1; +pragma solidity 0.7.4; contract Forum { @@ -133,10 +133,10 @@ contract Forum { function getTopic(uint topicID) public view returns (address, string memory, uint, uint[] memory) { require(topicID uint) voters; + uint[] voteCounts; // First element will hold total count + uint timestamp; + } + + mapping (uint => Poll) polls; + + event PollCreated(uint topicID); + event UserVoted(address userAddress); + + function createPoll(uint topicID, uint numOptions, string memory dataHash) public returns (uint) { + require(forum.hasUserSignedUp(msg.sender)); // Only registered users can create polls + require(topicID 0 && option <= poll.numOptions); // Verify that this option exists + address voter = msg.sender; + uint currentVote = poll.voters[voter]; + if(currentVote == option) + return; + if(currentVote == 0) // Voter hadn't voted before + poll.voteCounts[0]++; + else + poll.voteCounts[currentVote]--; + poll.voteCounts[option]++; + poll.voters[voter] = option; + emit UserVoted(voter); + } +} diff --git a/packages/concordia-contracts/migrations/2_deploy_contracts.js b/packages/concordia-contracts/migrations/2_deploy_contracts.js index ec5722e..3e81c0e 100644 --- a/packages/concordia-contracts/migrations/2_deploy_contracts.js +++ b/packages/concordia-contracts/migrations/2_deploy_contracts.js @@ -1,6 +1,7 @@ const Forum = artifacts.require('Forum'); +const Voting = artifacts.require('Voting'); // eslint-disable-next-line func-names module.exports = function (deployer) { - deployer.deploy(Forum); + deployer.deploy(Forum).then((forum) => deployer.deploy(Voting, forum.address)); }; diff --git a/packages/concordia-contracts/truffle-config.js b/packages/concordia-contracts/truffle-config.js index 3ef72a1..d66ff67 100644 --- a/packages/concordia-contracts/truffle-config.js +++ b/packages/concordia-contracts/truffle-config.js @@ -8,7 +8,7 @@ module.exports = { // to customize your Truffle configuration! compilers: { solc: { - version: '0.7.1', + version: '0.7.4', }, }, contracts_build_directory: path.join(__dirname, 'build/'),