public async Task BuyCurrency(string token, BuyCurrencyRequest body)
        {
            var client  = new RestClient(_baseUrl);
            var request = new RestRequest(_buyCurrencyRoute, Method.POST);

            request.AddHeader("Authorization", $"Bearer {token}");
            request.AddJsonBody(body);
            _ = await client.ExecuteAsync(request);
        }
Beispiel #2
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            await Task.Delay(5000);

            var token = await _apiClient.Authenticate();

            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {
                    var portfolio = await _apiClient.GetPortfolio(token);

                    var currencies = await _apiClient.GetCurrencies(token);

                    var euroId = currencies.Single(x => x.Name == "Euro");

                    var bestCurrencyId  = _exchangeTrend.GetBest();
                    var worstCurrencyId = _exchangeTrend.GetWorst();

                    if (bestCurrencyId != Guid.Empty && worstCurrencyId != Guid.Empty)
                    {
                        var worstCurrency = portfolio.Currencies.Single(x => x.CurrencyId == worstCurrencyId);

                        if (worstCurrency.Amount == 0M)
                        {
                            worstCurrency   = portfolio.Currencies.Single(x => x.CurrencyId == euroId.Id);
                            worstCurrencyId = worstCurrency.CurrencyId;
                        }

                        var buy = new BuyCurrencyRequest
                        {
                            FromCurrencyId = worstCurrencyId,
                            ToCurrencyId   = bestCurrencyId,
                            FromAmount     = worstCurrency.Amount / 3M
                        };

                        await _apiClient.BuyCurrency(token, buy);
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex.Message);
                }

                await Task.Delay(1000, stoppingToken);
            }
        }