mirror of
https://github.com/bisq-network/bisq.git
synced 2025-02-23 15:00:30 +01:00
create offer moved to domain
This commit is contained in:
parent
7cac1eceec
commit
b7004d639c
5 changed files with 82 additions and 35 deletions
|
@ -20,7 +20,6 @@ import io.bitsquare.gui.util.BitSquareValidator;
|
||||||
import io.bitsquare.locale.Localisation;
|
import io.bitsquare.locale.Localisation;
|
||||||
import io.bitsquare.settings.Settings;
|
import io.bitsquare.settings.Settings;
|
||||||
import io.bitsquare.trade.Direction;
|
import io.bitsquare.trade.Direction;
|
||||||
import io.bitsquare.trade.Offer;
|
|
||||||
import io.bitsquare.trade.TradeManager;
|
import io.bitsquare.trade.TradeManager;
|
||||||
import io.bitsquare.trade.orderbook.OrderBookFilter;
|
import io.bitsquare.trade.orderbook.OrderBookFilter;
|
||||||
import io.bitsquare.user.Arbitrator;
|
import io.bitsquare.user.Arbitrator;
|
||||||
|
@ -51,7 +50,7 @@ public class CreateOfferController implements Initializable, ChildController, Hi
|
||||||
|
|
||||||
private NavigationController navigationController;
|
private NavigationController navigationController;
|
||||||
private Direction direction;
|
private Direction direction;
|
||||||
private Offer offer;
|
// private Offer offer;
|
||||||
private AddressEntry addressEntry;
|
private AddressEntry addressEntry;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
|
@ -210,7 +209,7 @@ public class CreateOfferController implements Initializable, ChildController, Hi
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Arbitrator arbitrator = settings.getRandomArbitrator(getCollateral(), getAmountAsCoin());
|
Arbitrator arbitrator = settings.getRandomArbitrator(getAmountAsCoin());
|
||||||
if (arbitrator == null)
|
if (arbitrator == null)
|
||||||
{
|
{
|
||||||
Popups.openWarningPopup("No arbitrator available", "No arbitrator from your arbitrator list does match the collateral and amount value.");
|
Popups.openWarningPopup("No arbitrator available", "No arbitrator from your arbitrator list does match the collateral and amount value.");
|
||||||
|
@ -227,32 +226,21 @@ public class CreateOfferController implements Initializable, ChildController, Hi
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@FXML
|
||||||
public void onPlaceOffer()
|
public void onPlaceOffer()
|
||||||
{
|
{
|
||||||
if (inputsValid())
|
if (inputsValid())
|
||||||
{
|
{
|
||||||
placeOfferButton.setDisable(true);
|
placeOfferButton.setDisable(true);
|
||||||
|
|
||||||
double collateral = getCollateral();
|
double price = BitSquareConverter.stringToDouble(priceTextField.getText());
|
||||||
Arbitrator arbitrator = settings.getRandomArbitrator(collateral, getAmountAsCoin());
|
Coin amount = BitSquareFormatter.parseBtcToCoin(getAmountString());
|
||||||
Coin amountAsCoin = BitSquareFormatter.parseBtcToCoin(getAmountString());
|
Coin minAmount = BitSquareFormatter.parseBtcToCoin(getMinAmountString());
|
||||||
Coin minAmountAsCoin = BitSquareFormatter.parseBtcToCoin(getMinAmountString());
|
|
||||||
|
|
||||||
offer = new Offer(user.getMessagePublicKey(),
|
tradeManager.requestPlaceOffer(direction,
|
||||||
direction,
|
price,
|
||||||
BitSquareConverter.stringToDouble(priceTextField.getText()),
|
amount,
|
||||||
amountAsCoin,
|
minAmount,
|
||||||
minAmountAsCoin,
|
|
||||||
user.getCurrentBankAccount().getBankAccountType(),
|
|
||||||
user.getCurrentBankAccount().getCurrency(),
|
|
||||||
user.getCurrentBankAccount().getCountry(),
|
|
||||||
user.getCurrentBankAccount().getUid(),
|
|
||||||
arbitrator,
|
|
||||||
collateral,
|
|
||||||
settings.getAcceptedCountries(),
|
|
||||||
settings.getAcceptedLanguageLocales());
|
|
||||||
|
|
||||||
tradeManager.requestPlaceOffer(offer,
|
|
||||||
(transactionId) -> setupSuccessScreen(transactionId),
|
(transactionId) -> setupSuccessScreen(transactionId),
|
||||||
errorMessage -> {
|
errorMessage -> {
|
||||||
Popups.openErrorPopup("An error occurred", errorMessage);
|
Popups.openErrorPopup("An error occurred", errorMessage);
|
||||||
|
|
|
@ -106,9 +106,7 @@ public class Settings implements Serializable
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO
|
//TODO
|
||||||
@SuppressWarnings("UnusedParameters")
|
public Arbitrator getRandomArbitrator(Coin amount)
|
||||||
|
|
||||||
public Arbitrator getRandomArbitrator(@SuppressWarnings("UnusedParameters") double collateral, @SuppressWarnings("UnusedParameters") Coin amount)
|
|
||||||
{
|
{
|
||||||
List<Arbitrator> candidates = new ArrayList<>();
|
List<Arbitrator> candidates = new ArrayList<>();
|
||||||
//noinspection Convert2streamapi
|
//noinspection Convert2streamapi
|
||||||
|
|
|
@ -10,6 +10,7 @@ import io.bitsquare.crypto.CryptoFacade;
|
||||||
import io.bitsquare.gui.popups.Popups;
|
import io.bitsquare.gui.popups.Popups;
|
||||||
import io.bitsquare.msg.MessageFacade;
|
import io.bitsquare.msg.MessageFacade;
|
||||||
import io.bitsquare.msg.listeners.TakeOfferRequestListener;
|
import io.bitsquare.msg.listeners.TakeOfferRequestListener;
|
||||||
|
import io.bitsquare.settings.Settings;
|
||||||
import io.bitsquare.storage.Persistence;
|
import io.bitsquare.storage.Persistence;
|
||||||
import io.bitsquare.trade.handlers.ErrorMessageHandler;
|
import io.bitsquare.trade.handlers.ErrorMessageHandler;
|
||||||
import io.bitsquare.trade.handlers.PublishTransactionResultHandler;
|
import io.bitsquare.trade.handlers.PublishTransactionResultHandler;
|
||||||
|
@ -36,6 +37,7 @@ public class TradeManager
|
||||||
private static final Logger log = LoggerFactory.getLogger(TradeManager.class);
|
private static final Logger log = LoggerFactory.getLogger(TradeManager.class);
|
||||||
|
|
||||||
private final User user;
|
private final User user;
|
||||||
|
private Settings settings;
|
||||||
private final Persistence persistence;
|
private final Persistence persistence;
|
||||||
private final MessageFacade messageFacade;
|
private final MessageFacade messageFacade;
|
||||||
private final BlockChainFacade blockChainFacade;
|
private final BlockChainFacade blockChainFacade;
|
||||||
|
@ -62,9 +64,10 @@ public class TradeManager
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public TradeManager(User user, Persistence persistence, MessageFacade messageFacade, BlockChainFacade blockChainFacade, WalletFacade walletFacade, CryptoFacade cryptoFacade)
|
public TradeManager(User user, Settings settings, Persistence persistence, MessageFacade messageFacade, BlockChainFacade blockChainFacade, WalletFacade walletFacade, CryptoFacade cryptoFacade)
|
||||||
{
|
{
|
||||||
this.user = user;
|
this.user = user;
|
||||||
|
this.settings = settings;
|
||||||
this.persistence = persistence;
|
this.persistence = persistence;
|
||||||
this.messageFacade = messageFacade;
|
this.messageFacade = messageFacade;
|
||||||
this.blockChainFacade = blockChainFacade;
|
this.blockChainFacade = blockChainFacade;
|
||||||
|
@ -124,14 +127,36 @@ public class TradeManager
|
||||||
// Manage offers
|
// Manage offers
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
public void requestPlaceOffer(Offer offer, PublishTransactionResultHandler resultHandler, ErrorMessageHandler errorMessageHandler)
|
public void requestPlaceOffer(Direction direction,
|
||||||
|
double price,
|
||||||
|
Coin amount,
|
||||||
|
Coin minAmount,
|
||||||
|
PublishTransactionResultHandler resultHandler,
|
||||||
|
ErrorMessageHandler errorMessageHandler)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
Offer offer = new Offer(user.getMessagePublicKey(),
|
||||||
|
direction,
|
||||||
|
price,
|
||||||
|
amount,
|
||||||
|
minAmount,
|
||||||
|
user.getCurrentBankAccount().getBankAccountType(),
|
||||||
|
user.getCurrentBankAccount().getCurrency(),
|
||||||
|
user.getCurrentBankAccount().getCountry(),
|
||||||
|
user.getCurrentBankAccount().getUid(),
|
||||||
|
settings.getRandomArbitrator(amount),
|
||||||
|
settings.getCollateral(),
|
||||||
|
settings.getAcceptedCountries(),
|
||||||
|
settings.getAcceptedLanguageLocales());
|
||||||
|
|
||||||
if (createOfferCoordinatorMap.containsKey(offer.getId()))
|
if (createOfferCoordinatorMap.containsKey(offer.getId()))
|
||||||
{
|
{
|
||||||
errorMessageHandler.onFault("A createOfferCoordinator for the offer with the id " + offer.getId() + " already exists.");
|
errorMessageHandler.onFault("A createOfferCoordinator for the offer with the id " + offer.getId() + " already exists.");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
CreateOfferCoordinator createOfferCoordinator = new CreateOfferCoordinator(offer, walletFacade, messageFacade);
|
CreateOfferCoordinator createOfferCoordinator = new CreateOfferCoordinator(offer, walletFacade, messageFacade);
|
||||||
createOfferCoordinatorMap.put(offer.getId(), createOfferCoordinator);
|
createOfferCoordinatorMap.put(offer.getId(), createOfferCoordinator);
|
||||||
createOfferCoordinator.start(
|
createOfferCoordinator.start(
|
||||||
|
|
|
@ -7,6 +7,7 @@ import io.bitsquare.trade.handlers.FaultHandler;
|
||||||
import io.bitsquare.trade.handlers.PublishTransactionResultHandler;
|
import io.bitsquare.trade.handlers.PublishTransactionResultHandler;
|
||||||
import io.bitsquare.trade.protocol.createoffer.tasks.PayOfferFee;
|
import io.bitsquare.trade.protocol.createoffer.tasks.PayOfferFee;
|
||||||
import io.bitsquare.trade.protocol.createoffer.tasks.PublishOfferToDHT;
|
import io.bitsquare.trade.protocol.createoffer.tasks.PublishOfferToDHT;
|
||||||
|
import io.bitsquare.trade.protocol.createoffer.tasks.ValidateOffer;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -17,7 +18,7 @@ import org.slf4j.LoggerFactory;
|
||||||
//TODO recover policy, timer
|
//TODO recover policy, timer
|
||||||
public class CreateOfferCoordinator
|
public class CreateOfferCoordinator
|
||||||
{
|
{
|
||||||
private enum State
|
public enum State
|
||||||
{
|
{
|
||||||
INIT,
|
INIT,
|
||||||
OFFER_FEE_PAID,
|
OFFER_FEE_PAID,
|
||||||
|
@ -26,16 +27,17 @@ public class CreateOfferCoordinator
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(CreateOfferCoordinator.class);
|
private static final Logger log = LoggerFactory.getLogger(CreateOfferCoordinator.class);
|
||||||
|
|
||||||
public final Offer offer;
|
private final Offer offer;
|
||||||
private final WalletFacade walletFacade;
|
private final WalletFacade walletFacade;
|
||||||
private final MessageFacade messageFacade;
|
private final MessageFacade messageFacade;
|
||||||
private PublishTransactionResultHandler resultHandler;
|
private PublishTransactionResultHandler resultHandler;
|
||||||
private FaultHandler faultHandler;
|
private FaultHandler faultHandler;
|
||||||
|
|
||||||
|
|
||||||
private String transactionId;
|
|
||||||
private State state;
|
private State state;
|
||||||
|
|
||||||
|
// result
|
||||||
|
private String transactionId;
|
||||||
|
|
||||||
public CreateOfferCoordinator(Offer offer, WalletFacade walletFacade, MessageFacade messageFacade)
|
public CreateOfferCoordinator(Offer offer, WalletFacade walletFacade, MessageFacade messageFacade)
|
||||||
{
|
{
|
||||||
this.offer = offer;
|
this.offer = offer;
|
||||||
|
@ -49,10 +51,13 @@ public class CreateOfferCoordinator
|
||||||
this.faultHandler = faultHandler;
|
this.faultHandler = faultHandler;
|
||||||
|
|
||||||
state = State.INIT;
|
state = State.INIT;
|
||||||
|
ValidateOffer.run(this::onOfferValidated, this::onFailed, walletFacade, offer);
|
||||||
PayOfferFee.run(this::onOfferFeePaid, this::onFailed, walletFacade, offer);
|
PayOfferFee.run(this::onOfferFeePaid, this::onFailed, walletFacade, offer);
|
||||||
}
|
}
|
||||||
|
private void onOfferValidated()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
public void recover(State lastState, String transactionId, PublishTransactionResultHandler resultHandler, FaultHandler faultHandler)
|
public void recover(State lastState, String transactionId, PublishTransactionResultHandler resultHandler, FaultHandler faultHandler)
|
||||||
{
|
{
|
||||||
this.transactionId = transactionId;
|
this.transactionId = transactionId;
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
package io.bitsquare.trade.protocol.createoffer.tasks;
|
||||||
|
|
||||||
|
import io.bitsquare.trade.Offer;
|
||||||
|
import io.bitsquare.trade.handlers.ErrorMessageHandler;
|
||||||
|
import io.bitsquare.trade.handlers.ResultHandler;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
public class ValidateOffer
|
||||||
|
{
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(ValidateOffer.class);
|
||||||
|
|
||||||
|
public static void run(ResultHandler resultHandler, ErrorMessageHandler faultHandler, Offer offer)
|
||||||
|
{
|
||||||
|
boolean isValid = offer.getAmount().isGreaterThan(offer.getAmount());
|
||||||
|
|
||||||
|
if (offer.getAmount().compareTo(offer.getMinAmount()) >= 0)
|
||||||
|
{
|
||||||
|
faultHandler.onFault("Offer validation failed: Min. amount is larger than amount.");
|
||||||
|
|
||||||
|
}
|
||||||
|
else if (offer.getAcceptedCountries() == null || offer.getAcceptedCountries().size() == 0)
|
||||||
|
{
|
||||||
|
faultHandler.onFault("Offer validation failed: No accepted countries are defined");
|
||||||
|
} //TODO...
|
||||||
|
else
|
||||||
|
{
|
||||||
|
resultHandler.onResult();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue