private async Task <List <AvailableTokenInfoInCache> > GetExtraAcceptedTokensInfoFromCacheAsync()
        {
            var normalCache = _cacheProvider.GetExtraAcceptedTokensInfoFromNormalCache();

            if (normalCache != null)
            {
                return(normalCache);
            }
            var chain = await _blockchainService.GetChainAsync();

            var tokenStub = _tokenStTokenContractReaderFactory.Create(new ChainContext
            {
                BlockHash   = chain.LastIrreversibleBlockHash,
                BlockHeight = chain.LastIrreversibleBlockHeight
            });
            var tokenInfos = await tokenStub.GetSymbolsToPayTXSizeFee.CallAsync(new Empty());

            var tokenInfoList = new List <AvailableTokenInfoInCache>();

            if (tokenInfos != null)
            {
                foreach (var tokenInfo in tokenInfos.SymbolsToPayTxSizeFee)
                {
                    tokenInfoList.Add(new AvailableTokenInfoInCache
                    {
                        TokenSymbol      = tokenInfo.TokenSymbol,
                        AddedTokenWeight = tokenInfo.AddedTokenWeight,
                        BaseTokenWeight  = tokenInfo.BaseTokenWeight
                    });
                }
            }

            _cacheProvider.SetExtraAcceptedTokenInfoToCache(tokenInfoList);
            return(tokenInfoList);
        }
        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);
        }
Example #3
0
        public async Task <string> GetPrimaryTokenSymbol()
        {
            if (_primaryTokenSymbol != null)
            {
                return(_primaryTokenSymbol);
            }

            var chain = await _blockchainService.GetChainAsync();

            _primaryTokenSymbol = (await _tokenContractReaderFactory.Create(new ChainContext
            {
                BlockHash = chain.BestChainHash,
                BlockHeight = chain.BestChainHeight
            }).GetPrimaryTokenSymbol.CallAsync(new Empty())).Value;

            return(_primaryTokenSymbol);
        }
Example #4
0
        public async Task <bool> ValidateTransactionAsync(Transaction transaction)
        {
            // Skip if the sender is a contract.
            var deployedContractAddressList =
                await _deployedContractAddressProvider.GetDeployedContractAddressListAsync();

            if (deployedContractAddressList.Value.Contains(transaction.From))
            {
                return(true);
            }

            // Skip if this is a system transaction.
            if (IsSystemTransaction(transaction))
            {
                return(true);
            }

            var chain = await _blockchainService.GetChainAsync();

            // Skip this validation at the very beginning of current chain.
            if (chain.LastIrreversibleBlockHeight == Constants.GenesisBlockHeight)
            {
                return(true);
            }

            var tokenStub = _tokenContractReaderFactory.Create(new ChainContext
            {
                BlockHash   = chain.BestChainHash,
                BlockHeight = chain.BestChainHeight
            });
            var balance = (await tokenStub.GetBalance.CallAsync(new GetBalanceInput
            {
                Owner = transaction.From,
                Symbol = await _primaryTokenSymbolProvider.GetPrimaryTokenSymbol()
            }))?.Balance;

            // balance == null means token contract hasn't deployed.
            if (balance == null || balance > 0)
            {
                return(true);
            }

            Logger.LogError($"Empty balance of tx from address: {transaction}");
            return(false);
        }
        private async Task <long> GetUnitPriceAsync()
        {
            if (_unitPrice != null)
            {
//                Logger.LogTrace($"Get tx size fee unit price: {_unitPrice.Value}");
                return(_unitPrice.Value);
            }

            var chain = await _blockchainService.GetChainAsync();

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

            _unitPrice = (await tokenStub.GetTransactionSizeFeeUnitPrice.CallAsync(new Empty()))?.Value ?? 0;

            return(_unitPrice.Value);
        }
        public async Task <bool> ValidateTransactionAsync(Transaction transaction)
        {
            // Skip if this is a system transaction.
            if (_feeExemptionService.IsFree(transaction))
            {
                return(true);
            }

            var chain = await _blockchainService.GetChainAsync();

            var chainContext = new ChainContext
            {
                BlockHash   = chain.BestChainHash,
                BlockHeight = chain.BestChainHeight
            };

            // Skip this validation at the very beginning of current chain.
            if (chain.LastIrreversibleBlockHeight == AElfConstants.GenesisBlockHeight)
            {
                return(true);
            }

            var tokenStub = _tokenContractReaderFactory.Create(chainContext);
            var balance   = (await tokenStub.GetBalance.CallAsync(new GetBalanceInput
            {
                Owner = transaction.From,
                Symbol = await _primaryTokenSymbolProvider.GetPrimaryTokenSymbol()
            }))?.Balance;

            // balance == null means token contract hasn't deployed.
            if (balance == null || balance > 0)
            {
                return(true);
            }

            Logger.LogWarning($"Empty balance of tx from address: {transaction}");
            return(false);
        }