use of io.bisq.core.trade.Trade in project bisq-api by mrosseel.
the class MainViewModelHeadless method updateLockedBalance.
private void updateLockedBalance() {
Stream<Trade> lockedTrades = Stream.concat(closedTradableManager.getLockedTradesStream(), failedTradesManager.getLockedTradesStream());
lockedTrades = Stream.concat(lockedTrades, tradeManager.getLockedTradesStream());
Coin sum = Coin.valueOf(lockedTrades.mapToLong(trade -> {
final Optional<AddressEntry> addressEntryOptional = btcWalletService.getAddressEntry(trade.getId(), AddressEntry.Context.MULTI_SIG);
if (addressEntryOptional.isPresent())
return addressEntryOptional.get().getCoinLockedInMultiSig().getValue();
else
return 0;
}).sum());
lockedBalance.set(formatter.formatCoinWithCode(sum));
}
use of io.bisq.core.trade.Trade in project bisq-api by mrosseel.
the class BisqProxy method offerTake.
// / START TODO REFACTOR OFFER TAKE DEPENDENCIES //////////////////////////
public CompletableFuture<Trade> offerTake(String offerId, String paymentAccountId, String amount, boolean useSavingsWallet) {
final CompletableFuture<Trade> futureResult = new CompletableFuture<>();
final Offer offer;
try {
offer = getOffer(offerId);
} catch (NotFoundException e) {
return failFuture(futureResult, e);
}
// check the paymentAccountId is valid
final PaymentAccount paymentAccount = getPaymentAccount(paymentAccountId);
if (paymentAccount == null) {
return failFuture(futureResult, new PaymentAccountNotFoundException("Could not find payment account with id: " + paymentAccountId));
}
// check the paymentAccountId is compatible with the offer
if (!isPaymentAccountValidForOffer(offer, paymentAccount)) {
final String errorMessage = "PaymentAccount is not valid for offer, needs " + offer.getCurrencyCode();
return failFuture(futureResult, new IncompatiblePaymentAccountException(errorMessage));
}
// check the amount is within the range
Coin coinAmount = Coin.valueOf(Long.valueOf(amount));
// workaround because TradeTask does not have an error handler to notify us that something went wrong
if (btcWalletService.getAvailableBalance().isLessThan(coinAmount)) {
final String errorMessage = "Available balance " + btcWalletService.getAvailableBalance() + " is less than needed amount: " + coinAmount;
return failFuture(futureResult, new InsufficientMoneyException(errorMessage));
}
// check that the price is correct ??
// check taker fee
// check security deposit for BTC buyer
// check security deposit for BTC seller
Coin securityDeposit = offer.getDirection() == OfferPayload.Direction.SELL ? offer.getBuyerSecurityDeposit() : offer.getSellerSecurityDeposit();
Coin txFeeFromFeeService = feeService.getTxFee(600);
Coin fundsNeededForTradeTemp = securityDeposit.add(txFeeFromFeeService).add(txFeeFromFeeService);
final Coin fundsNeededForTrade;
if (offer.isBuyOffer())
fundsNeededForTrade = fundsNeededForTradeTemp.add(coinAmount);
else
fundsNeededForTrade = fundsNeededForTradeTemp;
Coin takerFee = getTakerFee(coinAmount);
checkNotNull(txFeeFromFeeService, "txFeeFromFeeService must not be null");
checkNotNull(takerFee, "takerFee must not be null");
tradeManager.onTakeOffer(coinAmount, txFeeFromFeeService, takerFee, isCurrencyForTakerFeeBtc(coinAmount), offer.getPrice().getValue(), fundsNeededForTrade, offer, paymentAccount.getId(), useSavingsWallet, futureResult::complete, error -> futureResult.completeExceptionally(new RuntimeException(error)));
return futureResult;
}
use of io.bisq.core.trade.Trade in project bisq-api by mrosseel.
the class OfferResource method takeOffer.
@ApiOperation(value = "Take offer", response = TradeDetails.class)
@POST
@Path("/{id}/take")
public void takeOffer(@Suspended final AsyncResponse asyncResponse, @PathParam("id") String id, @Valid TakeOffer data) {
// TODO how do we go about not blocking this REST thread?
final CompletableFuture<Trade> completableFuture = bisqProxy.offerTake(id, data.paymentAccountId, data.amount, true);
completableFuture.thenApply(trade -> asyncResponse.resume(new TradeDetails(trade))).exceptionally(e -> {
final Throwable cause = e.getCause();
final Response.ResponseBuilder responseBuilder;
if (cause instanceof ValidationException) {
final int status = 422;
responseBuilder = toValidationErrorResponse(cause, status);
} else if (cause instanceof IncompatiblePaymentAccountException) {
responseBuilder = toValidationErrorResponse(cause, 423);
} else if (cause instanceof NoAcceptedArbitratorException) {
responseBuilder = toValidationErrorResponse(cause, 424);
} else if (cause instanceof PaymentAccountNotFoundException) {
responseBuilder = toValidationErrorResponse(cause, 425);
} else if (cause instanceof InsufficientMoneyException) {
responseBuilder = toValidationErrorResponse(cause, 427);
} else if (cause instanceof NotFoundException) {
responseBuilder = toValidationErrorResponse(cause, 404);
} else {
final String message = cause.getMessage();
responseBuilder = Response.status(500);
if (null != message)
responseBuilder.entity(new ValidationErrorMessage(ImmutableList.of(message)));
log.error("Unable to take offer: " + id + " " + Json.pretty(data), cause);
}
return asyncResponse.resume(responseBuilder.build());
});
}
Aggregations