Exemple #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);
        }
Exemple #2
0
        private Hash GenerateSchemeId(CreateSchemeInput createSchemeInput)
        {
            var manager = createSchemeInput.Manager ?? Context.Sender;

            if (createSchemeInput.Token != null)
            {
                return(Context.GenerateId(Context.Self, createSchemeInput.Token));
            }
            var createdSchemeCount = State.ManagingSchemeIds[manager]?.SchemeIds.Count ?? 0;

            return(Context.GenerateId(Context.Self, createdSchemeCount.ToBytes(false)));
        }
Exemple #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)
        {
            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);
        }
        private Hash CreateProfitScheme(ProjectInfo projectInfo)
        {
            var token             = HashHelper.ComputeFrom(projectInfo);
            var createSchemeInput = new CreateSchemeInput
            {
                Manager = projectInfo.VirtualAddress,
                IsReleaseAllBalanceEveryTimeByDefault = true,
                CanRemoveBeneficiaryDirectly          = true,
                Token = token
            };

            State.ProfitContract.CreateScheme.Send(createSchemeInput);
            return(Context.GenerateId(State.ProfitContract.Value, token));
        }
Exemple #5
0
        private Scheme GetNewScheme(CreateSchemeInput input, Hash schemeId)
        {
            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
            };

            return(scheme);
        }
Exemple #6
0
        private Scheme GetNewScheme(CreateSchemeInput input, Hash schemeId, Address manager)
        {
            var scheme = new Scheme
            {
                SchemeId = schemeId,
                // The address of general ledger for current profit scheme.
                VirtualAddress = Context.ConvertVirtualAddressToContractAddress(schemeId),
                Manager        = manager,
                ProfitReceivingDuePeriodCount = input.ProfitReceivingDuePeriodCount,
                CurrentPeriod = 1,
                IsReleaseAllBalanceEveryTimeByDefault = input.IsReleaseAllBalanceEveryTimeByDefault,
                DelayDistributePeriodCount            = input.DelayDistributePeriodCount,
                CanRemoveBeneficiaryDirectly          = input.CanRemoveBeneficiaryDirectly
            };

            return(scheme);
        }
Exemple #7
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);
        }