MetacoSA / QBitNinja

An Open Source and powerful blockchain API
MIT License
68 stars 42 forks source link

QbitNinja.client transaction issue #32

Open james1278 opened 6 years ago

james1278 commented 6 years ago

Hi, I have installed Qbitninja client package in my c# project. Now i am trying to send transaction on Testnet, but could not see those transaction using following code. My question:

  1. do we need to add or install anything else on server?
  2. Do we need qbitninja.server?
  3. Do we need azure server or it can run on any pc?
  4. If possible then please provide sample code.
  5. What fee we need to used? because i tried 0.0004 as well as .0015.

public void SendCoin(string receiverAdress, decimal amount, string message = null) { BitcoinSecret secretPrivateKey = masterKey.ExtKey.Derive(1).PrivateKey.GetWif(network); var cl = new List(); Money minerFee = new Money(0.0004m, MoneyUnit.BTC); var transaction = new Transaction();

        Money currrentBalance = new Money(0, MoneyUnit.BTC);
        var historyTransaction = client.GetBalance(secretPrivateKey.ScriptPubKey, true).Result;
        foreach (var tx in historyTransaction.Operations)
        {
            currrentBalance += tx.Amount;
            foreach (var coin in tx.ReceivedCoins)
            {
                cl.Add(coin);
                transaction.Inputs.Add(new TxIn(coin.Outpoint, secretPrivateKey.ScriptPubKey));
            }
        }               

        // calculate what amount of bitcoin needed to be send
        Money receiverAmount = new Money(amount, MoneyUnit.BTC);
        Money changeBackAmount = currrentBalance - receiverAmount - minerFee;

        // make sure you have enough money
        var totalSpendAmount = amount + minerFee.ToDecimal(MoneyUnit.BTC);
        if (totalSpendAmount > currrentBalance.ToDecimal(MoneyUnit.BTC))
            throw new Exception("The total spend amount is greater than you have now");

        // add outputs to tx
        Script receiverScriptPubKey;
        receiverScriptPubKey = new BitcoinPubKeyAddress(receiverAdress).ScriptPubKey;

        TxOut txOut = new TxOut()
        {
            Value = receiverAmount,
            ScriptPubKey = receiverScriptPubKey
        };
        TxOut changeBackTxOut = new TxOut()
        {
            Value = changeBackAmount,
            ScriptPubKey = secretPrivateKey.ScriptPubKey
        };

        transaction.Outputs.Add(txOut);
        transaction.Outputs.Add(changeBackTxOut);

        // add message
        if (!String.IsNullOrWhiteSpace(message))
        {
            var bytes = Encoding.UTF8.GetBytes(message);
            transaction.Outputs.Add(new TxOut()
            {
                Value = Money.Zero,
                ScriptPubKey = TxNullDataTemplate.Instance.GenerateScriptPubKey(bytes)
            });
        }

        // Sign the transaction
        transaction.Sign(secretPrivateKey, false);

        // Broadcast your transaction to all miners
        BroadcastResponse broadcastResponse = client.Broadcast(transaction).Result;
        if (!broadcastResponse.Success)
            throw new Exception(broadcastResponse.Error.Reason);
    }
NicolasDorier commented 6 years ago

So by default you are using public server, you don't need yours. If you want yours, yet the indexer need to be hosted on azure. The front end does not matter too much.

This is not well documented, but with the README.md and some .NET knowledge, it should not be difficult to host yourself (in azure).

QBitNinja does not expose fees, you will need to depends on another system to get it (bitcoinfees has a simple json API for it).

QBItNinja broadcast sometimes is a bit buggy or does not give you big details about why your transaction is rejected. You can try to print transaction.ToHex() then use https://www.smartbit.com.au/txs/pushtx to push it, it will give you better info.

Also, you should use NBitcoin TransactionBuilder for creating transactions, it catches most of error for you before even broadcasting. (this is documented in the book)

james1278 commented 6 years ago

Thanks Nicolas, But we are doing testing TestNet Transaction and after we checking here that time getting like this error "Nbitcoin + No coin matching was found"

see this Transaction id : 769532910c1a85b805a84c88fb6c29961f0e789aa78a4b98401cbca03c2a9f61 My ID : mpLym6uutZBqiKhiYnejfHkiMU2QYsfGPt Sending To This : mztqvWDhMzxYrJdBXuXGVEkpf5JwbGJkEY Transaction hex: 0100000001619f2a3ca0bc1c40984b8aa79a780e1f96296cfb884ca805b8851a0c91329576000000006b483045022100adbde78d20682984a83aa5c84b24bb9e981e3758563c0b2f6e57ed888a4ec10e02204bf2a521d1cb8de9089686d0f04ec0ac6f05d9d4b1a2140f04db0f97cfab3aae012102c53763fb012a51da97f08b55aa51cf14fada83edcdaa4f28bcac960a5678a1b0ffffffff0220aa4400000000001976a914d48e38ec4f64c30e6ed6487cac6528a25ecfa56d88ac107a0700000000001976a91460d6324d255521b63814975710eaca545be3db6488ac00000000

so please can you suggest me for same we can solve it.

james1278 commented 6 years ago

Smartbit works for one transaction to push the transaction. But when I did same from code using BroadcastResponse broadcastResponse = client.Broadcast(transaction).Result; if (!broadcastResponse.Success) throw new Exception(broadcastResponse.Error.Reason);

Then it was not push . so please can you tell me how can I automatically push from my computer? Please can you provide me the proper solution.

NicolasDorier commented 6 years ago

@james1278 can you try again now I restarted testnet indexer?