Bitcoinj send payment from a wallet

0 votes

I want to make a send payment from my wallet.

public static Transaction send(Wallet wallet,String destinationAddress,long satoshis, NetworkParameters parameters)
            throws Exception {
        Address dest = Address.fromBase58(parameters, destinationAddress);
        SendRequest request = SendRequest.to(dest, Coin.valueOf(satoshis));
        Wallet.SendResult result = wallet.sendCoins(request);
        Transaction endTransaction = result.broadcastComplete.get();
        return endTransaction;
    }

or tried to make

 SendRequest req;
        Transaction transaction = new Transaction(parameters);
        Coin coinToSpend = Coin.valueOf(600);
        //Address addressoSpend = new Address(parameters,"1PSq12YPRBCGwmb2cqqXaGpRrLfotsthPv");
        transaction.addOutput(coinToSpend,Address.fromBase58(parameters,"18MQPpjbB5UUwZBT7DALE6Q55pKCtfPCK3"));
        req = SendRequest.forTx(transaction);
        Wallet.SendResult sendResult = restoredWallet.sendCoins(req);

both of them return

Exception in thread "main" org.bitcoinj.core.InsufficientMoneyException: Insufficient money,  missing 0.0004729 BTC

How to properly send payment to another BTC address?

Sep 6, 2018 in Blockchain by digger
• 26,720 points
992 views

1 answer to this question.

0 votes

The problem actually was with the input and output. In new versions of bitcoinj you should set unput and output ot make transaction. Unfortuanetly, it was not updated on officail page. Here below is the answer for my question:

            Coin value = Coin.valueOf(680l);
            Address to = Address.fromBase58(parameters, addressTo);

            Transaction transaction = new Transaction(parameters);
            transaction.addInput(wallet.getUnspents().get(0));// important to add proper input
            transaction.addOutput(value, to);

            SendRequest request = SendRequest.forTx(transaction);
            request.feePerKb = Coin.valueOf(1000);

            Wallet.SendResult sendResult = wallet.sendCoins(peerGroup, request);
answered Sep 6, 2018 by slayer
• 29,310 points
why only getUnspents().get(0)?  why not sufficient number of unspent outputs as needed?

Hi, @Avlo,

Because it returns information of the unspent UTXO assets .

Related Questions In Blockchain

0 votes
1 answer

Is there a way to send funds from a specific bitcoin address in a wallet?

I'll answer my own question, It was unbelievably ...READ MORE

answered Sep 3, 2018 in Blockchain by Perry
• 17,100 points
625 views
0 votes
1 answer

How do I send ether from an EOA to a smart contract?

You include ether to send in the ...READ MORE

answered Sep 27, 2018 in Blockchain by digger
• 26,720 points
783 views
0 votes
0 answers
0 votes
0 answers
0 votes
1 answer

BitcoinJ generate address from private key

Convert string private key into bytes[]: ECKey key = ...READ MORE

answered Sep 4, 2018 in Blockchain by slayer
• 29,310 points
1,405 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
968 views
+1 vote
3 answers

Removing double quotes from a string from JSON response in PHP

Just remove the json_encode call, and it should work: $resp ...READ MORE

answered Sep 12, 2018 in Blockchain by digger
• 26,720 points
32,856 views
0 votes
1 answer

Is it possible to send eth to a contract through its constructor from another contract?

I found the solution for this: pragma solidity ...READ MORE

answered Sep 26, 2018 in Blockchain by slayer
• 29,310 points
922 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP