Replace static TradeUtil with singleton TradeUtil

The API is going to need some desktop trade utilities, which should be
shared between :desktop and :core.api.
This commit is contained in:
ghubstan 2020-10-24 17:26:10 -03:00
parent 63cf436990
commit 296e4f98cb
No known key found for this signature in database
GPG Key ID: E35592D6800A861E
3 changed files with 55 additions and 17 deletions

View File

@ -120,6 +120,7 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
private final P2PService p2PService;
private final PriceFeedService priceFeedService;
private final TradeStatisticsManager tradeStatisticsManager;
private final TradeUtil tradeUtil;
@Getter
private final ArbitratorManager arbitratorManager;
private final MediatorManager mediatorManager;
@ -157,6 +158,7 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
P2PService p2PService,
PriceFeedService priceFeedService,
TradeStatisticsManager tradeStatisticsManager,
TradeUtil tradeUtil,
ArbitratorManager arbitratorManager,
MediatorManager mediatorManager,
ProcessModelServiceProvider processModelServiceProvider,
@ -175,6 +177,7 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
this.p2PService = p2PService;
this.priceFeedService = priceFeedService;
this.tradeStatisticsManager = tradeStatisticsManager;
this.tradeUtil = tradeUtil;
this.arbitratorManager = arbitratorManager;
this.mediatorManager = mediatorManager;
this.processModelServiceProvider = processModelServiceProvider;
@ -634,7 +637,7 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
// the relevant entries are changed, otherwise it's not added and no address entries are changed
private boolean recoverAddresses(Trade trade) {
// Find addresses associated with this trade.
var entries = TradeUtils.getAvailableAddresses(trade, btcWalletService, keyRing);
var entries = tradeUtil.getAvailableAddresses(trade);
if (entries == null)
return false;

View File

@ -1,18 +1,18 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* 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
* 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/>.
* along with bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.core.trade;
@ -23,20 +23,44 @@ import bisq.common.crypto.KeyRing;
import bisq.common.util.Tuple2;
import bisq.common.util.Utilities;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Objects;
public class TradeUtils {
import lombok.extern.slf4j.Slf4j;
// Returns <MULTI_SIG, TRADE_PAYOUT> if both are AVAILABLE, otherwise null
static Tuple2<String, String> getAvailableAddresses(Trade trade, BtcWalletService btcWalletService,
KeyRing keyRing) {
var addresses = getTradeAddresses(trade, btcWalletService, keyRing);
/**
* This class contains trade utility methods.
*/
@Slf4j
@Singleton
public class TradeUtil {
private final BtcWalletService btcWalletService;
private final KeyRing keyRing;
@Inject
public TradeUtil(BtcWalletService btcWalletService, KeyRing keyRing) {
this.btcWalletService = btcWalletService;
this.keyRing = keyRing;
}
/**
* Returns <MULTI_SIG, TRADE_PAYOUT> if and only if both are AVAILABLE,
* otherwise null.
* @param trade the trade being queried for MULTI_SIG, TRADE_PAYOUT addresses
* @return Tuple2 tuple containing MULTI_SIG, TRADE_PAYOUT addresses for trade
*/
public Tuple2<String, String> getAvailableAddresses(Trade trade) {
var addresses = getTradeAddresses(trade);
if (addresses == null)
return null;
if (btcWalletService.getAvailableAddressEntries().stream()
.noneMatch(e -> Objects.equals(e.getAddressString(), addresses.first)))
return null;
if (btcWalletService.getAvailableAddressEntries().stream()
.noneMatch(e -> Objects.equals(e.getAddressString(), addresses.second)))
return null;
@ -44,18 +68,25 @@ public class TradeUtils {
return new Tuple2<>(addresses.first, addresses.second);
}
// Returns <MULTI_SIG, TRADE_PAYOUT> addresses as strings if they're known by the wallet
public static Tuple2<String, String> getTradeAddresses(Trade trade, BtcWalletService btcWalletService,
KeyRing keyRing) {
/**
* Returns <MULTI_SIG, TRADE_PAYOUT> addresses as strings if they're known by the
* wallet.
* @param trade the trade being queried for MULTI_SIG, TRADE_PAYOUT addresses
* @return Tuple2 tuple containing MULTI_SIG, TRADE_PAYOUT addresses for trade
*/
public Tuple2<String, String> getTradeAddresses(Trade trade) {
var contract = trade.getContract();
if (contract == null)
return null;
// Get multisig address
var isMyRoleBuyer = contract.isMyRoleBuyer(keyRing.getPubKeyRing());
var multiSigPubKey = isMyRoleBuyer ? contract.getBuyerMultiSigPubKey() : contract.getSellerMultiSigPubKey();
var multiSigPubKey = isMyRoleBuyer
? contract.getBuyerMultiSigPubKey()
: contract.getSellerMultiSigPubKey();
if (multiSigPubKey == null)
return null;
var multiSigPubKeyString = Utilities.bytesAsHexString(multiSigPubKey);
var multiSigAddress = btcWalletService.getAddressEntryListAsImmutableList().stream()
.filter(e -> e.getKeyPair().getPublicKeyAsHex().equals(multiSigPubKeyString))
@ -65,8 +96,9 @@ public class TradeUtils {
return null;
// Get payout address
var payoutAddress = isMyRoleBuyer ?
contract.getBuyerPayoutAddressString() : contract.getSellerPayoutAddressString();
var payoutAddress = isMyRoleBuyer
? contract.getBuyerPayoutAddressString()
: contract.getSellerPayoutAddressString();
var payoutAddressEntry = btcWalletService.getAddressEntryListAsImmutableList().stream()
.filter(e -> Objects.equals(e.getAddressString(), payoutAddress))
.findAny()

View File

@ -24,7 +24,7 @@ import bisq.core.provider.price.PriceFeedService;
import bisq.core.trade.DumpDelayedPayoutTx;
import bisq.core.trade.TradableList;
import bisq.core.trade.Trade;
import bisq.core.trade.TradeUtils;
import bisq.core.trade.TradeUtil;
import bisq.common.crypto.KeyRing;
import bisq.common.persistence.PersistenceManager;
@ -50,6 +50,7 @@ public class FailedTradesManager implements PersistedDataHost {
private final PriceFeedService priceFeedService;
private final BtcWalletService btcWalletService;
private final PersistenceManager<TradableList<Trade>> persistenceManager;
private final TradeUtil tradeUtil;
private final DumpDelayedPayoutTx dumpDelayedPayoutTx;
@Setter
private Function<Trade, Boolean> unFailTradeCallback;
@ -59,12 +60,14 @@ public class FailedTradesManager implements PersistedDataHost {
PriceFeedService priceFeedService,
BtcWalletService btcWalletService,
PersistenceManager<TradableList<Trade>> persistenceManager,
TradeUtil tradeUtil,
DumpDelayedPayoutTx dumpDelayedPayoutTx) {
this.keyRing = keyRing;
this.priceFeedService = priceFeedService;
this.btcWalletService = btcWalletService;
this.dumpDelayedPayoutTx = dumpDelayedPayoutTx;
this.persistenceManager = persistenceManager;
this.tradeUtil = tradeUtil;
this.persistenceManager.initialize(failedTrades, "FailedTrades", PersistenceManager.Source.PRIVATE);
}
@ -127,7 +130,7 @@ public class FailedTradesManager implements PersistedDataHost {
}
public String checkUnFail(Trade trade) {
var addresses = TradeUtils.getTradeAddresses(trade, btcWalletService, keyRing);
var addresses = tradeUtil.getTradeAddresses(trade);
if (addresses == null) {
return "Addresses not found";
}