Skip to content

Commit

Permalink
Merge pull request #4697 from ghubstan/2-refactor-trade-utils
Browse files Browse the repository at this point in the history
Replace TradeUtils with singleton TradeUtil
  • Loading branch information
sqrrm authored Oct 29, 2020
2 parents 3356c8f + ccd3c99 commit 258d44e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
5 changes: 4 additions & 1 deletion core/src/main/java/bisq/core/trade/TradeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -157,6 +158,7 @@ public TradeManager(User user,
P2PService p2PService,
PriceFeedService priceFeedService,
TradeStatisticsManager tradeStatisticsManager,
TradeUtil tradeUtil,
ArbitratorManager arbitratorManager,
MediatorManager mediatorManager,
ProcessModelServiceProvider processModelServiceProvider,
Expand All @@ -175,6 +177,7 @@ public TradeManager(User user,
this.p2PService = p2PService;
this.priceFeedService = priceFeedService;
this.tradeStatisticsManager = tradeStatisticsManager;
this.tradeUtil = tradeUtil;
this.arbitratorManager = arbitratorManager;
this.mediatorManager = mediatorManager;
this.processModelServiceProvider = processModelServiceProvider;
Expand Down Expand Up @@ -634,7 +637,7 @@ private boolean unFailTrade(Trade trade) {
// 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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,70 @@
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;

/**
* This class contains trade utility methods.
*/
@Slf4j
@Singleton
public class TradeUtil {

private final BtcWalletService btcWalletService;
private final KeyRing keyRing;

// 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);
@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;

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))
Expand All @@ -65,8 +96,9 @@ public static Tuple2<String, String> getTradeAddresses(Trade trade, BtcWalletSer
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
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;
Expand All @@ -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;
Expand All @@ -59,12 +60,14 @@ public FailedTradesManager(KeyRing keyRing,
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);
}
Expand Down Expand Up @@ -127,7 +130,7 @@ public void unFailTrade(Trade trade) {
}

public String checkUnFail(Trade trade) {
var addresses = TradeUtils.getTradeAddresses(trade, btcWalletService, keyRing);
var addresses = tradeUtil.getTradeAddresses(trade);
if (addresses == null) {
return "Addresses not found";
}
Expand Down

0 comments on commit 258d44e

Please sign in to comment.