Exemplo n.º 1
0
        public override async Task SendCoinTransaction(string fromPrivateAddress,
                                                       string sendPublicAddress, string to,
                                                       decimal amountToSend, decimal amountInCurrency, string currency, string walletName,
                                                       ReportResult reportResult)
        {
            CoinBalanceStruct sbalance = await GetAccountBalance(fromPrivateAddress);

            var txBuilder = new TransactionBuilder();
            var secret    = new BitcoinSecret(fromPrivateAddress);
            var toAmount  = new Money(amountToSend, MoneyUnit.BTC);
            var minerFee  = new Money(0.001m, MoneyUnit.BTC);

            Database.TransactionData transactionSendData = new Database.TransactionData();

            transactionSendData.Symbol           = "BTC";
            transactionSendData.WalletName       = walletName;
            transactionSendData.TransactionType  = Database.TransactionTypeEnum.SEND;
            transactionSendData.AmountInCurrency = amountInCurrency;
            transactionSendData.Coins            = amountToSend;
            transactionSendData.Change           = sbalance.confirmedBalance - amountToSend;
            transactionSendData.To            = to;
            transactionSendData.ToAddress     = sendPublicAddress;
            transactionSendData.ChangeAddress = secret.GetAddress().ToString();
            //@@todo
            transactionSendData.Currency = currency;
            transactionSendData.Date     = DateTime.Now;

            if (amountToSend >= sbalance.confirmedBalance)
            {
                transactionSendData.Status   = "Fail";
                transactionSendData.Response = "Bitcoin transaction failed: There are not enough coins in your bitcoin account.";

                WriteTransactionToDatabase(transactionSendData);

                reportResult(transactionSendData.Response);

                return;
            }

            var tx = txBuilder.AddCoins(sbalance.coins)
                     .AddKeys(secret)
                     .Send(BitcoinAddress.Create(sendPublicAddress, Network.TestNet), toAmount)
                     .SendFees(minerFee)
                     .SetChange(secret.GetAddress())
                     .BuildTransaction(true);

            if (!txBuilder.Verify(tx))
            {
                transactionSendData.Status   = "Fail";
                transactionSendData.Response = "Bitcoin transaction failed: Unable to verify bitcoin transaction.";

                WriteTransactionToDatabase(transactionSendData);

                reportResult(transactionSendData.Response);

                return;
            }

            QBitNinjaClient client = new QBitNinjaClient(Network.TestNet);

            BroadcastResponse broadcastResponse = await Task.Run(() => client.Broadcast(tx).Result);

            if (!broadcastResponse.Success)
            {
                reportResult("Error broadcasting transaction " + broadcastResponse.Error.ErrorCode.ToString() + " : " +
                             broadcastResponse.Error.Reason.ToString());
                transactionSendData.Response = broadcastResponse.Error.Reason;
            }
            else
            {
                reportResult("Transaction was broadcasted successfully.");
                transactionSendData.Response = "Success";
            }

            transactionSendData.Status = broadcastResponse.Success ? "Success" : "Fail";

            transactionSendData.HasWitness  = tx.HasWitness;
            transactionSendData.MinerFee    = minerFee.ToDecimal(MoneyUnit.BTC);
            transactionSendData.TotalOut    = tx.TotalOut.ToDecimal(MoneyUnit.BTC);
            transactionSendData.Version     = tx.Version;
            transactionSendData.HashCode    = tx.GetHashCode();
            transactionSendData.VirtualSize = tx.GetVirtualSize();

            WriteTransactionToDatabase(transactionSendData);

            //using (var node = NBitcoin.Protocol.Node.Connect(Network.TestNet, ""))
            //{
            //    node.VersionHandshake();
            //    node.SendMessage(new NBitcoin.Protocol.InvPayload(tx));
            //    node.SendMessage(new NBitcoin.Protocol.TxPayload(tx));
            //    System.Threading.Thread.Sleep(500);
            //    node.Disconnect();
            //}
        }
Exemplo n.º 2
0
 private void WriteTransactionToDatabase(Database.TransactionData transactionSendData)
 {
     Database.DataLayer.WriteObject(transactionSendData);
 }