public void GetTransactionInfoWorks() { var monitor = new HttpMonitor(Network.MainNet); var transactionInfo = monitor.GetTransactionInfo("2d0a108be81fe0d807a0cdd65233158bac1642081033c21b379d776f278015a4"); Assert.True(transactionInfo.AllInOutsAdded); Assert.True(transactionInfo.Confirmed); Assert.Equal(transactionInfo.Network, Network.MainNet); Assert.True(transactionInfo.Fee > 0); Assert.True(transactionInfo.Inputs.Count > 10); Assert.Equal(transactionInfo.TotalOutputAmount, 15m); Assert.Equal(transactionInfo.Outputs.First().Address, "1EDt6Pe5psPLrAKmq7xawCFo9LxtKoJz7g"); }
private static void Part2Lesson1() { var network = Network.MainNet; // HttpMonitor can monitor The Blockchain through HTTP (QBitNinja) var httpMonitor = new HttpMonitor(network); // Checking address balances var balanceInfo = httpMonitor.GetAddressBalanceInfo("1ENCTCkqoJqy2XZ2m2Dy1bRax7hsSnC5Fc"); WriteLine($"Address balance: {balanceInfo.Balance}"); // 0,05474889 WriteLine($"Confirmed balance: {balanceInfo.Confirmed}"); // 0 WriteLine($"Unconfirmed balance: {balanceInfo.Unconfirmed}"); // 0,05474889 // Get history of an address var history = httpMonitor.GetAddressHistory("1ENCTCkqoJqy2XZ2m2Dy1bRax7hsSnC5Fc"); WriteLine("Number of transactions: " + history.Records.Count); // Exercise: are all transaction confirmed? var allTransactionsConfirmed = true; foreach (var record in history.Records) { WriteLine(record.TransactionId + " : " + record.Amount); allTransactionsConfirmed = allTransactionsConfirmed && record.Confirmed; } WriteLine("All transactions are confirmed: " + allTransactionsConfirmed); // Exercise: get the balance of the address WriteLine("Total received - Total spent = Balance"); WriteLine(history.TotalReceived + " - " + history.TotalSpent + " = " + (history.TotalReceived - history.TotalSpent)); // Get some data from the transaction var transactionInfo1 = httpMonitor.GetTransactionInfo(history.Records.First().TransactionId); WriteLine("txid: " + transactionInfo1.Id); WriteLine("Network: " + transactionInfo1.Network); WriteLine("Confirmed: " + transactionInfo1.Confirmed); WriteLine("Total amount of all inputs: " + transactionInfo1.TotalInputAmount); WriteLine("Total amount of all outputs: " + transactionInfo1.TotalOutputAmount); WriteLine("Fee : " + transactionInfo1.Fee); WriteLine(Environment.NewLine + "Input addresses and amounts: "); foreach (var input in transactionInfo1.Inputs) { WriteLine(input.Amount + " " + input.Address); } WriteLine(Environment.NewLine + "Output addresses and amounts: "); foreach (var output in transactionInfo1.Outputs) { WriteLine(output.Amount + " " + output.Address); } // Sometimes my API can't fully process a transaction, because it has OP_RETURN for example // It should not be a concern for a Bitcoin wallet that purely handles money, if a transaction output or input has not been added // that means it has some other purpose, a wallet API can dismiss it // This tx is exotic (has OP_RETURN) var transactionInfo2 = httpMonitor.GetTransactionInfo("8bae12b5f4c088d940733dcd1455efc6a3a69cf9340e17a981286d3778615684"); WriteLine(transactionInfo2.Id); WriteLine("There are exotic inputs or outputs, so not all of them have been added successfully: " + Environment.NewLine + !transactionInfo2.AllInOutsAdded); }
public static void Send(ConnectionType connectionType, TransactionInfo transactionInfo, int tryTimes = 1) { var monitor = new HttpMonitor(transactionInfo.Network); if (connectionType == ConnectionType.Http) { var client = new QBitNinjaClient(Convert.ToNBitcoinNetwork(transactionInfo.Network)); var transaction = FindTransaction(transactionInfo); var broadcastResponse = client.Broadcast(transaction).Result; if (!broadcastResponse.Success) { throw new Exception($"ErrorCode: {broadcastResponse.Error.ErrorCode}" + Environment.NewLine + broadcastResponse.Error.Reason); } } if (connectionType == ConnectionType.RandomNode) { var parameters = new NodeConnectionParameters(); var group = new NodesGroup(Convert.ToNBitcoinNetwork(transactionInfo.Network), parameters, new NodeRequirement { RequiredServices = NodeServices.Nothing }) { MaximumNodeConnection = 1 }; group.Connect(); while (group.ConnectedNodes.Count == 0) { Thread.Sleep(100); } var transaction = FindTransaction(transactionInfo); var payload = new TxPayload(transaction); group.ConnectedNodes.First().SendMessage(payload); } for (var i = 0; i < 10; i++) { try { monitor.GetTransactionInfo(transactionInfo.Id); } catch (NullReferenceException exception) { if (exception.Message != "Transaction does not exists") { throw; } Thread.Sleep(1000); continue; } if (i == 10) { if (tryTimes == 1) { throw new Exception("Transaction has not been broadcasted, try again!"); } Send(connectionType, transactionInfo, tryTimes - 1); } break; } }