public override Empty RemoveBeneficiary(RemoveBeneficiaryInput input) { Assert(input.SchemeId != null, "Invalid scheme id."); Assert(input.Beneficiary != null, "Invalid Beneficiary address."); var scheme = State.SchemeInfos[input.SchemeId]; Assert(scheme != null, "Scheme not found."); var currentDetail = State.ProfitDetailsMap[input.SchemeId][input.Beneficiary]; if (scheme == null || currentDetail == null) { return(new Empty()); } Assert(Context.Sender == scheme.Manager || Context.Sender == Context.GetContractAddressByName(SmartContractConstants.TokenHolderContractSystemName), "Only manager can remove beneficiary."); var expiryDetails = scheme.CanRemoveBeneficiaryDirectly ? currentDetail.Details.ToList() : currentDetail.Details .Where(d => d.EndPeriod < scheme.CurrentPeriod && !d.IsWeightRemoved).ToList(); if (!expiryDetails.Any()) { return(new Empty()); } var shares = expiryDetails.Sum(d => d.Shares); foreach (var expiryDetail in expiryDetails) { expiryDetail.IsWeightRemoved = true; if (expiryDetail.LastProfitPeriod >= scheme.CurrentPeriod) { currentDetail.Details.Remove(expiryDetail); } } // Clear old profit details. if (currentDetail.Details.Count != 0) { State.ProfitDetailsMap[input.SchemeId][input.Beneficiary] = currentDetail; } else { State.ProfitDetailsMap[input.SchemeId].Remove(input.Beneficiary); } scheme.TotalShares = scheme.TotalShares.Sub(shares); State.SchemeInfos[input.SchemeId] = scheme; return(new Empty()); }
public override Empty RemoveBeneficiary(RemoveBeneficiaryInput input) { Assert(input.SchemeId != null, "Invalid scheme id."); Assert(input.Beneficiary != null, "Invalid Beneficiary address."); var scheme = State.SchemeInfos[input.SchemeId]; Assert(scheme != null, "Scheme not found."); var currentDetail = State.ProfitDetailsMap[input.SchemeId][input.Beneficiary]; if (scheme == null || currentDetail == null) { return(new Empty()); } Assert(Context.Sender == scheme.Manager, "Only manager can remove beneficiary."); var expiryDetails = currentDetail.Details .Where(d => d.EndPeriod < scheme.CurrentPeriod && !d.IsWeightRemoved).ToList(); if (!expiryDetails.Any()) { return(new Empty()); } var shares = expiryDetails.Sum(d => d.Shares); foreach (var expiryDetail in expiryDetails) { expiryDetail.IsWeightRemoved = true; if (expiryDetail.LastProfitPeriod >= scheme.CurrentPeriod) { currentDetail.Details.Remove(expiryDetail); } } State.ProfitDetailsMap[input.SchemeId][input.Beneficiary] = currentDetail; // TODO: Recover this after key deletion in contract feature impled. // if (currentDetail.Details.Count != 0) // { // State.ProfitDetailsMap[input.SchemeId][input.Beneficiary] = currentDetail; // } // else // { // State.ProfitDetailsMap[input.SchemeId][input.Beneficiary] = null; // } scheme.TotalShares = scheme.TotalShares.Sub(shares); State.SchemeInfos[input.SchemeId] = scheme; return(new Empty()); }