Beispiel #1
0
        /// <summary>
        /// Create a Scheme of profit distribution.
        /// At the first time, the scheme's id is unknown,it may create by transaction id and createdSchemeIds;
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override Hash CreateScheme(CreateSchemeInput input)
        {
            ValidateContractState(State.TokenContract, SmartContractConstants.TokenContractSystemName);

            if (input.ProfitReceivingDuePeriodCount == 0)
            {
                input.ProfitReceivingDuePeriodCount = ProfitContractConstants.DefaultProfitReceivingDuePeriodCount;
            }
            else
            {
                Assert(
                    input.ProfitReceivingDuePeriodCount > 0 &&
                    input.ProfitReceivingDuePeriodCount <= ProfitContractConstants.MaximumProfitReceivingDuePeriodCount,
                    "Invalid profit receiving due period count.");
            }

            var manager  = input.Manager ?? Context.Sender;
            var schemeId = Context.TransactionId;
            // Why? Because one transaction may create many profit schemes via inline transactions.
            var createdSchemeIds = State.ManagingSchemeIds[manager]?.SchemeIds;

            if (createdSchemeIds != null && createdSchemeIds.Contains(schemeId))
            {
                // So we choose this way to avoid profit id conflicts in aforementioned situation.
                schemeId = Hash.FromTwoHashes(schemeId, createdSchemeIds.Last());
            }

            var scheme = GetNewScheme(input, schemeId, manager);

            State.SchemeInfos[schemeId] = scheme;

            var schemeIds = State.ManagingSchemeIds[scheme.Manager];

            if (schemeIds == null)
            {
                schemeIds = new CreatedSchemeIds
                {
                    SchemeIds = { schemeId }
                };
            }
            else
            {
                schemeIds.SchemeIds.Add(schemeId);
            }

            State.ManagingSchemeIds[scheme.Manager] = schemeIds;

            Context.LogDebug(() => $"Created scheme {State.SchemeInfos[schemeId]}");

            Context.Fire(new SchemeCreated
            {
                SchemeId = scheme.SchemeId,
                Manager  = scheme.Manager,
                IsReleaseAllBalanceEveryTimeByDefault = scheme.IsReleaseAllBalanceEveryTimeByDefault,
                ProfitReceivingDuePeriodCount         = scheme.ProfitReceivingDuePeriodCount,
                VirtualAddress = scheme.VirtualAddress
            });
            return(schemeId);
        }
Beispiel #2
0
        /// <summary>
        /// Create a Scheme of profit distribution.
        /// At the first time, the scheme's id is unknown,it may create by transaction id and createdSchemeIds;
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override Hash CreateScheme(CreateSchemeInput input)
        {
            ValidateContractState(State.TokenContract, SmartContractConstants.TokenContractSystemName);

            if (input.ProfitReceivingDuePeriodCount == 0)
            {
                input.ProfitReceivingDuePeriodCount = ProfitContractConstants.DefaultProfitReceivingDuePeriodCount;
            }
            else
            {
                Assert(
                    input.ProfitReceivingDuePeriodCount > 0 &&
                    input.ProfitReceivingDuePeriodCount <= ProfitContractConstants.MaximumProfitReceivingDuePeriodCount,
                    "Invalid profit receiving due period count.");
            }

            var schemeId = GenerateSchemeId(input);
            var manager  = input.Manager ?? Context.Sender;
            var scheme   = GetNewScheme(input, schemeId, manager);

            Assert(State.SchemeInfos[schemeId] == null, "Already exists.");
            State.SchemeInfos[schemeId] = scheme;

            var schemeIds = State.ManagingSchemeIds[scheme.Manager];

            if (schemeIds == null)
            {
                schemeIds = new CreatedSchemeIds
                {
                    SchemeIds = { schemeId }
                };
            }
            else
            {
                schemeIds.SchemeIds.Add(schemeId);
            }

            State.ManagingSchemeIds[scheme.Manager] = schemeIds;

            Context.LogDebug(() => $"Created scheme {State.SchemeInfos[schemeId]}");

            Context.Fire(new SchemeCreated
            {
                SchemeId = scheme.SchemeId,
                Manager  = scheme.Manager,
                IsReleaseAllBalanceEveryTimeByDefault = scheme.IsReleaseAllBalanceEveryTimeByDefault,
                ProfitReceivingDuePeriodCount         = scheme.ProfitReceivingDuePeriodCount,
                VirtualAddress = scheme.VirtualAddress
            });
            return(schemeId);
        }
Beispiel #3
0
        /// <summary>
        /// Create a Scheme of profit distribution.
        /// At the first time,the scheme's id is unknown,it may create by transaction id and createdSchemeIds;
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override Hash CreateScheme(CreateSchemeInput input)
        {
            if (State.TokenContract.Value == null)
            {
                State.TokenContract.Value =
                    Context.GetContractAddressByName(SmartContractConstants.TokenContractSystemName);
            }

            if (input.ProfitReceivingDuePeriodCount == 0)
            {
                input.ProfitReceivingDuePeriodCount = ProfitContractConstants.DefaultProfitReceivingDuePeriodCount;
            }

            var schemeId = Context.TransactionId;
            // Why? Because one transaction may create many profit items via inline transactions.
            var createdSchemeIds = State.ManagingSchemeIds[Context.Sender]?.SchemeIds;

            if (createdSchemeIds != null && createdSchemeIds.Contains(schemeId))
            {
                // So we choose this way to avoid profit id conflicts in aforementioned situation.
                schemeId = Hash.FromTwoHashes(schemeId, createdSchemeIds.Last());
            }

            var scheme = new Scheme
            {
                SchemeId = schemeId,
                // The address of general ledger for current profit item.
                VirtualAddress = Context.ConvertVirtualAddressToContractAddress(schemeId),
                Manager        = input.Manager == null ? Context.Sender : input.Manager,
                ProfitReceivingDuePeriodCount = input.ProfitReceivingDuePeriodCount,
                CurrentPeriod = 1,
                IsReleaseAllBalanceEveryTimeByDefault = input.IsReleaseAllBalanceEveryTimeByDefault,
                DelayDistributePeriodCount            = input.DelayDistributePeriodCount
            };

            State.SchemeInfos[schemeId] = scheme;

            var schemeIds = State.ManagingSchemeIds[scheme.Manager];

            if (schemeIds == null)
            {
                schemeIds = new CreatedSchemeIds
                {
                    SchemeIds = { schemeId }
                };
            }
            else
            {
                schemeIds.SchemeIds.Add(schemeId);
            }

            State.ManagingSchemeIds[Context.Sender] = schemeIds;

            Context.LogDebug(() => $"Created scheme {State.SchemeInfos[schemeId]}");

            Context.Fire(new SchemeCreated
            {
                SchemeId = scheme.SchemeId,
                Manager  = scheme.Manager,
                IsReleaseAllBalanceEveryTimeByDefault = scheme.IsReleaseAllBalanceEveryTimeByDefault,
                ProfitReceivingDuePeriodCount         = scheme.ProfitReceivingDuePeriodCount,
                VirtualAddress = scheme.VirtualAddress
            });
            return(schemeId);
        }