mirror of
https://github.com/bisq-network/bisq.git
synced 2025-02-22 14:42:37 +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.settings.Settings;
|
||||
import io.bitsquare.trade.Direction;
|
||||
import io.bitsquare.trade.Offer;
|
||||
import io.bitsquare.trade.TradeManager;
|
||||
import io.bitsquare.trade.orderbook.OrderBookFilter;
|
||||
import io.bitsquare.user.Arbitrator;
|
||||
|
@ -51,7 +50,7 @@ public class CreateOfferController implements Initializable, ChildController, Hi
|
|||
|
||||
private NavigationController navigationController;
|
||||
private Direction direction;
|
||||
private Offer offer;
|
||||
// private Offer offer;
|
||||
private AddressEntry addressEntry;
|
||||
|
||||
@FXML
|
||||
|
@ -210,7 +209,7 @@ public class CreateOfferController implements Initializable, ChildController, Hi
|
|||
return false;
|
||||
}
|
||||
|
||||
Arbitrator arbitrator = settings.getRandomArbitrator(getCollateral(), getAmountAsCoin());
|
||||
Arbitrator arbitrator = settings.getRandomArbitrator(getAmountAsCoin());
|
||||
if (arbitrator == null)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void onPlaceOffer()
|
||||
{
|
||||
if (inputsValid())
|
||||
{
|
||||
placeOfferButton.setDisable(true);
|
||||
|
||||
double price = BitSquareConverter.stringToDouble(priceTextField.getText());
|
||||
Coin amount = BitSquareFormatter.parseBtcToCoin(getAmountString());
|
||||
Coin minAmount = BitSquareFormatter.parseBtcToCoin(getMinAmountString());
|
||||
|
||||
double collateral = getCollateral();
|
||||
Arbitrator arbitrator = settings.getRandomArbitrator(collateral, getAmountAsCoin());
|
||||
Coin amountAsCoin = BitSquareFormatter.parseBtcToCoin(getAmountString());
|
||||
Coin minAmountAsCoin = BitSquareFormatter.parseBtcToCoin(getMinAmountString());
|
||||
|
||||
offer = new Offer(user.getMessagePublicKey(),
|
||||
direction,
|
||||
BitSquareConverter.stringToDouble(priceTextField.getText()),
|
||||
amountAsCoin,
|
||||
minAmountAsCoin,
|
||||
user.getCurrentBankAccount().getBankAccountType(),
|
||||
user.getCurrentBankAccount().getCurrency(),
|
||||
user.getCurrentBankAccount().getCountry(),
|
||||
user.getCurrentBankAccount().getUid(),
|
||||
arbitrator,
|
||||
collateral,
|
||||
settings.getAcceptedCountries(),
|
||||
settings.getAcceptedLanguageLocales());
|
||||
|
||||
tradeManager.requestPlaceOffer(offer,
|
||||
tradeManager.requestPlaceOffer(direction,
|
||||
price,
|
||||
amount,
|
||||
minAmount,
|
||||
(transactionId) -> setupSuccessScreen(transactionId),
|
||||
errorMessage -> {
|
||||
Popups.openErrorPopup("An error occurred", errorMessage);
|
||||
|
|
|
@ -106,9 +106,7 @@ public class Settings implements Serializable
|
|||
}
|
||||
|
||||
//TODO
|
||||
@SuppressWarnings("UnusedParameters")
|
||||
|
||||
public Arbitrator getRandomArbitrator(@SuppressWarnings("UnusedParameters") double collateral, @SuppressWarnings("UnusedParameters") Coin amount)
|
||||
public Arbitrator getRandomArbitrator(Coin amount)
|
||||
{
|
||||
List<Arbitrator> candidates = new ArrayList<>();
|
||||
//noinspection Convert2streamapi
|
||||
|
|
|
@ -10,6 +10,7 @@ import io.bitsquare.crypto.CryptoFacade;
|
|||
import io.bitsquare.gui.popups.Popups;
|
||||
import io.bitsquare.msg.MessageFacade;
|
||||
import io.bitsquare.msg.listeners.TakeOfferRequestListener;
|
||||
import io.bitsquare.settings.Settings;
|
||||
import io.bitsquare.storage.Persistence;
|
||||
import io.bitsquare.trade.handlers.ErrorMessageHandler;
|
||||
import io.bitsquare.trade.handlers.PublishTransactionResultHandler;
|
||||
|
@ -36,6 +37,7 @@ public class TradeManager
|
|||
private static final Logger log = LoggerFactory.getLogger(TradeManager.class);
|
||||
|
||||
private final User user;
|
||||
private Settings settings;
|
||||
private final Persistence persistence;
|
||||
private final MessageFacade messageFacade;
|
||||
private final BlockChainFacade blockChainFacade;
|
||||
|
@ -62,9 +64,10 @@ public class TradeManager
|
|||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@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.settings = settings;
|
||||
this.persistence = persistence;
|
||||
this.messageFacade = messageFacade;
|
||||
this.blockChainFacade = blockChainFacade;
|
||||
|
@ -124,14 +127,36 @@ public class TradeManager
|
|||
// 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()))
|
||||
{
|
||||
errorMessageHandler.onFault("A createOfferCoordinator for the offer with the id " + offer.getId() + " already exists.");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
|
||||
CreateOfferCoordinator createOfferCoordinator = new CreateOfferCoordinator(offer, walletFacade, messageFacade);
|
||||
createOfferCoordinatorMap.put(offer.getId(), createOfferCoordinator);
|
||||
createOfferCoordinator.start(
|
||||
|
|
|
@ -7,6 +7,7 @@ import io.bitsquare.trade.handlers.FaultHandler;
|
|||
import io.bitsquare.trade.handlers.PublishTransactionResultHandler;
|
||||
import io.bitsquare.trade.protocol.createoffer.tasks.PayOfferFee;
|
||||
import io.bitsquare.trade.protocol.createoffer.tasks.PublishOfferToDHT;
|
||||
import io.bitsquare.trade.protocol.createoffer.tasks.ValidateOffer;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
@ -17,7 +18,7 @@ import org.slf4j.LoggerFactory;
|
|||
//TODO recover policy, timer
|
||||
public class CreateOfferCoordinator
|
||||
{
|
||||
private enum State
|
||||
public enum State
|
||||
{
|
||||
INIT,
|
||||
OFFER_FEE_PAID,
|
||||
|
@ -26,16 +27,17 @@ public class CreateOfferCoordinator
|
|||
|
||||
private static final Logger log = LoggerFactory.getLogger(CreateOfferCoordinator.class);
|
||||
|
||||
public final Offer offer;
|
||||
private final Offer offer;
|
||||
private final WalletFacade walletFacade;
|
||||
private final MessageFacade messageFacade;
|
||||
private PublishTransactionResultHandler resultHandler;
|
||||
private FaultHandler faultHandler;
|
||||
|
||||
|
||||
private String transactionId;
|
||||
private State state;
|
||||
|
||||
// result
|
||||
private String transactionId;
|
||||
|
||||
public CreateOfferCoordinator(Offer offer, WalletFacade walletFacade, MessageFacade messageFacade)
|
||||
{
|
||||
this.offer = offer;
|
||||
|
@ -49,10 +51,13 @@ public class CreateOfferCoordinator
|
|||
this.faultHandler = faultHandler;
|
||||
|
||||
state = State.INIT;
|
||||
|
||||
ValidateOffer.run(this::onOfferValidated, 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)
|
||||
{
|
||||
this.transactionId = transactionId;
|
||||
|
@ -91,7 +96,7 @@ public class CreateOfferCoordinator
|
|||
private void onFailed(String message, Throwable throwable)
|
||||
{
|
||||
//TODO recover policy, timer
|
||||
|
||||
|
||||
faultHandler.onFault(message, throwable);
|
||||
}
|
||||
|
||||
|
|
|
@ -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