Example #1
0
        public async Task <BrokerExchangeResult> ExchangeStatus(string exchangeId)
        {
            _logger.LogInformation("ExchangeStatus: {@params}", exchangeId);

            if (string.IsNullOrEmpty(exchangeId))
            {
                throw new ApiException(ErrorCode.ExchangeIdEmpty);
            }

            _cache.TryGetValue(exchangeId, out Exchange exchange);
            if (exchange == null)
            {
                exchange = await _db.Exchange.FirstOrDefaultAsync(t => t.ExchangeId == exchangeId);

                if (exchange == null)
                {
                    throw new ApiException(ErrorCode.ExchangeNotFoundOrExpired);
                }
            }


            //todo -
            {
                await _cryptoProviderService.CheckExchange(exchange);

                if (exchange.Status == PaymentStatus.Received)
                {
                    exchange.Log($"Received Payment of {exchange.ReceivedAmount} {exchange.SellCurrency}");

                    // received amount can be different from initial amount,
                    // so we need to recalculate all amounts
                    ExchangeCalculator.Recalc(exchange, _settings);

                    await _wallet.ProcessExchange(exchange, _db);
                }


                _db.Exchange.Update(exchange);
                await _db.SaveChangesAsync();
            }

            var res = GetExchangeResult(exchange);

            _logger.LogInformation("ExchangeStatus Result: {@params}", res);
            return(res);
        }
        public async Task <BrokerExchangeResult> ExchangeStatus(string exchangeId)
        {
            _logger.LogInformation("ExchangeStatus: {@params}", exchangeId);

            Exchange exchange = await GetExchange(exchangeId);

            if (exchange.InTxStatus == PaymentStatus.Waiting)
            {
                if (await _cryptoProviderService.CheckExchange(exchange).ConfigureAwait(false))
                {
                    if (exchange.InTxStatus >= PaymentStatus.Received && exchange.OutTxStatus == PaymentStatus.New)
                    {
                        exchange.OutTxStatus = PaymentStatus.Fail;
                        try
                        {
                            exchange.Log($"Received Payment of {exchange.ReceivedAmount} {exchange.SellCurrency}");

                            // received amount can be different from initial amount,
                            // so we need to recalculate all amounts
                            ExchangeCalculator.Recalc(exchange, _settings);

                            exchange.OutTxStatus = await _graft.Pay(exchange.OutTxId, exchange.OutBlockNumber,
                                                                    exchange.BuyerWallet, exchange.BuyAmount);
                        }
                        catch (Exception ex)
                        {
                            exchange.OutTxStatus            = PaymentStatus.Fail;
                            exchange.OutTxStatusDescription = ex.Message;
                        }

                        _db.Exchange.Update(exchange);
                        await _db.SaveChangesAsync();
                    }
                }
            }

            var res = GetExchangeResult(exchange);

            _logger.LogInformation("ExchangeStatus Result: {@params}", res);
            return(res);
        }