Exemple #1
0
        void CheckTxs(ApplicationUser brokerUser, ApplicationUser user, BrokerOrder order)
        {
            // get wallet and only update from the blockchain one time
            IWallet wallet = _walletProvider.GetChain(order.AssetSend);

            var txs    = wallet.GetAddrUnacknowledgedTransactions(order.PaymentAddress);
            var ackTxs = new List <WalletTx>();

            foreach (var tx in txs)
            {
                // get invoice id
                string invoiceId = null;
                if (tx.ChainTx.Attachment != null)
                {
                    var att = System.Text.Encoding.UTF8.GetString(tx.ChainTx.Attachment.Data);
                    try
                    {
                        var dict = JsonConvert.DeserializeObject <Dictionary <string, string> >(att);
                        invoiceId = dict.FirstOrDefault(x => String.Equals(x.Key, "InvoiceId", StringComparison.OrdinalIgnoreCase)).Value;
                    }
                    catch {}
                }
                // check tx is incomming to our wallet
                if (tx.Direction == WalletDirection.Incomming)
                {
                    // check invoice id matches (if asset uses account model)
                    if (wallet.GetLedgerModel() == xchwallet.LedgerModel.Account && invoiceId == order.InvoiceId ||
                        wallet.GetLedgerModel() == xchwallet.LedgerModel.UTXO)
                    {
                        // check amount matches
                        var amount = wallet.AmountToString(tx.AmountOutputs());
                        if (order.AmountSend <= decimal.Parse(amount))
                        {
                            // bingo!
                            if (order.Status == BrokerOrderStatus.Ready.ToString())
                            {
                                order.Status      = BrokerOrderStatus.Incomming.ToString();
                                order.TxIdPayment = tx.ChainTx.TxId;
                                _context.BrokerOrders.Update(order);
                                _logger.LogInformation($"Payment detected for order {order.Token}, {tx}");

                                // send email
                                _emailSender.SendEmailBrokerSeenIncomingFunds(user.Email, order.AssetSend, wallet.AmountToString(order.AmountSend), order.InvoiceId).GetAwaiter().GetResult();
                                _logger.LogInformation($"Sent email to {user.Email}");
                            }
                            else if (order.Status == BrokerOrderStatus.Incomming.ToString() &&
                                     tx.ChainTx.Confirmations >= _walletProvider.ChainAssetSettings(order.AssetSend).MinConf)
                            {
                                order.Status = BrokerOrderStatus.Confirmed.ToString();
                                _context.BrokerOrders.Update(order);
                                ackTxs.Add(tx);
                                _logger.LogInformation($"Payment confirmed for order {order.Token}, {tx}");
                                DepositAndCreateTrade(brokerUser, order);
                            }
                        }
                    }
                }
            }
            wallet.AcknowledgeTransactions(ackTxs);
            wallet.Save();
        }