Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getRole(tradeId) to core api #4701

Merged
merged 10 commits into from
Oct 30, 2020
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
8 changes: 2 additions & 6 deletions core/src/main/java/bisq/core/api/model/OfferInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,9 @@ public bisq.proto.grpc.OfferInfo toProtoMessage() {
.build();
}

@SuppressWarnings({"unused", "SameReturnValue"})
public static OfferInfo fromProto(bisq.proto.grpc.OfferInfo proto) {
/*
TODO?
return new OfferInfo(proto.getOfferPayload().getId(),
proto.getOfferPayload().getDate());
*/
return null;
return null; // TODO
}

/*
Expand Down
148 changes: 144 additions & 4 deletions core/src/main/java/bisq/core/api/model/TradeInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import bisq.common.Payload;

import java.util.Objects;

import lombok.EqualsAndHashCode;
import lombok.Getter;

Expand All @@ -37,6 +39,17 @@ public class TradeInfo implements Payload {
private final OfferInfo offer;
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;
private final String takerFeeTxId;
private final String depositTxId;
private final String payoutTxId;
private final long tradeAmountAsLong;
private final long tradePrice;
private final String tradingPeerNodeAddress;
private final String state;
private final String phase;
private final String tradePeriodState;
Expand All @@ -46,11 +59,23 @@ public class TradeInfo implements Payload {
private final boolean isFiatReceived;
private final boolean isPayoutPublished;
private final boolean isWithdrawn;
private final String contractAsJson;

public TradeInfo(TradeInfoBuilder builder) {
this.offer = builder.offer;
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;
this.takerFeeTxId = builder.takerFeeTxId;
this.depositTxId = builder.depositTxId;
this.payoutTxId = builder.payoutTxId;
this.tradeAmountAsLong = builder.tradeAmountAsLong;
this.tradePrice = builder.tradePrice;
this.tradingPeerNodeAddress = builder.tradingPeerNodeAddress;
this.state = builder.state;
this.phase = builder.phase;
this.tradePeriodState = builder.tradePeriodState;
Expand All @@ -60,13 +85,31 @@ public TradeInfo(TradeInfoBuilder builder) {
this.isFiatReceived = builder.isFiatReceived;
this.isPayoutPublished = builder.isPayoutPublished;
this.isWithdrawn = builder.isWithdrawn;
this.contractAsJson = builder.contractAsJson;
}

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())
.withTakerFeeAsLong(trade.getTakerFeeAsLong())
.withTakerFeeTxId(trade.getTakerFeeTxId())
.withDepositTxId(trade.getDepositTxId())
.withPayoutTxId(trade.getPayoutTxId())
.withTradeAmountAsLong(trade.getTradeAmountAsLong())
.withTradePrice(trade.getTradePrice().getValue())
.withTradingPeerNodeAddress(Objects.requireNonNull(
trade.getTradingPeerNodeAddress()).getHostNameWithoutPostFix())
.withState(trade.getState().name())
.withPhase(trade.getPhase().name())
.withTradePeriodState(trade.getTradePeriodState().name())
Expand All @@ -76,6 +119,7 @@ public static TradeInfo toTradeInfo(Trade trade) {
.withIsFiatReceived(trade.isFiatReceived())
.withIsPayoutPublished(trade.isPayoutPublished())
.withIsWithdrawn(trade.isWithdrawn())
.withContractAsJson(trade.getContractAsJson())
.build();
}

Expand All @@ -89,6 +133,17 @@ public bisq.proto.grpc.TradeInfo toProtoMessage() {
.setOffer(offer.toProtoMessage())
.setTradeId(tradeId)
.setShortId(shortId)
.setDate(date)
.setRole(role)
.setIsCurrencyForTakerFeeBtc(isCurrencyForTakerFeeBtc)
.setTxFeeAsLong(txFeeAsLong)
.setTakerFeeAsLong(takerFeeAsLong)
.setTakerFeeTxId(takerFeeTxId == null ? "" : takerFeeTxId)
.setDepositTxId(depositTxId == null ? "" : depositTxId)
.setPayoutTxId(payoutTxId == null ? "" : payoutTxId)
.setTradeAmountAsLong(tradeAmountAsLong)
.setTradePrice(tradePrice)
.setTradingPeerNodeAddress(tradingPeerNodeAddress)
.setState(state)
.setPhase(phase)
.setTradePeriodState(tradePeriodState)
Expand All @@ -98,12 +153,13 @@ public bisq.proto.grpc.TradeInfo toProtoMessage() {
.setIsFiatReceived(isFiatReceived)
.setIsPayoutPublished(isPayoutPublished)
.setIsWithdrawn(isWithdrawn)
.setContractAsJson(contractAsJson == null ? "" : contractAsJson)
.build();
}

@SuppressWarnings({"unused", "SameReturnValue"})
public static TradeInfo fromProto(bisq.proto.grpc.TradeInfo proto) {
// TODO
return null;
return null; // TODO
}

/*
Expand All @@ -116,6 +172,17 @@ public static class TradeInfoBuilder {
private OfferInfo offer;
private String tradeId;
private String shortId;
private long date;
private String role;
private boolean isCurrencyForTakerFeeBtc;
private long txFeeAsLong;
private long takerFeeAsLong;
private String takerFeeTxId;
private String depositTxId;
private String payoutTxId;
private long tradeAmountAsLong;
private long tradePrice;
private String tradingPeerNodeAddress;
private String state;
private String phase;
private String tradePeriodState;
Expand All @@ -125,6 +192,7 @@ public static class TradeInfoBuilder {
private boolean isFiatReceived;
private boolean isPayoutPublished;
private boolean isWithdrawn;
private String contractAsJson;

public TradeInfoBuilder withOffer(OfferInfo offer) {
this.offer = offer;
Expand All @@ -141,6 +209,61 @@ public TradeInfoBuilder withShortId(String shortId) {
return this;
}

public TradeInfoBuilder withDate(long date) {
this.date = date;
return this;
}

public TradeInfoBuilder withRole(String role) {
this.role = role;
return this;
}

public TradeInfoBuilder withIsCurrencyForTakerFeeBtc(boolean isCurrencyForTakerFeeBtc) {
this.isCurrencyForTakerFeeBtc = isCurrencyForTakerFeeBtc;
return this;
}

public TradeInfoBuilder withTxFeeAsLong(long txFeeAsLong) {
this.txFeeAsLong = txFeeAsLong;
return this;
}

public TradeInfoBuilder withTakerFeeAsLong(long takerFeeAsLong) {
this.takerFeeAsLong = takerFeeAsLong;
return this;
}

public TradeInfoBuilder withTakerFeeTxId(String takerFeeTxId) {
this.takerFeeTxId = takerFeeTxId;
return this;
}

public TradeInfoBuilder withDepositTxId(String depositTxId) {
this.depositTxId = depositTxId;
return this;
}

public TradeInfoBuilder withPayoutTxId(String payoutTxId) {
this.payoutTxId = payoutTxId;
return this;
}

public TradeInfoBuilder withTradeAmountAsLong(long tradeAmountAsLong) {
this.tradeAmountAsLong = tradeAmountAsLong;
return this;
}

public TradeInfoBuilder withTradePrice(long tradePrice) {
this.tradePrice = tradePrice;
return this;
}

public TradeInfoBuilder withTradePeriodState(String tradePeriodState) {
this.tradePeriodState = tradePeriodState;
return this;
}

public TradeInfoBuilder withState(String state) {
this.state = state;
return this;
Expand All @@ -151,8 +274,8 @@ public TradeInfoBuilder withPhase(String phase) {
return this;
}

public TradeInfoBuilder withTradePeriodState(String tradePeriodState) {
this.tradePeriodState = tradePeriodState;
public TradeInfoBuilder withTradingPeerNodeAddress(String tradingPeerNodeAddress) {
this.tradingPeerNodeAddress = tradingPeerNodeAddress;
return this;
}

Expand Down Expand Up @@ -186,6 +309,11 @@ public TradeInfoBuilder withIsWithdrawn(boolean isWithdrawn) {
return this;
}

public TradeInfoBuilder withContractAsJson(String contractAsJson) {
this.contractAsJson = contractAsJson;
return this;
}

public TradeInfo build() {
return new TradeInfo(this);
}
Expand All @@ -196,6 +324,17 @@ public String toString() {
return "TradeInfo{" +
" tradeId='" + tradeId + '\'' + "\n" +
", shortId='" + shortId + '\'' + "\n" +
", date='" + date + '\'' + "\n" +
", role='" + role + '\'' + "\n" +
", isCurrencyForTakerFeeBtc='" + isCurrencyForTakerFeeBtc + '\'' + "\n" +
", txFeeAsLong='" + txFeeAsLong + '\'' + "\n" +
", takerFeeAsLong='" + takerFeeAsLong + '\'' + "\n" +
", takerFeeTxId='" + takerFeeTxId + '\'' + "\n" +
", depositTxId='" + depositTxId + '\'' + "\n" +
", payoutTxId='" + payoutTxId + '\'' + "\n" +
", tradeAmountAsLong='" + tradeAmountAsLong + '\'' + "\n" +
", tradePrice='" + tradePrice + '\'' + "\n" +
", tradingPeerNodeAddress='" + tradingPeerNodeAddress + '\'' + "\n" +
", state='" + state + '\'' + "\n" +
", phase='" + phase + '\'' + "\n" +
", tradePeriodState='" + tradePeriodState + '\'' + "\n" +
Expand All @@ -206,6 +345,7 @@ public String toString() {
", isPayoutPublished=" + isPayoutPublished + "\n" +
", isWithdrawn=" + isWithdrawn + "\n" +
", offer=" + offer + "\n" +
", contractAsJson=" + contractAsJson + "\n" +
'}';
}
}
4 changes: 4 additions & 0 deletions core/src/main/java/bisq/core/offer/OfferUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ public boolean isBsqForTakerFeeAvailable(@Nullable Coin amount) {
return !availableBalance.subtract(takerFee).isNegative();
}

public boolean isBlockChainPaymentMethod(Offer offer) {
return offer != null && offer.getPaymentMethod().isAsset();
}

public Optional<Volume> getFeeInUserFiatCurrency(Coin makerFee,
boolean isCurrencyForMakerFeeBtc,
CoinFormatter bsqFormatter) {
Expand Down
Loading