Ejemplo n.º 1
0
        public override Empty RegisterForProfits(RegisterForProfitsInput input)
        {
            var scheme = GetValidScheme(input.SchemeManager);

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

            State.TokenContract.Lock.Send(new LockInput
            {
                LockId  = Context.TransactionId,
                Symbol  = scheme.Symbol,
                Address = Context.Sender,
                Amount  = input.Amount,
            });
            State.LockIds[input.SchemeManager][Context.Sender] = Context.TransactionId;
            State.LockTimestamp[Context.TransactionId]         = 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())
            {
                foreach (var threshold in scheme.AutoDistributeThreshold)
                {
                    var originScheme = State.ProfitContract.GetScheme.Call(scheme.SchemeId);
                    var balance      = State.TokenContract.GetBalance.Call(new GetBalanceInput
                    {
                        Owner  = originScheme.VirtualAddress,
                        Symbol = threshold.Key
                    }).Balance;
                    if (balance < threshold.Value)
                    {
                        continue;
                    }
                    State.ProfitContract.DistributeProfits.Send(new Profit.DistributeProfitsInput
                    {
                        SchemeId = scheme.SchemeId,
                        Period   = scheme.Period.Add(1)
                    });
                    scheme.Period = scheme.Period.Add(1);
                    State.TokenHolderProfitSchemes[input.SchemeManager] = scheme;
                }
            }

            return(new Empty());
        }
Ejemplo n.º 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());
        }