Handling fees and setting them

2

Im trying to get the estimation of how much the fees will be when a final amount is decided on. I would say im getting familiar with bitcoinj but not completely with java so im still learning how to code and exploring how to get information.

this is my present code. I put everything inside a method.

public static String SendFund(String amount, String receiver){
        String text;
        Coin value = Coin.parseCoin(amount);
        LegacyAddress to = LegacyAddress.fromBase58(MainPage.params, receiver);
        try {
            System.out.println(gbal()+" - "+gaddress());
            Wallet.SendResult result = kit.wallet().sendCoins(kit.peerGroup(), to, value);
            text = "<html>"+amount+" BTC sent to address <br/><br/>"+receiver+".</html>";
            System.out.println("Check transaction : https://testnet.blockchain.info/tx/"+result.tx.getHashAsString());
        }catch(InsufficientMoneyException e){
            text = "Insufficient Funds.";
        }
        return text;
    }

Currently it doesnt deal with fees. But when i view a transaction such as this ==> https://testnet.blockchain.info/tx/194b2a7505a537307abe9443b048bc7ddce6ca0d833d5758c3be3c03a9c7b838 you can clearly see the fee.

Also when i purposely tried to send coins when i know the wallet doesnt have enough it doesnt tell me that it failed

0.8625106 - n2uEm2QXqe2PCSsbHTdAJN9aDm2mDjz6iZ
May 02, 2018 5:08:58 PM org.bitcoinj.wallet.Wallet completeTx
INFO: Completing send tx with 1 outputs totalling 1.00 BTC and a fee of 0.001 BTC/kB

What am i doing wrong for failing to check? do i have to actually perform an if statement just to check? And also how can i calculate reliably on what the final amount would be?

BTCDude

Posted 2018-05-03T12:12:47.613

Reputation: 41

bump Trying to bump this thread.BTCDude 2018-05-05T12:14:26.157

Perhaps you should gather the details of the code libraries you are using, and try the question on stackoverflow proper, since this might be more a programming question than a bitcoin question.qxotk 2018-10-06T19:53:09.617

Answers

2

I was able to resolve the issue.

public static String estFee(String amount, String receiver, WalletAppKit kit, NetworkParameters params){
        try {
            String value2 = String.format("%.8f",Double.parseDouble(amount) * MainPage.handleFee);
            //String value1 = String.format("%.8f",Double.parseDouble(amount) - Double.parseDouble(value2));
            String value1 = String.format("%.8f",Double.parseDouble(amount));
            Coin val1 = Coin.parseCoin(value1);
            //Coin val2 = Coin.parseCoin(value2);
            System.out.println(receiver);
            Address target1 = Address.fromBase58(params, receiver);
            //Address target2 = Address.fromBase58(params, MainPage.handleAddress);
            Transaction tx = new Transaction(params);
            tx.addOutput(val1, target1);
            //tx.addOutput(val2, target2);
            SendRequest req = SendRequest.forTx(tx);
            req.feePerKb = Coin.parseCoin("0.0001");
            kit.wallet().completeTx(req);
            String[] temp = req.tx.getFee().toFriendlyString().split(" ");
            return String.format("%.8f",Double.parseDouble(temp[0]));
        }catch(InsufficientMoneyException e){
            Double mis = (double)e.missing.getValue()/100000000;
            System.out.println(BackEnd.strFormat("ERROR","NSF","INFO","Not enough money. You are missing "+mis+". This was to retrieve an estimated fee."));
            return "0";
        }
    }

The library wasnt so forth coming so i had to improvise for the solution.

BTCDude

Posted 2018-05-03T12:12:47.613

Reputation: 41

Thanks for coming back to answer your own question!Murch 2018-11-06T17:58:13.607

1Well i try to close up my questions for other people.

The library was initially difficult to read but after a few weeks ive gotten a better understanding on how things work in the back. IT also helped me understand data types. – BTCDude 2018-11-06T18:53:05.980