forked from bisq-network/bisq
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This addresses task 4 in issue 4257. bisq-network#4257 This PR should be reviewed/merged after PR 4304. bisq-network#4304 This new gRPC Wallet service method creates a dummy PerfectMoney payment account for the given name, number and fiat currency code, as part of the required "simplest possible trading API" (for demo). An implementation supporting all payment methods is not in the scope. Changes specific to the new rpc method implementation follow: * New createpaymentacct method + help text was added to CliMain. Help text formatting was also changed to make room for larger method names and argument lists. * The PaymentAccount proto service def was renamed PaymentAccounts to avoid a name collision, and the new rpc CreatePaymentAccount was made part of the newly named PaymentAccounts service def. * New GrpcPaymentAccountsService (gRPC boilerplate) and CorePaymentAccountsService (method implementations) classes were added. * The gRPC GetPaymentAccountsService stub was moved from GrpcServer to the new GrpcPaymentAccountsService class, and GrpcPaymentAccountsService is injected into GrpcServer. * A new createpaymentacct unit test was added to the bats test suite (checks for successful return status code). Maybe bit out of scope, some small changes were made towards making sure the entire API is defined in CoreApi, which is used as a pass-through object to the new CorePaymentAccountsService. In the next PR, similar refactoring will be done to make CoreApi the pass-through object for all of the existing CoreWalletsService methods. (CoreWalletsService will be injected into CoreApi.) In the future, all Grpc*Service implementations will call core services through CoreApi, for the sake of consistency.
- Loading branch information
Showing
7 changed files
with
173 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
core/src/main/java/bisq/core/grpc/CorePaymentAccountsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package bisq.core.grpc; | ||
|
||
import bisq.core.account.witness.AccountAgeWitnessService; | ||
import bisq.core.locale.FiatCurrency; | ||
import bisq.core.payment.PaymentAccount; | ||
import bisq.core.payment.PaymentAccountFactory; | ||
import bisq.core.payment.PerfectMoneyAccount; | ||
import bisq.core.payment.payload.PaymentMethod; | ||
import bisq.core.user.User; | ||
|
||
import bisq.common.config.Config; | ||
|
||
import javax.inject.Inject; | ||
|
||
import java.util.Set; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class CorePaymentAccountsService { | ||
|
||
private final Config config; | ||
private final AccountAgeWitnessService accountAgeWitnessService; | ||
private final User user; | ||
|
||
@Inject | ||
public CorePaymentAccountsService(Config config, | ||
AccountAgeWitnessService accountAgeWitnessService, | ||
User user) { | ||
this.config = config; | ||
this.accountAgeWitnessService = accountAgeWitnessService; | ||
this.user = user; | ||
} | ||
|
||
public void createPaymentAccount(String accountName, String accountNumber, String fiatCurrencyCode) { | ||
// Create and persist a PerfectMoney dummy payment account. There is no guard | ||
// against creating accounts with duplicate names & numbers, only the uuid and | ||
// creation date are unique. | ||
PaymentMethod dummyPaymentMethod = PaymentMethod.getDummyPaymentMethod(PaymentMethod.PERFECT_MONEY_ID); | ||
PaymentAccount paymentAccount = PaymentAccountFactory.getPaymentAccount(dummyPaymentMethod); | ||
paymentAccount.init(); | ||
paymentAccount.setAccountName(accountName); | ||
((PerfectMoneyAccount) paymentAccount).setAccountNr(accountNumber); | ||
paymentAccount.setSingleTradeCurrency(new FiatCurrency(fiatCurrencyCode)); | ||
user.addPaymentAccount(paymentAccount); | ||
|
||
// Don't do this on mainnet until thoroughly tested. | ||
if (config.baseCurrencyNetwork.isRegtest()) | ||
accountAgeWitnessService.publishMyAccountAgeWitness(paymentAccount.getPaymentAccountPayload()); | ||
|
||
log.info("Payment account {} saved", paymentAccount.getId()); | ||
} | ||
|
||
public Set<PaymentAccount> getPaymentAccounts() { | ||
return user.getPaymentAccounts(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
core/src/main/java/bisq/core/grpc/GrpcPaymentAccountsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package bisq.core.grpc; | ||
|
||
import bisq.core.payment.PaymentAccount; | ||
|
||
import bisq.proto.grpc.CreatePaymentAccountReply; | ||
import bisq.proto.grpc.CreatePaymentAccountRequest; | ||
import bisq.proto.grpc.GetPaymentAccountsReply; | ||
import bisq.proto.grpc.GetPaymentAccountsRequest; | ||
import bisq.proto.grpc.PaymentAccountsGrpc; | ||
|
||
import io.grpc.stub.StreamObserver; | ||
|
||
import javax.inject.Inject; | ||
|
||
import java.util.stream.Collectors; | ||
|
||
|
||
public class GrpcPaymentAccountsService extends PaymentAccountsGrpc.PaymentAccountsImplBase { | ||
|
||
private final CoreApi coreApi; | ||
|
||
@Inject | ||
public GrpcPaymentAccountsService(CoreApi coreApi) { | ||
this.coreApi = coreApi; | ||
} | ||
|
||
@Override | ||
public void createPaymentAccount(CreatePaymentAccountRequest req, | ||
StreamObserver<CreatePaymentAccountReply> responseObserver) { | ||
coreApi.createPaymentAccount(req.getAccountName(), req.getAccountNumber(), req.getFiatCurrencyCode()); | ||
var reply = CreatePaymentAccountReply.newBuilder().build(); | ||
responseObserver.onNext(reply); | ||
responseObserver.onCompleted(); | ||
} | ||
|
||
@Override | ||
public void getPaymentAccounts(GetPaymentAccountsRequest req, | ||
StreamObserver<GetPaymentAccountsReply> responseObserver) { | ||
var tradeStatistics = coreApi.getPaymentAccounts().stream() | ||
.map(PaymentAccount::toProtoMessage) | ||
.collect(Collectors.toList()); | ||
var reply = GetPaymentAccountsReply.newBuilder().addAllPaymentAccounts(tradeStatistics).build(); | ||
responseObserver.onNext(reply); | ||
responseObserver.onCompleted(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters