Ejemplo n.º 1
0
        public override Empty ClaimProfits(ClaimProfitsInput input)
        {
            var scheme      = GetValidScheme(input.SchemeManager);
            var beneficiary = input.Beneficiary ?? Context.Sender;

            State.ProfitContract.ClaimProfits.Send(new Profit.ClaimProfitsInput
            {
                SchemeId    = scheme.SchemeId,
                Beneficiary = beneficiary
            });
            return(new Empty());
        }
Ejemplo n.º 2
0
        public override ReceivedProfitsMap GetProfitsMap(ClaimProfitsInput input)
        {
            var scheme = State.SchemeInfos[input.SchemeId];

            Assert(scheme != null, "Scheme not found.");
            var beneficiary   = input.Beneficiary ?? Context.Sender;
            var profitDetails = State.ProfitDetailsMap[input.SchemeId][beneficiary];

            if (profitDetails == null)
            {
                return(new ReceivedProfitsMap());
            }

            var profitVirtualAddress = Context.ConvertVirtualAddressToContractAddress(input.SchemeId);

            // ReSharper disable once PossibleNullReferenceException
            var availableDetails = profitDetails.Details.Where(d =>
                                                               d.LastProfitPeriod < scheme.CurrentPeriod && (d.LastProfitPeriod == 0
                    ? d.EndPeriod >= d.StartPeriod
                    : d.EndPeriod >= d.LastProfitPeriod)
                                                               ).ToList();

            var profitsDict = new Dictionary <string, long>();

            for (var i = 0;
                 i < Math.Min(ProfitContractConstants.ProfitReceivingLimitForEachTime, availableDetails.Count);
                 i++)
            {
                var profitDetail = availableDetails[i];
                if (profitDetail.LastProfitPeriod == 0)
                {
                    profitDetail.LastProfitPeriod = profitDetail.StartPeriod;
                }

                var profitsDictForEachProfitDetail = ProfitAllPeriods(scheme, profitDetail, profitVirtualAddress, beneficiary, true);
                foreach (var kv in profitsDictForEachProfitDetail)
                {
                    if (profitsDict.ContainsKey(kv.Key))
                    {
                        profitsDict[kv.Key] = profitsDict[kv.Key].Add(kv.Value);
                    }
                    else
                    {
                        profitsDict[kv.Key] = kv.Value;
                    }
                }
            }

            return(new ReceivedProfitsMap
            {
                Value = { profitsDict }
            });
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gain the profit form SchemeId from Details.lastPeriod to scheme.currentPeriod - 1;
        /// </summary>
        /// <param name="input">ClaimProfitsInput</param>
        /// <returns></returns>
        public override Empty ClaimProfits(ClaimProfitsInput input)
        {
            Assert(input.Symbol != null && input.Symbol.Any(), "Invalid token symbol.");
            if (input.Symbol == null)
            {
                return(new Empty());                      // Just to avoid IDE warning.
            }
            var scheme = State.SchemeInfos[input.SchemeId];

            Assert(scheme != null, "Scheme not found.");
            var profitDetails = State.ProfitDetailsMap[input.SchemeId][Context.Sender];

            Assert(profitDetails != null, "Profit details not found.");
            if (profitDetails == null || scheme == null)
            {
                return(new Empty());                                         // Just to avoid IDE warning.
            }
            Context.LogDebug(
                () => $"{Context.Sender} is trying to profit {input.Symbol} from {input.SchemeId.ToHex()}.");

            var profitVirtualAddress = Context.ConvertVirtualAddressToContractAddress(input.SchemeId);

            var availableDetails  = profitDetails.Details.Where(d => d.EndPeriod >= d.LastProfitPeriod).ToList();
            var profitableDetails = availableDetails.Where(d => d.LastProfitPeriod < scheme.CurrentPeriod).ToList();

            Context.LogDebug(() =>
                             $"Profitable details: {profitableDetails.Aggregate("\n", (profit1, profit2) => profit1.ToString() + "\n" + profit2.ToString())}");

            // Only can get profit from last profit period to actual last period (profit.CurrentPeriod - 1),
            // because current period not released yet.
            for (var i = 0;
                 i < Math.Min(ProfitContractConstants.ProfitReceivingLimitForEachTime, profitableDetails.Count);
                 i++)
            {
                var profitDetail = profitableDetails[i];
                if (profitDetail.LastProfitPeriod == 0)
                {
                    // This detail never performed profit before.
                    profitDetail.LastProfitPeriod = profitDetail.StartPeriod;
                }

                ProfitAllPeriods(scheme, input.Symbol, profitDetail, profitVirtualAddress);
            }

            State.ProfitDetailsMap[input.SchemeId][Context.Sender] = new ProfitDetails {
                Details = { availableDetails }
            };

            return(new Empty());
        }
Ejemplo n.º 4
0
        public override ReceivedProfitsMap GetProfitsMap(ClaimProfitsInput input)
        {
            var scheme     = State.TokenHolderProfitSchemes[input.SchemeManager];
            var profitsMap = State.ProfitContract.GetProfitsMap.Call(new Profit.ClaimProfitsInput
            {
                SchemeId    = scheme.SchemeId,
                Beneficiary = input.Beneficiary ?? Context.Sender
            });

            return(new ReceivedProfitsMap
            {
                Value = { profitsMap.Value }
            });
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Gain the profit form SchemeId from Details.lastPeriod to scheme.currentPeriod - 1;
        /// </summary>
        /// <param name="input">ClaimProfitsInput</param>
        /// <returns></returns>
        public override Empty ClaimProfits(ClaimProfitsInput input)
        {
            var scheme = State.SchemeInfos[input.SchemeId];

            Assert(scheme != null, "Scheme not found.");
            var beneficiary   = input.Beneficiary ?? Context.Sender;
            var profitDetails = State.ProfitDetailsMap[input.SchemeId][beneficiary];

            Assert(profitDetails != null, "Profit details not found.");

            Context.LogDebug(
                () => $"{Context.Sender} is trying to profit from {input.SchemeId.ToHex()} for {beneficiary}.");

            var profitVirtualAddress = Context.ConvertVirtualAddressToContractAddress(input.SchemeId);

            // ReSharper disable once PossibleNullReferenceException
            var availableDetails = profitDetails.Details.Where(d =>
                                                               d.LastProfitPeriod == 0 ? d.EndPeriod >= d.StartPeriod : d.EndPeriod >= d.LastProfitPeriod).ToList();
            // ReSharper disable once PossibleNullReferenceException
            var profitableDetails = availableDetails.Where(d => d.LastProfitPeriod < scheme.CurrentPeriod).ToList();

            Context.LogDebug(() =>
                             $"Profitable details: {profitableDetails.Aggregate("\n", (profit1, profit2) => profit1.ToString() + "\n" + profit2)}");

            // Only can get profit from last profit period to actual last period (profit.CurrentPeriod - 1),
            // because current period not released yet.
            for (var i = 0;
                 i < Math.Min(ProfitContractConstants.ProfitReceivingLimitForEachTime, profitableDetails.Count);
                 i++)
            {
                var profitDetail = profitableDetails[i];
                if (profitDetail.LastProfitPeriod == 0)
                {
                    // This detail never performed profit before.
                    profitDetail.LastProfitPeriod = profitDetail.StartPeriod;
                }

                ProfitAllPeriods(scheme, profitDetail, profitVirtualAddress, beneficiary);
            }

            State.ProfitDetailsMap[input.SchemeId][beneficiary] = new ProfitDetails {
                Details = { availableDetails }
            };

            return(new Empty());
        }
Ejemplo n.º 6
0
        public override SInt64Value GetProfitAmount(ClaimProfitsInput input)
        {
            var profitItem = State.SchemeInfos[input.SchemeId];

            Assert(profitItem != null, "Scheme not found.");
            var beneficiary   = input.Beneficiary ?? Context.Sender;
            var profitDetails = State.ProfitDetailsMap[input.SchemeId][beneficiary];

            if (profitDetails == null)
            {
                return(new SInt64Value {
                    Value = 0
                });
            }

            var profitVirtualAddress = Context.ConvertVirtualAddressToContractAddress(input.SchemeId);

            // ReSharper disable once PossibleNullReferenceException
            var availableDetails = profitDetails.Details.Where(d =>
                                                               d.LastProfitPeriod < profitItem.CurrentPeriod && d.EndPeriod >= d.LastProfitPeriod
                                                               ).ToList();

            var amount = 0L;

            for (var i = 0;
                 i < Math.Min(ProfitContractConstants.ProfitReceivingLimitForEachTime, availableDetails.Count);
                 i++)
            {
                var profitDetail = availableDetails[i];
                if (profitDetail.LastProfitPeriod == 0)
                {
                    profitDetail.LastProfitPeriod = profitDetail.StartPeriod;
                }

                amount = amount.Add(
                    ProfitAllPeriods(profitItem, input.Symbol, profitDetail, profitVirtualAddress, beneficiary, true));
            }

            return(new SInt64Value {
                Value = amount
            });
        }