public PriceProfile()
 {
     CreateMap <IExchangeEntity, PriceDto>()
     .ForMember(dest => dest.Symbol, m => m.MapFrom(src => src.TokenSymbol))
     .ForMember(dest => dest.Price,
                m => m.MapFrom(src => UniswapUtils.CalculateMarginalRate(src.EthLiquidity, src.TokenLiquidity)))
     .ForMember(dest => dest.InvPrice,
                m => m.MapFrom(src => UniswapUtils.CalculateInvMarginRate(src.EthLiquidity, src.TokenLiquidity)));
 }
Beispiel #2
0
 public ExchangeProfile()
 {
     CreateMap <IExchangeEntity, ExchangeDto>()
     .ForMember(dest => dest.Name, m => m.MapFrom(src => src.TokenName))
     .ForMember(dest => dest.Symbol, m => m.MapFrom(src => src.TokenSymbol))
     .ForMember(dest => dest.Fee, m => m.MapFrom(src => UniswapUtils.Fee))
     .ForMember(dest => dest.Version, m => m.MapFrom(src => UniswapUtils.Version))
     .ForMember(dest => dest.ExchangeAddress, m => m.MapFrom(src => src.Id))
     .ForMember(dest => dest.EthDecimals, m => m.MapFrom(src => EthUtils.Decimals))
     .ForMember(dest => dest.Price, m => m.MapFrom(src => UniswapUtils.CalculateMarginalRate(src.EthLiquidity, src.TokenLiquidity)));
 }
        public async Task <OperationResult <ExchangeTicker> > GetByAddress(string exchangeAddress)
        {
            var exchangeEntity = await _exchangeRepository.FindByAsync(exchangeAddress : exchangeAddress);

            if (exchangeEntity == null)
            {
                return(new OperationResult <ExchangeTicker>());
            }

            var exchangeEvents = await _repository.GetForLastDayByExchangeAddressAsync(exchangeAddress);

            if (!exchangeEvents.Any())
            {
                return(new OperationResult <ExchangeTicker>(null));
            }

            var orderedPurchaseEvents = exchangeEvents
                                        .Where(IsEthOrTokenPurchaseEvent)
                                        .OrderBy(x => x.BlockNumber)
                                        .ThenBy(x => x.LogIndex);

            var ticker = new ExchangeTicker();

            if (orderedPurchaseEvents.Any())
            {
                var firstTrade = orderedPurchaseEvents.First();

                var lastTrade = orderedPurchaseEvents.Last();

                var startPrice = firstTrade.TokenAmount / firstTrade.EthAmount;

                var priceChange = UniswapUtils.CalculateMarginalRate(lastTrade.EthLiquidityBeforeEvent,
                                                                     lastTrade.TokenLiquidityBeforeEvent) -
                                  UniswapUtils.CalculateMarginalRate(firstTrade.EthLiquidityBeforeEvent,
                                                                     firstTrade.TokenLiquidityBeforeEvent);

                var ethTradeVolume = orderedPurchaseEvents.Sum(@event => @event.EthAmount);

                var highPrice = orderedPurchaseEvents.Max(@event =>
                                                          UniswapUtils.CalculateMarginalRate(@event.EthLiquidityBeforeEvent,
                                                                                             @event.TokenLiquidityBeforeEvent));

                var lowPrice = orderedPurchaseEvents.Min(@event =>
                                                         UniswapUtils.CalculateMarginalRate(@event.EthLiquidityBeforeEvent,
                                                                                            @event.TokenLiquidityBeforeEvent));

                var weightedAveragePrice =
                    orderedPurchaseEvents.Sum(@event =>
                                              @event.EthAmount *
                                              UniswapUtils.CalculateMarginalRate(@event.EthLiquidityBeforeEvent,
                                                                                 @event.TokenLiquidityBeforeEvent)) / ethTradeVolume;

                ticker.Count             = orderedPurchaseEvents.Count();
                ticker.LowPrice          = lowPrice;
                ticker.HighPrice         = highPrice;
                ticker.LastTradeErc20Qty = lastTrade.TokenAmount;
                ticker.LastTradeEthQty   = lastTrade.EthAmount;
                ticker.LastTradePrice    = UniswapUtils.CalculateMarginalRate(lastTrade.EthLiquidityBeforeEvent,
                                                                              lastTrade.TokenLiquidityBeforeEvent);
                ticker.PriceChange        = priceChange;
                ticker.TradeVolume        = ethTradeVolume;
                ticker.PriceChangePercent = priceChange / startPrice;
                ticker.WeightedAvgPrice   = weightedAveragePrice;
            }

            ticker.EndTime        = DateTime.UtcNow.ToUnixTimestamp();
            ticker.Erc20Liquidity = exchangeEntity.TokenLiquidity;
            ticker.EthLiquidity   = exchangeEntity.EthLiquidity;
            ticker.Price          =
                UniswapUtils.CalculateMarginalRate(exchangeEntity.EthLiquidity, exchangeEntity.TokenLiquidity);
            ticker.InvPrice =
                UniswapUtils.CalculateInvMarginRate(exchangeEntity.EthLiquidity, exchangeEntity.TokenLiquidity);
            ticker.Symbol    = exchangeEntity.TokenSymbol;
            ticker.StartTime = DateTime.UtcNow.Subtract(TimeSpan.FromDays(1)).ToUnixTimestamp();
            ticker.Theme     = exchangeEntity.Theme;

            return(new OperationResult <ExchangeTicker>(ticker));
        }