javabitcoinj

How to create Replace-By-Fee TX with BitcoinJ


First was faced with endless pending Transaction in BitcoinJ FrameWork

The main documentation says that it can be made by Replace-By-Fee. So you need to take the old transaction and create a new one but based on previous.

Sounds good, but how correctly, using Bitcoinj framework create another one?

NetworkParameters params = MainNetParams.get();

WalletAppKit wallet = new WalletAppKit(params, new File("."), "_mywallet");
walletKit.startAsync();
walletKit.awaitRunning();

Wallet wallet = walletKit.wallet();

... There are one Pending

ArrayList<Transaction> pendingList = new ArrayList<>(wallet.getPendingTransactions());
Transaction nextTx = new Transaction(pendingList.get(0));
// → throws Exeption

Solution

  • Ok, so I solved it.

    When you have infinity pending transaction the first you need to do is to make sure that it marked as «replace-by-fee» you can do this by calling:

    ArrayList<Transaction> pendingList = new ArrayList<>(wallet.getPendingTransactions());
    
    pendingList.get(0).verify(); // Read console to see transaction info
    

    After that you need to use the code below to realize the replace-by-fee algorithm:

    Transaction transaction = pendingList.get(0);
    SendRequest request = SendRequest.forTx(transaction);
    request.feePerKb = Transaction.REFERENCE_DEFAULT_MIN_TX_FEE; // Or you can make other highter fee to spped it up
    
    wallet.completeTx(request);
    wallet.commitTx(request);
    

    In the output, you will see more TransactionOutputs in this Transaction details.

    Thats it