예제 #1
0
        protected override async Task OnUpdate()
        {
            _dbContext.DetachEverything();

            var rows = await
                           (from r in _dbContext.EthSending where r.Status == EthereumOperationStatus.Initial select r)
                       .Include(_ => _.User)
                       .Include(_ => _.RelFinHistory)
                       .AsTracking()
                       .Take(_rowsPerRound)
                       .ToArrayAsync(CancellationToken)
            ;

            if (IsCancelled() || rows.Length == 0)
            {
                return;
            }

            var ethAmount = await _ethReader.GetEtherBalance(await _ethWriter.GetEthSender());

            foreach (var r in rows)
            {
                if (IsCancelled() || ethAmount < r.Amount.ToEther())
                {
                    return;
                }

                try {
                    r.Status = EthereumOperationStatus.BlockchainInit;
                    _dbContext.SaveChanges();

                    var tx = await _ethWriter.SendEth(r.Address, r.Amount.ToEther());

                    r.Status        = EthereumOperationStatus.BlockchainConfirm;
                    r.Transaction   = tx;
                    r.TimeNextCheck = DateTime.UtcNow.AddSeconds(60);

                    ethAmount -= r.Amount.ToEther();

                    try {
                        // notification
                        await EmailComposer.FromTemplate(await _templateProvider.GetEmailTemplate(EmailTemplate.ExchangeEthTransferred, Locale.En))
                        .ReplaceBodyTag("REQUEST_ID", r.Id.ToString())
                        .ReplaceBodyTag("ETHERSCAN_LINK", _appConfig.Services.Ethereum.EtherscanTxView + tx)
                        .ReplaceBodyTag("DETAILS_SOURCE", r.RelFinHistory?.SourceAmount ?? "?" + " GOLD")
                        .ReplaceBodyTag("DETAILS_ESTIMATED", r.RelFinHistory?.DestinationAmount ?? "?" + " ETH")
                        .ReplaceBodyTag("DETAILS_ADDRESS", TextFormatter.MaskBlockchainAddress(r.Address))
                        .Send(r.User.Email, r.User.UserName, _notificationQueue)
                        ;
                    } catch { }
                } catch (Exception e) {
                    Logger.Error(e, $"Failed to process #{r.Id}");
                }
            }
        }
예제 #2
0
        protected override async Task OnUpdate()
        {
            _dbContext.DetachEverything();

            var rows = await
                           (from r in _dbContext.SellGoldEth where r.Status == BuySellGoldRequestStatus.Confirmed select r)
                       .Include(_ => _.RelFinHistory)
                       .AsTracking()
                       .Take(_rowsPerRound)
                       .ToArrayAsync(CancellationToken)
            ;

            if (IsCancelled() || rows.Length == 0)
            {
                return;
            }

            var ethAmount = await _ethereumReader.GetEtherBalance(await _ethereumWriter.GetEthSender());

            foreach (var r in rows)
            {
                if (IsCancelled() || ethAmount < r.EthAmount.ToEther())
                {
                    return;
                }

                try {
                    using (var tx = await _dbContext.Database.BeginTransactionAsync()) {
                        r.Status        = BuySellGoldRequestStatus.Success;
                        r.TimeCompleted = DateTime.UtcNow;
                        _dbContext.SaveChanges();

                        var sending = new DAL.Models.EthSending()
                        {
                            Status          = EthereumOperationStatus.Initial,
                            Amount          = r.EthAmount,
                            Address         = r.Destination,
                            TimeCreated     = DateTime.UtcNow,
                            TimeNextCheck   = DateTime.UtcNow,
                            RelFinHistoryId = r.RelFinHistory?.Id,
                            UserId          = r.UserId,
                        };
                        _dbContext.EthSending.Add(sending);
                        _dbContext.SaveChanges();

                        tx.Commit();
                    }
                    ethAmount -= r.EthAmount.ToEther();
                } catch (Exception e) {
                    Logger.Error(e, $"Failed to process #{r.Id}");

                    try {
                        r.Status        = BuySellGoldRequestStatus.Failed;
                        r.TimeCompleted = DateTime.UtcNow;
                        if (r.RelFinHistory != null)
                        {
                            r.RelFinHistory.Status = UserFinHistoryStatus.Failed;
                        }
                        _dbContext.SaveChanges();

                        await CoreLogic.Finance.SumusWallet.Refill(_services, r.UserId, r.GoldAmount, SumusToken.Gold);
                    } catch (Exception e1) {
                        Logger.Error(e1, $"Failed to update request status #{r.Id}");
                    }
                }
            }
        }