Search in sources :

Example 6 with Trade

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));
}
Also used : Trade(io.bisq.core.trade.Trade) Coin(org.bitcoinj.core.Coin) AddressEntry(io.bisq.core.btc.AddressEntry)

Example 7 with Trade

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;
}
Also used : PaymentAccount(io.bisq.core.payment.PaymentAccount) SellerAsMakerTrade(io.bisq.core.trade.SellerAsMakerTrade) Trade(io.bisq.core.trade.Trade) BuyerAsMakerTrade(io.bisq.core.trade.BuyerAsMakerTrade) Coin(org.bitcoinj.core.Coin) CompletableFuture(java.util.concurrent.CompletableFuture) PaymentAccountUtil.isPaymentAccountValidForOffer(io.bisq.core.payment.PaymentAccountUtil.isPaymentAccountValidForOffer)

Example 8 with Trade

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());
    });
}
Also used : io.bisq.api(io.bisq.api) AsyncResponse(javax.ws.rs.container.AsyncResponse) Json(io.swagger.util.Json) CompletableFuture(java.util.concurrent.CompletableFuture) Suspended(javax.ws.rs.container.Suspended) Offer(io.bisq.core.offer.Offer) Valid(javax.validation.Valid) ApiOperation(io.swagger.annotations.ApiOperation) ResourceHelper(io.bisq.api.service.ResourceHelper) Slf4j(lombok.extern.slf4j.Slf4j) MediaType(javax.ws.rs.core.MediaType) Collectors.toList(java.util.stream.Collectors.toList) Trade(io.bisq.core.trade.Trade) ImmutableList(com.google.common.collect.ImmutableList) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) ValidationException(javax.validation.ValidationException) NotFoundException(io.bisq.api.NotFoundException) io.bisq.api.model(io.bisq.api.model) NotEmpty(org.hibernate.validator.constraints.NotEmpty) ResourceHelper.toValidationErrorResponse(io.bisq.api.service.ResourceHelper.toValidationErrorResponse) ValidationErrorMessage(io.dropwizard.jersey.validation.ValidationErrorMessage) Api(io.swagger.annotations.Api) ValidationException(javax.validation.ValidationException) NotFoundException(io.bisq.api.NotFoundException) ValidationErrorMessage(io.dropwizard.jersey.validation.ValidationErrorMessage) AsyncResponse(javax.ws.rs.container.AsyncResponse) Response(javax.ws.rs.core.Response) ResourceHelper.toValidationErrorResponse(io.bisq.api.service.ResourceHelper.toValidationErrorResponse) Trade(io.bisq.core.trade.Trade) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

Trade (io.bisq.core.trade.Trade)8 Coin (org.bitcoinj.core.Coin)6 AddressEntry (io.bisq.core.btc.AddressEntry)5 BuyerAsMakerTrade (io.bisq.core.trade.BuyerAsMakerTrade)5 SellerAsMakerTrade (io.bisq.core.trade.SellerAsMakerTrade)5 Slf4j (lombok.extern.slf4j.Slf4j)4 io.bisq.api.model (io.bisq.api.model)3 DevEnv (io.bisq.common.app.DevEnv)3 KeyRing (io.bisq.common.crypto.KeyRing)3 CurrencyUtil (io.bisq.common.locale.CurrencyUtil)3 TradeCurrency (io.bisq.common.locale.TradeCurrency)3 BisqEnvironment (io.bisq.core.app.BisqEnvironment)3 ArbitratorManager (io.bisq.core.arbitration.ArbitratorManager)3 BtcWalletService (io.bisq.core.btc.wallet.BtcWalletService)3 WalletsSetup (io.bisq.core.btc.wallet.WalletsSetup)3 AccountAgeWitnessService (io.bisq.core.payment.AccountAgeWitnessService)3 CryptoCurrencyAccount (io.bisq.core.payment.CryptoCurrencyAccount)3 FeeService (io.bisq.core.provider.fee.FeeService)3 TradeManager (io.bisq.core.trade.TradeManager)3 ClosedTradableManager (io.bisq.core.trade.closed.ClosedTradableManager)3