Skip to content

Commit

Permalink
Merge pull request #5076 from ghubstan/03-support-trigger-price
Browse files Browse the repository at this point in the history
Stub out support for OpenOffer's triggerPrice in api
  • Loading branch information
sqrrm authored Jan 14, 2021
2 parents 01bfbe7 + a8d15d0 commit f838b1a
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 17 deletions.
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
14 changes: 12 additions & 2 deletions core/src/main/java/bisq/core/api/CoreOffersService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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 Down Expand Up @@ -123,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 @@ -132,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 @@ -163,6 +172,7 @@ void createAndPlaceOffer(String currencyCode,
//noinspection ConstantConditions
placeOffer(offer,
buyerSecurityDeposit,
triggerPrice,
useSavingsWallet,
transaction -> resultHandler.accept(offer));
}
Expand Down Expand Up @@ -204,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
25 changes: 22 additions & 3 deletions core/src/main/java/bisq/core/api/model/OfferInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ public class OfferInfo implements Payload {
private final long volume;
private final long minVolume;
private final long buyerSecurityDeposit;
private final long triggerPrice;
private final boolean isCurrencyForMakerFeeBtc;
private final String paymentAccountId; // only used when creating offer
private final String paymentAccountId;
private final String paymentMethodId;
private final String paymentMethodShortName;
// For fiat offer the baseCurrencyCode is BTC and the counterCurrencyCode is the fiat currency
Expand All @@ -68,6 +69,7 @@ public OfferInfo(OfferInfoBuilder builder) {
this.volume = builder.volume;
this.minVolume = builder.minVolume;
this.buyerSecurityDeposit = builder.buyerSecurityDeposit;
this.triggerPrice = builder.triggerPrice;
this.isCurrencyForMakerFeeBtc = builder.isCurrencyForMakerFeeBtc;
this.paymentAccountId = builder.paymentAccountId;
this.paymentMethodId = builder.paymentMethodId;
Expand All @@ -79,6 +81,16 @@ public OfferInfo(OfferInfoBuilder builder) {
}

public static OfferInfo toOfferInfo(Offer offer) {
return getOfferInfoBuilder(offer).build();
}

public static OfferInfo toOfferInfo(Offer offer, long triggerPrice) {
// The Offer does not have a triggerPrice attribute, so we get
// the base OfferInfoBuilder, then add the OpenOffer's triggerPrice.
return getOfferInfoBuilder(offer).withTriggerPrice(triggerPrice).build();
}

private static OfferInfo.OfferInfoBuilder getOfferInfoBuilder(Offer offer) {
return new OfferInfo.OfferInfoBuilder()
.withId(offer.getId())
.withDirection(offer.getDirection().name())
Expand All @@ -97,8 +109,7 @@ public static OfferInfo toOfferInfo(Offer offer) {
.withBaseCurrencyCode(offer.getOfferPayload().getBaseCurrencyCode())
.withCounterCurrencyCode(offer.getOfferPayload().getCounterCurrencyCode())
.withDate(offer.getDate().getTime())
.withState(offer.getState().name())
.build();
.withState(offer.getState().name());
}

///////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -118,6 +129,7 @@ public bisq.proto.grpc.OfferInfo toProtoMessage() {
.setVolume(volume)
.setMinVolume(minVolume)
.setBuyerSecurityDeposit(buyerSecurityDeposit)
.setTriggerPrice(triggerPrice)
.setIsCurrencyForMakerFeeBtc(isCurrencyForMakerFeeBtc)
.setPaymentAccountId(paymentAccountId)
.setPaymentMethodId(paymentMethodId)
Expand All @@ -142,6 +154,7 @@ public static OfferInfo fromProto(bisq.proto.grpc.OfferInfo proto) {
.withVolume(proto.getVolume())
.withMinVolume(proto.getMinVolume())
.withBuyerSecurityDeposit(proto.getBuyerSecurityDeposit())
.withTriggerPrice(proto.getTriggerPrice())
.withIsCurrencyForMakerFeeBtc(proto.getIsCurrencyForMakerFeeBtc())
.withPaymentAccountId(proto.getPaymentAccountId())
.withPaymentMethodId(proto.getPaymentMethodId())
Expand Down Expand Up @@ -170,6 +183,7 @@ public static class OfferInfoBuilder {
private long volume;
private long minVolume;
private long buyerSecurityDeposit;
private long triggerPrice;
private boolean isCurrencyForMakerFeeBtc;
private String paymentAccountId;
private String paymentMethodId;
Expand Down Expand Up @@ -229,6 +243,11 @@ public OfferInfoBuilder withBuyerSecurityDeposit(long buyerSecurityDeposit) {
return this;
}

public OfferInfoBuilder withTriggerPrice(long triggerPrice) {
this.triggerPrice = triggerPrice;
return this;
}

public OfferInfoBuilder withIsCurrencyForMakerFeeBtc(boolean isCurrencyForMakerFeeBtc) {
this.isCurrencyForMakerFeeBtc = isCurrencyForMakerFeeBtc;
return this;
Expand Down
5 changes: 4 additions & 1 deletion daemon/src/main/java/bisq/daemon/grpc/GrpcOffersService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import bisq.core.api.CoreApi;
import bisq.core.api.model.OfferInfo;
import bisq.core.offer.Offer;
import bisq.core.offer.OpenOffer;

import bisq.proto.grpc.CancelOfferReply;
import bisq.proto.grpc.CancelOfferRequest;
Expand Down Expand Up @@ -78,8 +79,9 @@ public void getMyOffer(GetMyOfferRequest req,
StreamObserver<GetMyOfferReply> responseObserver) {
try {
Offer offer = coreApi.getMyOffer(req.getId());
OpenOffer openOffer = coreApi.getMyOpenOffer(req.getId());
var reply = GetMyOfferReply.newBuilder()
.setOffer(toOfferInfo(offer).toProtoMessage())
.setOffer(toOfferInfo(offer, openOffer.getTriggerPrice()).toProtoMessage())
.build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
Expand Down Expand Up @@ -139,6 +141,7 @@ public void createOffer(CreateOfferRequest req,
req.getAmount(),
req.getMinAmount(),
req.getBuyerSecurityDeposit(),
req.getTriggerPrice(),
req.getPaymentAccountId(),
req.getMakerFeeCurrencyCode(),
offer -> {
Expand Down
22 changes: 12 additions & 10 deletions proto/src/main/proto/grpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ message CreateOfferRequest {
uint64 amount = 6;
uint64 minAmount = 7;
double buyerSecurityDeposit = 8;
string paymentAccountId = 9;
string makerFeeCurrencyCode = 10;
uint64 triggerPrice = 9;
string paymentAccountId = 10;
string makerFeeCurrencyCode = 11;
}

message CreateOfferReply {
Expand All @@ -128,14 +129,15 @@ message OfferInfo {
uint64 volume = 8;
uint64 minVolume = 9;
uint64 buyerSecurityDeposit = 10;
bool isCurrencyForMakerFeeBtc = 11;
string paymentAccountId = 12;
string paymentMethodId = 13;
string paymentMethodShortName = 14;
string baseCurrencyCode = 15;
string counterCurrencyCode = 16;
uint64 date = 17;
string state = 18;
uint64 triggerPrice = 11;
bool isCurrencyForMakerFeeBtc = 12;
string paymentAccountId = 13;
string paymentMethodId = 14;
string paymentMethodShortName = 15;
string baseCurrencyCode = 16;
string counterCurrencyCode = 17;
uint64 date = 18;
string state = 19;
}

///////////////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit f838b1a

Please sign in to comment.