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?

  • bump Trying to bump this thread. – BTCDude May 5 '18 at 12:14
  • 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 Oct 6 '18 at 19:53
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.

  • Thanks for coming back to answer your own question! – Murch Nov 6 '18 at 17:58
  • 1
    Well 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 Nov 6 '18 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.