protected override async Task <Transaction> SendTransactionAsync(TxOut[] data) { var tx = new Transaction(); tx.Outputs.AddRange(data); var changeAddress = _RPCClient.GetRawChangeAddress(); FundRawTransactionResponse response = null; try { response = await _RPCClient.FundRawTransactionAsync(tx, new FundRawTransactionOptions() { ChangeAddress = changeAddress, FeeRate = FeeRate, LockUnspents = true }).ConfigureAwait(false); } catch (RPCException ex) { var balance = await _RPCClient.GetBalanceAsync(0, false).ConfigureAwait(false); var needed = tx.Outputs.Select(o => o.Value).Sum() + FeeRate.GetFee(2000); var missing = needed - balance; if (missing > Money.Zero || ex.Message.Equals("Insufficient funds", StringComparison.OrdinalIgnoreCase)) { throw new NotEnoughFundsException("Not enough funds", "", missing); } throw; } var result = await _RPCClient.SendCommandAsync("signrawtransaction", response.Transaction.ToHex()).ConfigureAwait(false); return(new Transaction(((JObject)result.Result)["hex"].Value <string>())); }
private static async Task <uint256> FundAndBroadcast(RPCClient rpcClient, Transaction tx) { var change = await rpcClient.GetRawChangeAddressAsync().ConfigureAwait(false); var funded = await rpcClient.FundRawTransactionAsync(tx, new FundRawTransactionOptions() { ChangeAddress = change, LockUnspents = true, IncludeWatching = false }).ConfigureAwait(false); var result = await rpcClient.SendCommandAsync("signrawtransaction", funded.Transaction.ToHex()).ConfigureAwait(false); var txId = await rpcClient.SendCommandAsync("sendrawtransaction", ((JObject)result.Result)["hex"].Value <string>()); return(new uint256(txId.Result.Value <string>())); }
public async Task TxSelection() { var network = Network.RegTest; var client = new RPCClient( credentials: new NetworkCredential( userName: "******", password: "******"), address: new Uri("http://127.0.0.1:443"), network: network); Dictionary <string, int> salaries = GetSalaryList(); decimal btcPerTl = 0.00001m; // Create a transaction var transaction = Transaction.Create(network); transaction.Inputs.Add(new OutPoint() { Hash = new uint256("52560426854bb267b96f6f2fc425b88d86a5c49058e230abb5400b14a9433782"), N = 3 }); foreach (var salary in salaries) { transaction.Outputs.Add(new TxOut() { Value = new Money(salary.Value * btcPerTl, MoneyUnit.BTC), ScriptPubKey = BitcoinAddress.Create(salary.Key, network).ScriptPubKey }); } // Func the transaction var fundTxResponse = await client.FundRawTransactionAsync(transaction); // Sign & send the transaction var signedTxResponse = await client.SignRawTransactionWithWalletAsync(new SignRawTransactionRequest() { Transaction = fundTxResponse.Transaction, }); var txHash = await client.SendRawTransactionAsync(signedTxResponse.SignedTransaction); }
public async Task AutoFund() { var network = Network.RegTest; var client = new RPCClient( credentials: new NetworkCredential( userName: "******", password: "******"), address: new Uri("http://127.0.0.1:443"), network: network); Dictionary <string, int> salaries = GetSalaryList(); decimal btcPerTl = 0.00001m; // Create a transaction var transaction = Transaction.Create(network); // Add outputs foreach (var salary in salaries) { transaction.Outputs.Add(new TxOut() { Value = new Money(salary.Value * btcPerTl, MoneyUnit.BTC), ScriptPubKey = BitcoinAddress.Create(salary.Key, network).ScriptPubKey }); } // Func the transaction var fundTxResponse = await client.FundRawTransactionAsync(transaction); // Sign & send the transaction var signedTxResponse = await client.SignRawTransactionWithWalletAsync(new SignRawTransactionRequest() { Transaction = fundTxResponse.Transaction, }); var txHash = await client.SendRawTransactionAsync(signedTxResponse.SignedTransaction); }