Пример #1
0
 public static Hash GetHash(this VotingEvent votingEvent)
 {
     return(Hash.FromMessage(new VotingEvent
     {
         Sponsor = votingEvent.Sponsor,
         Topic = votingEvent.Topic
     }));
 }
Пример #2
0
        public override Empty Withdraw(WithdrawInput input)
        {
            var votingRecord = State.VotingRecords[input.VoteId];

            Assert(votingRecord != null, "Voting record not found.");

            var votingEventHash = new VotingEvent
            {
                Topic   = votingRecord.Topic,
                Sponsor = votingRecord.Sponsor
            }.GetHash();

            var votingEvent = State.VotingEvents[votingEventHash];

            Assert(votingEvent.CurrentEpoch > votingRecord.EpochNumber,
                   "Cannot withdraw votes of on-going voting event.");

            // Update VotingRecord.
            votingRecord.IsWithdrawn          = true;
            votingRecord.WithdrawTimestamp    = Context.CurrentBlockTime.ToTimestamp();
            State.VotingRecords[input.VoteId] = votingRecord;

            var votingGoingHash = new VotingResult
            {
                Sponsor     = votingRecord.Sponsor,
                Topic       = votingRecord.Topic,
                EpochNumber = votingRecord.EpochNumber
            }.GetHash();

            var votingHistories = UpdateHistoryAfterWithdrawing(votingRecord.Voter, votingEventHash, input.VoteId);

            var votingResult = State.VotingResults[votingGoingHash];

            votingResult.Results[votingRecord.Option] -= votingRecord.Amount;
            if (!votingHistories.Votes[votingEventHash.ToHex()].ActiveVotes.Any())
            {
                votingResult.VotersCount -= 1;
            }

            State.VotingResults[votingGoingHash] = votingResult;

            if (!State.VotingEvents[votingEventHash].Delegated)
            {
                State.TokenContract.Unlock.Send(new UnlockInput
                {
                    From   = votingRecord.Voter,
                    Symbol = votingRecord.Currency,
                    Amount = votingRecord.Amount,
                    LockId = input.VoteId,
                    To     = Context.Self,
                    Usage  = $"Withdraw votes for {votingRecord.Topic}"
                });
            }

            return(new Empty());
        }
Пример #3
0
 private VotingEventViewModel ToVotinEventViewModel(VotingEvent votingEvent)
 {
     return(new VotingEventViewModel
     {
         Id = votingEvent.Id,
         Name = votingEvent.Name,
         Description = votingEvent.Description,
         StartDate = votingEvent.StartDate,
         EndDate = votingEvent.EndDate,
         ImageUrl = votingEvent.ImageUrl
     });
 }
Пример #4
0
        private VotingEvent AssertVotingEvent(string topic, Address sponsor)
        {
            var votingEvent = new VotingEvent
            {
                Topic   = topic,
                Sponsor = sponsor
            };
            var votingEventHash = votingEvent.GetHash();

            Assert(State.VotingEvents[votingEventHash] != null, "Voting event not found.");
            return(State.VotingEvents[votingEventHash]);
        }
Пример #5
0
        public override VotingEvent GetVotingEvent(GetVotingEventInput input)
        {
            var votingEventHash = new VotingEvent
            {
                Topic   = input.Topic,
                Sponsor = input.Sponsor
            }.GetHash();
            var votingEvent = State.VotingEvents[votingEventHash];

            Assert(votingEvent != null, "Voting Event not found.");
            return(votingEvent);
        }
Пример #6
0
        public override Empty Register(VotingRegisterInput input)
        {
            input.Topic = input.Topic.Trim();

            if (input.TotalEpoch == 0)
            {
                input.TotalEpoch = 1;
            }

            Assert(!string.IsNullOrEmpty(input.Topic), "Topic cannot be null or empty.");
            Assert(input.TotalEpoch > 0, "Total epoch number must be greater than 0.");
            Assert(input.ActiveDays > 0, "Total active days must be greater than 0.");
            Assert(input.Options != null && input.Options.Any(), "Options cannot be null or empty.");

            if (input.ActiveDays == int.MaxValue)
            {
                Assert(input.TotalEpoch != 1, "Cannot created endless voting event.");
            }

            InitializeDependentContracts();

            if (input.StartTimestamp == null || input.StartTimestamp.ToDateTime() < Context.CurrentBlockTime)
            {
                input.StartTimestamp = Context.CurrentBlockTime.ToTimestamp();
            }

            var votingEvent = new VotingEvent
            {
                Sponsor = Context.Sender,
                Topic   = input.Topic
            };
            var votingEventHash = votingEvent.GetHash();

            Assert(State.VotingEvents[votingEventHash] == null, "Voting event already exists.");
            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.
            votingEvent.AcceptedCurrency = input.AcceptedCurrency;
            votingEvent.ActiveDays       = input.ActiveDays;
            votingEvent.Delegated        = input.Delegated;
            votingEvent.TotalEpoch       = input.TotalEpoch;
            votingEvent.Options.AddRange(input.Options);
            votingEvent.CurrentEpoch        = 1;
            votingEvent.EpochStartTimestamp = input.StartTimestamp;
            votingEvent.RegisterTimestamp   = Context.CurrentBlockTime.ToTimestamp();
            votingEvent.StartTimestamp      = input.StartTimestamp;

            State.VotingEvents[votingEventHash] = votingEvent;

            // Initialize first voting going information of registered voting event.
            var votingResultHash = Hash.FromMessage(new GetVotingResultInput
            {
                Sponsor     = Context.Sender,
                Topic       = input.Topic,
                EpochNumber = 1
            });

            State.VotingResults[votingResultHash] = new VotingResult
            {
                Topic       = input.Topic,
                Sponsor     = Context.Sender,
                EpochNumber = 1
            };

            return(new Empty());
        }