Browse Source

Minor reformatting

develop
Ezerous 4 years ago
parent
commit
00510656f1
  1. 12
      packages/concordia-contracts/contracts/Forum.sol
  2. 12
      packages/concordia-contracts/contracts/PostVoting.sol
  3. 10
      packages/concordia-contracts/contracts/Voting.sol

12
packages/concordia-contracts/contracts/Forum.sol

@ -24,7 +24,7 @@ contract Forum {
event UsernameUpdated(string newName, string oldName, address userAddress); event UsernameUpdated(string newName, string oldName, address userAddress);
function signUp(string memory username) public returns (bool) { function signUp(string memory username) public returns (bool) {
require (!hasUserSignedUp(msg.sender), USER_HAS_NOT_SIGNED_UP); require(!hasUserSignedUp(msg.sender), USER_HAS_NOT_SIGNED_UP);
require(!isUserNameTaken(username), USERNAME_TAKEN); require(!isUserNameTaken(username), USERNAME_TAKEN);
users[msg.sender] = User(username, new uint[](0), new uint[](0), block.timestamp, true); users[msg.sender] = User(username, new uint[](0), new uint[](0), block.timestamp, true);
userAddresses[username] = msg.sender; userAddresses[username] = msg.sender;
@ -33,7 +33,7 @@ contract Forum {
} }
function updateUsername(string memory newUsername) public returns (bool) { function updateUsername(string memory newUsername) public returns (bool) {
require (hasUserSignedUp(msg.sender), USER_HAS_NOT_SIGNED_UP); require(hasUserSignedUp(msg.sender), USER_HAS_NOT_SIGNED_UP);
require(!isUserNameTaken(newUsername), USERNAME_TAKEN); require(!isUserNameTaken(newUsername), USERNAME_TAKEN);
string memory oldUsername = getUsername(msg.sender); string memory oldUsername = getUsername(msg.sender);
delete userAddresses[users[msg.sender].username]; delete userAddresses[users[msg.sender].username];
@ -44,7 +44,7 @@ contract Forum {
} }
function getUsername(address userAddress) public view returns (string memory) { function getUsername(address userAddress) public view returns (string memory) {
require (hasUserSignedUp(userAddress), USER_HAS_NOT_SIGNED_UP); require(hasUserSignedUp(userAddress), USER_HAS_NOT_SIGNED_UP);
return users[userAddress].username; return users[userAddress].username;
} }
@ -63,17 +63,17 @@ contract Forum {
} }
function getUserTopics(address userAddress) public view returns (uint[] memory) { function getUserTopics(address userAddress) public view returns (uint[] memory) {
require (hasUserSignedUp(userAddress), USER_HAS_NOT_SIGNED_UP); require(hasUserSignedUp(userAddress), USER_HAS_NOT_SIGNED_UP);
return users[userAddress].topicIDs; return users[userAddress].topicIDs;
} }
function getUserPosts(address userAddress) public view returns (uint[] memory) { function getUserPosts(address userAddress) public view returns (uint[] memory) {
require (hasUserSignedUp(userAddress), USER_HAS_NOT_SIGNED_UP); require(hasUserSignedUp(userAddress), USER_HAS_NOT_SIGNED_UP);
return users[userAddress].postIDs; return users[userAddress].postIDs;
} }
function getUserDateOfRegister(address userAddress) public view returns (uint) { function getUserDateOfRegister(address userAddress) public view returns (uint) {
require (hasUserSignedUp(userAddress), USER_HAS_NOT_SIGNED_UP); require(hasUserSignedUp(userAddress), USER_HAS_NOT_SIGNED_UP);
return users[userAddress].timestamp; return users[userAddress].timestamp;
} }

12
packages/concordia-contracts/contracts/PostVoting.sol

@ -77,11 +77,11 @@ contract PostVoting {
address voter = msg.sender; address voter = msg.sender;
Option prevOption = postBallot.votes[voter]; Option prevOption = postBallot.votes[voter];
if(prevOption == option) if (prevOption == option)
return; return;
// Remove previous vote if exists // Remove previous vote if exists
if(prevOption != Option.DEFAULT){ if (prevOption != Option.DEFAULT) {
uint voterIndex = getVoterIndex(postID, voter); uint voterIndex = getVoterIndex(postID, voter);
// Swap with last voter address and delete vote // Swap with last voter address and delete vote
postBallot.voters[prevOption][voterIndex] = postBallot.voters[prevOption][postBallot.voters[prevOption].length - 1]; postBallot.voters[prevOption][voterIndex] = postBallot.voters[prevOption][postBallot.voters[prevOption].length - 1];
@ -89,21 +89,21 @@ contract PostVoting {
} }
// Add new vote // Add new vote
if(option != Option.DEFAULT) if (option != Option.DEFAULT)
postBallot.voters[option].push(voter); postBallot.voters[option].push(voter);
postBallot.votes[voter] = option; postBallot.votes[voter] = option;
emit UserVotedPost(voter, postID, option); emit UserVotedPost(voter, postID, option);
} }
function upvote(uint postID) public{ function upvote(uint postID) public {
vote(postID, Option.UP); vote(postID, Option.UP);
} }
function downvote(uint postID) public{ function downvote(uint postID) public {
vote(postID, Option.DOWN); vote(postID, Option.DOWN);
} }
function unvote(uint postID) public{ function unvote(uint postID) public {
vote(postID, Option.DEFAULT); vote(postID, Option.DEFAULT);
} }
} }

