Skip to content

Commit

Permalink
Merge pull request #3083 from HenrikJannsen/small-fixes
Browse files Browse the repository at this point in the history
Small fixes
  • Loading branch information
HenrikJannsen authored Jan 6, 2025
2 parents 3fbaae8 + 56f4460 commit 327c824
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -396,14 +396,12 @@ private void updateIsAnyTradeInMediation() {
}

private Optional<BisqEasyOpenTradesView.ListItem> findListItem(BisqEasyTrade trade) {
return model.getListItems().stream()
.filter(e -> e.getTrade().equals(trade))
.findAny();
return findListItem(trade.getId());
}

private Optional<BisqEasyOpenTradesView.ListItem> findListItem(String tradeId) {
return model.getListItems().stream()
.filter(e -> e.getTrade().getId().equals(tradeId))
.filter(item -> item.getTrade().getId().equals(tradeId))
.findAny();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ public Optional<BisqEasyOfferbookMessage> findMessageByOffer(BisqEasyOffer offer
.findAny();
}

public Optional<BisqEasyOfferbookMessage> findMessageByOfferId(String offerId) {
return getChannels().stream()
.flatMap(channel -> channel.getChatMessages().stream())
.filter(chatMessage -> chatMessage.getBisqEasyOffer().isPresent() &&
chatMessage.getBisqEasyOffer().get().getId().equals(offerId))
.findAny();
}


///////////////////////////////////////////////////////////////////////////////////////////////////
// Protected
Expand Down
2 changes: 1 addition & 1 deletion i18n/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ splash.bootstrapState.CONNECTED_TO_PEERS=Connecting to peers

tac.headline=User Agreement
tac.confirm=I have read and understood
tac.accept=Accept user Agreement
tac.accept=Accept user agreement
tac.reject=Reject and quit application


Expand Down
2 changes: 1 addition & 1 deletion i18n/src/main/resources/application_pcm.properties
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ splash.bootstrapState.CONNECTED_TO_PEERS=To connect to peers

tac.headline=User Agreement
tac.confirm=I don read and understand
tac.accept=Accept user Agreement
tac.accept=Accept user agreement
tac.reject=Reject and comot for application


Expand Down
31 changes: 10 additions & 21 deletions trade/src/main/java/bisq/trade/Trade.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
import java.util.Optional;
import java.util.UUID;

import static com.google.common.base.Preconditions.checkArgument;

@Slf4j
@Getter
@ToString(callSuper = true)
Expand All @@ -63,36 +61,40 @@ private static TradeRole createRole(boolean isBuyer, boolean isTaker) {
private final P maker;
// transient fields are excluded by default for EqualsAndHashCode
private transient final TradeRole tradeRole;
private final Observable<C> contract = new Observable<>();
private final C contract;
private final Observable<String> errorMessage = new Observable<>();
private final Observable<String> errorStackTrace = new Observable<>();
private final Observable<String> peersErrorMessage = new Observable<>();
private final Observable<String> peersErrorStackTrace = new Observable<>();
private final Observable<String> protocolVersion = new Observable<>();

public Trade(State state,
public Trade(C contract,
State state,
boolean isBuyer,
boolean isTaker,
Identity myIdentity,
T offer,
P taker,
P maker) {
this(state,
this(contract,
state,
createId(offer.getId(), taker.getNetworkId().getId()),
createRole(isBuyer, isTaker),
myIdentity,
taker,
maker);
}

protected Trade(State state,
protected Trade(C contract,
State state,
String id,
TradeRole tradeRole,
Identity myIdentity,
P taker,
P maker) {
super(state);

this.contract = contract;
this.id = id;
this.tradeRole = tradeRole;
this.myIdentity = myIdentity;
Expand All @@ -102,32 +104,20 @@ protected Trade(State state,

protected bisq.trade.protobuf.Trade.Builder getTradeBuilder(boolean serializeForHash) {
bisq.trade.protobuf.Trade.Builder builder = bisq.trade.protobuf.Trade.newBuilder()
.setContract(contract.toProto(serializeForHash))
.setId(id)
.setTradeRole(tradeRole.toProtoEnum())
.setMyIdentity(myIdentity.toProto(serializeForHash))
.setTaker(taker.toProto(serializeForHash))
.setMaker(maker.toProto(serializeForHash))
.setState(getState().name());
Optional.ofNullable(contract.get()).ifPresent(contract -> builder.setContract(contract.toProto(serializeForHash)));
Optional.ofNullable(getErrorMessage()).ifPresent(builder::setErrorMessage);
Optional.ofNullable(getErrorStackTrace()).ifPresent(builder::setErrorStackTrace);
Optional.ofNullable(getPeersErrorMessage()).ifPresent(builder::setPeersErrorMessage);
Optional.ofNullable(getPeersErrorStackTrace()).ifPresent(builder::setPeersErrorStackTrace);
return builder;
}

public C getContract() {
return contract.get();
}

public ReadOnlyObservable<C> contractObservable() {
return contract;
}

public void setContract(C contract) {
this.contract.set(contract);
}

public void setErrorMessage(String errorMessage) {
this.errorMessage.set(errorMessage);
}
Expand Down Expand Up @@ -181,8 +171,7 @@ public String getProtocolVersion() {
///////////////////////////////////////////////////////////////////////////////////////////////////

public T getOffer() {
checkArgument(contract.get() != null, "Cannot get offer, since contract has not yet been set.");
return contract.get().getOffer();
return contract.getOffer();
}

public boolean isBuyer() {
Expand Down
27 changes: 14 additions & 13 deletions trade/src/main/java/bisq/trade/bisq_easy/BisqEasyTrade.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ public final class BisqEasyTrade extends Trade<BisqEasyOffer, BisqEasyContract,

private final transient Observable<BisqEasyTradeState> tradeState = new Observable<>();

public BisqEasyTrade(boolean isBuyer,
public BisqEasyTrade(BisqEasyContract contract,
boolean isBuyer,
boolean isTaker,
Identity myIdentity,
BisqEasyOffer offer,
NetworkId takerNetworkId,
NetworkId makerNetworkId) {
super(BisqEasyTradeState.INIT,
super(contract,
BisqEasyTradeState.INIT,
isBuyer,
isTaker,
myIdentity,
Expand All @@ -71,13 +73,14 @@ public BisqEasyTrade(boolean isBuyer,
stateObservable().addObserver(s -> tradeState.set((BisqEasyTradeState) s));
}

public BisqEasyTrade(BisqEasyTradeState state,
String id,
TradeRole tradeRole,
Identity myIdentity,
BisqEasyTradeParty taker,
BisqEasyTradeParty maker) {
super(state, id, tradeRole, myIdentity, taker, maker);
public BisqEasyTrade(BisqEasyContract contract,
BisqEasyTradeState state,
String id,
TradeRole tradeRole,
Identity myIdentity,
BisqEasyTradeParty taker,
BisqEasyTradeParty maker) {
super(contract, state, id, tradeRole, myIdentity, taker, maker);

stateObservable().addObserver(s -> tradeState.set((BisqEasyTradeState) s));
}
Expand Down Expand Up @@ -106,15 +109,13 @@ private bisq.trade.protobuf.BisqEasyTrade.Builder getBisqEasyTradeBuilder(boolea
}

public static BisqEasyTrade fromProto(bisq.trade.protobuf.Trade proto) {
BisqEasyTrade trade = new BisqEasyTrade(ProtobufUtils.enumFromProto(BisqEasyTradeState.class, proto.getState()),
BisqEasyTrade trade = new BisqEasyTrade(BisqEasyContract.fromProto(proto.getContract()),
ProtobufUtils.enumFromProto(BisqEasyTradeState.class, proto.getState()),
proto.getId(),
TradeRole.fromProto(proto.getTradeRole()),
Identity.fromProto(proto.getMyIdentity()),
TradeParty.protoToBisqEasyTradeParty(proto.getTaker()),
TradeParty.protoToBisqEasyTradeParty(proto.getMaker()));
if (proto.hasContract()) {
trade.setContract(BisqEasyContract.fromProto(proto.getContract()));
}
if (proto.hasErrorMessage()) {
trade.setErrorMessage(proto.getErrorMessage());
}
Expand Down
54 changes: 18 additions & 36 deletions trade/src/main/java/bisq/trade/bisq_easy/BisqEasyTradeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,51 +172,20 @@ public void onMessage(EnvelopePayloadMessage envelopePayloadMessage) {

private void onBisqEasyTakeOfferMessage(BisqEasyTakeOfferRequest message) {
BisqEasyContract bisqEasyContract = checkNotNull(message.getBisqEasyContract());
BisqEasyProtocol protocol = getOrCreateProtocol(message.getTradeId(),
bisqEasyContract.getOffer(),
message.getSender(),
message.getReceiver());
checkArgument(protocol.getModel().getContract() == null,
"Trade must not have a contract set yet.");
protocol.getModel().setContract(bisqEasyContract);
BisqEasyProtocol protocol = createProtocol(bisqEasyContract, message.getSender(), message.getReceiver());
handleEvent(protocol, message);
}

private void onBisqEasyBtcAddressMessage(BisqEasyBtcAddressMessage message) {
BisqEasyProtocol protocol = getOrCreateProtocol(message.getTradeId(),
message.getBisqEasyOffer(),
message.getSender(),
message.getReceiver());
BisqEasyProtocol protocol = getProtocol(message.getTradeId());
handleEvent(protocol, message);
}

private void onBisqEasySendAccountDataMessage(BisqEasyAccountDataMessage message) {
BisqEasyProtocol protocol = getOrCreateProtocol(message.getTradeId(),
message.getBisqEasyOffer(),
message.getSender(),
message.getReceiver());
BisqEasyProtocol protocol = getProtocol(message.getTradeId());
handleEvent(protocol, message);
}

private BisqEasyProtocol getOrCreateProtocol(String tradeId, BisqEasyOffer offer, NetworkId sender, NetworkId receiver) {
return findProtocol(tradeId).isPresent()
? getProtocol(tradeId)
: createTradeProtocol(offer, sender, receiver);
}

private BisqEasyProtocol createTradeProtocol(BisqEasyOffer bisqEasyOffer, NetworkId sender, NetworkId receiver) {
// We only create the data required for the protocol creation.
// Verification will happen in the BisqEasyTakeOfferRequestHandler
boolean isBuyer = bisqEasyOffer.getMakersDirection().isBuy();
Identity myIdentity = serviceProvider.getIdentityService().findAnyIdentityByNetworkId(bisqEasyOffer.getMakerNetworkId()).orElseThrow();
BisqEasyTrade bisqEasyTrade = new BisqEasyTrade(isBuyer, false, myIdentity, bisqEasyOffer, sender, receiver);
String tradeId = bisqEasyTrade.getId();
checkArgument(findProtocol(tradeId).isEmpty(), "We received the BisqEasyTakeOfferRequest for an already existing protocol");
checkArgument(!tradeExists(tradeId), "A trade with that ID exists already");
persistableStore.addTrade(bisqEasyTrade);
return createAndAddTradeProtocol(bisqEasyTrade);
}


///////////////////////////////////////////////////////////////////////////////////////////////////
// Events
Expand Down Expand Up @@ -248,8 +217,7 @@ public BisqEasyProtocol createBisqEasyProtocol(Identity takerIdentity,
marketPrice);
boolean isBuyer = bisqEasyOffer.getTakersDirection().isBuy();
NetworkId makerNetworkId = contract.getMaker().getNetworkId();
BisqEasyTrade bisqEasyTrade = new BisqEasyTrade(isBuyer, true, takerIdentity, bisqEasyOffer, takerNetworkId, makerNetworkId);
bisqEasyTrade.setContract(contract);
BisqEasyTrade bisqEasyTrade = new BisqEasyTrade(contract, isBuyer, true, takerIdentity, bisqEasyOffer, takerNetworkId, makerNetworkId);

checkArgument(findProtocol(bisqEasyTrade.getId()).isEmpty(),
"We received the BisqEasyTakeOfferRequest for an already existing protocol");
Expand Down Expand Up @@ -318,6 +286,20 @@ private void handleEvent(BisqEasyProtocol protocol, Event event) {
// Misc API
///////////////////////////////////////////////////////////////////////////////////////////////////

private BisqEasyProtocol createProtocol(BisqEasyContract contract, NetworkId sender, NetworkId receiver) {
// We only create the data required for the protocol creation.
// Verification will happen in the BisqEasyTakeOfferRequestHandler
BisqEasyOffer offer = contract.getOffer();
boolean isBuyer = offer.getMakersDirection().isBuy();
Identity myIdentity = serviceProvider.getIdentityService().findAnyIdentityByNetworkId(offer.getMakerNetworkId()).orElseThrow();
BisqEasyTrade bisqEasyTrade = new BisqEasyTrade(contract, isBuyer, false, myIdentity, offer, sender, receiver);
String tradeId = bisqEasyTrade.getId();
checkArgument(findProtocol(tradeId).isEmpty(), "We received the BisqEasyTakeOfferRequest for an already existing protocol");
checkArgument(!tradeExists(tradeId), "A trade with that ID exists already");
persistableStore.addTrade(bisqEasyTrade);
return createAndAddTradeProtocol(bisqEasyTrade);
}

public Optional<BisqEasyProtocol> findProtocol(String id) {
return Optional.ofNullable(tradeProtocolById.get(id));
}
Expand Down
19 changes: 9 additions & 10 deletions trade/src/main/java/bisq/trade/bisq_musig/BisqMuSigTrade.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,31 @@
public final class BisqMuSigTrade extends Trade<BisqMuSigOffer, BisqMuSigContract, BisqMuSigTradeParty> {
private final Observable<BisqMuSigTradeState> tradeState = new Observable<>();

public BisqMuSigTrade(boolean isBuyer,
public BisqMuSigTrade(BisqMuSigContract contract,
boolean isBuyer,
boolean isTaker,
Identity myIdentity,
BisqMuSigContract contract,
NetworkId takerNetworkId) {
super(BisqMuSigTradeState.INIT,
super(contract,
BisqMuSigTradeState.INIT,
isBuyer,
isTaker,
myIdentity,
contract.getOffer(),
new BisqMuSigTradeParty(takerNetworkId),
new BisqMuSigTradeParty(contract.getMaker().getNetworkId()));

setContract(contract);
stateObservable().addObserver(s -> tradeState.set((BisqMuSigTradeState) s));
}

private BisqMuSigTrade(BisqMuSigTradeState state,
private BisqMuSigTrade(BisqMuSigContract contract,
BisqMuSigTradeState state,
String id,
TradeRole tradeRole,
Identity myIdentity,
BisqMuSigTradeParty taker,
BisqMuSigTradeParty maker) {
super(state, id, tradeRole, myIdentity, taker, maker);
super(contract, state, id, tradeRole, myIdentity, taker, maker);

stateObservable().addObserver(s -> tradeState.set((BisqMuSigTradeState) s));
}
Expand All @@ -77,15 +78,13 @@ public bisq.trade.protobuf.Trade toProto(boolean serializeForHash) {
}

public static BisqMuSigTrade fromProto(bisq.trade.protobuf.Trade proto) {
BisqMuSigTrade trade = new BisqMuSigTrade(ProtobufUtils.enumFromProto(BisqMuSigTradeState.class, proto.getState()),
BisqMuSigTrade trade = new BisqMuSigTrade(BisqMuSigContract.fromProto(proto.getContract()),
ProtobufUtils.enumFromProto(BisqMuSigTradeState.class, proto.getState()),
proto.getId(),
TradeRole.fromProto(proto.getTradeRole()),
Identity.fromProto(proto.getMyIdentity()),
TradeParty.protoToBisqMuSigTradeParty(proto.getTaker()),
TradeParty.protoToBisqMuSigTradeParty(proto.getMaker()));
if (proto.hasContract()) {
trade.setContract(BisqMuSigContract.fromProto(proto.getContract()));
}
if (proto.hasErrorMessage()) {
trade.setErrorMessage(proto.getErrorMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public BisqMuSigTrade onTakeOffer(Identity takerIdentity, BisqMuSigOffer bisqMuS
bisqMuSigOffer,
takerNetworkId);
boolean isBuyer = bisqMuSigOffer.getTakersDirection().isBuy();
BisqMuSigTrade tradeModel = new BisqMuSigTrade(isBuyer, true, takerIdentity, contract, takerNetworkId);
BisqMuSigTrade tradeModel = new BisqMuSigTrade(contract, isBuyer, true, takerIdentity, takerNetworkId);
checkArgument(findProtocol(tradeModel.getId()).isPresent(),
"We received the TakeOfferRequest for an already existing protocol");

Expand Down
Loading

0 comments on commit 327c824

Please sign in to comment.