Merge pull request #4701 from ghubstan/5-refactor-trade-utils

Add getRole(tradeId) to core api
This commit is contained in:
sqrrm 2020-10-30 13:43:15 +01:00 committed by GitHub
commit 3c56268149
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 72 additions and 21 deletions

View file

@ -193,6 +193,10 @@ public class CoreApi {
return coreTradesService.getTrade(tradeId);
}
public String getTradeRole(String tradeId) {
return coreTradesService.getTradeRole(tradeId);
}
///////////////////////////////////////////////////////////////////////////////////////////
// Wallets
///////////////////////////////////////////////////////////////////////////////////////////

View file

@ -21,6 +21,7 @@ import bisq.core.offer.Offer;
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 @@ class CoreTradesService {
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 @@ class CoreTradesService {
}
}
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)));

View file

@ -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 class TradeInfo implements Payload {
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 class TradeInfo implements Payload {
}
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 class TradeInfo implements Payload {
.setTradeId(tradeId)
.setShortId(shortId)
.setDate(date)
.setRole(role)
.setIsCurrencyForTakerFeeBtc(isCurrencyForTakerFeeBtc)
.setTxFeeAsLong(txFeeAsLong)
.setTakerFeeAsLong(takerFeeAsLong)
@ -165,6 +173,7 @@ public class TradeInfo implements Payload {
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 class TradeInfo implements Payload {
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 class TradeInfo implements Payload {
" tradeId='" + tradeId + '\'' + "\n" +
", shortId='" + shortId + '\'' + "\n" +
", date='" + date + '\'' + "\n" +
", role='" + role + '\'' + "\n" +
", isCurrencyForTakerFeeBtc='" + isCurrencyForTakerFeeBtc + '\'' + "\n" +
", txFeeAsLong='" + txFeeAsLong + '\'' + "\n" +
", takerFeeAsLong='" + takerFeeAsLong + '\'' + "\n" +

View file

@ -39,6 +39,7 @@ import static bisq.core.locale.CurrencyUtil.getCurrencyPair;
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 class TradeUtil {
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 class TradeUtil {
? Res.get("formatter.asMaker", currencyCode, Res.get("shared.buyer"))
: Res.get("formatter.asTaker", currencyCode, Res.get("shared.seller"));
}
}
}

View file

@ -56,8 +56,9 @@ class GrpcTradesService extends TradesGrpc.TradesImplBase {
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();

View file

@ -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;
}
///////////////////////////////////////////////////////////////////////////////////////////