10
packages/concordia-contracts/contracts/Voting.sol

@ -41,7 +41,7 @@ contract Voting {
function createPoll(uint topicID, uint numOptions, string memory dataHash, bool enableVoteChanges) public returns (uint) { function createPoll(uint topicID, uint numOptions, string memory dataHash, bool enableVoteChanges) public returns (uint) {
require(forum.hasUserSignedUp(msg.sender), forum.USER_HAS_NOT_SIGNED_UP()); require(forum.hasUserSignedUp(msg.sender), forum.USER_HAS_NOT_SIGNED_UP());
require(forum.topicExists(topicID), forum.TOPIC_DOES_NOT_EXIST()); require(forum.topicExists(topicID), forum.TOPIC_DOES_NOT_EXIST());
require (forum.getTopicAuthor(topicID) == msg.sender, TOPIC_POLL_DIFFERENT_CREATOR); require(forum.getTopicAuthor(topicID) == msg.sender, TOPIC_POLL_DIFFERENT_CREATOR);
require(!pollExists(topicID), POLL_EXISTS); require(!pollExists(topicID), POLL_EXISTS);
Poll storage poll = polls[topicID]; Poll storage poll = polls[topicID];
@ -132,21 +132,21 @@ contract Voting {
Poll storage poll = polls[topicID]; Poll storage poll = polls[topicID];
address voter = msg.sender; address voter = msg.sender;
uint prevOption = poll.votes[voter]; uint prevOption = poll.votes[voter];
if(prevOption == option) if (prevOption == option)
return; return;
// Voter hasn't voted before // Voter hasn't voted before
if(prevOption == 0){ if (prevOption == 0) {
poll.voters[option].push(voter); poll.voters[option].push(voter);
poll.votes[voter] = option; poll.votes[voter] = option;
emit UserVotedPoll(voter, topicID, option); emit UserVotedPoll(voter, topicID, option);
} }
else if (poll.enableVoteChanges){ else if (poll.enableVoteChanges) {
uint voterIndex = getVoterIndex(topicID, voter); uint voterIndex = getVoterIndex(topicID, voter);
// Swap with last voter address and delete vote // Swap with last voter address and delete vote
poll.voters[prevOption][voterIndex] = poll.voters[prevOption][poll.voters[prevOption].length - 1]; poll.voters[prevOption][voterIndex] = poll.voters[prevOption][poll.voters[prevOption].length - 1];
poll.voters[prevOption].pop(); poll.voters[prevOption].pop();
if(option != 0) if (option != 0)
poll.voters[option].push(voter); poll.voters[option].push(voter);
poll.votes[voter] = option; poll.votes[voter] = option;
emit UserVotedPoll(voter, topicID, option); emit UserVotedPoll(voter, topicID, option);

Loading…
Cancel
Save