public override Empty DistributeProfits(DistributeProfitsInput input)
        {
            var scheme = GetValidScheme(input.SchemeManager, true);

            Assert(Context.Sender == Context.GetContractAddressByName(SmartContractConstants.TokenContractSystemName) ||
                   Context.Sender == input.SchemeManager, "No permission to distribute profits.");
            var distributeProfitsInput = new Profit.DistributeProfitsInput
            {
                SchemeId = scheme.SchemeId,
                Period   = scheme.Period
            };

            if (input.AmountsMap != null && input.AmountsMap.Any())
            {
                distributeProfitsInput.AmountsMap.Add(input.AmountsMap);
            }
            State.ProfitContract.DistributeProfits.Send(distributeProfitsInput);
            scheme.Period = scheme.Period.Add(1);
            State.TokenHolderProfitSchemes[input.SchemeManager] = scheme;
            return(new Empty());
        }
Beispiel #2
0
        public override Empty RegisterForProfits(RegisterForProfitsInput input)
        {
            Assert(State.LockIds[input.SchemeManager][Context.Sender] == null, "Already registered.");
            var scheme = GetValidScheme(input.SchemeManager);

            if (State.TokenContract.Value == null)
            {
                State.TokenContract.Value =
                    Context.GetContractAddressByName(SmartContractConstants.TokenContractSystemName);
            }

            var lockId = Context.GenerateId(Context.Self,
                                            ByteArrayHelper.ConcatArrays(input.SchemeManager.ToByteArray(), Context.Sender.ToByteArray()));

            State.TokenContract.Lock.Send(new LockInput
            {
                LockId  = lockId,
                Symbol  = scheme.Symbol,
                Address = Context.Sender,
                Amount  = input.Amount,
            });
            State.LockIds[input.SchemeManager][Context.Sender] = lockId;
            State.LockTimestamp[lockId] = Context.CurrentBlockTime;
            State.ProfitContract.AddBeneficiary.Send(new AddBeneficiaryInput
            {
                SchemeId         = scheme.SchemeId,
                BeneficiaryShare = new BeneficiaryShare
                {
                    Beneficiary = Context.Sender,
                    Shares      = input.Amount
                }
            });

            // Check auto-distribute threshold.
            if (scheme.AutoDistributeThreshold != null && scheme.AutoDistributeThreshold.Any())
            {
                var originScheme   = State.ProfitContract.GetScheme.Call(scheme.SchemeId);
                var virtualAddress = originScheme.VirtualAddress;
                Profit.DistributeProfitsInput distributedInput = null;
                foreach (var threshold in scheme.AutoDistributeThreshold)
                {
                    var balance = State.TokenContract.GetBalance.Call(new GetBalanceInput
                    {
                        Owner  = virtualAddress,
                        Symbol = threshold.Key
                    }).Balance;
                    if (balance < threshold.Value)
                    {
                        continue;
                    }
                    if (distributedInput == null)
                    {
                        distributedInput = new Profit.DistributeProfitsInput
                        {
                            SchemeId = scheme.SchemeId,
                            Period   = scheme.Period
                        }
                    }
                    ;
                    distributedInput.AmountsMap[threshold.Key] = 0;
                    break;
                }

                if (distributedInput == null)
                {
                    return(new Empty());
                }
                State.ProfitContract.DistributeProfits.Send(distributedInput);
                scheme.Period = scheme.Period.Add(1);
                State.TokenHolderProfitSchemes[input.SchemeManager] = scheme;
            }

            return(new Empty());
        }