Bitcoinj

手續費及設定

  • November 6, 2018

我試圖估算最終金額確定後的費用。我會說我熟悉bitcoinj,但不完全熟悉java,所以我仍在學習如何編碼和探索如何獲取資訊。

這是我現在的程式碼。我把所有東西都放在一個方法裡。

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;
   }

目前它不處理費用。但是當我查看這樣的交易時 ==> <https://testnet.blockchain.info/tx/194b2a7505a537307abe9443b048bc7ddce6ca0d833d5758c3be3c03a9c7b838>你可以清楚地看到費用。

另外,當我知道錢包沒有足夠的錢時故意嘗試發送硬幣時,它並沒有告訴我它失敗了

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

我做錯了什麼沒有檢查?我是否必須實際執行 if 語句才能檢查?還有我怎樣才能可靠地計算最終金額是多少?

我能夠解決這個問題。

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";
       }
   }

圖書館不是這樣來的,所以我不得不即興創作解決方案。

引用自:https://bitcoin.stackexchange.com/questions/74679