Skip to content

Commit

Permalink
Improve functionality of 'BtcdClient', add examples to 'ApiUsage'
Browse files Browse the repository at this point in the history
  • Loading branch information
priiduneemre committed Feb 26, 2015
1 parent cfb94c8 commit 13288a8
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 15 deletions.
8 changes: 5 additions & 3 deletions src/main/java/com/neemre/btcdcli4j/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/com/neemre/btcdcli4j/client/BtcdClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
25 changes: 20 additions & 5 deletions src/main/java/com/neemre/btcdcli4j/client/BtcdClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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());
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/temp/ApiUsage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"},
Expand All @@ -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();
Expand Down

0 comments on commit 13288a8

Please sign in to comment.