public override Empty Register(VotingRegisterInput input) { var votingItemId = AssertValidNewVotingItem(input); if (State.TokenContract.Value == null) { State.TokenContract.Value = Context.GetContractAddressByName(SmartContractConstants.TokenContractSystemName); } // Accepted currency is in white list means this token symbol supports voting. //Length of symbol has been set and unnecessary to set length of AcceptedCurrency. var isInWhiteList = State.TokenContract.IsInWhiteList.Call(new IsInWhiteListInput { Symbol = input.AcceptedCurrency, Address = Context.Self }).Value; Assert(isInWhiteList, "Claimed accepted token is not available for voting."); // Initialize voting event. var votingItem = new VotingItem { Sponsor = Context.Sender, VotingItemId = votingItemId, AcceptedCurrency = input.AcceptedCurrency, IsLockToken = input.IsLockToken, TotalSnapshotNumber = input.TotalSnapshotNumber, CurrentSnapshotNumber = 1, CurrentSnapshotStartTimestamp = input.StartTimestamp, StartTimestamp = input.StartTimestamp, EndTimestamp = input.EndTimestamp, RegisterTimestamp = Context.CurrentBlockTime, Options = { input.Options } }; State.VotingItems[votingItemId] = votingItem; // Initialize first voting going information of registered voting event. var votingResultHash = GetVotingResultHash(votingItemId, 1); State.VotingResults[votingResultHash] = new VotingResult { VotingItemId = votingItemId, SnapshotNumber = 1, SnapshotStartTimestamp = input.StartTimestamp }; return(new Empty()); }
/// <summary> /// Update the State.VotingResults.include the VotersCount,VotesAmount and the votes int the results[option] /// </summary> /// <param name="votingItem"></param> /// <param name="option"></param> /// <param name="amount"></param> private void UpdateVotingResult(VotingItem votingItem, string option, long amount) { // Update VotingResult based on this voting behaviour. var votingResultHash = GetVotingResultHash(votingItem.VotingItemId, votingItem.CurrentSnapshotNumber); var votingResult = State.VotingResults[votingResultHash]; if (!votingResult.Results.ContainsKey(option)) { votingResult.Results.Add(option, 0); } var currentVotes = votingResult.Results[option]; votingResult.Results[option] = currentVotes.Add(amount); votingResult.VotersCount = votingResult.VotersCount.Add(1); votingResult.VotesAmount = votingResult.VotesAmount.Add(amount); State.VotingResults[votingResultHash] = votingResult; }
private void UpdateVotedItems(Hash voteId, Address voter, VotingItem votingItem) { var votedItems = State.VotedItemsMap[voter] ?? new VotedItems(); if (votedItems.VotedItemVoteIds.ContainsKey(votingItem.VotingItemId.ToHex())) { votedItems.VotedItemVoteIds[votingItem.VotingItemId.ToHex()].ActiveVotes.Add(voteId); } else { votedItems.VotedItemVoteIds[votingItem.VotingItemId.ToHex()] = new VotedIds { ActiveVotes = { voteId } }; } State.VotedItemsMap[voter] = votedItems; }
private void UpdateVotedItems(Hash voteId, Address voter, VotingItem votingItem) { var votedItems = State.VotedItemsMap[voter] ?? new VotedItems(); var voterItemIndex = votingItem.VotingItemId.ToHex(); if (votedItems.VotedItemVoteIds.ContainsKey(voterItemIndex)) { votedItems.VotedItemVoteIds[voterItemIndex].ActiveVotes.Add(voteId); } else { votedItems.VotedItemVoteIds[voterItemIndex] = new VotedIds { ActiveVotes = { voteId } }; } votedItems.VotedItemVoteIds[voterItemIndex].WithdrawnVotes.Remove(voteId); State.VotedItemsMap[voter] = votedItems; }
private void AssertOption(VotingItem votingItem, string option) { Assert(option.Length <= VoteContractConstants.OptionLengthLimit, "Invalid input."); Assert(!votingItem.Options.Contains(option), "Option already exists."); }