Skip to content

Commit

Permalink
Add rpc method 'getaddressbalance'
Browse files Browse the repository at this point in the history
This addresses task 2 in issue 4257
	bisq-network#4257

This new gRPC Wallet service method displays the balance and number
of confimirmations of the most recent transaction for the given BTC
wallet address.

The new method required the usual boilerplate changes to grpc.proto,
CliMain, and GrpcWalletService.

Two unit tests to check error msgs was added to cli/test.sh.
  • Loading branch information
ghubstan committed Jun 14, 2020
1 parent 2e415de commit b1228e5
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.
13 changes: 13 additions & 0 deletions cli/src/main/java/bisq/cli/CliMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package bisq.cli;

import bisq.proto.grpc.GetAddressBalanceRequest;
import bisq.proto.grpc.GetBalanceRequest;
import bisq.proto.grpc.GetFundingAddressesRequest;
import bisq.proto.grpc.GetVersionGrpc;
Expand Down Expand Up @@ -59,6 +60,7 @@ public class CliMain {
private enum Method {
getversion,
getbalance,
getaddressbalance,
getfundingaddresses,
lockwallet,
unlockwallet,
Expand Down Expand Up @@ -154,6 +156,16 @@ public static void run(String[] args) {
out.println(btcBalance);
return;
}
case getaddressbalance: {
if (nonOptionArgs.size() < 2)
throw new IllegalArgumentException("no address specified");

var request = GetAddressBalanceRequest.newBuilder()
.setAddress(nonOptionArgs.get(1)).build();
var reply = walletService.getAddressBalance(request);
out.println(reply.getAddressBalanceInfo());
return;
}
case getfundingaddresses: {
var request = GetFundingAddressesRequest.newBuilder().build();
var reply = walletService.getFundingAddresses(request);
Expand Down Expand Up @@ -230,6 +242,7 @@ private static void printHelp(OptionParser parser, PrintStream stream) {
stream.format("%-19s%-30s%s%n", "------", "------", "------------");
stream.format("%-19s%-30s%s%n", "getversion", "", "Get server version");
stream.format("%-19s%-30s%s%n", "getbalance", "", "Get server wallet balance");
stream.format("%-19s%-30s%s%n", "getaddressbalance", "", "Get server wallet address balance");
stream.format("%-19s%-30s%s%n", "getfundingaddresses", "", "Get BTC funding addresses");
stream.format("%-19s%-30s%s%n", "lockwallet", "", "Remove wallet password from memory, locking the wallet");
stream.format("%-19s%-30s%s%n", "unlockwallet", "password timeout",
Expand Down
14 changes: 14 additions & 0 deletions cli/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@
[ "$status" -eq 0 ]
}

@test "test getaddressbalance missing address argument" {
run ./bisq-cli --password=xyz getaddressbalance
[ "$status" -eq 1 ]
echo "actual output: $output" >&2
[ "$output" = "Error: no address specified" ]
}

@test "test getaddressbalance bogus address argument" {
run ./bisq-cli --password=xyz getaddressbalance bogus
[ "$status" -eq 1 ]
echo "actual output: $output" >&2
[ "$output" = "Error: address bogus not found in wallet" ]
}

@test "test help displayed on stderr if no options or arguments" {
run ./bisq-cli
[ "$status" -eq 1 ]
Expand Down
9 changes: 9 additions & 0 deletions core/src/main/java/bisq/core/grpc/CoreWalletsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ public long getAddressBalance(String addressString) {
return btcWalletService.getBalanceForAddress(address).value;
}

public String getAddressBalanceInfo(String addressString) {
var satoshiBalance = getAddressBalance(addressString);
var btcBalance = formatSatoshis.apply(satoshiBalance);
var numConfirmations = getNumConfirmationsForMostRecentTransaction(addressString);
return addressString
+ " balance: " + format("%13s", btcBalance)
+ ((numConfirmations > 0) ? (" confirmations: " + format("%6d", numConfirmations)) : "");
}

public String getFundingAddresses() {
if (!walletsManager.areWalletsAvailable())
throw new IllegalStateException("wallet is not yet available");
Expand Down
19 changes: 18 additions & 1 deletion core/src/main/java/bisq/core/grpc/GrpcWalletService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package bisq.core.grpc;

import bisq.proto.grpc.GetAddressBalanceReply;
import bisq.proto.grpc.GetAddressBalanceRequest;
import bisq.proto.grpc.GetBalanceReply;
import bisq.proto.grpc.GetBalanceRequest;
import bisq.proto.grpc.GetFundingAddressesReply;
Expand Down Expand Up @@ -42,7 +44,22 @@ public void getBalance(GetBalanceRequest req, StreamObserver<GetBalanceReply> re
throw ex;
}
}


@Override
public void getAddressBalance(GetAddressBalanceRequest req,
StreamObserver<GetAddressBalanceReply> responseObserver) {
try {
String result = walletsService.getAddressBalanceInfo(req.getAddress());
var reply = GetAddressBalanceReply.newBuilder().setAddressBalanceInfo(result).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
} catch (IllegalStateException cause) {
var ex = new StatusRuntimeException(Status.UNKNOWN.withDescription(cause.getMessage()));
responseObserver.onError(ex);
throw ex;
}
}

@Override
public void getFundingAddresses(GetFundingAddressesRequest req,
StreamObserver<GetFundingAddressesReply> responseObserver) {
Expand Down
10 changes: 10 additions & 0 deletions proto/src/main/proto/grpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ message PlaceOfferReply {
service Wallet {
rpc GetBalance (GetBalanceRequest) returns (GetBalanceReply) {
}
rpc GetAddressBalance (GetAddressBalanceRequest) returns (GetAddressBalanceReply) {
}
rpc GetFundingAddresses (GetFundingAddressesRequest) returns (GetFundingAddressesReply) {
}
rpc SetWalletPassword (SetWalletPasswordRequest) returns (SetWalletPasswordReply) {
Expand All @@ -138,6 +140,14 @@ message GetBalanceReply {
uint64 balance = 1;
}

message GetAddressBalanceRequest {
string address = 1;
}

message GetAddressBalanceReply {
string addressBalanceInfo = 1;
}

message GetFundingAddressesRequest {
}

Expand Down

0 comments on commit b1228e5

Please sign in to comment.