var client = new QBitNinjaClient(network);
var transactionId = uint256.Parse("e44587cf08b4f03b0e8b4ae7562217796ec47b8c91666681d71329b764add2e3");
var transactionResponse = client.GetTransaction(transactionId).Result;
Console.WriteLine(transactionResponse.TransactionId); // e44587cf08b4f03b0e8b4ae7562217796ec47b8c91666681d71329b764add2e3
Console.WriteLine(transactionResponse.Block.Confirmations);
var receivedCoins = transactionResponse.ReceivedCoins;
OutPoint outPointToSpend = null;
foreach (var coin in receivedCoins)
{
if (coin.TxOut.ScriptPubKey == bitcoinPrivateKey.ScriptPubKey)
{
outPointToSpend = coin.Outpoint;
}
}
if(outPointToSpend == null)
throw new Exception("エラー:どのトランザクションアウトプットも送ったコインのScriptPubKeyを持ってない!");
Console.WriteLine("{0}番目のアウトポイントを使いたい。", outPointToSpend.N + 1);
var transaction = new Transaction();
transaction.Inputs.Add(new TxIn()
{
PrevOut = outPointToSpend
});
var hallOfTheMakersAddress = new BitcoinPubKeyAddress("1KF8kUVHK42XzgcmJF4Lxz4wcL5WDL97PB");
var hallOfTheMakersAddress = BitcoinAddress.Create("mzp4No5cmCXjZUpf112B1XWsvWBfws5bbB");
// 成功者の殿堂に送る分
TxOut hallOfTheMakersTxOut = new TxOut()
{
Value = new Money((decimal)0.5, MoneyUnit.BTC),
ScriptPubKey = hallOfTheMakersAddress.ScriptPubKey
};
// 自分に戻す分 (おつり)
TxOut changeBackTxOut = new TxOut()
{
Value = new Money((decimal)0.4999, MoneyUnit.BTC),
ScriptPubKey = bitcoinPrivateKey.ScriptPubKey
};
transaction.Outputs.Add(hallOfTheMakersTxOut);
transaction.Outputs.Add(changeBackTxOut);
// ビットコインをいくら送金先に送りたいか
var hallOfTheMakersAmount = new Money(0.5m, MoneyUnit.BTC);
/* これを書いている時点では、マイニング手数料は5セント
* だが、ビットコインのマーケット価格と
* その時の推奨手数料レートに合わせて
* 手数料を上げるか、もしくは、下げるかを考慮する必要がある。
*/
var minerFee = new Money(0.0001m, MoneyUnit.BTC);
// UTXOからいくらトータルでビットコインを 使いたいか。
var txInAmount = receivedCoins[(int) outPointToSpend.N].TxOut.Value;
Money changeBackAmount = txInAmount - hallOfTheMakersAmount - minerFee;
TxOut hallOfTheMakersTxOut = new TxOut()
{
Value = hallOfTheMakersAmount,
ScriptPubKey = hallOfTheMakersAddress.ScriptPubKey
};
TxOut changeBackTxOut = new TxOut()
{
Value = changeBackAmount,
ScriptPubKey = bitcoinPrivateKey.ScriptPubKey
};
BroadcastResponse broadcastResponse = client.Broadcast(transaction).Result;
if (!broadcastResponse.Success)
{
Console.WriteLine("ErrorCode: " + broadcastResponse.Error.ErrorCode);
Console.WriteLine("Error message: " + broadcastResponse.Error.Reason);
}
else
{
Console.WriteLine("Success! You can check out the hash of the transaciton in any block explorer:");
Console.WriteLine(transaction.GetHash());
}
using (var node = Node.ConnectToLocal(network)) //Connect to the node
{
node.VersionHandshake(); //Say hello
//Advertize your transaction (send just the hash)
node.SendMessage(new InvPayload(InventoryType.MSG_TX, transaction.GetHash()));
//Send it
node.SendMessage(new TxPayload(transaction));
Thread.Sleep(500); //Wait a bit
}