Пример #1
0
 private void AssertValidInput(AddBeneficiaryInput input)
 {
     Assert(input.SchemeId != null, "Invalid scheme id.");
     Assert(input.BeneficiaryShare?.Beneficiary != null, "Invalid beneficiary address.");
     Assert(input.BeneficiaryShare?.Shares >= 0, "Invalid share.");
 }
Пример #2
0
        public override Empty AddBeneficiary(AddBeneficiaryInput input)
        {
            Assert(input.SchemeId != null, "Invalid scheme id.");
            Assert(input.BeneficiaryShare?.Beneficiary != null, "Invalid beneficiary address.");
            Assert(input.BeneficiaryShare?.Shares >= 0, "Invalid share.");
            if (input.BeneficiaryShare == null)
            {
                return(new Empty());
            }

            if (input.EndPeriod == 0)
            {
                // Which means this profit Beneficiary will never expired.
                input.EndPeriod = long.MaxValue;
            }

            var schemeId = input.SchemeId;
            var scheme   = State.SchemeInfos[schemeId];

            Assert(scheme != null, "Scheme not found.");
            if (scheme == null)
            {
                return(new Empty());
            }

            Assert(Context.Sender == scheme.Manager, "Only manager can add beneficiary.");

            Context.LogDebug(() =>
                             $"{input.SchemeId}.\n End Period: {input.EndPeriod}, Current Period: {scheme.CurrentPeriod}");

            Assert(input.EndPeriod >= scheme.CurrentPeriod, "Invalid end period.");

            scheme.TotalShares = scheme.TotalShares.Add(input.BeneficiaryShare.Shares);

            State.SchemeInfos[schemeId] = scheme;

            var profitDetail = new ProfitDetail
            {
                StartPeriod = scheme.CurrentPeriod.Add(scheme.DelayDistributePeriodCount),
                EndPeriod   = input.EndPeriod,
                Shares      = input.BeneficiaryShare.Shares,
            };

            var currentProfitDetails = State.ProfitDetailsMap[schemeId][input.BeneficiaryShare.Beneficiary];

            if (currentProfitDetails == null)
            {
                currentProfitDetails = new ProfitDetails
                {
                    Details = { profitDetail }
                };
            }
            else
            {
                currentProfitDetails.Details.Add(profitDetail);
            }

            // Remove details too old.
            foreach (var detail in currentProfitDetails.Details.Where(
                         d => d.EndPeriod != long.MaxValue && d.LastProfitPeriod >= d.EndPeriod &&
                         d.EndPeriod.Add(scheme.ProfitReceivingDuePeriodCount) < scheme.CurrentPeriod))
            {
                currentProfitDetails.Details.Remove(detail);
            }

            State.ProfitDetailsMap[schemeId][input.BeneficiaryShare.Beneficiary] = currentProfitDetails;

            Context.LogDebug(() =>
                             $"Added {input.BeneficiaryShare.Shares} weights to scheme {input.SchemeId.ToHex()}: {profitDetail}");

            return(new Empty());
        }
Пример #3
0
        public override Empty AddBeneficiary(AddBeneficiaryInput input)
        {
            AssertValidInput(input);
            if (input.EndPeriod == 0)
            {
                // Which means this profit Beneficiary will never expired unless removed.
                input.EndPeriod = long.MaxValue;
            }

            var schemeId = input.SchemeId;
            var scheme   = State.SchemeInfos[schemeId];

            Assert(scheme != null, "Scheme not found.");

            // ReSharper disable once PossibleNullReferenceException
            Assert(
                Context.Sender == scheme.Manager || Context.Sender ==
                Context.GetContractAddressByName(SmartContractConstants.TokenHolderContractSystemName),
                "Only manager can add beneficiary.");

            Context.LogDebug(() =>
                             $"{input.SchemeId}.\n End Period: {input.EndPeriod}, Current Period: {scheme.CurrentPeriod}");

            Assert(input.EndPeriod >= scheme.CurrentPeriod,
                   $"Invalid end period. End Period: {input.EndPeriod}, Current Period: {scheme.CurrentPeriod}");

            scheme.TotalShares = scheme.TotalShares.Add(input.BeneficiaryShare.Shares);

            State.SchemeInfos[schemeId] = scheme;

            var profitDetail = new ProfitDetail
            {
                StartPeriod = scheme.CurrentPeriod.Add(scheme.DelayDistributePeriodCount),
                EndPeriod   = input.EndPeriod,
                Shares      = input.BeneficiaryShare.Shares,
            };

            var currentProfitDetails = State.ProfitDetailsMap[schemeId][input.BeneficiaryShare.Beneficiary];

            if (currentProfitDetails == null)
            {
                currentProfitDetails = new ProfitDetails
                {
                    Details = { profitDetail }
                };
            }
            else
            {
                currentProfitDetails.Details.Add(profitDetail);
            }

            // Remove details too old.
            var oldProfitDetails = currentProfitDetails.Details.Where(
                d => d.EndPeriod != long.MaxValue && d.LastProfitPeriod >= d.EndPeriod &&
                d.EndPeriod.Add(scheme.ProfitReceivingDuePeriodCount) < scheme.CurrentPeriod).ToList();

            foreach (var detail in oldProfitDetails)
            {
                currentProfitDetails.Details.Remove(detail);
            }

            State.ProfitDetailsMap[schemeId][input.BeneficiaryShare.Beneficiary] = currentProfitDetails;

            Context.LogDebug(() =>
                             $"Added {input.BeneficiaryShare.Shares} weights to scheme {input.SchemeId.ToHex()}: {profitDetail}");

            return(new Empty());
        }