Skip to content

Commit

Permalink
Merge pull request #945 from HenrikJannsen/refactor_offer_handling
Browse files Browse the repository at this point in the history
Refactor offer handling
  • Loading branch information
alvasw authored Jun 29, 2023
2 parents 5a8850b + 472d389 commit 6e9c43b
Show file tree
Hide file tree
Showing 86 changed files with 1,571 additions and 676 deletions.
11 changes: 6 additions & 5 deletions account/src/main/java/bisq/account/AccountService.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import bisq.common.application.Service;
import bisq.common.observable.Observable;
import bisq.common.observable.collection.ObservableSet;
import bisq.identity.IdentityService;
import bisq.network.NetworkService;
import bisq.persistence.Persistence;
import bisq.persistence.PersistenceClient;
import bisq.persistence.PersistenceService;
Expand All @@ -48,12 +46,15 @@ public class AccountService implements PersistenceClient<AccountStore>, Service
@Getter
private transient final ObservableSet<Account<?, ? extends PaymentMethod<?>>> accounts = new ObservableSet<>();

public AccountService(NetworkService networkService,
PersistenceService persistenceService,
IdentityService identityService) {
public AccountService(PersistenceService persistenceService) {
persistence = persistenceService.getOrCreatePersistence(this, persistableStore);
}

@Override
public void onPersistedApplied(AccountStore persisted) {
accounts.setAll(persisted.getAccountByName().values());
}


///////////////////////////////////////////////////////////////////////////////////////////////////
// Service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

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

@Getter
@Slf4j
@ToString
@EqualsAndHashCode(callSuper = true)
public final class UserDefinedFiatAccountPayload extends AccountPayload {
public static final int MAX_DATA_LENGTH = 1000;
private final String accountData;

public UserDefinedFiatAccountPayload(String id, String paymentMethodName, String accountData) {
super(id, paymentMethodName);
checkArgument(accountData.length() <= MAX_DATA_LENGTH);
this.accountData = accountData;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public DefaultApplicationService(String[] args) {

oracleService = new OracleService(OracleService.Config.from(getConfig("oracle")), config.getVersion(), networkService);

accountService = new AccountService(networkService, persistenceService, identityService);
accountService = new AccountService(persistenceService);

contractService = new ContractService(securityService);

Expand All @@ -142,7 +142,8 @@ public DefaultApplicationService(String[] args) {

supportService = new SupportService(networkService, chatService, userService);

tradeService = new TradeService(networkService, identityService, persistenceService, offerService, contractService, supportService);
tradeService = new TradeService(networkService, identityService, persistenceService, offerService,
contractService, supportService, chatService, oracleService);
}

@Override
Expand Down
4 changes: 4 additions & 0 deletions application/src/main/resources/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ application {
// todo
}

blockchainExplorer = {
// todo
}

daoBridgeHttpService = {
url = "http://localhost:8082"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,12 @@ private void processMessage(BisqEasyPrivateTradeChatMessage message) {
message.getSender(),
message.getMediator()));
} else {
// It could be that taker sends quickly a message after take offer and we receive them
// It could be that taker sends quickly a message after take offer, and we receive them
// out of order. In that case the seconds message (which arrived first) would get dropped.
// This is a very unlikely case, so we ignore it.
log.error("We received the first message for a new channel without an offer. " +
// It also happens if we left a trade channel and receive a message again.
// We ignore that and do not re-open the channel.
log.debug("We received the first message for a new channel without an offer. " +
"We drop that message. Message={}", message);
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

package bisq.chat.bisqeasy.message;

import bisq.offer.bisq_easy.BisqEasyOffer;

import java.util.Optional;

public interface BisqEasyOfferMessage {
Optional<String> getBisqEasyOfferId();
Optional<BisqEasyOffer> getBisqEasyOffer();

boolean hasBisqEasyOffer();
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import bisq.network.p2p.services.data.storage.MetaData;
import bisq.network.protobuf.ExternalNetworkMessage;
import bisq.network.protobuf.NetworkMessage;
import bisq.offer.Offer;
import bisq.offer.bisq_easy.BisqEasyOffer;
import bisq.user.profile.UserProfile;
import com.google.protobuf.Any;
Expand All @@ -45,7 +44,6 @@ public final class BisqEasyPrivateTradeChatMessage extends PrivateChatMessage im

private final Optional<UserProfile> mediator;
private final Optional<BisqEasyOffer> bisqEasyOffer;
private final Optional<String> bisqEasyOfferId;

public BisqEasyPrivateTradeChatMessage(String messageId,
String channelId,
Expand Down Expand Up @@ -89,7 +87,6 @@ private BisqEasyPrivateTradeChatMessage(String messageId,
super(messageId, chatChannelDomain, channelId, sender, receiverUserProfileId, text, citation, date, wasEdited, chatMessageType, metaData);
this.mediator = mediator;
this.bisqEasyOffer = bisqEasyOffer;
bisqEasyOfferId = bisqEasyOffer.map(Offer::getId);
}

public static BisqEasyPrivateTradeChatMessage createTakeOfferMessage(String channelId,
Expand Down Expand Up @@ -122,7 +119,6 @@ private BisqEasyPrivateTradeChatMessage(String channelId,
new MetaData(TTL, 100000, BisqEasyPrivateTradeChatMessage.class.getSimpleName()));
this.mediator = mediator;
this.bisqEasyOffer = Optional.of(bisqEasyOffer);
bisqEasyOfferId = this.bisqEasyOffer.map(Offer::getId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import bisq.chat.message.PublicChatMessage;
import bisq.common.util.StringUtils;
import bisq.network.p2p.services.data.storage.MetaData;
import bisq.offer.bisq_easy.BisqEasyOffer;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
Expand All @@ -36,11 +37,11 @@
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public final class BisqEasyPublicChatMessage extends PublicChatMessage implements BisqEasyOfferMessage {
private final Optional<String> bisqEasyOfferId;
private final Optional<BisqEasyOffer> bisqEasyOffer;

public BisqEasyPublicChatMessage(String channelId,
String authorUserProfileId,
Optional<String> bisqEasyOfferId,
Optional<BisqEasyOffer> bisqEasyOffer,
Optional<String> text,
Optional<Citation> citation,
long date,
Expand All @@ -49,7 +50,7 @@ public BisqEasyPublicChatMessage(String channelId,
ChatChannelDomain.BISQ_EASY,
channelId,
authorUserProfileId,
bisqEasyOfferId,
bisqEasyOffer,
text,
citation,
date,
Expand All @@ -62,7 +63,7 @@ private BisqEasyPublicChatMessage(String messageId,
ChatChannelDomain chatChannelDomain,
String channelId,
String authorUserProfileId,
Optional<String> bisqEasyOfferId,
Optional<BisqEasyOffer> bisqEasyOffer,
Optional<String> text,
Optional<Citation> citation,
long date,
Expand All @@ -79,12 +80,12 @@ private BisqEasyPublicChatMessage(String messageId,
wasEdited,
chatMessageType,
metaData);
this.bisqEasyOfferId = bisqEasyOfferId;
this.bisqEasyOffer = bisqEasyOffer;
}

public bisq.chat.protobuf.ChatMessage toProto() {
bisq.chat.protobuf.BisqEasyPublicChatMessage.Builder builder = bisq.chat.protobuf.BisqEasyPublicChatMessage.newBuilder();
bisqEasyOfferId.ifPresent(builder::setBisqEasyOfferId);
bisqEasyOffer.ifPresent(e -> builder.setBisqEasyOffer(e.toProto()));
return getChatMessageBuilder().setPublicBisqEasyOfferChatMessage(builder).build();
}

Expand All @@ -95,15 +96,15 @@ public static BisqEasyPublicChatMessage fromProto(bisq.chat.protobuf.ChatMessage
Optional<String> text = baseProto.hasText() ?
Optional.of(baseProto.getText()) :
Optional.empty();
Optional<String> bisqEasyOfferId = baseProto.getPublicBisqEasyOfferChatMessage().hasBisqEasyOfferId() ?
Optional.of(baseProto.getPublicBisqEasyOfferChatMessage().getBisqEasyOfferId()) :
Optional<BisqEasyOffer> bisqEasyOffer = baseProto.getPublicBisqEasyOfferChatMessage().hasBisqEasyOffer() ?
Optional.of(BisqEasyOffer.fromProto(baseProto.getPublicBisqEasyOfferChatMessage().getBisqEasyOffer())) :
Optional.empty();
return new BisqEasyPublicChatMessage(
baseProto.getId(),
ChatChannelDomain.fromProto(baseProto.getChatChannelDomain()),
baseProto.getChannelId(),
baseProto.getAuthorUserProfileId(),
bisqEasyOfferId,
bisqEasyOffer,
text,
citation,
baseProto.getDate(),
Expand All @@ -119,6 +120,6 @@ public MetaData getMetaData() {

@Override
public boolean hasBisqEasyOffer() {
return bisqEasyOfferId.isPresent();
return bisqEasyOffer.isPresent();
}
}
2 changes: 1 addition & 1 deletion chat/src/main/proto/chat.proto
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ message BisqEasyPrivateTradeChatChannelStore {
}

message BisqEasyPublicChatMessage {
optional string bisqEasyOfferId = 1;
optional offer.Offer bisqEasyOffer = 1;
}
message BisqEasyPublicChatChannel {
common.Market market = 1;
Expand Down
48 changes: 48 additions & 0 deletions common/src/main/java/bisq/common/encoding/Csv.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.common.encoding;

import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class Csv {
public static String toCsv(List<List<String>> data) {
StringBuilder sb = new StringBuilder();
for (List<String> row : data) {
for (int i = 0; i < row.size(); i++) {
sb.append(escapeSpecialCharacters(row.get(i)));

if (i != row.size() - 1) {
sb.append(",");
}
}
sb.append(System.lineSeparator());
}
return sb.toString();
}

public static String escapeSpecialCharacters(String value) {
if (value.contains(",") || value.contains("\"") || value.contains("'")) {
value = value.replace("\"", "\"\"");
value = "\"" + value + "\"";
}
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,23 @@
public class BisqEasyContract extends TwoPartyContract<BisqEasyOffer> {
private final long baseSideAmount;
private final long quoteSideAmount;
protected final BitcoinPaymentMethodSpec baseSidePaymentMethodSpecs;
protected final BitcoinPaymentMethodSpec baseSidePaymentMethodSpec;
protected final FiatPaymentMethodSpec quoteSidePaymentMethodSpec;
private final Optional<UserProfile> mediator;

public BisqEasyContract(BisqEasyOffer offer,
NetworkId takerNetworkId,
long baseSideAmount,
long quoteSideAmount,
BitcoinPaymentMethodSpec baseSidePaymentMethodSpecs,
BitcoinPaymentMethodSpec baseSidePaymentMethodSpec,
FiatPaymentMethodSpec quoteSidePaymentMethodSpec,
Optional<UserProfile> mediator) {
this(offer,
TradeProtocolType.BISQ_EASY,
new Party(Role.TAKER, takerNetworkId),
baseSideAmount,
quoteSideAmount,
baseSidePaymentMethodSpecs,
baseSidePaymentMethodSpec,
quoteSidePaymentMethodSpec,
mediator);
}
Expand All @@ -65,13 +65,13 @@ private BisqEasyContract(BisqEasyOffer offer,
Party taker,
long baseSideAmount,
long quoteSideAmount,
BitcoinPaymentMethodSpec baseSidePaymentMethodSpecs,
BitcoinPaymentMethodSpec baseSidePaymentMethodSpec,
FiatPaymentMethodSpec quoteSidePaymentMethodSpec,
Optional<UserProfile> mediator) {
super(offer, protocolType, taker);
this.baseSideAmount = baseSideAmount;
this.quoteSideAmount = quoteSideAmount;
this.baseSidePaymentMethodSpecs = baseSidePaymentMethodSpecs;
this.baseSidePaymentMethodSpec = baseSidePaymentMethodSpec;
this.quoteSidePaymentMethodSpec = quoteSidePaymentMethodSpec;
this.mediator = mediator;
}
Expand All @@ -81,7 +81,7 @@ public bisq.contract.protobuf.Contract toProto() {
var bisqEasyContract = bisq.contract.protobuf.BisqEasyContract.newBuilder()
.setBaseSideAmount(baseSideAmount)
.setQuoteSideAmount(quoteSideAmount)
.setBaseSidePaymentMethodSpecs(baseSidePaymentMethodSpecs.toProto())
.setBaseSidePaymentMethodSpec(baseSidePaymentMethodSpec.toProto())
.setQuoteSidePaymentMethodSpec(quoteSidePaymentMethodSpec.toProto());
mediator.ifPresent(mediator -> bisqEasyContract.setMediator(mediator.toProto()));
var twoPartyContract = getTwoPartyContractBuilder().setBisqEasyContract(bisqEasyContract);
Expand All @@ -96,7 +96,7 @@ public static BisqEasyContract fromProto(bisq.contract.protobuf.Contract proto)
Party.fromProto(twoPartyContractProto.getTaker()),
bisqEasyContractProto.getBaseSideAmount(),
bisqEasyContractProto.getQuoteSideAmount(),
PaymentMethodSpec.protoToBitcoinPaymentMethodSpec(bisqEasyContractProto.getBaseSidePaymentMethodSpecs()),
PaymentMethodSpec.protoToBitcoinPaymentMethodSpec(bisqEasyContractProto.getBaseSidePaymentMethodSpec()),
PaymentMethodSpec.protoToFiatPaymentMethodSpec(bisqEasyContractProto.getQuoteSidePaymentMethodSpec()),
bisqEasyContractProto.hasMediator() ?
Optional.of(UserProfile.fromProto(bisqEasyContractProto.getMediator())) :
Expand Down
2 changes: 1 addition & 1 deletion contract/src/main/proto/contract.proto
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ message SignedBisqEasyContract {
message BisqEasyContract {
uint64 baseSideAmount = 1;
uint64 quoteSideAmount = 2;
offer.PaymentMethodSpec baseSidePaymentMethodSpecs = 3;
offer.PaymentMethodSpec baseSidePaymentMethodSpec = 3;
offer.PaymentMethodSpec quoteSidePaymentMethodSpec = 4;
optional user.UserProfile mediator = 12;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
@Slf4j
public class FileChooserUtil {
@Nullable
public static File openFile(Scene scene) {
public static File openFile(Scene scene, String initialFileName) {
FileChooser fileChooser = new FileChooser();
fileChooser.setInitialFileName(initialFileName);
String initialDirectory = SettingsService.getInstance().getCookie().asString(CookieKey.FILE_CHOOSER_DIR)
.orElse(OsUtils.getDownloadOfHomeDir());
File initDir = new File(initialDirectory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class MaterialTextField extends Pane {
protected final Region selectionLine = new Region();
protected final Label descriptionLabel = new Label();
protected final TextInputControl textInputControl;
@Getter
protected final Label helpLabel = new Label();
@Getter
private final BisqIconButton iconButton = new BisqIconButton();
Expand Down Expand Up @@ -338,7 +339,8 @@ protected void onWidthChanged(double width) {
line.setPrefWidth(width);
selectionLine.setPrefWidth(textInputControl.isFocused() && textInputControl.isEditable() ? width : 0);
descriptionLabel.setPrefWidth(width - 2 * descriptionLabel.getLayoutX());
textInputControl.setPrefWidth(width - 2 * textInputControl.getLayoutX());
double iconWidth = iconButton.isVisible() ? 25 : 0;
textInputControl.setPrefWidth(width - 2 * textInputControl.getLayoutX() - iconWidth);
helpLabel.setPrefWidth(width - 2 * helpLabel.getLayoutX());
}
}
Expand Down
Loading

0 comments on commit 6e9c43b

Please sign in to comment.