mirror of
https://github.com/haveno-dex/haveno.git
synced 2025-04-16 05:53:06 -04:00
Rename methods, cleanup
This commit is contained in:
parent
9c9064091a
commit
6888eacc24
@ -162,11 +162,6 @@ public class MainView extends InitializableView<StackPane, MainViewModel> {
|
||||
}
|
||||
});
|
||||
|
||||
model.featureNotImplementedWarning.addListener((ov, oldValue, newValue) -> {
|
||||
if (oldValue == null && newValue != null)
|
||||
Popups.openWarningPopup(newValue);
|
||||
});
|
||||
|
||||
// Delay a bit to give time for rendering the splash screen
|
||||
Platform.runLater(() -> model.initBackend());
|
||||
}
|
||||
|
@ -96,7 +96,6 @@ class MainViewModel implements ViewModel {
|
||||
final ObjectProperty<BankAccount> currentBankAccount = new SimpleObjectProperty<>();
|
||||
|
||||
final BooleanProperty showAppScreen = new SimpleBooleanProperty();
|
||||
final StringProperty featureNotImplementedWarning = new SimpleStringProperty();
|
||||
final StringProperty numPendingTradesAsString = new SimpleStringProperty();
|
||||
final BooleanProperty showPendingTradesNotification = new SimpleBooleanProperty();
|
||||
|
||||
@ -142,13 +141,6 @@ class MainViewModel implements ViewModel {
|
||||
});
|
||||
bankAccountsComboBoxDisable.set(user.getBankAccounts().isEmpty());
|
||||
bankAccountsComboBoxPrompt.set(user.getBankAccounts().isEmpty() ? "No accounts" : "");
|
||||
|
||||
tradeManager.featureNotImplementedWarningProperty().addListener((ov, oldValue, newValue) -> {
|
||||
if (oldValue == null && newValue != null) {
|
||||
featureNotImplementedWarning.set(newValue);
|
||||
tradeManager.setFeatureNotImplementedWarning(null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void restart() {
|
||||
|
@ -79,7 +79,7 @@ class OffersDataModel implements Activatable, DataModel {
|
||||
}
|
||||
|
||||
void removeOpenOffer(String offerId, ResultHandler resultHandler, ErrorMessageHandler errorMessageHandler) {
|
||||
tradeManager.requestRemoveOpenOffer(offerId, resultHandler, errorMessageHandler);
|
||||
tradeManager.onRemoveOpenOfferRequested(offerId, resultHandler, errorMessageHandler);
|
||||
}
|
||||
|
||||
|
||||
|
@ -164,12 +164,12 @@ class PendingTradesDataModel implements Activatable, DataModel {
|
||||
|
||||
void fiatPaymentStarted() {
|
||||
getTrade().setState(Trade.State.FIAT_PAYMENT_STARTED);
|
||||
tradeManager.fiatPaymentStarted(getTrade().getId());
|
||||
tradeManager.onFiatPaymentStarted(getTrade().getId());
|
||||
}
|
||||
|
||||
void fiatPaymentReceived() {
|
||||
getTrade().setState(Trade.State.FIAT_PAYMENT_RECEIVED);
|
||||
tradeManager.fiatPaymentReceived(getTrade().getId());
|
||||
tradeManager.onFiatPaymentReceived(getTrade().getId());
|
||||
}
|
||||
|
||||
void withdraw(String toAddress) {
|
||||
@ -200,7 +200,7 @@ class PendingTradesDataModel implements Activatable, DataModel {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
|
||||
tradeManager.closeTrade(getTrade());
|
||||
tradeManager.onCloseTradeRequested(getTrade());
|
||||
|
||||
/*
|
||||
Action response = Popups.openConfirmPopup(
|
||||
|
@ -172,7 +172,7 @@ class CreateOfferDataModel implements Activatable, DataModel {
|
||||
|
||||
void placeOffer() {
|
||||
// data validation is done in the trade domain
|
||||
tradeManager.requestPlaceOffer(offerId,
|
||||
tradeManager.onPlaceOfferRequested(offerId,
|
||||
direction,
|
||||
priceAsFiat.get(),
|
||||
amountAsCoin.get(),
|
||||
|
@ -115,7 +115,7 @@ class OfferBookDataModel implements Activatable, DataModel {
|
||||
}
|
||||
|
||||
void removeOpenOffer(String offerId, ResultHandler resultHandler, ErrorMessageHandler errorMessageHandler) {
|
||||
tradeManager.requestRemoveOpenOffer(offerId, resultHandler, errorMessageHandler);
|
||||
tradeManager.onRemoveOpenOfferRequested(offerId, resultHandler, errorMessageHandler);
|
||||
}
|
||||
|
||||
void calculateVolume() {
|
||||
|
@ -103,7 +103,7 @@ class TakeOfferDataModel implements Activatable, DataModel {
|
||||
@Override
|
||||
public void deactivate() {
|
||||
btcCode.unbind();
|
||||
tradeManager.stopRequestIsOfferAvailableRequest(offer);
|
||||
tradeManager.onGetOfferAvailableStateRequestCanceled(offer);
|
||||
}
|
||||
|
||||
void initWithData(Coin amount, Offer offer) {
|
||||
@ -134,11 +134,11 @@ class TakeOfferDataModel implements Activatable, DataModel {
|
||||
offer.getStateProperty().addListener((observable, oldValue, newValue) -> {
|
||||
offerIsAvailable.set(newValue);
|
||||
});
|
||||
tradeManager.requestIsOfferAvailable(offer);
|
||||
tradeManager.onGetOfferAvailableStateRequested(offer);
|
||||
}
|
||||
|
||||
void takeOffer() {
|
||||
final Trade trade = tradeManager.requestTakeOffer(amountAsCoin.get(), offer);
|
||||
final Trade trade = tradeManager.onTakeOfferRequested(amountAsCoin.get(), offer);
|
||||
trade.stateProperty().addListener((ov, oldValue, newValue) -> {
|
||||
log.debug("trade state = " + newValue);
|
||||
switch (newValue) {
|
||||
|
@ -54,7 +54,7 @@ public class OfferBook {
|
||||
private final User user;
|
||||
|
||||
private final ObservableList<OfferBookListItem> offerBookListItems = FXCollections.observableArrayList();
|
||||
private final OfferBookService.Listener remoteOfferBookListener;
|
||||
private final OfferBookService.Listener offerBookServiceListener;
|
||||
private final ChangeListener<BankAccount> bankAccountChangeListener;
|
||||
private final ChangeListener<Number> invalidationListener;
|
||||
private String fiatCode;
|
||||
@ -75,7 +75,7 @@ public class OfferBook {
|
||||
bankAccountChangeListener = (observableValue, oldValue, newValue) -> setBankAccount(newValue);
|
||||
invalidationListener = (ov, oldValue, newValue) -> requestGetOffers();
|
||||
|
||||
remoteOfferBookListener = new OfferBookService.Listener() {
|
||||
offerBookServiceListener = new OfferBookService.Listener() {
|
||||
@Override
|
||||
public void onOfferAdded(Offer offer) {
|
||||
addOfferToOfferBookListItems(offer);
|
||||
@ -150,14 +150,14 @@ public class OfferBook {
|
||||
private void addListeners() {
|
||||
log.debug("addListeners ");
|
||||
user.currentBankAccountProperty().addListener(bankAccountChangeListener);
|
||||
offerBookService.addListener(remoteOfferBookListener);
|
||||
offerBookService.addListener(offerBookServiceListener);
|
||||
offerBookService.invalidationTimestampProperty().addListener(invalidationListener);
|
||||
}
|
||||
|
||||
private void removeListeners() {
|
||||
log.debug("removeListeners ");
|
||||
user.currentBankAccountProperty().removeListener(bankAccountChangeListener);
|
||||
offerBookService.removeListener(remoteOfferBookListener);
|
||||
offerBookService.removeListener(offerBookServiceListener);
|
||||
offerBookService.invalidationTimestampProperty().removeListener(invalidationListener);
|
||||
}
|
||||
|
||||
|
@ -52,8 +52,6 @@ import java.util.Map;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.beans.property.StringProperty;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableMap;
|
||||
|
||||
@ -91,7 +89,6 @@ public class TradeManager {
|
||||
|
||||
// the latest pending trade
|
||||
private Trade currentPendingTrade;
|
||||
final StringProperty featureNotImplementedWarning = new SimpleStringProperty();
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
@ -141,16 +138,32 @@ public class TradeManager {
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Manage offers
|
||||
// Called from UI
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public void requestPlaceOffer(String id,
|
||||
Direction direction,
|
||||
Fiat price,
|
||||
Coin amount,
|
||||
Coin minAmount,
|
||||
TransactionResultHandler resultHandler,
|
||||
ErrorMessageHandler errorMessageHandler) {
|
||||
public void onGetOfferAvailableStateRequested(Offer offer) {
|
||||
if (!requestIsOfferAvailableProtocolMap.containsKey(offer.getId())) {
|
||||
RequestIsOfferAvailableProtocol protocol = new RequestIsOfferAvailableProtocol(offer, tradeMessageService);
|
||||
requestIsOfferAvailableProtocolMap.put(offer.getId(), protocol);
|
||||
protocol.start();
|
||||
}
|
||||
else {
|
||||
log.warn("requestIsOfferAvailable already called for offer with ID:" + offer.getId());
|
||||
}
|
||||
}
|
||||
|
||||
// When closing take offer view, we are not interested in the requestIsOfferAvailable result anymore, so remove from the map
|
||||
public void onGetOfferAvailableStateRequestCanceled(Offer offer) {
|
||||
requestIsOfferAvailableProtocolMap.remove(offer.getId());
|
||||
}
|
||||
|
||||
public void onPlaceOfferRequested(String id,
|
||||
Direction direction,
|
||||
Fiat price,
|
||||
Coin amount,
|
||||
Coin minAmount,
|
||||
TransactionResultHandler resultHandler,
|
||||
ErrorMessageHandler errorMessageHandler) {
|
||||
|
||||
BankAccount currentBankAccount = user.getCurrentBankAccount().get();
|
||||
Offer offer = new Offer(id,
|
||||
@ -174,147 +187,22 @@ public class TradeManager {
|
||||
walletService,
|
||||
offerBookService,
|
||||
(transaction) -> {
|
||||
createOpenOffer(offer);
|
||||
OpenOffer openOffer = createOpenOffer(offer);
|
||||
createOffererAsBuyerProtocol(openOffer);
|
||||
resultHandler.handleResult(transaction);
|
||||
},
|
||||
(message, throwable) -> errorMessageHandler.handleErrorMessage(message)
|
||||
);
|
||||
|
||||
placeOfferProtocol.placeOffer();
|
||||
placeOfferProtocol.onPlaceOfferRequested();
|
||||
}
|
||||
|
||||
private void createOpenOffer(Offer offer) {
|
||||
OpenOffer openOffer = new OpenOffer(offer);
|
||||
openOffers.put(openOffer.getId(), openOffer);
|
||||
persistOpenOffers();
|
||||
|
||||
createOffererAsBuyerProtocol(openOffer);
|
||||
}
|
||||
|
||||
public void requestRemoveOpenOffer(String offerId, ResultHandler resultHandler, ErrorMessageHandler errorMessageHandler) {
|
||||
offerBookService.removeOffer(openOffers.get(offerId).getOffer(),
|
||||
() -> {
|
||||
if (openOffers.containsKey(offerId)) {
|
||||
openOffers.remove(offerId);
|
||||
persistOpenOffers();
|
||||
resultHandler.handleResult();
|
||||
}
|
||||
else {
|
||||
log.error("Locally stored offers does not contain the offer with the ID " + offerId);
|
||||
errorMessageHandler.handleErrorMessage("Locally stored offers does not contain the offer with the ID " + offerId);
|
||||
}
|
||||
},
|
||||
(message, throwable) -> errorMessageHandler.handleErrorMessage(message));
|
||||
public void onRemoveOpenOfferRequested(String offerId, ResultHandler resultHandler, ErrorMessageHandler errorMessageHandler) {
|
||||
removeOpenOffer(offerId, resultHandler, errorMessageHandler, true);
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Manage trades
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public Trade createTrade(Offer offer) {
|
||||
if (pendingTrades.containsKey(offer.getId()))
|
||||
log.error("trades contains already an trade with the ID " + offer.getId());
|
||||
|
||||
Trade trade = new Trade(offer);
|
||||
pendingTrades.put(offer.getId(), trade);
|
||||
persistPendingTrades();
|
||||
|
||||
currentPendingTrade = trade;
|
||||
|
||||
return trade;
|
||||
}
|
||||
|
||||
public void closeTrade(Trade trade) {
|
||||
if (!pendingTrades.containsKey(trade.getId()))
|
||||
log.error("trades does not contain the trade with the ID " + trade.getId());
|
||||
|
||||
pendingTrades.remove(trade.getId());
|
||||
persistPendingTrades();
|
||||
|
||||
if (takerAsSellerProtocolMap.containsKey(trade.getId()))
|
||||
takerAsSellerProtocolMap.remove(trade.getId());
|
||||
else if (offererAsBuyerProtocolMap.containsKey(trade.getId()))
|
||||
offererAsBuyerProtocolMap.remove(trade.getId());
|
||||
|
||||
closedTrades.put(trade.getId(), trade);
|
||||
persistClosedTrades();
|
||||
}
|
||||
|
||||
private void removeFailedTrade(Trade trade) {
|
||||
if (!pendingTrades.containsKey(trade.getId()))
|
||||
log.error("trades does not contain the trade with the ID " + trade.getId());
|
||||
|
||||
pendingTrades.remove(trade.getId());
|
||||
persistPendingTrades();
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Trading protocols
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
private void createOffererAsBuyerProtocol(OpenOffer openOffer) {
|
||||
BuyerAsOffererModel model = new BuyerAsOffererModel(
|
||||
openOffer,
|
||||
tradeMessageService,
|
||||
walletService,
|
||||
blockChainService,
|
||||
signatureService,
|
||||
user);
|
||||
|
||||
openOffer.stateProperty().addListener((ov, oldValue, newValue) -> {
|
||||
log.debug("trade state = " + newValue);
|
||||
switch (newValue) {
|
||||
case OPEN:
|
||||
break;
|
||||
case OFFER_ACCEPTED:
|
||||
requestRemoveOpenOffer(openOffer.getId(),
|
||||
() -> log.debug("remove offer was successful"),
|
||||
(message) -> log.error(message));
|
||||
|
||||
Trade trade = model.getTrade();
|
||||
pendingTrades.put(trade.getId(), trade);
|
||||
persistPendingTrades();
|
||||
currentPendingTrade = trade;
|
||||
|
||||
// TODO check, remove listener
|
||||
trade.stateProperty().addListener((ov2, oldValue2, newValue2) -> {
|
||||
log.debug("trade state = " + newValue);
|
||||
switch (newValue2) {
|
||||
case OPEN:
|
||||
break;
|
||||
case OFFERER_ACCEPTED: // only taker side
|
||||
case DEPOSIT_PUBLISHED:
|
||||
case DEPOSIT_CONFIRMED:
|
||||
case FIAT_PAYMENT_STARTED:
|
||||
case FIAT_PAYMENT_RECEIVED:
|
||||
case PAYOUT_PUBLISHED:
|
||||
persistPendingTrades();
|
||||
break;
|
||||
case OFFERER_REJECTED:
|
||||
case FAILED:
|
||||
removeFailedTrade(trade);
|
||||
break;
|
||||
default:
|
||||
log.error("Unhandled trade state: " + newValue);
|
||||
break;
|
||||
}
|
||||
});
|
||||
break;
|
||||
default:
|
||||
log.error("Unhandled trade state: " + newValue);
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
BuyerAsOffererProtocol buyerAcceptsOfferProtocol = new BuyerAsOffererProtocol(model);
|
||||
offererAsBuyerProtocolMap.put(openOffer.getId(), buyerAcceptsOfferProtocol);
|
||||
buyerAcceptsOfferProtocol.start();
|
||||
}
|
||||
|
||||
|
||||
public Trade requestTakeOffer(Coin amount, Offer offer) {
|
||||
public Trade onTakeOfferRequested(Coin amount, Offer offer) {
|
||||
Trade trade = createTrade(offer);
|
||||
trade.setTradeAmount(amount);
|
||||
|
||||
@ -353,60 +241,186 @@ public class TradeManager {
|
||||
SellerAsTakerProtocol sellerTakesOfferProtocol = new SellerAsTakerProtocol(model);
|
||||
takerAsSellerProtocolMap.put(trade.getId(), sellerTakesOfferProtocol);
|
||||
|
||||
sellerTakesOfferProtocol.handleRequestTakeOfferUIEvent();
|
||||
sellerTakesOfferProtocol.onTakeOfferRequested();
|
||||
|
||||
return trade;
|
||||
}
|
||||
|
||||
//TODO we don't support interruptions yet.
|
||||
// If the user has shut down the app we lose the offererAsBuyerProtocolMap
|
||||
// Also we don't support yet offline messaging (mail box)
|
||||
public void fiatPaymentStarted(String tradeId) {
|
||||
if (offererAsBuyerProtocolMap.get(tradeId) != null) {
|
||||
offererAsBuyerProtocolMap.get(tradeId).handleBankTransferStartedUIEvent();
|
||||
public void onFiatPaymentStarted(String tradeId) {
|
||||
// TODO remove if check when peristence is impl.
|
||||
if (offererAsBuyerProtocolMap.containsKey(tradeId)) {
|
||||
offererAsBuyerProtocolMap.get(tradeId).onFiatPaymentStarted();
|
||||
persistPendingTrades();
|
||||
}
|
||||
else {
|
||||
featureNotImplementedWarning.set("Sorry, you cannot continue. You have restarted the application in the " +
|
||||
"meantime. Interruption of the trade process is not supported yet. Will need more time to be " +
|
||||
"implemented.");
|
||||
}
|
||||
}
|
||||
|
||||
public void fiatPaymentReceived(String tradeId) {
|
||||
takerAsSellerProtocolMap.get(tradeId).handleFiatReceivedUIEvent();
|
||||
public void onFiatPaymentReceived(String tradeId) {
|
||||
takerAsSellerProtocolMap.get(tradeId).onFiatPaymentReceived();
|
||||
}
|
||||
|
||||
public void requestIsOfferAvailable(Offer offer) {
|
||||
if (!requestIsOfferAvailableProtocolMap.containsKey(offer.getId())) {
|
||||
RequestIsOfferAvailableProtocol protocol = new RequestIsOfferAvailableProtocol(offer, tradeMessageService);
|
||||
requestIsOfferAvailableProtocolMap.put(offer.getId(), protocol);
|
||||
protocol.start();
|
||||
}
|
||||
else {
|
||||
log.warn("requestIsOfferAvailable already called for offer with ID:" + offer.getId());
|
||||
}
|
||||
|
||||
public void onCloseTradeRequested(Trade trade) {
|
||||
closeTrade(trade, false);
|
||||
}
|
||||
|
||||
// When closing take offer view, we are not interested in the requestIsOfferAvailable result anymore, so remove from the map
|
||||
public void stopRequestIsOfferAvailableRequest(Offer offer) {
|
||||
requestIsOfferAvailableProtocolMap.remove(offer.getId());
|
||||
}
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Called from Offerbook (DHT)
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
public void onOfferRemovedFromRemoteOfferBook(Offer offer) {
|
||||
requestIsOfferAvailableProtocolMap.remove(offer.getId());
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Private
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
private OpenOffer createOpenOffer(Offer offer) {
|
||||
OpenOffer openOffer = new OpenOffer(offer);
|
||||
openOffers.put(openOffer.getId(), openOffer);
|
||||
persistOpenOffers();
|
||||
return openOffer;
|
||||
}
|
||||
|
||||
private void removeOpenOffer(String offerId,
|
||||
ResultHandler resultHandler,
|
||||
ErrorMessageHandler errorMessageHandler,
|
||||
boolean removeFromOffererAsBuyerProtocolMap) {
|
||||
offerBookService.removeOffer(openOffers.get(offerId).getOffer(),
|
||||
() -> {
|
||||
if (openOffers.containsKey(offerId)) {
|
||||
openOffers.remove(offerId);
|
||||
persistOpenOffers();
|
||||
if (removeFromOffererAsBuyerProtocolMap && offererAsBuyerProtocolMap.containsKey(offerId)) {
|
||||
offererAsBuyerProtocolMap.get(offerId).cleanup();
|
||||
offererAsBuyerProtocolMap.remove(offerId);
|
||||
}
|
||||
resultHandler.handleResult();
|
||||
}
|
||||
else {
|
||||
log.error("Locally stored offers does not contain the offer with the ID " + offerId);
|
||||
errorMessageHandler.handleErrorMessage("Locally stored offers does not contain the offer with the ID " + offerId);
|
||||
}
|
||||
},
|
||||
(message, throwable) -> errorMessageHandler.handleErrorMessage(message));
|
||||
}
|
||||
|
||||
private Trade createTrade(Offer offer) {
|
||||
if (pendingTrades.containsKey(offer.getId()))
|
||||
log.error("trades contains already an trade with the ID " + offer.getId());
|
||||
|
||||
Trade trade = new Trade(offer);
|
||||
pendingTrades.put(offer.getId(), trade);
|
||||
persistPendingTrades();
|
||||
|
||||
currentPendingTrade = trade;
|
||||
|
||||
return trade;
|
||||
}
|
||||
|
||||
private void createOffererAsBuyerProtocol(OpenOffer openOffer) {
|
||||
BuyerAsOffererModel model = new BuyerAsOffererModel(
|
||||
openOffer,
|
||||
tradeMessageService,
|
||||
walletService,
|
||||
blockChainService,
|
||||
signatureService,
|
||||
user);
|
||||
|
||||
openOffer.stateProperty().addListener((ov, oldValue, newValue) -> {
|
||||
log.debug("trade state = " + newValue);
|
||||
switch (newValue) {
|
||||
case OPEN:
|
||||
break;
|
||||
case OFFER_ACCEPTED:
|
||||
removeOpenOffer(openOffer.getId(),
|
||||
() -> log.debug("remove offer was successful"),
|
||||
(message) -> log.error(message),
|
||||
false);
|
||||
|
||||
Trade trade = model.getTrade();
|
||||
pendingTrades.put(trade.getId(), trade);
|
||||
persistPendingTrades();
|
||||
currentPendingTrade = trade;
|
||||
|
||||
// TODO check, remove listener
|
||||
trade.stateProperty().addListener((ov2, oldValue2, newValue2) -> {
|
||||
log.debug("trade state = " + newValue);
|
||||
switch (newValue2) {
|
||||
case OPEN:
|
||||
break;
|
||||
case OFFERER_ACCEPTED: // only taker side
|
||||
case DEPOSIT_PUBLISHED:
|
||||
case DEPOSIT_CONFIRMED:
|
||||
case FIAT_PAYMENT_STARTED:
|
||||
case FIAT_PAYMENT_RECEIVED:
|
||||
case PAYOUT_PUBLISHED:
|
||||
persistPendingTrades();
|
||||
break;
|
||||
case OFFERER_REJECTED:
|
||||
case FAILED:
|
||||
removeFailedTrade(trade);
|
||||
offererAsBuyerProtocolMap.get(trade.getId()).cleanup();
|
||||
break;
|
||||
default:
|
||||
log.error("Unhandled trade state: " + newValue);
|
||||
break;
|
||||
}
|
||||
});
|
||||
break;
|
||||
default:
|
||||
log.error("Unhandled trade state: " + newValue);
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
BuyerAsOffererProtocol buyerAcceptsOfferProtocol = new BuyerAsOffererProtocol(model);
|
||||
offererAsBuyerProtocolMap.put(openOffer.getId(), buyerAcceptsOfferProtocol);
|
||||
}
|
||||
|
||||
private void removeFailedTrade(Trade trade) {
|
||||
closeTrade(trade, true);
|
||||
}
|
||||
|
||||
private void closeTrade(Trade trade, boolean failed) {
|
||||
if (!pendingTrades.containsKey(trade.getId()))
|
||||
log.error("trades does not contain the trade with the ID " + trade.getId());
|
||||
|
||||
pendingTrades.remove(trade.getId());
|
||||
persistPendingTrades();
|
||||
|
||||
if (takerAsSellerProtocolMap.containsKey(trade.getId())) {
|
||||
takerAsSellerProtocolMap.get(trade.getId()).cleanup();
|
||||
takerAsSellerProtocolMap.remove(trade.getId());
|
||||
}
|
||||
else if (offererAsBuyerProtocolMap.containsKey(trade.getId())) {
|
||||
offererAsBuyerProtocolMap.get(trade.getId()).cleanup();
|
||||
offererAsBuyerProtocolMap.remove(trade.getId());
|
||||
}
|
||||
|
||||
|
||||
if (!failed) {
|
||||
closedTrades.put(trade.getId(), trade);
|
||||
persistClosedTrades();
|
||||
}
|
||||
else {
|
||||
// TODO add failed trades to history
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Process new tradeMessages
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// TODO remove
|
||||
// Routes the incoming messages to the responsible protocol
|
||||
private void handleNewMessage(Message message, Peer sender) {
|
||||
log.trace("handleNewMessage: message = " + message.getClass().getSimpleName());
|
||||
log.trace("handleNewMessage: sender = " + sender);
|
||||
|
||||
// TODO remove
|
||||
|
||||
if (message instanceof OfferMessage) {
|
||||
OfferMessage offerMessage = (OfferMessage) message;
|
||||
// Before starting any take offer activity we check if the offer is still available.
|
||||
@ -441,10 +455,6 @@ public class TradeManager {
|
||||
// Setters
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public void setFeatureNotImplementedWarning(String featureNotImplementedWarning) {
|
||||
this.featureNotImplementedWarning.set(featureNotImplementedWarning);
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Getters
|
||||
@ -466,13 +476,6 @@ public class TradeManager {
|
||||
return currentPendingTrade;
|
||||
}
|
||||
|
||||
public String getFeatureNotImplementedWarning() {
|
||||
return featureNotImplementedWarning.get();
|
||||
}
|
||||
|
||||
public StringProperty featureNotImplementedWarningProperty() {
|
||||
return featureNotImplementedWarning;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Private
|
||||
|
@ -55,7 +55,7 @@ public class PlaceOfferProtocol {
|
||||
this.offerBookService = offerBookService;
|
||||
}
|
||||
|
||||
public void placeOffer() {
|
||||
public void onPlaceOfferRequested() {
|
||||
try {
|
||||
validateOffer();
|
||||
Transaction transaction = createOfferFeeTx();
|
||||
|
@ -45,15 +45,6 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import static io.bitsquare.util.Validator.nonEmptyStringOf;
|
||||
|
||||
/**
|
||||
* Responsible for the correct execution of the sequence of tasks, message passing to the peer and message processing
|
||||
* from the peer.
|
||||
* <p/>
|
||||
* This class handles the role of the offerer as the Bitcoin buyer.
|
||||
* <p/>
|
||||
* It uses sub tasks to not pollute the main class too much with all the async result/fault handling.
|
||||
* Any data from incoming messages need to be validated before further processing.
|
||||
*/
|
||||
public class BuyerAsOffererProtocol {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(BuyerAsOffererProtocol.class);
|
||||
@ -67,6 +58,8 @@ public class BuyerAsOffererProtocol {
|
||||
|
||||
public BuyerAsOffererProtocol(BuyerAsOffererModel model) {
|
||||
this.model = model;
|
||||
|
||||
model.getTradeMessageService().addMessageHandler(this::handleMessage);
|
||||
}
|
||||
|
||||
|
||||
@ -74,10 +67,6 @@ public class BuyerAsOffererProtocol {
|
||||
// Public methods
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public void start() {
|
||||
model.getTradeMessageService().addMessageHandler(this::handleMessage);
|
||||
}
|
||||
|
||||
public void cleanup() {
|
||||
model.getTradeMessageService().removeMessageHandler(this::handleMessage);
|
||||
}
|
||||
@ -118,7 +107,7 @@ public class BuyerAsOffererProtocol {
|
||||
|
||||
BuyerAsOffererTaskRunner<BuyerAsOffererModel> sequence = new BuyerAsOffererTaskRunner<>(model,
|
||||
() -> {
|
||||
log.debug("sequence0 completed");
|
||||
log.debug("sequence at handleRequestTakeOfferMessage completed");
|
||||
},
|
||||
(message, throwable) -> {
|
||||
log.error(message);
|
||||
@ -136,7 +125,7 @@ public class BuyerAsOffererProtocol {
|
||||
|
||||
BuyerAsOffererTaskRunner<BuyerAsOffererModel> sequence = new BuyerAsOffererTaskRunner<>(model,
|
||||
() -> {
|
||||
log.debug("sequence1 completed");
|
||||
log.debug("sequence at handleTakeOfferFeePayedMessage completed");
|
||||
},
|
||||
(message, throwable) -> {
|
||||
log.error(message);
|
||||
@ -155,7 +144,7 @@ public class BuyerAsOffererProtocol {
|
||||
|
||||
BuyerAsOffererTaskRunner<BuyerAsOffererModel> sequence = new BuyerAsOffererTaskRunner<>(model,
|
||||
() -> {
|
||||
log.debug("sequence2 completed");
|
||||
log.debug("sequence at handleRequestOffererPublishDepositTxMessage completed");
|
||||
},
|
||||
(message, throwable) -> {
|
||||
log.error(message);
|
||||
@ -174,14 +163,14 @@ public class BuyerAsOffererProtocol {
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
// UI event handling
|
||||
// Called from UI
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// User clicked the "bank transfer started" button
|
||||
public void handleBankTransferStartedUIEvent() {
|
||||
public void onFiatPaymentStarted() {
|
||||
BuyerAsOffererTaskRunner<BuyerAsOffererModel> sequence = new BuyerAsOffererTaskRunner<>(model,
|
||||
() -> {
|
||||
log.debug("sequence3 completed");
|
||||
log.debug("sequence at handleBankTransferStartedUIEvent completed");
|
||||
},
|
||||
(message, throwable) -> {
|
||||
log.error(message);
|
||||
@ -205,7 +194,7 @@ public class BuyerAsOffererProtocol {
|
||||
|
||||
BuyerAsOffererTaskRunner<BuyerAsOffererModel> sequence = new BuyerAsOffererTaskRunner<>(model,
|
||||
() -> {
|
||||
log.debug("sequence4 completed");
|
||||
log.debug("sequence at handlePayoutTxPublishedMessage completed");
|
||||
},
|
||||
(message, throwable) -> {
|
||||
log.error(message);
|
||||
@ -214,5 +203,4 @@ public class BuyerAsOffererProtocol {
|
||||
sequence.addTasks(ProcessPayoutTxPublishedMessage.class);
|
||||
sequence.run();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -48,13 +48,6 @@ import org.slf4j.LoggerFactory;
|
||||
import static io.bitsquare.util.Validator.nonEmptyStringOf;
|
||||
|
||||
|
||||
/**
|
||||
* Responsible for the correct execution of the sequence of tasks, message passing to the peer and message processing
|
||||
* from the peer.
|
||||
* That class handles the role of the taker as the Bitcoin seller.
|
||||
* It uses sub tasks to not pollute the main class too much with all the async result/fault handling.
|
||||
* Any data from incoming messages as well data used to send to the peer need to be validated before further processing.
|
||||
*/
|
||||
public class SellerAsTakerProtocol {
|
||||
private static final Logger log = LoggerFactory.getLogger(SellerAsTakerProtocol.class);
|
||||
|
||||
@ -71,15 +64,15 @@ public class SellerAsTakerProtocol {
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
// UI event handling
|
||||
// Called from UI
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public void handleRequestTakeOfferUIEvent() {
|
||||
public void onTakeOfferRequested() {
|
||||
model.getTradeMessageService().addMessageHandler(this::handleMessage);
|
||||
|
||||
SellerAsTakerTaskRunner<SellerAsTakerModel> sequence = new SellerAsTakerTaskRunner<>(model,
|
||||
() -> {
|
||||
log.debug("sequence1 completed");
|
||||
log.debug("sequence at handleRequestTakeOfferUIEvent completed");
|
||||
},
|
||||
(message, throwable) -> {
|
||||
log.error(message);
|
||||
@ -128,96 +121,96 @@ public class SellerAsTakerProtocol {
|
||||
private void handleRespondToTakeOfferRequestMessage(RespondToTakeOfferRequestMessage tradeMessage) {
|
||||
model.setTradeMessage(tradeMessage);
|
||||
|
||||
SellerAsTakerTaskRunner<SellerAsTakerModel> sequence2 = new SellerAsTakerTaskRunner<>(model,
|
||||
SellerAsTakerTaskRunner<SellerAsTakerModel> sequence = new SellerAsTakerTaskRunner<>(model,
|
||||
() -> {
|
||||
log.debug("sequence2 completed");
|
||||
log.debug("sequence at handleRespondToTakeOfferRequestMessage completed");
|
||||
},
|
||||
(message, throwable) -> {
|
||||
log.error(message);
|
||||
}
|
||||
);
|
||||
sequence2.addTasks(
|
||||
sequence.addTasks(
|
||||
ProcessRespondToTakeOfferRequestMessage.class,
|
||||
PayTakeOfferFee.class,
|
||||
SendTakeOfferFeePayedMessage.class
|
||||
);
|
||||
sequence2.run();
|
||||
sequence.run();
|
||||
}
|
||||
|
||||
private void handleTakerDepositPaymentRequestMessage(TakerDepositPaymentRequestMessage tradeMessage) {
|
||||
model.setTradeMessage(tradeMessage);
|
||||
|
||||
SellerAsTakerTaskRunner<SellerAsTakerModel> sequence3 = new SellerAsTakerTaskRunner<>(model,
|
||||
SellerAsTakerTaskRunner<SellerAsTakerModel> sequence = new SellerAsTakerTaskRunner<>(model,
|
||||
() -> {
|
||||
log.debug("sequence3 completed");
|
||||
log.debug("sequence at handleTakerDepositPaymentRequestMessage completed");
|
||||
},
|
||||
(message, throwable) -> {
|
||||
log.error(message);
|
||||
}
|
||||
);
|
||||
sequence3.addTasks(
|
||||
sequence.addTasks(
|
||||
ProcessTakerDepositPaymentRequestMessage.class,
|
||||
VerifyOffererAccount.class,
|
||||
CreateAndSignContract.class,
|
||||
PayDeposit.class,
|
||||
SendSignedTakerDepositTxAsHex.class
|
||||
);
|
||||
sequence3.run();
|
||||
sequence.run();
|
||||
}
|
||||
|
||||
private void handleDepositTxPublishedMessage(DepositTxPublishedMessage tradeMessage) {
|
||||
model.setTradeMessage(tradeMessage);
|
||||
|
||||
SellerAsTakerTaskRunner<SellerAsTakerModel> sequence4 = new SellerAsTakerTaskRunner<>(model,
|
||||
SellerAsTakerTaskRunner<SellerAsTakerModel> sequence = new SellerAsTakerTaskRunner<>(model,
|
||||
() -> {
|
||||
log.debug("sequence4 completed");
|
||||
log.debug("sequence at handleDepositTxPublishedMessage completed");
|
||||
},
|
||||
(message, throwable) -> {
|
||||
log.error(message);
|
||||
}
|
||||
);
|
||||
sequence4.addTasks(
|
||||
sequence.addTasks(
|
||||
ProcessDepositTxPublishedMessage.class,
|
||||
TakerCommitDepositTx.class
|
||||
);
|
||||
sequence4.run();
|
||||
sequence.run();
|
||||
}
|
||||
|
||||
private void handleBankTransferInitedMessage(BankTransferInitedMessage tradeMessage) {
|
||||
model.setTradeMessage(tradeMessage);
|
||||
|
||||
SellerAsTakerTaskRunner<SellerAsTakerModel> sequence5 = new SellerAsTakerTaskRunner<>(model,
|
||||
SellerAsTakerTaskRunner<SellerAsTakerModel> sequence = new SellerAsTakerTaskRunner<>(model,
|
||||
() -> {
|
||||
log.debug("sequence5 completed");
|
||||
log.debug("sequence at handleBankTransferInitedMessage completed");
|
||||
model.getTrade().setState(Trade.State.FIAT_PAYMENT_STARTED);
|
||||
},
|
||||
(message, throwable) -> {
|
||||
log.error(message);
|
||||
}
|
||||
);
|
||||
sequence5.addTasks(ProcessBankTransferInitedMessage.class);
|
||||
sequence5.run();
|
||||
sequence.addTasks(ProcessBankTransferInitedMessage.class);
|
||||
sequence.run();
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
// UI event handling
|
||||
// Called from UI
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// User clicked the "bank transfer received" button, so we release the funds for pay out
|
||||
public void handleFiatReceivedUIEvent() {
|
||||
SellerAsTakerTaskRunner<SellerAsTakerModel> sequence6 = new SellerAsTakerTaskRunner<>(model,
|
||||
public void onFiatPaymentReceived() {
|
||||
SellerAsTakerTaskRunner<SellerAsTakerModel> sequence = new SellerAsTakerTaskRunner<>(model,
|
||||
() -> {
|
||||
log.debug("sequence6 completed");
|
||||
log.debug("sequence at handleFiatReceivedUIEvent completed");
|
||||
},
|
||||
(message, throwable) -> {
|
||||
log.error(message);
|
||||
}
|
||||
);
|
||||
sequence6.addTasks(
|
||||
sequence.addTasks(
|
||||
SignAndPublishPayoutTx.class,
|
||||
VerifyOfferFeePayment.class,
|
||||
SendPayoutTxToOfferer.class
|
||||
);
|
||||
sequence6.run();
|
||||
sequence.run();
|
||||
}
|
||||
}
|
||||
|
@ -277,7 +277,7 @@ public class PlaceOfferProtocolTest {
|
||||
countDownLatch.countDown();
|
||||
};
|
||||
PlaceOfferProtocol placeOfferProtocol = getPlaceOfferProtocol(getOffer(), resultHandler, faultHandler);
|
||||
placeOfferProtocol.placeOffer();
|
||||
placeOfferProtocol.onPlaceOfferRequested();
|
||||
countDownLatch.await();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user