diff --git a/core/src/main/java/bisq/core/api/CoreApi.java b/core/src/main/java/bisq/core/api/CoreApi.java index 01f70d312f4..05886dd6e9e 100644 --- a/core/src/main/java/bisq/core/api/CoreApi.java +++ b/core/src/main/java/bisq/core/api/CoreApi.java @@ -193,6 +193,10 @@ public Trade getTrade(String tradeId) { return coreTradesService.getTrade(tradeId); } + public String getTradeRole(String tradeId) { + return coreTradesService.getTradeRole(tradeId); + } + /////////////////////////////////////////////////////////////////////////////////////////// // Wallets /////////////////////////////////////////////////////////////////////////////////////////// diff --git a/core/src/main/java/bisq/core/api/CoreTradesService.java b/core/src/main/java/bisq/core/api/CoreTradesService.java index d0200a78ce5..85877488315 100644 --- a/core/src/main/java/bisq/core/api/CoreTradesService.java +++ b/core/src/main/java/bisq/core/api/CoreTradesService.java @@ -21,6 +21,7 @@ import bisq.core.offer.takeoffer.TakeOfferModel; import bisq.core.trade.Trade; import bisq.core.trade.TradeManager; +import bisq.core.trade.TradeUtil; import bisq.core.trade.protocol.BuyerProtocol; import bisq.core.trade.protocol.SellerProtocol; import bisq.core.user.User; @@ -38,14 +39,17 @@ class CoreTradesService { private final TakeOfferModel takeOfferModel; private final TradeManager tradeManager; + private final TradeUtil tradeUtil; private final User user; @Inject public CoreTradesService(TakeOfferModel takeOfferModel, TradeManager tradeManager, + TradeUtil tradeUtil, User user) { this.takeOfferModel = takeOfferModel; this.tradeManager = tradeManager; + this.tradeUtil = tradeUtil; this.user = user; } @@ -57,6 +61,7 @@ void takeOffer(Offer offer, throw new IllegalArgumentException(format("payment account with id '%s' not found", paymentAccountId)); var useSavingsWallet = true; + //noinspection ConstantConditions takeOfferModel.initModel(offer, paymentAccount, useSavingsWallet); log.info("Initiating take {} offer, {}", offer.isBuyOffer() ? "buy" : "sell", @@ -111,6 +116,10 @@ void confirmPaymentReceived(String tradeId) { } } + String getTradeRole(String tradeId) { + return tradeUtil.getRole(getTrade(tradeId)); + } + Trade getTrade(String tradeId) { return tradeManager.getTradeById(tradeId).orElseThrow(() -> new IllegalArgumentException(format("trade with id '%s' not found", tradeId))); diff --git a/core/src/main/java/bisq/core/api/model/TradeInfo.java b/core/src/main/java/bisq/core/api/model/TradeInfo.java index 6e4adeca4ae..1a717a7672e 100644 --- a/core/src/main/java/bisq/core/api/model/TradeInfo.java +++ b/core/src/main/java/bisq/core/api/model/TradeInfo.java @@ -40,6 +40,7 @@ public class TradeInfo implements Payload { private final String tradeId; private final String shortId; private final long date; + private final String role; private final boolean isCurrencyForTakerFeeBtc; private final long txFeeAsLong; private final long takerFeeAsLong; @@ -65,6 +66,7 @@ public TradeInfo(TradeInfoBuilder builder) { this.tradeId = builder.tradeId; this.shortId = builder.shortId; this.date = builder.date; + this.role = builder.role; this.isCurrencyForTakerFeeBtc = builder.isCurrencyForTakerFeeBtc; this.txFeeAsLong = builder.txFeeAsLong; this.takerFeeAsLong = builder.takerFeeAsLong; @@ -87,11 +89,16 @@ public TradeInfo(TradeInfoBuilder builder) { } public static TradeInfo toTradeInfo(Trade trade) { + return toTradeInfo(trade, null); + } + + public static TradeInfo toTradeInfo(Trade trade, String role) { return new TradeInfo.TradeInfoBuilder() .withOffer(toOfferInfo(trade.getOffer())) .withTradeId(trade.getId()) .withShortId(trade.getShortId()) .withDate(trade.getDate().getTime()) + .withRole(role == null ? "" : role) .withIsCurrencyForTakerFeeBtc(trade.isCurrencyForTakerFeeBtc()) .withTxFeeAsLong(trade.getTxFeeAsLong()) .withTakerFeeAsLong(trade.getTakerFeeAsLong()) @@ -127,6 +134,7 @@ public bisq.proto.grpc.TradeInfo toProtoMessage() { .setTradeId(tradeId) .setShortId(shortId) .setDate(date) + .setRole(role) .setIsCurrencyForTakerFeeBtc(isCurrencyForTakerFeeBtc) .setTxFeeAsLong(txFeeAsLong) .setTakerFeeAsLong(takerFeeAsLong) @@ -165,6 +173,7 @@ public static class TradeInfoBuilder { private String tradeId; private String shortId; private long date; + private String role; private boolean isCurrencyForTakerFeeBtc; private long txFeeAsLong; private long takerFeeAsLong; @@ -205,6 +214,11 @@ public TradeInfoBuilder withDate(long date) { return this; } + public TradeInfoBuilder withRole(String role) { + this.role = role; + return this; + } + public TradeInfoBuilder withIsCurrencyForTakerFeeBtc(boolean isCurrencyForTakerFeeBtc) { this.isCurrencyForTakerFeeBtc = isCurrencyForTakerFeeBtc; return this; @@ -311,6 +325,7 @@ public String toString() { " tradeId='" + tradeId + '\'' + "\n" + ", shortId='" + shortId + '\'' + "\n" + ", date='" + date + '\'' + "\n" + + ", role='" + role + '\'' + "\n" + ", isCurrencyForTakerFeeBtc='" + isCurrencyForTakerFeeBtc + '\'' + "\n" + ", txFeeAsLong='" + txFeeAsLong + '\'' + "\n" + ", takerFeeAsLong='" + takerFeeAsLong + '\'' + "\n" + diff --git a/core/src/main/java/bisq/core/trade/TradeUtil.java b/core/src/main/java/bisq/core/trade/TradeUtil.java index a89dec32459..cd4fd074aad 100644 --- a/core/src/main/java/bisq/core/trade/TradeUtil.java +++ b/core/src/main/java/bisq/core/trade/TradeUtil.java @@ -39,6 +39,7 @@ import static bisq.core.locale.CurrencyUtil.isFiatCurrency; import static bisq.core.util.FormattingUtils.formatDurationAsWords; import static com.google.common.base.Preconditions.checkNotNull; +import static java.lang.String.format; /** * This class contains trade utility methods. @@ -173,6 +174,27 @@ public String getPaymentMethodNameWithCountryCode(Trade trade) { return paymentMethodDescription; } + /** + * Returns a string describing a trader's role for a given trade. + * @param trade Trade + * @return String describing a trader's role for a given trade + */ + public String getRole(Trade trade) { + Contract contract = trade.getContract(); + if (contract == null) + throw new IllegalStateException(format("could not get role because no contract was found for trade '%s'", + trade.getShortId())); + + Offer offer = trade.getOffer(); + if (offer == null) + throw new IllegalStateException(format("could not get role because no offer was found for trade '%s'", + trade.getShortId())); + + return getRole(contract.isBuyerMakerAndSellerTaker(), + offer.isMyOffer(keyRing), + offer.getCurrencyCode()); + } + /** * Returns a string describing a trader's role. * @@ -202,6 +224,5 @@ public String getRole(boolean isBuyerMakerAndSellerTaker, boolean isMaker, Strin ? Res.get("formatter.asMaker", currencyCode, Res.get("shared.buyer")) : Res.get("formatter.asTaker", currencyCode, Res.get("shared.seller")); } - } } diff --git a/daemon/src/main/java/bisq/daemon/grpc/GrpcTradesService.java b/daemon/src/main/java/bisq/daemon/grpc/GrpcTradesService.java index 0ffbd71f044..e23651bb207 100644 --- a/daemon/src/main/java/bisq/daemon/grpc/GrpcTradesService.java +++ b/daemon/src/main/java/bisq/daemon/grpc/GrpcTradesService.java @@ -56,8 +56,9 @@ public void getTrade(GetTradeRequest req, StreamObserver responseObserver) { try { Trade trade = coreApi.getTrade(req.getTradeId()); + String role = coreApi.getTradeRole(req.getTradeId()); var reply = GetTradeReply.newBuilder() - .setTrade(toTradeInfo(trade).toProtoMessage()) + .setTrade(toTradeInfo(trade, role).toProtoMessage()) .build(); responseObserver.onNext(reply); responseObserver.onCompleted(); diff --git a/proto/src/main/proto/grpc.proto b/proto/src/main/proto/grpc.proto index 68121d00596..041f1278ea2 100644 --- a/proto/src/main/proto/grpc.proto +++ b/proto/src/main/proto/grpc.proto @@ -218,25 +218,26 @@ message TradeInfo { string tradeId = 2; string shortId = 3; uint64 date = 4; - bool isCurrencyForTakerFeeBtc = 5; - uint64 txFeeAsLong = 6; - uint64 takerFeeAsLong = 7; - string takerFeeTxId = 8; - string depositTxId = 9; - string payoutTxId = 10; - uint64 tradeAmountAsLong = 11; - uint64 tradePrice = 12; - string tradingPeerNodeAddress = 13; - string state = 14; - string phase = 15; - string tradePeriodState = 16; - bool isDepositPublished = 17; - bool isDepositConfirmed = 18; - bool isFiatSent = 19; - bool isFiatReceived = 20; - bool isPayoutPublished = 21; - bool isWithdrawn = 22; - string contractAsJson = 23; + string role = 5; + bool isCurrencyForTakerFeeBtc = 6; + uint64 txFeeAsLong = 7; + uint64 takerFeeAsLong = 8; + string takerFeeTxId = 9; + string depositTxId = 10; + string payoutTxId = 11; + uint64 tradeAmountAsLong = 12; + uint64 tradePrice = 13; + string tradingPeerNodeAddress = 14; + string state = 15; + string phase = 16; + string tradePeriodState = 17; + bool isDepositPublished = 18; + bool isDepositConfirmed = 19; + bool isFiatSent = 20; + bool isFiatReceived = 21; + bool isPayoutPublished = 22; + bool isWithdrawn = 23; + string contractAsJson = 24; } ///////////////////////////////////////////////////////////////////////////////////////////