public async Task <Model> Handle(Query request, CancellationToken cancellationToken) { NetInfo netInfo = await _statsCache.GetAsync <NetInfo>(NetInfo.DefaultCacheKey, null).AnyContext(); if (netInfo == null) { return(null); } IEnumerable <Promotion> promotions = await _promotionRepository.GetPromotions(cancellationToken : cancellationToken); return(new Model { BestBlock = netInfo.BestBlock, BestBlockTimestamp = netInfo.BestBlockTimestamp, AverageBlockTime = netInfo.AverageBlockTime, AverageNetworkHashRate = netInfo.AverageNetworkHashRate, Difficulty = netInfo.Difficulty, ConnectedPeerCount = netInfo.ConnectedPeerCount, Promotions = promotions.Select(promotion => new Model.PromotionModel { Display = promotion.Display, Url = promotion.Url, Location = promotion.Location }) }); }
public async Task <IPage <Model> > Handle(Query request, CancellationToken cancellationToken) { CirculatingSupply supply = await _statsCache.GetAsync <CirculatingSupply>(CirculatingSupply.DefaultCacheKey, null) .AnyContext(); IPage <CinderAddress> page = await _addressRepository.GetRichest(request.Page, request.Size, _settings.Performance.RichListMinimumBalance, _settings.Performance.QueryCountLimiter, cancellationToken) .AnyContext(); IEnumerable <CinderAddressMeta> metas = await _addressMetaRepository .GetByAddresses(page.Items.Select(address => address.Hash).Distinct(), cancellationToken) .AnyContext(); decimal circulatingSupply = supply?.Supply ?? 0; int rank = 1 * (page.Page - 1) * page.Size + 1; IEnumerable <Model> models = page.Items.Select(address => { CinderAddressMeta meta = metas.FirstOrDefault(x => x.Id == address.Id); return(new Model { Rank = rank++, Name = meta?.Name, Hash = address.Hash, Balance = address.Balance, Percent = address.Balance > 0 && circulatingSupply > 0 ? address.Balance / circulatingSupply * 100 : default,
public async Task <Model> Handle(Query request, CancellationToken cancellationToken) { CirculatingSupply supply = await _statsCache.GetAsync(CirculatingSupply.DefaultCacheKey, new CirculatingSupply()) .AnyContext(); return(new Model { Supply = supply.Supply }); }
protected override async Task <JobResult> RunInternalAsync(JobContext context) { try { CirculatingSupply supply = await _statsCache.GetAsync(CirculatingSupply.DefaultCacheKey, new CirculatingSupply()) .AnyContext(); supply.Supply = await _addressRepository.GetSupply().AnyContext(); await _statsCache.SetAsync(CirculatingSupply.DefaultCacheKey, supply).AnyContext(); } catch (Exception e) { return(JobResult.FromException(e)); } return(JobResult.Success); }
public async Task <Model> Handle(Query request, CancellationToken cancellationToken) { Price price = await _statsCache.GetAsync <Price>(Price.DefaultCacheKey, null).AnyContext(); if (price == null) { return(null); } return(new Model { BtcPrice = price.BtcPrice, BtcMarketCap = price.BtcMarketCap, Btc24HrVol = price.Btc24HrVol, Btc24HrChange = price.Btc24HrChange, UsdPrice = price.UsdPrice, UsdMarketCap = price.UsdMarketCap, Usd24HrVol = price.Usd24HrVol, Usd24HrChange = price.Usd24HrChange }); }
protected override async Task <JobResult> RunInternalAsync(JobContext context) { try { NetInfo netInfo = await _statsCache.GetAsync <NetInfo>(NetInfo.DefaultCacheKey, null).AnyContext(); if (netInfo == null) { return(JobResult.FailedWithMessage("NetInfo is null")); } IEnumerable <CinderAddress> addresses = await _addressRepository.GetStaleAddresses(_settings.Age, _settings.Limit).AnyContext(); IEnumerable <CinderAddress> enumerable = addresses as CinderAddress[] ?? addresses.ToArray(); _logger.LogDebug("Found {Count} addresses to update", enumerable.Count()); List <CinderAddress> updated = new(); foreach (CinderAddress address in enumerable) { _logger.LogDebug("Updating stats for {Hash}", address.Hash); HexBigInteger balance; try { balance = await _web3.Eth.GetBalance.SendRequestAsync(address.Hash, new BlockParameter()).AnyContext(); } catch (Exception e) { _logger.LogDebug(e, "Could not get balance for {Hash}", address.Hash); continue; } address.Balance = UnitConversion.Convert.FromWei(balance); address.Timestamp = (ulong)DateTimeOffset.UtcNow.ToUnixTimeSeconds(); address.ForceRefresh = false; if (_settings.BalanceHistory.Enabled && _settings.BalanceHistory.Days.Any()) { IEnumerable <Task <HexBigInteger> > tasks = _settings.BalanceHistory.Days.Select(d => { ulong block = GetPastBlock(netInfo.BestBlock, netInfo.AverageBlockTime, d); return(_web3.Eth.GetBalance.SendRequestAsync(address.Hash, new BlockParameter(block))); }); try { HexBigInteger[] results = await Task.WhenAll(tasks).AnyContext(); address.BalanceHistory.Clear(); for (int i = 0; i < _settings.BalanceHistory.Days.Length; i++) { address.BalanceHistory.Add(_settings.BalanceHistory.Days[i].ToString(), UnitConversion.Convert.FromWei(results[i])); } } catch (Exception e) { _logger.LogDebug(e, "Could not get historical balance for {Hash}", address.Hash); } } updated.Add(address); } if (!updated.Any()) { return(JobResult.Success); } await _addressRepository.BulkUpsertAddresses(updated).AnyContext(); } catch (Exception e) { return(JobResult.FromException(e)); } return(JobResult.Success); }