public async Task DeleteAsync(string id) { BalanceHistoryEntity company = await _balanceHistoryRepository.GetAsync(id); if (company == null) { throw new NotFoundException(string.Format("Could not find balance history redord by id {0}", id)); } await _balanceHistoryRepository.DeleteAsync(id); }
public async Task <DomainBalanceHistory> AddAsync(DomainBalanceHistory record) { BalanceHistoryEntity entity = _mapper.Map <DomainBalanceHistory, BalanceHistoryEntity>(record); entity.Date = DateTime.UtcNow; entity = await _balanceHistoryRepository.AddAsync(entity); DomainBalanceHistory result = _mapper.Map <BalanceHistoryEntity, DomainBalanceHistory>(entity); return(result); }
private async ValueTask HandleEvents(IReadOnlyList <ExBalanceUpdateMessage> messages) { await Task.Delay(5000); using var locker = await _locker.GetLocker(); using var activity = MyTelemetry.StartActivity("Handle ExBalanceUpdateMessage's") ?.AddTag("count-events", messages.Count); try { var sw = new Stopwatch(); sw.Start(); var list = new List <BalanceHistoryEntity>(); foreach (var balanceUpdateMessage in messages) { if (balanceUpdateMessage.Update.Result != ExBalanceUpdate.BalanceUpdateResult.Ok) { continue; } var groupedUpdates = balanceUpdateMessage.Update.Updates.GroupBy(t => new { t.WalletId, t.AssetId }); foreach (var update in groupedUpdates) { using var _ = MyTelemetry.StartActivity("Update balance") ?.AddTag("walletId", update.Key.WalletId) .AddTag("symbol", update.Key.AssetId); var firstUpdate = update.OrderBy(t => t.Number).First(); var lastUpdate = update.OrderBy(t => t.Number).Last(); var newBalance = lastUpdate.NewBalance; var oldBalance = firstUpdate.OldBalance; var newReserve = lastUpdate.ReserveNewBalance; var oldReserve = firstUpdate.ReserveOldBalance; newBalance += newReserve; oldBalance += oldReserve; var amountBalance = newBalance - oldBalance; var amountReserve = newReserve - oldReserve; var availableBalance = newBalance - newReserve; var balanceVersion = balanceUpdateMessage.Update.Balances.Single(balance => balance.AssetId == update.Key.AssetId && balance.WalletId == update.Key.WalletId).Version; var entity = new BalanceHistoryEntity() { WalletId = update.Key.WalletId, Symbol = update.Key.AssetId, EventType = balanceUpdateMessage.Update.EventType, SequenceId = balanceVersion, OperationId = balanceUpdateMessage.Update.OperationId, Timestamp = balanceUpdateMessage.Update.Timestamp, NewBalance = (double)newBalance, OldBalance = (double)oldBalance, NewReserve = (double)newReserve, OldReserve = (double)oldReserve, AmountBalance = (double)amountBalance, AmountReserve = (double)amountReserve, AvailableBalance = (double)availableBalance, IsBalanceChanged = amountBalance != 0 }; list.Add(entity); } } await using var ctx = DatabaseContext.Create(_dbContextOptionsBuilder); await ctx.UpsetAsync(list).ConfigureAwait(false); await PopulateBalances(list, ctx); sw.Stop(); _logger.LogDebug( "Write {countUpdates} balance updates in database from {countEvents} ExBalanceUpdateMessage events. Time: {elapsedText} [{elapsedMilliseconds} ms]", list.Count, messages.Count, sw.Elapsed.ToString(), sw.ElapsedMilliseconds); } catch (Exception ex) { ex.FailActivity(); messages.AddToActivityAsJsonTag("me events"); throw; } }