Move getRandomOfferId method to OfferUtil

Add more methods to OfferUtil
Cleanups
This commit is contained in:
chimp1984 2021-10-20 23:54:12 +02:00
parent a3b3d561ec
commit 9d106468b0
No known key found for this signature in database
GPG Key ID: 9801B4EC591F90E3
10 changed files with 43 additions and 20 deletions

View File

@ -183,7 +183,7 @@ class CoreOffersService {
throw new IllegalArgumentException(format("payment account with id %s not found", paymentAccountId));
String upperCaseCurrencyCode = currencyCode.toUpperCase();
String offerId = createOfferService.getRandomOfferId();
String offerId = OfferUtil.getRandomOfferId();
OfferDirection direction = OfferDirection.valueOf(directionAsString.toUpperCase());
Price price = Price.valueOf(upperCaseCurrencyCode, priceStringToLong(priceAsString, upperCaseCurrencyCode));
Coin amount = Coin.valueOf(amountAsLong);

View File

@ -57,6 +57,7 @@ public class BsqBalanceInfo implements Payload {
///////////////////////////////////////////////////////////////////////////////////////////
// PROTO BUFFER
///////////////////////////////////////////////////////////////////////////////////////////
// TODO rename availableConfirmedBalance in proto if possible
@Override
public bisq.proto.grpc.BsqBalanceInfo toProtoMessage() {

View File

@ -34,7 +34,7 @@ import static bisq.core.api.model.PaymentAccountPayloadInfo.toPaymentAccountPayl
@Getter
public class TradeInfo implements Payload {
// The client cannot see bisq.core.trade.Trade or its fromProto method. We use the
// The client cannot see Trade or its fromProto method. We use the
// lighter weight TradeInfo proto wrapper instead, containing just enough fields to
// view and interact with trades.

View File

@ -84,7 +84,8 @@ public class P2PNetworkSetup {
this.preferences = preferences;
}
BooleanProperty init(Runnable initWalletServiceHandler, @Nullable Consumer<Boolean> displayTorNetworkSettingsHandler) {
BooleanProperty init(Runnable initWalletServiceHandler,
@Nullable Consumer<Boolean> displayTorNetworkSettingsHandler) {
StringProperty bootstrapState = new SimpleStringProperty();
StringProperty bootstrapWarning = new SimpleStringProperty();
BooleanProperty hiddenServicePublished = new SimpleBooleanProperty();

View File

@ -45,8 +45,10 @@ import bisq.core.util.coin.CoinUtil;
import bisq.network.p2p.P2PService;
import bisq.common.app.Capabilities;
import bisq.common.app.Version;
import bisq.common.util.MathUtils;
import bisq.common.util.Tuple2;
import bisq.common.util.Utilities;
import org.bitcoinj.core.Coin;
import org.bitcoinj.core.Transaction;
@ -60,6 +62,7 @@ import javax.inject.Singleton;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Predicate;
import lombok.extern.slf4j.Slf4j;
@ -115,6 +118,40 @@ public class OfferUtil {
this.tradeStatisticsManager = tradeStatisticsManager;
}
public static String getRandomOfferId() {
return Utilities.getRandomPrefix(5, 8) + "-" +
UUID.randomUUID() + "-" +
getStrippedVersion();
}
public static String getStrippedVersion() {
return Version.VERSION.replace(".", "");
}
// We add a counter at the end of the offer id signalling the number of times that offer has
// been mutated ether due edit or due pow adjustments.
public static String getOfferIdWithMutationCounter(String id) {
String[] split = id.split("-");
String base = id;
int counter = 0;
if (split.length > 7) {
String counterString = split[7];
int endIndex = id.length() - counterString.length() - 1;
base = id.substring(0, endIndex);
try {
counter = Integer.parseInt(counterString);
} catch (Exception ignore) {
}
}
counter++;
return base + "-" + counter;
}
public static String getVersionFromId(String id) {
String[] split = id.split("-");
return split[6];
}
public void maybeSetFeePaymentCurrencyPreference(String feeCurrencyCode) {
if (!feeCurrencyCode.isEmpty()) {
if (!isValidFeePaymentCurrencyCode.test(feeCurrencyCode))

View File

@ -39,7 +39,6 @@ import bisq.network.p2p.P2PService;
import bisq.common.app.Version;
import bisq.common.crypto.PubKeyRing;
import bisq.common.util.Tuple2;
import bisq.common.util.Utilities;
import org.bitcoinj.core.Coin;
@ -52,7 +51,6 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
@ -96,12 +94,6 @@ public class CreateOfferService {
// API
///////////////////////////////////////////////////////////////////////////////////////////
public String getRandomOfferId() {
return Utilities.getRandomPrefix(5, 8) + "-" +
UUID.randomUUID().toString() + "-" +
Version.VERSION.replace(".", "");
}
public Offer createAndGetOffer(String offerId,
OfferDirection direction,
String currencyCode,

View File

@ -176,7 +176,7 @@ public abstract class MutableOfferDataModel extends OfferDataModel implements Bs
this.navigation = navigation;
this.tradeStatisticsManager = tradeStatisticsManager;
offerId = createOfferService.getRandomOfferId();
offerId = OfferUtil.getRandomOfferId();
shortOfferId = Utilities.getShortId(offerId);
addressEntry = btcWalletService.getOrCreateAddressEntry(offerId, AddressEntry.Context.OFFER_FUNDING);

View File

@ -24,7 +24,6 @@ import org.bitcoinj.core.Coin;
import javafx.collections.FXCollections;
import java.util.HashSet;
import java.util.UUID;
import org.junit.Before;
import org.junit.Test;
@ -61,7 +60,6 @@ public class CreateOfferDataModelTest {
when(btcWalletService.getOrCreateAddressEntry(anyString(), any())).thenReturn(addressEntry);
when(preferences.isUsePercentageBasedPrice()).thenReturn(true);
when(preferences.getBuyerSecurityDepositAsPercent(null)).thenReturn(0.01);
when(createOfferService.getRandomOfferId()).thenReturn(UUID.randomUUID().toString());
when(tradeStats.getObservableTradeStatisticsSet()).thenReturn(FXCollections.observableSet());
model = new CreateOfferDataModel(createOfferService,

View File

@ -56,8 +56,6 @@ import javafx.collections.FXCollections;
import java.time.Instant;
import java.util.UUID;
import org.junit.Before;
import org.junit.Test;
@ -118,7 +116,6 @@ public class CreateOfferViewModelTest {
when(preferences.getUserCountry()).thenReturn(new Country("ES", "Spain", null));
when(bsqFormatter.formatCoin(any())).thenReturn("0");
when(bsqWalletService.getAvailableBalance()).thenReturn(Coin.ZERO);
when(createOfferService.getRandomOfferId()).thenReturn(UUID.randomUUID().toString());
when(tradeStats.getObservableTradeStatisticsSet()).thenReturn(FXCollections.observableSet());
CreateOfferDataModel dataModel = new CreateOfferDataModel(createOfferService,

View File

@ -33,8 +33,6 @@ import javafx.collections.FXCollections;
import java.time.Instant;
import java.util.UUID;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@ -95,7 +93,6 @@ public class EditOfferDataModelTest {
when(preferences.getUserCountry()).thenReturn(new Country("US", "United States", null));
when(bsqFormatter.formatCoin(any())).thenReturn("0");
when(bsqWalletService.getAvailableBalance()).thenReturn(Coin.ZERO);
when(createOfferService.getRandomOfferId()).thenReturn(UUID.randomUUID().toString());
model = new EditOfferDataModel(createOfferService,
null,