use of bisq.common.handlers.ResultHandler in project bisq-core by bisq-network.
the class Offer method checkOfferAvailability.
// /////////////////////////////////////////////////////////////////////////////////////////
// Availability
// /////////////////////////////////////////////////////////////////////////////////////////
public void checkOfferAvailability(OfferAvailabilityModel model, ResultHandler resultHandler, ErrorMessageHandler errorMessageHandler) {
availabilityProtocol = new OfferAvailabilityProtocol(model, () -> {
cancelAvailabilityRequest();
resultHandler.handleResult();
}, (errorMessage) -> {
if (availabilityProtocol != null)
availabilityProtocol.cancel();
log.error(errorMessage);
errorMessageHandler.handleErrorMessage(errorMessage);
});
availabilityProtocol.sendOfferAvailabilityRequest();
}
use of bisq.common.handlers.ResultHandler in project bisq-desktop by bisq-network.
the class BuyerStep4View method doWithdrawal.
private void doWithdrawal(Coin amount, Coin fee) {
String toAddress = withdrawAddressTextField.getText();
ResultHandler resultHandler = this::handleTradeCompleted;
FaultHandler faultHandler = (errorMessage, throwable) -> {
useSavingsWalletButton.setDisable(false);
withdrawToExternalWalletButton.setDisable(false);
if (throwable != null && throwable.getMessage() != null)
new Popup<>().error(errorMessage + "\n\n" + throwable.getMessage()).show();
else
new Popup<>().error(errorMessage).show();
};
if (model.dataModel.btcWalletService.isEncrypted()) {
UserThread.runAfter(() -> model.dataModel.walletPasswordWindow.onAesKey(aesKey -> doWithdrawRequest(toAddress, amount, fee, aesKey, resultHandler, faultHandler)).show(), 300, TimeUnit.MILLISECONDS);
} else
doWithdrawRequest(toAddress, amount, fee, null, resultHandler, faultHandler);
}
use of bisq.common.handlers.ResultHandler in project bisq-api by mrosseel.
the class BisqProxy method paymentReceived.
public CompletableFuture<Void> paymentReceived(String tradeId) {
final CompletableFuture<Void> futureResult = new CompletableFuture<>();
Trade trade;
try {
trade = getTrade(tradeId);
} catch (NotFoundException e) {
return failFuture(futureResult, e);
}
if (!Trade.State.SELLER_RECEIVED_FIAT_PAYMENT_INITIATED_MSG.equals(trade.getState())) {
return failFuture(futureResult, new ValidationException("Trade is not in the correct state to receive payment: " + trade.getState()));
}
TradeProtocol tradeProtocol = trade.getTradeProtocol();
if (!(tradeProtocol instanceof SellerAsTakerProtocol || tradeProtocol instanceof SellerAsMakerProtocol)) {
return failFuture(futureResult, new ValidationException("Trade is not in the correct state to receive payment: " + trade.getState()));
}
ResultHandler resultHandler = () -> futureResult.complete(null);
ErrorMessageHandler errorResultHandler = message -> futureResult.completeExceptionally(new RuntimeException(message));
// TODO I think we should check instance of tradeProtocol here instead of trade
if (trade instanceof SellerAsMakerTrade) {
((SellerAsMakerProtocol) tradeProtocol).onFiatPaymentReceived(resultHandler, errorResultHandler);
} else {
((SellerAsTakerProtocol) tradeProtocol).onFiatPaymentReceived(resultHandler, errorResultHandler);
}
return futureResult;
}
use of bisq.common.handlers.ResultHandler in project bisq-core by bisq-network.
the class WalletsSetup method initialize.
// /////////////////////////////////////////////////////////////////////////////////////////
// Lifecycle
// /////////////////////////////////////////////////////////////////////////////////////////
public void initialize(@Nullable DeterministicSeed seed, ResultHandler resultHandler, ExceptionHandler exceptionHandler) {
Log.traceCall();
// Tell bitcoinj to execute event handlers on the JavaFX UI thread. This keeps things simple and means
// we cannot forget to switch threads when adding event handlers. Unfortunately, the DownloadListener
// we give to the app kit is currently an exception and runs on a library thread. It'll get fixed in
// a future version.
Threading.USER_THREAD = UserThread.getExecutor();
Timer timeoutTimer = UserThread.runAfter(() -> exceptionHandler.handleException(new TimeoutException("Wallet did not initialize in " + STARTUP_TIMEOUT + " seconds.")), STARTUP_TIMEOUT);
backupWallets();
final Socks5Proxy socks5Proxy = preferences.getUseTorForBitcoinJ() ? socks5ProxyProvider.getSocks5Proxy() : null;
log.info("Socks5Proxy for bitcoinj: socks5Proxy=" + socks5Proxy);
walletConfig = new WalletConfig(params, socks5Proxy, walletDir, bisqEnvironment, userAgent, numConnectionForBtc, btcWalletFileName, BSQ_WALLET_FILE_NAME, SPV_CHAIN_FILE_NAME) {
@Override
protected void onSetupCompleted() {
// We are here in the btcj thread Thread[ STARTING,5,main]
super.onSetupCompleted();
final PeerGroup peerGroup = walletConfig.peerGroup();
// We don't want to get our node white list polluted with nodes from AddressMessage calls.
if (preferences.getBitcoinNodes() != null && !preferences.getBitcoinNodes().isEmpty())
peerGroup.setAddPeersFromAddressMessage(false);
peerGroup.addConnectedEventListener((peer, peerCount) -> {
// We get called here on our user thread
numPeers.set(peerCount);
connectedPeers.set(peerGroup.getConnectedPeers());
});
peerGroup.addDisconnectedEventListener((peer, peerCount) -> {
// We get called here on our user thread
numPeers.set(peerCount);
connectedPeers.set(peerGroup.getConnectedPeers());
});
// Map to user thread
UserThread.execute(() -> {
addressEntryList.onWalletReady(walletConfig.getBtcWallet());
timeoutTimer.stop();
setupCompletedHandlers.stream().forEach(Runnable::run);
});
// onSetupCompleted in walletAppKit is not the called on the last invocations, so we add a bit of delay
UserThread.runAfter(resultHandler::handleResult, 100, TimeUnit.MILLISECONDS);
}
};
if (params == RegTestParams.get()) {
walletConfig.setMinBroadcastConnections(1);
if (regTestHost == RegTestHost.LOCALHOST) {
walletConfig.setPeerNodesForLocalHost();
} else if (regTestHost == RegTestHost.REG_TEST_SERVER) {
walletConfig.setMinBroadcastConnections(1);
configPeerNodesForRegTestServer();
} else {
configPeerNodes(socks5Proxy);
}
} else if (bisqEnvironment.isBitcoinLocalhostNodeRunning()) {
walletConfig.setMinBroadcastConnections(1);
walletConfig.setPeerNodesForLocalHost();
} else {
configPeerNodes(socks5Proxy);
}
walletConfig.setDownloadListener(downloadListener).setBlockingStartup(false);
// If seed is non-null it means we are restoring from backup.
walletConfig.setSeed(seed);
walletConfig.addListener(new Service.Listener() {
@Override
public void failed(@NotNull Service.State from, @NotNull Throwable failure) {
walletConfig = null;
log.error("Service failure from state: {}; failure={}", from, failure);
timeoutTimer.stop();
UserThread.execute(() -> exceptionHandler.handleException(failure));
}
}, Threading.USER_THREAD);
walletConfig.startAsync();
}
use of bisq.common.handlers.ResultHandler in project bisq-api by mrosseel.
the class BisqProxy method paymentStarted.
public CompletableFuture<Void> paymentStarted(String tradeId) {
final CompletableFuture<Void> futureResult = new CompletableFuture<>();
Trade trade;
try {
trade = getTrade(tradeId);
} catch (NotFoundException e) {
return failFuture(futureResult, e);
}
if (!Trade.State.DEPOSIT_CONFIRMED_IN_BLOCK_CHAIN.equals(trade.getState())) {
return failFuture(futureResult, new ValidationException("Trade is not in the correct state to start payment: " + trade.getState()));
}
TradeProtocol tradeProtocol = trade.getTradeProtocol();
ResultHandler resultHandler = () -> futureResult.complete(null);
ErrorMessageHandler errorResultHandler = message -> futureResult.completeExceptionally(new RuntimeException(message));
if (trade instanceof BuyerAsMakerTrade) {
((BuyerAsMakerProtocol) tradeProtocol).onFiatPaymentStarted(resultHandler, errorResultHandler);
} else {
((BuyerAsTakerProtocol) tradeProtocol).onFiatPaymentStarted(resultHandler, errorResultHandler);
}
return futureResult;
}
Aggregations