Exemple #1
0
        public async Task AddAsync_InvertedRateAdded()
        {
            const decimal bidPrice = 1;

            const decimal askPrice = 2;

            const string baseAssetId = "BTC";

            const string quotingAssetId = "LKK";

            const int assetPairAccuracy = 1;

            var rates = new List <IAssetPairRate>();

            _assetPairSettingsServiceMock.Setup(o => o.GetAsync(It.IsAny <string>(), It.IsAny <string>())).ReturnsAsync(
                new AssetPairSetting
            {
                BaseAssetId    = baseAssetId,
                QuotingAssetId = quotingAssetId,
                Accuracy       = assetPairAccuracy
            });

            _assetPairRateRepositoryMock.Setup(o => o.AddAsync(It.IsAny <AssetPairRate>()))
            .ReturnsAsync((IAssetPairRate rate) => rate)
            .Callback((IAssetPairRate rate) => rates.Add(rate));

            var newRateCommand = new AddAssetPairRateCommand
            {
                BidPrice       = bidPrice,
                AskPrice       = askPrice,
                BaseAssetId    = baseAssetId,
                QuotingAssetId = quotingAssetId,
            };

            await _assetRatesService.AddAsync(newRateCommand);

            Assert.IsNotNull(rates.Single(x =>
                                          x.BaseAssetId == newRateCommand.BaseAssetId && x.QuotingAssetId == newRateCommand.QuotingAssetId));
            Assert.IsNotNull(rates.Single(x =>
                                          x.BaseAssetId == newRateCommand.QuotingAssetId && x.QuotingAssetId == newRateCommand.BaseAssetId));
            Assert.IsTrue(rates.Single(x => x.BaseAssetId == baseAssetId).BidPrice.Equals(newRateCommand.BidPrice));
            Assert.IsTrue(rates.Single(x => x.BaseAssetId == baseAssetId).AskPrice.Equals(newRateCommand.AskPrice));
            Assert.IsTrue(rates.Single(x => x.BaseAssetId == quotingAssetId).BidPrice
                          .Equals(newRateCommand.BidPrice > 0 ? 1 / newRateCommand.BidPrice : 0));
            Assert.IsTrue(rates.Single(x => x.BaseAssetId == quotingAssetId).AskPrice
                          .Equals(newRateCommand.AskPrice > 0 ? 1 / newRateCommand.AskPrice : 0));
        }
Exemple #2
0
        public async Task <IActionResult> AddRate([FromBody] AddAssetRateModel request)
        {
            try
            {
                string lykkeBaseAssetId = await _lykkeAssetsResolver.GetLykkeId(request.BaseAssetId);

                if (await _assetsLocalCache.GetAssetByIdAsync(lykkeBaseAssetId) == null)
                {
                    return(NotFound(ErrorResponse.Create("Base asset not found")));
                }

                string lykkeQuotingAssetId = await _lykkeAssetsResolver.GetLykkeId(request.QuotingAssetId);

                if (await _assetsLocalCache.GetAssetByIdAsync(lykkeQuotingAssetId) == null)
                {
                    return(NotFound(ErrorResponse.Create("Quoting asset not found")));
                }

                IAssetPairRate newRate = await _assetRatesService.AddAsync(Mapper.Map <AddAssetPairRateCommand>(request, opt =>
                {
                    opt.Items["BaseAssetId"]    = lykkeBaseAssetId;
                    opt.Items["QuotingAssetId"] = lykkeQuotingAssetId;
                }));

                return(Ok(Mapper.Map <AssetRateResponse>(newRate, opt =>
                {
                    opt.Items["BaseAssetId"] = request.BaseAssetId;
                    opt.Items["QuotingAssetId"] = request.QuotingAssetId;
                })));
            }
            catch (AssetUnknownException e)
            {
                _log.ErrorWithDetails(e, new { e.Asset });

                return(NotFound(ErrorResponse.Create($"Asset not found [{e.Asset}]")));
            }
            catch (AssetPairRateStorageNotSupportedException e)
            {
                _log.ErrorWithDetails(e, new
                {
                    e.BaseAssetId,
                    e.QuotingAssetId
                });

                return(StatusCode((int)HttpStatusCode.NotImplemented, ErrorResponse.Create(e.Message)));
            }
        }