Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stub out support for OpenOffer's triggerPrice in api #5076

Merged
merged 10 commits into from
Jan 14, 2021
5 changes: 3 additions & 2 deletions cli/src/main/java/bisq/cli/CliMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@

import static bisq.cli.CurrencyFormat.formatTxFeeRateInfo;
import static bisq.cli.CurrencyFormat.toSatoshis;
import static bisq.cli.CurrencyFormat.toSecurityDepositAsPct;
import static bisq.cli.NegativeNumberOptions.hasNegativeNumberOptions;
import static bisq.cli.TableFormat.*;
import static java.lang.String.format;
Expand Down Expand Up @@ -376,7 +377,7 @@ public static void run(String[] args) {
else
fixedPrice = nonOptionArgs.get(7);

var securityDeposit = new BigDecimal(nonOptionArgs.get(8));
var securityDeposit = toSecurityDepositAsPct(nonOptionArgs.get(8));
var makerFeeCurrencyCode = nonOptionArgs.size() == 10
? nonOptionArgs.get(9)
: "btc";
Expand All @@ -389,7 +390,7 @@ public static void run(String[] args) {
.setUseMarketBasedPrice(useMarketBasedPrice)
.setPrice(fixedPrice)
.setMarketPriceMargin(marketPriceMargin.doubleValue())
.setBuyerSecurityDeposit(securityDeposit.doubleValue())
.setBuyerSecurityDeposit(securityDeposit)
.setPaymentAccountId(paymentAcctId)
.setMakerFeeCurrencyCode(makerFeeCurrencyCode)
.build();
Expand Down
11 changes: 11 additions & 0 deletions cli/src/main/java/bisq/cli/CurrencyFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class CurrencyFormat {
static final BigDecimal BSQ_SATOSHI_DIVISOR = new BigDecimal(100);
static final DecimalFormat BSQ_FORMAT = new DecimalFormat("###,###,###,##0.00");

static final BigDecimal SECURITY_DEPOSIT_MULTIPLICAND = new BigDecimal("0.01");

@SuppressWarnings("BigDecimalMethodWithoutRoundingCalled")
public static String formatSatoshis(long sats) {
return BTC_FORMAT.format(BigDecimal.valueOf(sats).divide(SATOSHI_DIVISOR));
Expand Down Expand Up @@ -99,6 +101,15 @@ static long toSatoshis(String btc) {
}
}

static double toSecurityDepositAsPct(String securityDepositInput) {
try {
return new BigDecimal(securityDepositInput)
.multiply(SECURITY_DEPOSIT_MULTIPLICAND).doubleValue();
} catch (NumberFormatException e) {
throw new IllegalArgumentException(format("'%s' is not a number", securityDepositInput));
}
}

@SuppressWarnings("BigDecimalMethodWithoutRoundingCalled")
private static String formatFeeSatoshis(long sats) {
return BTC_TX_FEE_FORMAT.format(BigDecimal.valueOf(sats).divide(SATOSHI_DIVISOR));
Expand Down
22 changes: 18 additions & 4 deletions cli/src/main/java/bisq/cli/NegativeNumberOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.function.Predicate;

import static java.util.Arrays.stream;
import static java.util.stream.IntStream.range;

class NegativeNumberOptions {

Expand All @@ -35,13 +36,13 @@ class NegativeNumberOptions {
String[] removeNegativeNumberOptions(String[] args) {
// Cache any negative number params that will be rejected by the parser.
// This should be called before command line parsing.
for (int i = 1; i < args.length; i++) {
// Start at i=1; args[0] is the method name.
int skipped = getIndexOfMethodInArgs(args);
for (int i = skipped; i < args.length; i++) {
if (isNegativeNumber.test(args[i])) {
String param = args[i];
negativeNumberParams.put(i - 1, new BigDecimal(param).toString());
negativeNumberParams.put(i - skipped, new BigDecimal(param).toString());
// Substitute a zero placeholder at the index containing the
// negative number option value.
// negative number positional option value.
args[i] = "0";
}
}
Expand Down Expand Up @@ -80,4 +81,17 @@ static boolean hasNegativeNumberOptions(String[] args) {
}
return false;
};

private int getIndexOfMethodInArgs(String[] args) {
// The first argument that does not start with '-' or '--' is the method name.
// Skip over the --password=xyz [--host=s --port=n] options.
int skipped = range(0, args.length)
.filter(i -> !args[i].startsWith("-"))
.findFirst()
.orElse(-1);
if (skipped >= 0)
return skipped;
else
throw new IllegalArgumentException("required --password option not found");
}
}
2 changes: 1 addition & 1 deletion cli/src/main/java/bisq/cli/TableFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ static String formatOfferTable(List<OfferInfo> offerInfo, String fiatCurrency) {
+ padEnd(COL_HEADER_PAYMENT_METHOD, paymentMethodColWidth, ' ') + COL_HEADER_DELIMITER
+ COL_HEADER_CREATION_DATE + COL_HEADER_DELIMITER
+ COL_HEADER_UUID.trim() + "%n";
String headerLine = format(headersFormat, fiatCurrency, fiatCurrency);
String headerLine = format(headersFormat, fiatCurrency.toUpperCase(), fiatCurrency.toUpperCase());

String colDataFormat = "%-" + (COL_HEADER_DIRECTION.length() + COL_HEADER_DELIMITER.length()) + "s" // left
+ "%" + (COL_HEADER_PRICE.length() - 1) + "s" // rt justify to end of hdr
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/java/bisq/core/api/CoreApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import bisq.core.monetary.Price;
import bisq.core.offer.Offer;
import bisq.core.offer.OfferPayload;
import bisq.core.offer.OpenOffer;
import bisq.core.payment.PaymentAccount;
import bisq.core.payment.payload.PaymentMethod;
import bisq.core.trade.Trade;
Expand Down Expand Up @@ -120,6 +121,10 @@ public List<Offer> getMyOffers(String direction, String currencyCode) {
return coreOffersService.getMyOffers(direction, currencyCode);
}

public OpenOffer getMyOpenOffer(String id) {
return coreOffersService.getMyOpenOffer(id);
}

public void createAnPlaceOffer(String currencyCode,
String directionAsString,
String priceAsString,
Expand All @@ -128,6 +133,7 @@ public void createAnPlaceOffer(String currencyCode,
long amountAsLong,
long minAmountAsLong,
double buyerSecurityDeposit,
long triggerPrice,
String paymentAccountId,
String makerFeeCurrencyCode,
Consumer<Offer> resultHandler) {
Expand All @@ -139,6 +145,7 @@ public void createAnPlaceOffer(String currencyCode,
amountAsLong,
minAmountAsLong,
buyerSecurityDeposit,
triggerPrice,
paymentAccountId,
makerFeeCurrencyCode,
resultHandler);
Expand Down
39 changes: 39 additions & 0 deletions core/src/main/java/bisq/core/api/CoreContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.core.api;

import javax.inject.Inject;
import javax.inject.Singleton;

import lombok.extern.slf4j.Slf4j;

import static java.lang.Thread.currentThread;

@Singleton
@Slf4j
class CoreContext {

@Inject
public CoreContext() {
}

public boolean isApiUser() {
String threadName = currentThread().getName();
return threadName.equals("BisqDaemonMain") || threadName.contains("grpc");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.bitcoinj.core.ECKey;

import javax.inject.Inject;
import javax.inject.Singleton;

import java.util.Date;
import java.util.List;
Expand All @@ -49,6 +50,7 @@
import static java.net.InetAddress.getLoopbackAddress;
import static java.util.Arrays.asList;

@Singleton
@Slf4j
class CoreDisputeAgentsService {

Expand Down
27 changes: 24 additions & 3 deletions core/src/main/java/bisq/core/api/CoreOffersService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import bisq.core.offer.CreateOfferService;
import bisq.core.offer.Offer;
import bisq.core.offer.OfferBookService;
import bisq.core.offer.OfferFilter;
import bisq.core.offer.OfferUtil;
import bisq.core.offer.OpenOffer;
import bisq.core.offer.OpenOfferManager;
import bisq.core.payment.PaymentAccount;
import bisq.core.user.User;
Expand All @@ -34,6 +36,7 @@
import org.bitcoinj.utils.Fiat;

import javax.inject.Inject;
import javax.inject.Singleton;

import java.math.BigDecimal;

Expand All @@ -54,6 +57,7 @@
import static java.lang.String.format;
import static java.util.Comparator.comparing;

@Singleton
@Slf4j
class CoreOffersService {

Expand All @@ -63,28 +67,35 @@ class CoreOffersService {
private final KeyRing keyRing;
private final CreateOfferService createOfferService;
private final OfferBookService offerBookService;
private final OfferFilter offerFilter;
private final OpenOfferManager openOfferManager;
private final OfferUtil offerUtil;
private final User user;
private final boolean isApiUser;

@Inject
public CoreOffersService(KeyRing keyRing,
public CoreOffersService(CoreContext coreContext,
KeyRing keyRing,
CreateOfferService createOfferService,
OfferBookService offerBookService,
OfferFilter offerFilter,
OpenOfferManager openOfferManager,
OfferUtil offerUtil,
User user) {
this.keyRing = keyRing;
this.createOfferService = createOfferService;
this.offerBookService = offerBookService;
this.offerFilter = offerFilter;
this.openOfferManager = openOfferManager;
this.offerUtil = offerUtil;
this.user = user;
this.isApiUser = coreContext.isApiUser();
}

Offer getOffer(String id) {
return offerBookService.getOffers().stream()
.filter(o -> o.getId().equals(id))
.filter(o -> offerFilter.canTakeOffer(o, isApiUser).isValid())
.findAny().orElseThrow(() ->
new IllegalStateException(format("offer with id '%s' not found", id)));
}
Expand All @@ -100,6 +111,7 @@ Offer getMyOffer(String id) {
List<Offer> getOffers(String direction, String currencyCode) {
return offerBookService.getOffers().stream()
.filter(o -> offerMatchesDirectionAndCurrency(o, direction, currencyCode))
.filter(o -> offerFilter.canTakeOffer(o, isApiUser).isValid())
.sorted(priceComparator(direction))
.collect(Collectors.toList());
}
Expand All @@ -112,6 +124,13 @@ List<Offer> getMyOffers(String direction, String currencyCode) {
.collect(Collectors.toList());
}

OpenOffer getMyOpenOffer(String id) {
return openOfferManager.getOpenOfferById(id)
.filter(open -> open.getOffer().isMyOffer(keyRing))
.orElseThrow(() ->
new IllegalStateException(format("openoffer with id '%s' not found", id)));
}

// Create and place new offer.
void createAndPlaceOffer(String currencyCode,
String directionAsString,
Expand All @@ -121,6 +140,7 @@ void createAndPlaceOffer(String currencyCode,
long amountAsLong,
long minAmountAsLong,
double buyerSecurityDeposit,
long triggerPrice,
String paymentAccountId,
String makerFeeCurrencyCode,
Consumer<Offer> resultHandler) {
Expand Down Expand Up @@ -152,6 +172,7 @@ void createAndPlaceOffer(String currencyCode,
//noinspection ConstantConditions
placeOffer(offer,
buyerSecurityDeposit,
triggerPrice,
useSavingsWallet,
transaction -> resultHandler.accept(offer));
}
Expand Down Expand Up @@ -193,13 +214,13 @@ void cancelOffer(String id) {

private void placeOffer(Offer offer,
double buyerSecurityDeposit,
long triggerPrice,
boolean useSavingsWallet,
Consumer<Transaction> resultHandler) {
// TODO add support for triggerPrice parameter. If value is 0 it is interpreted as not used. Its an optional value
openOfferManager.placeOffer(offer,
buyerSecurityDeposit,
useSavingsWallet,
0,
triggerPrice,
resultHandler::accept,
log::error);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import bisq.core.user.User;

import javax.inject.Inject;
import javax.inject.Singleton;

import java.io.File;

Expand All @@ -34,6 +35,7 @@

import lombok.extern.slf4j.Slf4j;

@Singleton
@Slf4j
class CorePaymentAccountsService {

Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/bisq/core/api/CorePriceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
import bisq.core.provider.price.PriceFeedService;

import javax.inject.Inject;
import javax.inject.Singleton;

import lombok.extern.slf4j.Slf4j;

import static bisq.common.util.MathUtils.roundDouble;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;


@Singleton
@Slf4j
class CorePriceService {

Expand Down
9 changes: 7 additions & 2 deletions core/src/main/java/bisq/core/api/CoreTradesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.bitcoinj.core.Coin;

import javax.inject.Inject;
import javax.inject.Singleton;

import java.util.Optional;
import java.util.function.Consumer;
Expand All @@ -44,6 +45,7 @@
import static bisq.core.btc.model.AddressEntry.Context.TRADE_PAYOUT;
import static java.lang.String.format;

@Singleton
@Slf4j
class CoreTradesService {

Expand All @@ -59,9 +61,11 @@ class CoreTradesService {
private final TradeManager tradeManager;
private final TradeUtil tradeUtil;
private final User user;
private final boolean isApiUser;

@Inject
public CoreTradesService(CoreWalletsService coreWalletsService,
public CoreTradesService(CoreContext coreContext,
CoreWalletsService coreWalletsService,
BtcWalletService btcWalletService,
OfferUtil offerUtil,
ClosedTradableManager closedTradableManager,
Expand All @@ -77,6 +81,7 @@ public CoreTradesService(CoreWalletsService coreWalletsService,
this.tradeManager = tradeManager;
this.tradeUtil = tradeUtil;
this.user = user;
this.isApiUser = coreContext.isApiUser();
}

void takeOffer(Offer offer,
Expand Down Expand Up @@ -108,7 +113,7 @@ void takeOffer(Offer offer,
offer,
paymentAccountId,
useSavingsWallet,
true,
isApiUser,
resultHandler::accept,
errorMessage -> {
log.error(errorMessage);
Expand Down
Loading