Skip to content

Commit

Permalink
[resolves #50] Replace the use of double with BigDecimal
Browse files Browse the repository at this point in the history
  • Loading branch information
tdiesler committed Jul 24, 2018
1 parent 9dc4ce4 commit 97723d7
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 261 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,10 @@ public String createRawTransaction(List<TxInput> inputs, List<TxOutput> outputs)
});
}

Map<String, Double> pOutputs = new LinkedHashMap();

Double oldValue;
Map<String, BigDecimal> pOutputs = new LinkedHashMap();

for (TxOutput txOutput : outputs) {
if ((oldValue = pOutputs.put(txOutput.address(), txOutput.amount())) != null)
pOutputs.put(txOutput.address(), BitcoinUtil.normalizeAmount(oldValue + txOutput.amount()));
// throw new BitcoinRpcException("Duplicate output");
pOutputs.put(txOutput.address(), txOutput.amount());
}

return (String) query("createrawtransaction", pInputs, pOutputs);
Expand All @@ -291,18 +288,18 @@ public List<String> getAddressesByAccount(String account) throws GenericRpcExcep
}

@Override
public double getBalance() throws GenericRpcException {
return ((Number) query("getbalance")).doubleValue();
public BigDecimal getBalance() throws GenericRpcException {
return (BigDecimal) query("getbalance");
}

@Override
public double getBalance(String account) throws GenericRpcException {
return ((Number) query("getbalance", account)).doubleValue();
public BigDecimal getBalance(String account) throws GenericRpcException {
return (BigDecimal) query("getbalance", account);
}

@Override
public double getBalance(String account, int minConf) throws GenericRpcException {
return ((Number) query("getbalance", account, minConf)).doubleValue();
public BigDecimal getBalance(String account, int minConf) throws GenericRpcException {
return (BigDecimal) query("getbalance", account, minConf);
}

@Override
Expand All @@ -317,8 +314,8 @@ public InfoWrapper(Map m) {
}

@Override
public double balance() {
return mapDouble("balance");
public BigDecimal balance() {
return mapBigDecimal("balance");
}

@Override
Expand All @@ -332,8 +329,8 @@ public int connections() {
}

@Override
public double difficulty() {
return mapDouble("difficulty");
public BigDecimal difficulty() {
return mapBigDecimal("difficulty");
}

@Override
Expand All @@ -352,8 +349,8 @@ public long keyPoolSize() {
}

@Override
public double payTxFee() {
return mapDouble("paytxfee");
public BigDecimal payTxFee() {
return mapBigDecimal("paytxfee");
}

@Override
Expand All @@ -367,8 +364,8 @@ public String proxy() {
}

@Override
public double relayFee() {
return mapDouble("relayfee");
public BigDecimal relayFee() {
return mapBigDecimal("relayfee");
}

@Override
Expand Down Expand Up @@ -678,13 +675,13 @@ public String category() {
}

@Override
public double amount() {
return mapDouble(m, "amount");
public BigDecimal amount() {
return mapBigDecimal(m, "amount");
}

@Override
public double fee() {
return mapDouble(m, "fee");
public BigDecimal fee() {
return mapBigDecimal(m, "fee");
}

@Override
Expand Down Expand Up @@ -837,8 +834,8 @@ public int currentBlockTx() {
}

@Override
public double difficulty() {
return mapDouble("difficulty");
public BigDecimal difficulty() {
return mapBigDecimal("difficulty");
}

@Override
Expand All @@ -847,8 +844,8 @@ public String errors() {
}

@Override
public double networkHashps() {
return Double.valueOf(mapStr("networkhashps"));
public BigDecimal networkHashps() {
return mapBigDecimal("networkhashps");
}

@Override
Expand Down Expand Up @@ -889,13 +886,13 @@ public String bestBlockHash() {
}

@Override
public double difficulty() {
return mapDouble("difficulty");
public BigDecimal difficulty() {
return mapBigDecimal("difficulty");
}

@Override
public double verificationProgress() {
return mapDouble("verificationprogress");
public BigDecimal verificationProgress() {
return mapBigDecimal("verificationprogress");
}

@Override
Expand All @@ -911,8 +908,8 @@ public SmartFeeResultMapWrapper(Map m) {
}

@Override
public double feeRate() {
return mapDouble("feerate");
public BigDecimal feeRate() {
return mapBigDecimal("feerate");
}

@Override
Expand Down Expand Up @@ -984,8 +981,8 @@ public String bits() {
}

@Override
public double difficulty() {
return mapDouble("difficulty");
public BigDecimal difficulty() {
return mapBigDecimal("difficulty");
}

@Override
Expand Down Expand Up @@ -1232,8 +1229,8 @@ public OutImpl(Map m) {
}

@Override
public double value() {
return mapDouble("value");
public BigDecimal value() {
return mapBigDecimal("value");
}

@Override
Expand Down Expand Up @@ -1444,13 +1441,13 @@ public RawTransaction getRawTransaction(String txId) throws GenericRpcException
}

@Override
public double getReceivedByAddress(String address) throws GenericRpcException {
return ((Number) query("getreceivedbyaddress", address)).doubleValue();
public BigDecimal getReceivedByAddress(String address) throws GenericRpcException {
return (BigDecimal) query("getreceivedbyaddress", address);
}

@Override
public double getReceivedByAddress(String address, int minConf) throws GenericRpcException {
return ((Number) query("getreceivedbyaddress", address, minConf)).doubleValue();
public BigDecimal getReceivedByAddress(String address, int minConf) throws GenericRpcException {
return (BigDecimal) query("getreceivedbyaddress", address, minConf);
}

@Override
Expand Down Expand Up @@ -1508,8 +1505,8 @@ public String account() {
}

@Override
public double amount() {
return ((Number) e.get("amount")).doubleValue();
public BigDecimal amount() {
return (BigDecimal) e.get("amount");
}

@Override
Expand Down Expand Up @@ -1661,8 +1658,8 @@ public String account() {
}

@Override
public double amount() {
return MapWrapper.mapDouble(m, "amount");
public BigDecimal amount() {
return MapWrapper.mapBigDecimal(m, "amount");
}

@Override
Expand Down Expand Up @@ -1704,42 +1701,42 @@ public boolean lockUnspent(boolean unlock, String txid, int vout) throws Generic
}

@Override
public boolean move(String fromAccount, String toAddress, double amount) throws GenericRpcException {
public boolean move(String fromAccount, String toAddress, BigDecimal amount) throws GenericRpcException {
return (boolean) query("move", fromAccount, toAddress, amount);
}

@Override
public boolean move(String fromAccount, String toAddress, double amount, String comment) throws GenericRpcException {
public boolean move(String fromAccount, String toAddress, BigDecimal amount, String comment) throws GenericRpcException {
return (boolean) query("move", fromAccount, toAddress, amount, 0, comment);
}

@Override
public boolean move(String fromAccount, String toAddress, double amount, int minConf) throws GenericRpcException {
public boolean move(String fromAccount, String toAddress, BigDecimal amount, int minConf) throws GenericRpcException {
return (boolean) query("move", fromAccount, toAddress, amount, minConf);
}

@Override
public boolean move(String fromAccount, String toAddress, double amount, int minConf, String comment) throws GenericRpcException {
public boolean move(String fromAccount, String toAddress, BigDecimal amount, int minConf, String comment) throws GenericRpcException {
return (boolean) query("move", fromAccount, toAddress, amount, minConf, comment);
}

@Override
public String sendFrom(String fromAccount, String toAddress, double amount) throws GenericRpcException {
public String sendFrom(String fromAccount, String toAddress, BigDecimal amount) throws GenericRpcException {
return (String) query("sendfrom", fromAccount, toAddress, amount);
}

@Override
public String sendFrom(String fromAccount, String toAddress, double amount, int minConf) throws GenericRpcException {
public String sendFrom(String fromAccount, String toAddress, BigDecimal amount, int minConf) throws GenericRpcException {
return (String) query("sendfrom", fromAccount, toAddress, amount, minConf);
}

@Override
public String sendFrom(String fromAccount, String toAddress, double amount, int minConf, String comment) throws GenericRpcException {
public String sendFrom(String fromAccount, String toAddress, BigDecimal amount, int minConf, String comment) throws GenericRpcException {
return (String) query("sendfrom", fromAccount, toAddress, amount, minConf, comment);
}

@Override
public String sendFrom(String fromAccount, String toAddress, double amount, int minConf, String comment, String commentTo) throws GenericRpcException {
public String sendFrom(String fromAccount, String toAddress, BigDecimal amount, int minConf, String comment, String commentTo) throws GenericRpcException {
return (String) query("sendfrom", fromAccount, toAddress, amount, minConf, comment, commentTo);
}

Expand All @@ -1749,17 +1746,17 @@ public String sendRawTransaction(String hex) throws GenericRpcException {
}

@Override
public String sendToAddress(String toAddress, double amount) throws GenericRpcException {
public String sendToAddress(String toAddress, BigDecimal amount) throws GenericRpcException {
return (String) query("sendtoaddress", toAddress, amount);
}

@Override
public String sendToAddress(String toAddress, double amount, String comment) throws GenericRpcException {
public String sendToAddress(String toAddress, BigDecimal amount, String comment) throws GenericRpcException {
return (String) query("sendtoaddress", toAddress, amount, comment);
}

@Override
public String sendToAddress(String toAddress, double amount, String comment, String commentTo) throws GenericRpcException {
public String sendToAddress(String toAddress, BigDecimal amount, String comment, String commentTo) throws GenericRpcException {
return (String) query("sendtoaddress", toAddress, amount, comment, commentTo);
}

Expand Down Expand Up @@ -1907,13 +1904,13 @@ public List<String> generateToAddress(int numBlocks, String address) throws Bitc
//// System.out.println(b.listReceivedByAddress());
// }
@Override
public double getEstimateFee(int nBlocks) throws GenericRpcException {
return ((Number) query("estimatefee", nBlocks)).doubleValue();
public BigDecimal getEstimateFee(int nBlocks) throws GenericRpcException {
return (BigDecimal) query("estimatefee", nBlocks);
}

@Override
public double getEstimatePriority(int nBlocks) throws GenericRpcException {
return ((Number) query("estimatepriority", nBlocks)).doubleValue();
public BigDecimal getEstimatePriority(int nBlocks) throws GenericRpcException {
return (BigDecimal) query("estimatepriority", nBlocks);
}

@Override
Expand Down Expand Up @@ -1984,8 +1981,8 @@ public int getTimeOffset() {
}

@Override
public double getPingTime() {
return mapDouble("pingtime");
public BigDecimal getPingTime() {
return mapBigDecimal("pingtime");
}

@Override
Expand Down Expand Up @@ -2067,17 +2064,13 @@ public long getConnectionCount() throws GenericRpcException {
}

@Override
public double getUnconfirmedBalance() throws GenericRpcException {
return (double) query("getunconfirmedbalance");
public BigDecimal getUnconfirmedBalance() throws GenericRpcException {
return (BigDecimal) query("getunconfirmedbalance");
}

@Override
public double getDifficulty() throws GenericRpcException {
if (query("getdifficulty") instanceof Long) {
return ((Long) query("getdifficulty")).doubleValue();
} else {
return (double) query("getdifficulty");
}
public BigDecimal getDifficulty() throws GenericRpcException {
return (BigDecimal) query("getdifficulty");
}

@Override
Expand All @@ -2102,8 +2095,8 @@ public boolean getGenerate() throws BitcoinRPCException {
}

@Override
public double getNetworkHashPs() throws GenericRpcException {
return (Double) query("getnetworkhashps");
public BigDecimal getNetworkHashPs() throws GenericRpcException {
return (BigDecimal) query("getnetworkhashps");
}

@Override
Expand Down
Loading

0 comments on commit 97723d7

Please sign in to comment.