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 6, 2018
1 parent 7bbed09 commit e42f503
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 @@ -250,13 +250,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 @@ -283,18 +280,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 @@ -309,8 +306,8 @@ public InfoWrapper(Map m) {
}

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

@Override
Expand All @@ -324,8 +321,8 @@ public int connections() {
}

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

@Override
Expand All @@ -344,8 +341,8 @@ public long keyPoolSize() {
}

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

@Override
Expand All @@ -359,8 +356,8 @@ public String proxy() {
}

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

@Override
Expand Down Expand Up @@ -670,13 +667,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 @@ -829,8 +826,8 @@ public int currentBlockTx() {
}

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

@Override
Expand All @@ -839,8 +836,8 @@ public String errors() {
}

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

@Override
Expand Down Expand Up @@ -881,13 +878,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 @@ -903,8 +900,8 @@ public SmartFeeResultMapWrapper(Map m) {
}

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

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

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

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

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

@Override
Expand Down Expand Up @@ -1435,13 +1432,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 @@ -1499,8 +1496,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 @@ -1652,8 +1649,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 @@ -1688,42 +1685,42 @@ public List<Unspent> listUnspent(int minConf, int maxConf, String... addresses)
}

@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 @@ -1733,17 +1730,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 @@ -1891,13 +1888,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 @@ -1968,8 +1965,8 @@ public int getTimeOffset() {
}

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

@Override
Expand Down Expand Up @@ -2051,17 +2048,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 @@ -2086,8 +2079,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 e42f503

Please sign in to comment.