Exemplo n.º 1
0
        public async Task <bool> CheckExchange(Exchange exchange)
        {
            bool changed = false;

            try
            {
                var sw = new Stopwatch();
                sw.Start();

                var status = await checker.GetTransactionStatusesByAddress(exchange.PayWalletAddress, tryCount, delayMs);

                if (status == null || status.Status == TransactionStatus.NotFound)
                {
                    return(changed);
                }

                decimal receivedAmount = BitcoinConvert.FromAtomicUnits(status.Amount);

                PaymentStatus newStatus = PaymentStatus.Received;
                if (status.Status == TransactionStatus.DoubleSpent)
                {
                    newStatus = PaymentStatus.DoubleSpend;
                }

                if (exchange.InTxStatus != newStatus ||
                    exchange.ReceivedConfirmations != status.Confirmations ||
                    exchange.ReceivedAmount != receivedAmount)
                {
                    exchange.InTxStatus            = newStatus;
                    exchange.ReceivedConfirmations = status.Confirmations;
                    exchange.ReceivedAmount        = receivedAmount;
                    changed = true;
                }

                if (State != WatchableServiceState.OK)
                {
                    SetState(WatchableServiceState.OK);
                }

                sw.Stop();
                UpdateStopwatchMetrics(sw, true);

                totalExchangesBySession++;
                Metrics[$"Total Payments By Session"] = totalExchangesBySession.ToString();

                totalAmountExchangeBySession += exchange.ReceivedAmount;
                Metrics[$"Total Received Exchange By Session"] = totalAmountExchangeBySession.ToString();
            }
            catch (Exception ex)
            {
                SetState(WatchableServiceState.Error, ex.Message);
                throw;
            }
            finally
            {
                LastOperationTime = DateTime.UtcNow;
            }
            return(changed);
        }
Exemplo n.º 2
0
        public async Task CheckPayment(Payment payment)
        {
            try
            {
                var sw = new Stopwatch();
                sw.Start();

                var status = await checker.GetTransactionStatusesByAddress(payment.PayWalletAddress, tryCount, delayMs);

                if (status == null || status.Status == TransactionStatus.NotFound)
                {
                    return;
                }

                decimal receivedAmount = BitcoinConvert.FromAtomicUnits(status.Amount);

                if (status.Status == TransactionStatus.DoubleSpent)
                {
                    payment.Status = PaymentStatus.DoubleSpend;
                }
                else if ((payment.PayAmount - receivedAmount) * payment.PayToSaleRate > 0.01M)
                {
                    payment.Status = PaymentStatus.NotEnoughAmount;
                }
                else
                {
                    payment.Status = PaymentStatus.Received;
                }

                payment.ReceivedConfirmations = status.Confirmations;
                payment.ReceivedAmount        = receivedAmount;

                if (State != WatchableServiceState.OK)
                {
                    SetState(WatchableServiceState.OK);
                }

                sw.Stop();
                UpdateStopwatchMetrics(sw, true);

                totalPaymentsBySession++;
                Metrics[$"Total Payments By Session"] = totalPaymentsBySession.ToString();

                totalAmountPaymentBySession += payment.ReceivedAmount;
                Metrics[$"Total Received Payments By Session"] = totalAmountPaymentBySession.ToString();
            }
            catch (Exception ex)
            {
                SetState(WatchableServiceState.Error, ex.Message);
                throw;
            }
            finally
            {
                LastOperationTime = DateTime.UtcNow;
            }
        }