Beispiel #1
0
        private async Task ProcessMessageAsync(LykkeTrade message)
        {
            var sw = new Stopwatch();

            try
            {
                var model = Mapper.Map <Core.Domain.Trades.LykkeTrade>(message);

                await Task.WhenAll(
                    _lykkeTradeService.HandleAsync(model),
                    _realisedPnLService.CalculateAsync(model));
            }
            catch (Exception exception)
            {
                _log.ErrorWithDetails(exception, "An error occurred during processing Lykke trade", message);
            }
            finally
            {
                sw.Stop();

                _log.InfoWithDetails("Lykke trade was received",
                                     new
                {
                    message,
                    sw.ElapsedMilliseconds
                });
            }
        }
        public async Task InsertAsync(LykkeTrade lykkeTrade)
        {
            var entity = new LykkeTradeEntity(GetPartitionKey(lykkeTrade.Time), GetRowKey(lykkeTrade.Id));

            Mapper.Map(lykkeTrade, entity);

            await _storage.InsertOrReplaceAsync(entity);
        }
        public async Task CalculateAsync(LykkeTrade lykkeTrade)
        {
            WalletSettings walletSettings = await _walletSettingsService.GetWalletAsync(lykkeTrade.ClientId);

            if (walletSettings == null || !walletSettings.Enabled)
            {
                return;
            }

            AssetPair assetPair = await _assetsServiceWithCache.TryGetAssetPairAsync(lykkeTrade.AssetPairId);

            string[] assets = walletSettings.Assets
                              .Intersect(new[] { assetPair.BaseAssetId, assetPair.QuotingAssetId })
                              .ToArray();

            if (!assets.Any())
            {
                return;
            }

            var tradeData = new TradeData
            {
                Id                   = lykkeTrade.Id,
                Exchange             = ExchangeNames.Lykke,
                AssetPair            = assetPair.Id,
                BaseAsset            = assetPair.BaseAssetId,
                QuoteAsset           = assetPair.QuotingAssetId,
                Price                = lykkeTrade.Price,
                Volume               = lykkeTrade.Volume,
                Type                 = lykkeTrade.Type,
                Time                 = lykkeTrade.Time,
                LimitOrderId         = lykkeTrade.LimitOrderId,
                OppositeClientId     = lykkeTrade.OppositeClientId,
                OppositeLimitOrderId = lykkeTrade.OppositeLimitOrderId
            };

            foreach (string assetId in assets)
            {
                AssetRealisedPnL assetRealisedPnL =
                    await CalculateAsync(tradeData, walletSettings.Id, assetId);

                await _assetRealisedPnLRepository.InsertAsync(assetRealisedPnL);

                _cache.Set(assetRealisedPnL);
            }

            _log.InfoWithDetails("Lykke trade handled", tradeData);
        }
 public Task HandleAsync(LykkeTrade lykkeTrade)
 {
     return(_lykkeTradeRepository.InsertAsync(lykkeTrade));
 }