示例#1
0
        public async Task <IActionResult> SetDefault(string assetPairId, [FromBody] UpdateMarkupRequest request)
        {
            if (!string.IsNullOrEmpty(request.PriceAssetPairId))
            {
                AssetPair priceAssetPair = await _assetsLocalCache.GetAssetPairByIdAsync(request.PriceAssetPairId);

                if (priceAssetPair == null)
                {
                    return(NotFound(ErrorResponse.Create("Price asset pair doesn't exist")));
                }
            }

            try
            {
                await _markupService.SetDefaultAsync(Uri.UnescapeDataString(assetPairId), request.PriceAssetPairId,
                                                     request.PriceMethod, request);

                return(Ok());
            }
            catch (InvalidRowKeyValueException e)
            {
                _log.ErrorWithDetails(e, new
                {
                    e.Variable,
                    e.Value
                });

                return(NotFound(ErrorResponse.Create("Asset pair not found")));
            }
        }
示例#2
0
        public async Task <IActionResult> SetDefault(string assetPairId, [FromBody] UpdateMarkupRequest request)
        {
            if (!string.IsNullOrEmpty(request.PriceAssetPairId))
            {
                AssetPair priceAssetPair = await _assetsLocalCache.GetAssetPairByIdAsync(request.PriceAssetPairId);

                if (priceAssetPair == null)
                {
                    return(NotFound(ErrorResponse.Create("Price asset pair doesn't exist")));
                }
            }

            try
            {
                await _markupService.SetDefaultAsync(assetPairId, request.PriceAssetPairId, request.PriceMethod,
                                                     request);

                return(Ok());
            }
            catch (Exception ex)
            {
                await _log.WriteErrorAsync(nameof(MarkupsController), nameof(SetDefault), ex);

                throw;
            }
        }
        public async Task <decimal> GetRateAsync(
            string baseAssetId,
            string quotingAssetId,
            decimal markupPercent,
            int markupPips,
            IMarkup merchantMarkup)
        {
            decimal askPrice, bidPrice;

            AssetPair priceAssetPair = null, assetPair = null;

            if (!string.IsNullOrEmpty(merchantMarkup.PriceAssetPairId))
            {
                _log.Info($"Price asset pair will be used: {merchantMarkup.PriceAssetPairId}");

                priceAssetPair = await _assetsLocalCache.GetAssetPairByIdAsync(merchantMarkup.PriceAssetPairId);

                IAssetPairRate assetPairRate =
                    await _assetRatesService.GetCurrentRateAsync(priceAssetPair.BaseAssetId, priceAssetPair.QuotingAssetId);

                _log.Info($"Price method: {merchantMarkup.PriceMethod.ToString()}");

                switch (merchantMarkup.PriceMethod)
                {
                case PriceMethod.None:
                case PriceMethod.Direct:
                    askPrice = assetPairRate.AskPrice;
                    bidPrice = assetPairRate.BidPrice;
                    break;

                case PriceMethod.Reverse:
                    askPrice = Math.Abs(assetPairRate.AskPrice) > 0
                            ? 1 / assetPairRate.AskPrice
                            : throw new MarketPriceZeroException("ask");
                    bidPrice = Math.Abs(assetPairRate.BidPrice) > 0
                            ? 1 / assetPairRate.BidPrice
                            : throw new MarketPriceZeroException("bid");
                    break;

                default:
                    throw new UnexpectedAssetPairPriceMethodException(merchantMarkup.PriceMethod);
                }
            }
            else
            {
                assetPair = await _assetsLocalCache.GetAssetPairAsync(baseAssetId, quotingAssetId);

                try
                {
                    IAssetPairRate assetPairRate = await _assetRatesService.GetCurrentRateAsync(baseAssetId, quotingAssetId);

                    askPrice = assetPairRate.AskPrice;

                    bidPrice = assetPairRate.BidPrice;
                }
                catch (Exception)
                {
                    askPrice = bidPrice = 1;
                }
            }

            _log.Info($"Market rate that will be used for calculation, askPrice = {askPrice}, bidPrice = {bidPrice}");

            Asset baseAsset = await _assetsLocalCache.GetAssetByIdAsync(baseAssetId);

            int pairAccuracy = priceAssetPair?.Accuracy ?? assetPair?.Accuracy ?? baseAsset.Accuracy;

            return(CalculatePrice(askPrice, bidPrice, pairAccuracy, baseAsset.Accuracy, markupPercent,
                                  markupPips, PriceCalculationMethod.ByBid, merchantMarkup));
        }
示例#4
0
        public async Task <decimal> GetRateAsync(
            string baseAssetId,
            string quotingAssetId,
            double markupPercent,
            int markupPips,
            IMarkup merchantMarkup)
        {
            double askPrice, bidPrice;

            AssetPair priceAssetPair = null, assetPair = null;

            if (!string.IsNullOrEmpty(merchantMarkup.PriceAssetPairId))
            {
                await _log.WriteInfoAsync(nameof(CalculationService), nameof(GetRateAsync),
                                          new { merchantMarkup.PriceAssetPairId }.ToJson(), "Price asset pair will be used");

                priceAssetPair = await _assetsLocalCache.GetAssetPairByIdAsync(merchantMarkup.PriceAssetPairId);

                AssetPairModel assetPairRate = await InvokeMarketProfileServiceAsync(priceAssetPair.Id);

                await _log.WriteInfoAsync(nameof(CalculationService), nameof(GetRateAsync),
                                          new { PriceMethod = merchantMarkup.PriceMethod.ToString() }.ToJson(), "Price method");

                switch (merchantMarkup.PriceMethod)
                {
                case PriceMethod.None:
                case PriceMethod.Direct:
                    askPrice = assetPairRate.AskPrice;
                    bidPrice = assetPairRate.BidPrice;
                    break;

                case PriceMethod.Reverse:
                    askPrice = Math.Abs(assetPairRate.AskPrice) > 0
                            ? 1 / assetPairRate.AskPrice
                            : throw new MarketPriceZeroException("ask");
                    bidPrice = Math.Abs(assetPairRate.BidPrice) > 0
                            ? 1 / assetPairRate.BidPrice
                            : throw new MarketPriceZeroException("bid");
                    break;

                default:
                    throw new UnexpectedAssetPairPriceMethodException(merchantMarkup.PriceMethod);
                }
            }
            else
            {
                assetPair = await _assetsLocalCache.GetAssetPairAsync(baseAssetId, quotingAssetId);

                if (assetPair != null)
                {
                    await _log.WriteInfoAsync(nameof(CalculationService), nameof(GetRateAsync),
                                              new { AssetPairId = assetPair.Id }.ToJson(), "Asset pair will be used");

                    AssetPairModel assetPairRate = await InvokeMarketProfileServiceAsync(assetPair.Id);

                    askPrice = assetPairRate.AskPrice;

                    bidPrice = assetPairRate.BidPrice;
                }
                else
                {
                    askPrice = bidPrice = 1D;
                }
            }

            await _log.WriteInfoAsync(nameof(CalculationService), nameof(GetRateAsync),
                                      new { askPrice, bidPrice }.ToJson(), "Market rate that will be used for calculation");

            Asset baseAsset = await _assetsLocalCache.GetAssetByIdAsync(baseAssetId);

            int pairAccuracy = priceAssetPair?.Accuracy ?? assetPair?.Accuracy ?? baseAsset.Accuracy;

            return(CalculatePrice(askPrice, bidPrice, pairAccuracy, baseAsset.Accuracy, markupPercent, markupPips,
                                  PriceCalculationMethod.ByBid, merchantMarkup));
        }