示例#1
0
        private async Task <Dictionary <int, ICalculateWay> > GetDefaultPieceWiseFunctionAsync()
        {
            var tempCache = _cacheCacheProvider.GetPieceWiseFunctionFromNormalCache();

            if (tempCache != null)
            {
                return(tempCache);
            }

            var chain = await _blockchainService.GetChainAsync();

            var tokenStub = _tokenStTokenContractReaderFactory.Create(new ChainContext
            {
                BlockHash   = chain.LastIrreversibleBlockHash,
                BlockHeight = chain.LastIrreversibleBlockHeight
            });

            CalculateFeeCoefficientsOfType parameters;

            if (CalculateAlgorithmContext.CalculateFeeTypeEnum == (int)FeeTypeEnum.Tx)
            {
                parameters = await tokenStub.GetCalculateFeeCoefficientOfSender.CallAsync(new Empty());
            }
            else
            {
                parameters = await tokenStub.GetCalculateFeeCoefficientOfContract.CallAsync(new SInt32Value
                                                                                            { Value = CalculateAlgorithmContext.CalculateFeeTypeEnum });
            }
            var calWayDic = new Dictionary <int, ICalculateWay>();

            foreach (var func in parameters.Coefficients)
            {
                ICalculateWay newCalculateWay = null;
                switch (func.FunctionType)
                {
                case CalculateFunctionTypeEnum.Liner:
                    newCalculateWay = new LinerCalculateWay();
                    break;

                case CalculateFunctionTypeEnum.Power:
                    newCalculateWay = new PowerCalculateWay();
                    break;
                }

                if (newCalculateWay == null)
                {
                    Logger.LogWarning($"could not find mapped function type {func.FunctionType}");
                    continue;
                }

                var funDicToLowerKey = func.CoefficientDic.ToDictionary(x => x.Key.ToLower(), x => x.Value);
                calWayDic[func.PieceKey] = newCalculateWay;
                calWayDic[func.PieceKey].InitParameter(funDicToLowerKey);
            }
            _cacheCacheProvider.SetPieceWiseFunctionToNormalCache(calWayDic);
            return(calWayDic);
        }