public async Task <BlockchainTransactionsInfo> GetTransactionsInfoAsync(IEnumerable <Asset> assets, string address, string assetId = null, DateTime?fromDate = null, int?fromBlock = null)
        {
            assetId = string.IsNullOrEmpty(assetId) ? "BTC" : assetId;

            var result = new BlockchainTransactionsInfo
            {
                AssetId = assetId
            };

            if (!fromBlock.HasValue)
            {
                return(result);
            }

            var(btcAddress, isColored) = GetAddress(address);

            var asset = assets.First(x => x.Id == assetId);

            try
            {
                _client.Colored = isColored;
                var balance = await _client.GetBalanceBetween(new BalanceSelector(btcAddress), until : new BlockFeature(fromBlock.Value));

                foreach (var operation in balance.Operations)
                {
                    result.ReceivedAmount += operation.ReceivedCoins.Sum(x => x.Amount.GetAmount(asset.MultiplierPower));
                    result.SpentAmount    += operation.SpentCoins.Sum(x => x.Amount.GetAmount(asset.MultiplierPower));
                }

                result.TransactionsCount = balance.Operations.Count;

                return(result);
            }
            catch (Exception e)
            {
                throw new RetryNeededException(e);
            }
        }
Esempio n. 2
0
        public async Task <BlockchainTransactionsInfo> GetTransactionsInfoAsync(IEnumerable <Asset> assets, string address, string assetId = null, DateTime?fromDate = null, int?fromBlock = null)
        {
            var ethAsset = (await _assetsService.AssetGetAllAsync()).FirstOrDefault(x => x.BlockChainAssetId == "ETH");

            if (ethAsset == null)
            {
                return(new BlockchainTransactionsInfo());
            }

            var result = new BlockchainTransactionsInfo
            {
                AssetId = ethAsset.Id
            };

            if (!fromDate.HasValue)
            {
                return(result);
            }

            var hasTransactions = true;
            int count           = 100;
            int start           = 0;

            while (hasTransactions)
            {
                var addressHistoryResponse = await _client.ApiTransactionsHistoryPostAsync(new AddressTransactions
                {
                    Address = address,
                    Count   = count,
                    Start   = start
                });

                if (addressHistoryResponse is ApiException error)
                {
                    continue;
                }

                if (addressHistoryResponse is FilteredAddressHistoryResponse historyResponse)
                {
                    var transactions = historyResponse.History.Where(x => x.BlockTimeUtc >= fromDate.Value).ToList();
                    hasTransactions = transactions.Any();

                    foreach (var historyItem in transactions)
                    {
                        var fees = BigInteger.Parse(historyItem.GasPrice ?? "0") * BigInteger.Parse(historyItem.GasUsed ?? "0");
                        var sign = EthServiceHelpers.CalculateSign(address, historyItem.FromProperty, historyItem.To);

                        var value = sign == 1
                            ? historyItem.Value
                            : (BigInteger.Parse(historyItem.Value) + fees).ToString();

                        decimal ethValue = sign * EthServiceHelpers
                                           .ConvertFromContract(
                            value,
                            ethAsset.MultiplierPower,
                            ethAsset.Accuracy);

                        result.TransactionsCount++;

                        if (sign == 1)
                        {
                            result.ReceivedAmount += ethValue;
                        }

                        if (sign == -1)
                        {
                            result.SpentAmount += ethValue;
                        }
                    }

                    start += count;
                    count += count;
                }
            }

            return(result);
        }