From 13288a8499771cbf969a265cf42c4329a7071cc1 Mon Sep 17 00:00:00 2001 From: Priidu Neemre <priidu.neemre@gmail.com> Date: Thu, 26 Feb 2015 02:21:38 +0200 Subject: [PATCH] Improve functionality of 'BtcdClient', add examples to 'ApiUsage' --- .../java/com/neemre/btcdcli4j/Commands.java | 8 +++--- .../neemre/btcdcli4j/client/BtcdClient.java | 14 +++++++---- .../btcdcli4j/client/BtcdClientImpl.java | 25 +++++++++++++++---- .../jsonrpc/client/JsonRpcClientImpl.java | 2 +- src/main/java/temp/ApiUsage.java | 16 +++++++++++- 5 files changed, 50 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/neemre/btcdcli4j/Commands.java b/src/main/java/com/neemre/btcdcli4j/Commands.java index 2b5d012..daba0b4 100644 --- a/src/main/java/com/neemre/btcdcli4j/Commands.java +++ b/src/main/java/com/neemre/btcdcli4j/Commands.java @@ -12,6 +12,7 @@ public enum Commands { ENCRYPT_WALLET("encryptwallet", 1, 1), GET_ACCOUNT("getaccount", 1, 1), GET_ACCOUNT_ADDRESS("getaccountaddress", 1, 1), + GET_ADDRESSES_BY_ACCOUNT("getaddressesbyaccount", 1, 1), GET_BALANCE("getbalance", 0, 3), GET_DIFFICULTY("getdifficulty", 0, 0), GET_GENERATE("getgenerate", 0, 0), @@ -24,15 +25,16 @@ public enum Commands { GET_RECEIVED_BY_ACCOUNT("getreceivedbyaccount", 1, 2), GET_RECEIVED_BY_ADDRESS("getreceivedbyaddress", 1, 2), LIST_ACCOUNTS("listaccounts", 0, 2), + SET_ACCOUNT("setaccount", 2, 2), SET_GENERATE("setgenerate", 1, 2), + SET_TX_FEE("settxfee", 1, 1), STOP("stop", 0, 0), WALLET_LOCK("walletlock", 0, 0), WALLET_PASSPHRASE("walletpassphrase", 2, 2), WALLET_PASSPHRASE_CHANGE("walletpassphrasechange", 2, 2), - GET_ADDRESSES_BY_ACCOUNT("getaddressesbyaccount", 1, 1), - SET_ACCOUNT("setaccount", 2, 2), - SET_TX_FEE("settxfee", 1, 1); + GET_WALLET_INFO("getwalletinfo", 0, 0); + @Getter diff --git a/src/main/java/com/neemre/btcdcli4j/client/BtcdClient.java b/src/main/java/com/neemre/btcdcli4j/client/BtcdClient.java index ebfa14d..f1a436f 100644 --- a/src/main/java/com/neemre/btcdcli4j/client/BtcdClient.java +++ b/src/main/java/com/neemre/btcdcli4j/client/BtcdClient.java @@ -45,27 +45,31 @@ public interface BtcdClient { BigDecimal getReceivedByAccount(String account); - BigDecimal getReceivedByAccount(String account, int confirmations); + BigDecimal getReceivedByAccount(String account, Integer confirmations); BigDecimal getReceivedByAddress(String address); - BigDecimal getReceivedByAddress(String address, int confirmations); + BigDecimal getReceivedByAddress(String address, Integer confirmations); Map<String, BigDecimal> listAccounts(); - Map<String, BigDecimal> listAccounts(int confirmations); + Map<String, BigDecimal> listAccounts(Integer confirmations); - Map<String, BigDecimal> listAccounts(int confirmations, boolean withWatchOnly); + Map<String, BigDecimal> listAccounts(Integer confirmations, Boolean withWatchOnly); + + String setAccount(String address, String account); void setGenerate(Boolean isGenerate); void setGenerate(Boolean isGenerate, Integer processors); + Boolean setTxFee(BigDecimal txFee); + String stop(); void walletLock(); - void walletPassphrase(String passphrase, int authTimeout); + void walletPassphrase(String passphrase, Integer authTimeout); void walletPassphraseChange(String curPassphrase, String newPassphrase); } \ No newline at end of file diff --git a/src/main/java/com/neemre/btcdcli4j/client/BtcdClientImpl.java b/src/main/java/com/neemre/btcdcli4j/client/BtcdClientImpl.java index e2bbcc1..82ef354 100644 --- a/src/main/java/com/neemre/btcdcli4j/client/BtcdClientImpl.java +++ b/src/main/java/com/neemre/btcdcli4j/client/BtcdClientImpl.java @@ -151,7 +151,7 @@ public BigDecimal getReceivedByAccount(String account) { } @Override - public BigDecimal getReceivedByAccount(String account, int confirmations) { + public BigDecimal getReceivedByAccount(String account, Integer confirmations) { List<Object> params = CollectionUtils.asList(account, confirmations); String totalReceivedJson = rpcClient.execute(Commands.GET_RECEIVED_BY_ACCOUNT.getName(), params); @@ -168,7 +168,7 @@ public BigDecimal getReceivedByAddress(String address) { } @Override - public BigDecimal getReceivedByAddress(String address, int confirmations) { + public BigDecimal getReceivedByAddress(String address, Integer confirmations) { List<Object> params = CollectionUtils.asList(address, confirmations); String totalReceivedJson = rpcClient.execute(Commands.GET_RECEIVED_BY_ADDRESS.getName(), params); @@ -186,7 +186,7 @@ public Map<String, BigDecimal> listAccounts() { } @Override - public Map<String, BigDecimal> listAccounts(int confirmations) { + public Map<String, BigDecimal> listAccounts(Integer confirmations) { String accountsJson = rpcClient.execute(Commands.LIST_ACCOUNTS.getName(), confirmations); Map<String, BigDecimal> accounts = rpcClient.getMapper().mapToMap(accountsJson, String.class, BigDecimal.class); @@ -195,7 +195,7 @@ public Map<String, BigDecimal> listAccounts(int confirmations) { } @Override - public Map<String, BigDecimal> listAccounts(int confirmations, boolean withWatchOnly) { + public Map<String, BigDecimal> listAccounts(Integer confirmations, Boolean withWatchOnly) { List<Object> params = CollectionUtils.asList(confirmations, withWatchOnly); String accountsJson = rpcClient.execute(Commands.LIST_ACCOUNTS.getName(), params); Map<String, BigDecimal> accounts = rpcClient.getMapper().mapToMap(accountsJson, @@ -204,6 +204,14 @@ public Map<String, BigDecimal> listAccounts(int confirmations, boolean withWatch return accounts; } + @Override + public String setAccount(String address, String account) { + List<Object> params = CollectionUtils.asList(address, account); + String nullMsgJson = rpcClient.execute(Commands.SET_ACCOUNT.getName(), params); + String nullMsg = rpcClient.getParser().parseString(nullMsgJson); + return nullMsg; + } + @Override public void setGenerate(Boolean isGenerate) { rpcClient.execute(Commands.SET_GENERATE.getName(), isGenerate); @@ -215,6 +223,13 @@ public void setGenerate(Boolean isGenerate, Integer processors) { rpcClient.execute(Commands.SET_GENERATE.getName(), params); } + @Override + public Boolean setTxFee(BigDecimal txFee) { + String resultJson = rpcClient.execute(Commands.SET_TX_FEE.getName(), txFee); + Boolean result = rpcClient.getParser().parseBoolean(resultJson); + return result; + } + @Override public String stop() { String noticeMsgJson = rpcClient.execute(Commands.STOP.getName()); @@ -228,7 +243,7 @@ public void walletLock() { } @Override - public void walletPassphrase(String passphrase, int authTimeout) { + public void walletPassphrase(String passphrase, Integer authTimeout) { List<Object> params = CollectionUtils.asList(passphrase, authTimeout); rpcClient.execute(Commands.WALLET_PASSPHRASE.getName(), params); } diff --git a/src/main/java/com/neemre/btcdcli4j/jsonrpc/client/JsonRpcClientImpl.java b/src/main/java/com/neemre/btcdcli4j/jsonrpc/client/JsonRpcClientImpl.java index a610089..cddaf8b 100644 --- a/src/main/java/com/neemre/btcdcli4j/jsonrpc/client/JsonRpcClientImpl.java +++ b/src/main/java/com/neemre/btcdcli4j/jsonrpc/client/JsonRpcClientImpl.java @@ -84,7 +84,7 @@ private String getNewUuid() { private <T> JsonRpcResponse validateResponse(JsonRpcRequest<T> request, JsonRpcResponse response) { if(!response.getId().equals(request.getId())) { - //throw new IllegalArgumentException("I am broken."); //TODO + throw new IllegalArgumentException("I am broken."); //TODO } return response; } diff --git a/src/main/java/temp/ApiUsage.java b/src/main/java/temp/ApiUsage.java index 00f699b..932b544 100644 --- a/src/main/java/temp/ApiUsage.java +++ b/src/main/java/temp/ApiUsage.java @@ -54,14 +54,16 @@ public static void main(String[] args) throws Exception { supportedCalls.listAccounts(); supportedCalls.listAccounts(6); supportedCalls.listAccounts(6, true); + supportedCalls.setAccount("1NRpYDf2GdAL4yLZEAww8uUSEGM7Df6KKc", "aardvark"); supportedCalls.setGenerate(false); supportedCalls.setGenerate(false, 7); + supportedCalls.setTxFee(new BigDecimal("0.00004900")); //supportedCalls.stop(); supportedCalls.walletLock(); supportedCalls.walletPassphrase("strawberry", Defaults.WALLET_AUTH_TIMEOUT); supportedCalls.walletPassphraseChange("strawberry", "raspberry"); } - + static class ApiCalls { private BtcdClient btcdClient; @@ -200,6 +202,12 @@ private void listAccounts(int confirmations, boolean withWatchOnly) { "withWatchOnly"}, new Object[]{confirmations, withWatchOnly}, accounts); } + public void setAccount(String address, String account) { + String nullMsg = btcdClient.setAccount(address, account); + printResult(Commands.SET_ACCOUNT.getName(), new String[]{"address", "account"}, + new Object[]{address, account}, nullMsg); + } + private void setGenerate(boolean isGenerate) { btcdClient.setGenerate(isGenerate); printResult(Commands.SET_GENERATE.getName(), new String[]{"isGenerate"}, @@ -211,6 +219,12 @@ private void setGenerate(boolean isGenerate, int processors) { printResult(Commands.SET_GENERATE.getName(), new String[]{"isGenerate", "processors"}, new Object[]{isGenerate, processors}, null); } + + private void setTxFee(BigDecimal txFee) { + Boolean result = btcdClient.setTxFee(txFee); + printResult(Commands.SET_TX_FEE.getName(), new String[]{"txFee"}, new Object[]{txFee}, + result); + } private void stop() { String noticeMsg = btcdClient.stop();