private void SaveDbTransaction(string txid, string rawData)
        {
            try
            {
                var raw = Newtonsoft.Json.JsonConvert.SerializeObject(CoinService.DecodeRawTransaction(rawData));

                string cmdString = "INSERT INTO [TransactionBroadcast] ([TxId],[BroadcastTime],[RawTransaction]) VALUES (@TxId, @BroadcastTime, @RawTransaction)";
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    using (SqlCommand comm = new SqlCommand())
                    {
                        comm.Connection  = conn;
                        comm.CommandText = cmdString;
                        comm.Parameters.AddWithValue("@TxId", txid);
                        comm.Parameters.AddWithValue("@BroadcastTime", DateTime.UtcNow);
                        comm.Parameters.AddWithValue("@RawTransaction", raw);
                        try
                        {
                            conn.Open();
                            comm.ExecuteNonQuery();
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("SQL Error" + ex.Message);
                        }
                    }
                }
            }
            catch
            {
            }
        }
Esempio n. 2
0
        public IActionResult SendTransaction([FromBody] GenericRequestModel <string> txHex)
        {
            string txid       = string.Empty;
            string errMessage = string.Empty;

            try
            {
                txid = CoinService.SendRawTransaction(txHex.Data, false);
                var raw = Newtonsoft.Json.JsonConvert.SerializeObject(CoinService.DecodeRawTransaction(txHex.Data));

                string cmdString = "INSERT INTO [TransactionBroadcast] ([TxId],[BroadcastTime],[RawTransaction]) VALUES (@TxId, @BroadcastTime, @RawTransaction)";
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    using (SqlCommand comm = new SqlCommand())
                    {
                        comm.Connection  = conn;
                        comm.CommandText = cmdString;
                        comm.Parameters.AddWithValue("@TxId", txid);
                        comm.Parameters.AddWithValue("@BroadcastTime", DateTime.UtcNow);
                        comm.Parameters.AddWithValue("@RawTransaction", raw);
                        try
                        {
                            conn.Open();
                            comm.ExecuteNonQuery();
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("SQL Error" + ex.Message);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(new ErrorModel()
                {
                    Error = "Transactions pending, please try again after 1 minute."
                }));
            }

            return(new ObjectResult(new { tx = txid }));
        }
        public IActionResult SendTransaction([FromBody] GenericRequestModel <string> txHex)
        {
            var    watch        = System.Diagnostics.Stopwatch.StartNew();
            string txid         = string.Empty;
            string errMessage   = string.Empty;
            bool   isInstantPay = true;

            try
            {
                //txid = CoinService.SendRawTransaction(txHex.Data, false);

                var rawTx = CoinService.DecodeRawTransaction(txHex.Data);

                var senderTxs = rawTx.Vin.Select(t => new TransactionList()
                {
                    Txid = t.TxId, Index = Convert.ToInt32(t.Vout)
                }).ToList();
                isInstantPay = CheckInstantPay(senderTxs);

                try
                {
                    txid = CoinService.SendRawTransaction(txHex.Data, false, isInstantPay);
                }
                catch (System.Exception ex)
                {
                    if (ex.Message.IndexOf("Not a valid InstantSend") > -1)
                    {
                        try
                        {
                            txid = CoinService.SendRawTransaction(txHex.Data, false, false);
                        }
                        catch (System.Exception newEx)
                        {
                            return(BadRequest(new ErrorModel()
                            {
                                Error = "Transactions pending, please try again after 1 minute.", Description = newEx.Message
                            }));
                        }
                    }
                    return(BadRequest(new ErrorModel()
                    {
                        Error = "Transactions pending, please try again after 1 minute.", Description = ex.Message
                    }));
                }

                Task.Run(() => SaveDbTransaction(txid, txHex.Data));
            }
            catch (Exception ex)
            {
                return(BadRequest(new ErrorModel()
                {
                    Error = "Transactions pending, please try again after 1 minute.", Description = ex.Message
                }));
            }
            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;

            return(new ObjectResult(new
            {
                tx = txid,
                isInstantPay = isInstantPay,
                Execution = elapsedMs
            }));
        }