Skip to content

Commit

Permalink
Call core wallets service methods from CoreApi
Browse files Browse the repository at this point in the history
This change is a refactoring of the gRPC Wallets service
for the purpose of making CoreApi the entry point to
all core implementations.  These changes should have been
made in PR 4295.
See bisq-network#4295

The gRPC Wallet proto def name was changed to Wallets because
this service manages BSQ and BTC wallets, and GrpcWalletService
was changed to GrpcWalletsService for the same reason.

This PR should be reviewed/merged after PR 4308.
See bisq-network#4308

This PR's branch was created from the PR 4308 branch.
  • Loading branch information
ghubstan authored and eigentsmis committed Jun 26, 2020
1 parent c9f04d2 commit fd9d182
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 32 deletions.
18 changes: 9 additions & 9 deletions cli/src/main/java/bisq/cli/CliMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import bisq.proto.grpc.RemoveWalletPasswordRequest;
import bisq.proto.grpc.SetWalletPasswordRequest;
import bisq.proto.grpc.UnlockWalletRequest;
import bisq.proto.grpc.WalletGrpc;
import bisq.proto.grpc.WalletsGrpc;

import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;
Expand Down Expand Up @@ -139,7 +139,7 @@ public static void run(String[] args) {

var versionService = GetVersionGrpc.newBlockingStub(channel).withCallCredentials(credentials);
var paymentAccountsService = PaymentAccountsGrpc.newBlockingStub(channel).withCallCredentials(credentials);
var walletService = WalletGrpc.newBlockingStub(channel).withCallCredentials(credentials);
var walletsService = WalletsGrpc.newBlockingStub(channel).withCallCredentials(credentials);

try {
switch (method) {
Expand All @@ -151,7 +151,7 @@ public static void run(String[] args) {
}
case getbalance: {
var request = GetBalanceRequest.newBuilder().build();
var reply = walletService.getBalance(request);
var reply = walletsService.getBalance(request);
var satoshiBalance = reply.getBalance();
var satoshiDivisor = new BigDecimal(100000000);
var btcFormat = new DecimalFormat("###,##0.00000000");
Expand All @@ -166,13 +166,13 @@ public static void run(String[] args) {

var request = GetAddressBalanceRequest.newBuilder()
.setAddress(nonOptionArgs.get(1)).build();
var reply = walletService.getAddressBalance(request);
var reply = walletsService.getAddressBalance(request);
out.println(reply.getAddressBalanceInfo());
return;
}
case getfundingaddresses: {
var request = GetFundingAddressesRequest.newBuilder().build();
var reply = walletService.getFundingAddresses(request);
var reply = walletsService.getFundingAddresses(request);
out.println(reply.getFundingAddressesInfo());
return;
}
Expand Down Expand Up @@ -202,7 +202,7 @@ public static void run(String[] args) {
}
case lockwallet: {
var request = LockWalletRequest.newBuilder().build();
walletService.lockWallet(request);
walletsService.lockWallet(request);
out.println("wallet locked");
return;
}
Expand All @@ -222,7 +222,7 @@ public static void run(String[] args) {
var request = UnlockWalletRequest.newBuilder()
.setPassword(nonOptionArgs.get(1))
.setTimeout(timeout).build();
walletService.unlockWallet(request);
walletsService.unlockWallet(request);
out.println("wallet unlocked");
return;
}
Expand All @@ -231,7 +231,7 @@ public static void run(String[] args) {
throw new IllegalArgumentException("no password specified");

var request = RemoveWalletPasswordRequest.newBuilder().setPassword(nonOptionArgs.get(1)).build();
walletService.removeWalletPassword(request);
walletsService.removeWalletPassword(request);
out.println("wallet decrypted");
return;
}
Expand All @@ -243,7 +243,7 @@ public static void run(String[] args) {
var hasNewPassword = nonOptionArgs.size() == 3;
if (hasNewPassword)
requestBuilder.setNewPassword(nonOptionArgs.get(2));
walletService.setWalletPassword(requestBuilder.build());
walletsService.setWalletPassword(requestBuilder.build());
out.println("wallet encrypted" + (hasNewPassword ? " with new password" : ""));
return;
}
Expand Down
62 changes: 54 additions & 8 deletions core/src/main/java/bisq/core/grpc/CoreApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
@Slf4j
public class CoreApi {
private final CorePaymentAccountsService paymentAccountsService;
private final CoreWalletsService walletsService;
private final OfferBookService offerBookService;
private final TradeStatisticsManager tradeStatisticsManager;
private final CreateOfferService createOfferService;
Expand All @@ -56,12 +57,14 @@ public class CoreApi {

@Inject
public CoreApi(CorePaymentAccountsService paymentAccountsService,
CoreWalletsService walletsService,
OfferBookService offerBookService,
TradeStatisticsManager tradeStatisticsManager,
CreateOfferService createOfferService,
OpenOfferManager openOfferManager,
User user) {
this.paymentAccountsService = paymentAccountsService;
this.walletsService = walletsService;
this.offerBookService = offerBookService;
this.tradeStatisticsManager = tradeStatisticsManager;
this.createOfferService = createOfferService;
Expand All @@ -73,20 +76,52 @@ public String getVersion() {
return Version.VERSION;
}

public List<TradeStatistics2> getTradeStatistics() {
return new ArrayList<>(tradeStatisticsManager.getObservableTradeStatisticsSet());
///////////////////////////////////////////////////////////////////////////////////////////
// Wallets
///////////////////////////////////////////////////////////////////////////////////////////

public long getAvailableBalance() {
return walletsService.getAvailableBalance();
}

public List<Offer> getOffers() {
return offerBookService.getOffers();
public long getAddressBalance(String addressString) {
return walletsService.getAddressBalance(addressString);
}

public void createPaymentAccount(String accountName, String accountNumber, String fiatCurrencyCode) {
paymentAccountsService.createPaymentAccount(accountName, accountNumber, fiatCurrencyCode);
public String getAddressBalanceInfo(String addressString) {
return walletsService.getAddressBalanceInfo(addressString);
}

public Set<PaymentAccount> getPaymentAccounts() {
return paymentAccountsService.getPaymentAccounts();
public String getFundingAddresses() {
return walletsService.getFundingAddresses();
}

public void setWalletPassword(String password, String newPassword) {
walletsService.setWalletPassword(password, newPassword);
}

public void lockWallet() {
walletsService.lockWallet();
}

public void unlockWallet(String password, long timeout) {
walletsService.unlockWallet(password, timeout);
}

public void removeWalletPassword(String password) {
walletsService.removeWalletPassword(password);
}

public List<TradeStatistics2> getTradeStatistics() {
return new ArrayList<>(tradeStatisticsManager.getObservableTradeStatisticsSet());
}

public int getNumConfirmationsForMostRecentTransaction(String addressString) {
return walletsService.getNumConfirmationsForMostRecentTransaction(addressString);
}

public List<Offer> getOffers() {
return offerBookService.getOffers();
}

public void placeOffer(String currencyCode,
Expand Down Expand Up @@ -152,4 +187,15 @@ public void placeOffer(String offerId,
log::error);
}

///////////////////////////////////////////////////////////////////////////////////////////
// PaymentAccounts
///////////////////////////////////////////////////////////////////////////////////////////

public void createPaymentAccount(String accountName, String accountNumber, String fiatCurrencyCode) {
paymentAccountsService.createPaymentAccount(accountName, accountNumber, fiatCurrencyCode);
}

public Set<PaymentAccount> getPaymentAccounts() {
return paymentAccountsService.getPaymentAccounts();
}
}
2 changes: 1 addition & 1 deletion core/src/main/java/bisq/core/grpc/GrpcServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class GrpcServer {
public GrpcServer(Config config,
CoreApi coreApi,
GrpcPaymentAccountsService paymentAccountsService,
GrpcWalletService walletService) {
GrpcWalletsService walletService) {
this.coreApi = coreApi;
this.server = ServerBuilder.forPort(config.apiPort)
.addService(new GetVersionService())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@
import bisq.proto.grpc.SetWalletPasswordRequest;
import bisq.proto.grpc.UnlockWalletReply;
import bisq.proto.grpc.UnlockWalletRequest;
import bisq.proto.grpc.WalletGrpc;
import bisq.proto.grpc.WalletsGrpc;

import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.grpc.stub.StreamObserver;

import javax.inject.Inject;

class GrpcWalletService extends WalletGrpc.WalletImplBase {
class GrpcWalletsService extends WalletsGrpc.WalletsImplBase {

private final CoreWalletsService walletsService;
private final CoreApi coreApi;

@Inject
public GrpcWalletService(CoreWalletsService walletsService) {
this.walletsService = walletsService;
public GrpcWalletsService(CoreApi coreApi) {
this.coreApi = coreApi;
}

@Override
public void getBalance(GetBalanceRequest req, StreamObserver<GetBalanceReply> responseObserver) {
try {
long result = walletsService.getAvailableBalance();
long result = coreApi.getAvailableBalance();
var reply = GetBalanceReply.newBuilder().setBalance(result).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
Expand All @@ -49,7 +49,7 @@ public void getBalance(GetBalanceRequest req, StreamObserver<GetBalanceReply> re
public void getAddressBalance(GetAddressBalanceRequest req,
StreamObserver<GetAddressBalanceReply> responseObserver) {
try {
String result = walletsService.getAddressBalanceInfo(req.getAddress());
String result = coreApi.getAddressBalanceInfo(req.getAddress());
var reply = GetAddressBalanceReply.newBuilder().setAddressBalanceInfo(result).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
Expand All @@ -64,7 +64,7 @@ public void getAddressBalance(GetAddressBalanceRequest req,
public void getFundingAddresses(GetFundingAddressesRequest req,
StreamObserver<GetFundingAddressesReply> responseObserver) {
try {
String result = walletsService.getFundingAddresses();
String result = coreApi.getFundingAddresses();
var reply = GetFundingAddressesReply.newBuilder().setFundingAddressesInfo(result).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
Expand All @@ -79,7 +79,7 @@ public void getFundingAddresses(GetFundingAddressesRequest req,
public void setWalletPassword(SetWalletPasswordRequest req,
StreamObserver<SetWalletPasswordReply> responseObserver) {
try {
walletsService.setWalletPassword(req.getPassword(), req.getNewPassword());
coreApi.setWalletPassword(req.getPassword(), req.getNewPassword());
var reply = SetWalletPasswordReply.newBuilder().build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
Expand All @@ -94,7 +94,7 @@ public void setWalletPassword(SetWalletPasswordRequest req,
public void removeWalletPassword(RemoveWalletPasswordRequest req,
StreamObserver<RemoveWalletPasswordReply> responseObserver) {
try {
walletsService.removeWalletPassword(req.getPassword());
coreApi.removeWalletPassword(req.getPassword());
var reply = RemoveWalletPasswordReply.newBuilder().build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
Expand All @@ -109,7 +109,7 @@ public void removeWalletPassword(RemoveWalletPasswordRequest req,
public void lockWallet(LockWalletRequest req,
StreamObserver<LockWalletReply> responseObserver) {
try {
walletsService.lockWallet();
coreApi.lockWallet();
var reply = LockWalletReply.newBuilder().build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
Expand All @@ -124,7 +124,7 @@ public void lockWallet(LockWalletRequest req,
public void unlockWallet(UnlockWalletRequest req,
StreamObserver<UnlockWalletReply> responseObserver) {
try {
walletsService.unlockWallet(req.getPassword(), req.getTimeout());
coreApi.unlockWallet(req.getPassword(), req.getTimeout());
var reply = UnlockWalletReply.newBuilder().build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
Expand Down
4 changes: 2 additions & 2 deletions proto/src/main/proto/grpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ message PlaceOfferReply {
}

///////////////////////////////////////////////////////////////////////////////////////////
// Wallet
// Wallets
///////////////////////////////////////////////////////////////////////////////////////////

service Wallet {
service Wallets {
rpc GetBalance (GetBalanceRequest) returns (GetBalanceReply) {
}
rpc GetAddressBalance (GetAddressBalanceRequest) returns (GetAddressBalanceReply) {
Expand Down

0 comments on commit fd9d182

Please sign in to comment.