Skip to content

Commit

Permalink
Merge pull request #4701 from ghubstan/5-refactor-trade-utils
Browse files Browse the repository at this point in the history
Add getRole(tradeId) to core api
  • Loading branch information
sqrrm authored Oct 30, 2020
2 parents b6badee + 161dbad commit 3c56268
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 21 deletions.
4 changes: 4 additions & 0 deletions core/src/main/java/bisq/core/api/CoreApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ public Trade getTrade(String tradeId) {
return coreTradesService.getTrade(tradeId);
}

public String getTradeRole(String tradeId) {
return coreTradesService.getTradeRole(tradeId);
}

///////////////////////////////////////////////////////////////////////////////////////////
// Wallets
///////////////////////////////////////////////////////////////////////////////////////////
Expand Down
9 changes: 9 additions & 0 deletions core/src/main/java/bisq/core/api/CoreTradesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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",
Expand Down Expand Up @@ -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)));
Expand Down
15 changes: 15 additions & 0 deletions core/src/main/java/bisq/core/api/model/TradeInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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())
Expand Down Expand Up @@ -127,6 +134,7 @@ public bisq.proto.grpc.TradeInfo toProtoMessage() {
.setTradeId(tradeId)
.setShortId(shortId)
.setDate(date)
.setRole(role)
.setIsCurrencyForTakerFeeBtc(isCurrencyForTakerFeeBtc)
.setTxFeeAsLong(txFeeAsLong)
.setTakerFeeAsLong(takerFeeAsLong)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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" +
Expand Down
23 changes: 22 additions & 1 deletion core/src/main/java/bisq/core/trade/TradeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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"));
}

}
}
3 changes: 2 additions & 1 deletion daemon/src/main/java/bisq/daemon/grpc/GrpcTradesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ public void getTrade(GetTradeRequest req,
StreamObserver<GetTradeReply> 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();
Expand Down
39 changes: 20 additions & 19 deletions proto/src/main/proto/grpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

///////////////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 3c56268

Please sign in to comment.