Skip to content

Commit

Permalink
Merge pull request #3059 from HenrikJannsen/add-offer-and-trade-apis
Browse files Browse the repository at this point in the history
Add offer and trade apis
  • Loading branch information
HenrikJannsen authored Dec 23, 2024
2 parents 4cb0f02 + 4665efc commit ee0f393
Show file tree
Hide file tree
Showing 93 changed files with 2,905 additions and 549 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ public DesktopApplicationService(String[] args, ShutDownHandler shutDownHandler)
networkService,
userService,
bondedRolesService,
chatService);
chatService,
supportService,
tradeService);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public void onActivate() {
PriceSpecFormatter.getFormattedPriceSpec(bisqEasyTrade.getOffer().getPriceSpec())));
model.getSellerPriceDescriptionApprovalOverlay().set(
Res.get("bisqEasy.tradeState.acceptOrRejectSellersPrice.description.sellersPrice",
PriceSpecFormatter.getFormattedPriceSpec(bisqEasyTrade.getContract().getAgreedPriceSpec())));
PriceSpecFormatter.getFormattedPriceSpec(bisqEasyTrade.getContract().getPriceSpec())));
});
}

Expand Down Expand Up @@ -526,7 +526,7 @@ && requiresSellerPriceAcceptance()

private boolean requiresSellerPriceAcceptance() {
PriceSpec buyerPriceSpec = model.getBisqEasyTrade().get().getOffer().getPriceSpec();
PriceSpec sellerPriceSpec = model.getBisqEasyTrade().get().getContract().getAgreedPriceSpec();
PriceSpec sellerPriceSpec = model.getBisqEasyTrade().get().getContract().getPriceSpec();
boolean priceSpecChanged = !buyerPriceSpec.equals(sellerPriceSpec);

Set<BisqEasyTradeState> validStatesToRejectPrice = Set.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,11 @@ public void init(BisqEasyOffer bisqEasyOffer) {
.ifPresent(model::setTakersQuoteSideAmount);
}

PriceSpec priceSpec = bisqEasyOffer.getPriceSpec();
model.setSellersPriceSpec(priceSpec);

Optional<PriceQuote> priceQuote = PriceUtil.findQuote(marketPriceService, bisqEasyOffer);
priceQuote.ifPresent(priceInput::setQuote);

applyPriceQuote(priceQuote);
applyPriceDetails(priceSpec, market);
applyPriceDetails(bisqEasyOffer.getPriceSpec(), market);
}

public void setTakersBaseSideAmount(Monetary amount) {
Expand Down Expand Up @@ -192,7 +189,7 @@ private void doTakeOffer(BisqEasyOffer bisqEasyOffer, UserIdentity takerIdentity
Monetary takersQuoteSideAmount = model.getTakersQuoteSideAmount();
BitcoinPaymentMethodSpec bitcoinPaymentMethodSpec = model.getBitcoinPaymentMethodSpec();
FiatPaymentMethodSpec fiatPaymentMethodSpec = model.getFiatPaymentMethodSpec();
PriceSpec sellersPriceSpec = model.getSellersPriceSpec();
PriceSpec priceSpec = bisqEasyOffer.getPriceSpec();
long marketPrice = model.getMarketPrice();
BisqEasyProtocol bisqEasyProtocol = bisqEasyTradeService.createBisqEasyProtocol(takerIdentity.getIdentity(),
bisqEasyOffer,
Expand All @@ -201,7 +198,7 @@ private void doTakeOffer(BisqEasyOffer bisqEasyOffer, UserIdentity takerIdentity
bitcoinPaymentMethodSpec,
fiatPaymentMethodSpec,
mediator,
sellersPriceSpec,
priceSpec,
marketPrice);
BisqEasyTrade bisqEasyTrade = bisqEasyProtocol.getModel();
log.info("Selected mediator for trade {}: {}", bisqEasyTrade.getShortId(), mediator.map(UserProfile::getUserName).orElse("N/A"));
Expand Down Expand Up @@ -335,8 +332,7 @@ private void applyPriceDetails(PriceSpec priceSpec, Market market) {
marketPrice.ifPresent(price -> model.setMarketPrice(price.getPriceQuote().getValue()));
Optional<PriceQuote> marketPriceQuote = marketPrice.map(MarketPrice::getPriceQuote);
String marketPriceAsString = marketPriceQuote.map(PriceFormatter::formatWithCode).orElse(Res.get("data.na"));
Optional<Double> percentFromMarketPrice;
percentFromMarketPrice = PriceUtil.findPercentFromMarketPrice(marketPriceService, priceSpec, market);
Optional<Double> percentFromMarketPrice = PriceUtil.findPercentFromMarketPrice(marketPriceService, priceSpec, market);
double percent = percentFromMarketPrice.orElse(0d);
if ((priceSpec instanceof FloatPriceSpec || priceSpec instanceof MarketPriceSpec) && percent == 0) {
model.setPriceDetails(Res.get("bisqEasy.tradeWizard.review.priceDetails", marketPriceAsString));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import bisq.offer.bisq_easy.BisqEasyOffer;
import bisq.offer.payment_method.BitcoinPaymentMethodSpec;
import bisq.offer.payment_method.FiatPaymentMethodSpec;
import bisq.offer.price.spec.PriceSpec;
import bisq.trade.bisq_easy.BisqEasyTrade;
import bisq.user.profile.UserProfile;
import javafx.beans.property.ObjectProperty;
Expand Down Expand Up @@ -50,8 +49,6 @@ class TakeOfferReviewModel implements Model {
private Monetary takersBaseSideAmount;
@Setter
private Monetary takersQuoteSideAmount;
@Setter
private PriceSpec sellersPriceSpec;
private final ObjectProperty<TakeOfferStatus> takeOfferStatus = new SimpleObjectProperty<>(TakeOfferStatus.NOT_STARTED);
@Setter
private String price;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -612,11 +612,10 @@ private void applyPriceDetails(PriceSpec priceSpec, Market market) {
marketPrice.ifPresent(price -> model.setMarketPrice(price.getPriceQuote().getValue()));
Optional<PriceQuote> marketPriceQuote = marketPriceService.findMarketPrice(market).map(MarketPrice::getPriceQuote);
String marketPriceAsString = marketPriceQuote.map(PriceFormatter::formatWithCode).orElse(Res.get("data.na"));
Optional<Double> percentFromMarketPrice;
percentFromMarketPrice = PriceUtil.findPercentFromMarketPrice(marketPriceService, priceSpec, market);
Optional<Double> percentFromMarketPrice = PriceUtil.findPercentFromMarketPrice(marketPriceService, priceSpec, market);
double percent = percentFromMarketPrice.orElse(0d);
if ((priceSpec instanceof FloatPriceSpec || priceSpec instanceof MarketPriceSpec) && percent == 0) {
model.setPriceDetails(Res.get("bisqEasy.tradeWizard.review.priceDetails", marketPriceAsString));
model.setPriceDetails(Res.get("bisqEasy.tradeWizard.review.priceDetails"));
} else {
String aboveOrBelow = percent > 0 ? Res.get("offer.price.above") : Res.get("offer.price.below");
String percentAsString = percentFromMarketPrice.map(Math::abs).map(PercentageFormatter::formatToPercentWithSymbol)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ public HttpApiApplicationService(String[] args) {
networkService,
userService,
bondedRolesService,
chatService);
chatService,
supportService,
tradeService);
}

@Override
Expand Down
16 changes: 8 additions & 8 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ tasks.register("buildAll") {
doLast {
listOf(
"build",
":wallets:build",
":apps:seed-node-app:build",
":apps:seed-node-app:installDist",
//":apps:seed-node-app:installDist",
":apps:desktop:desktop:build",
":apps:desktop:desktop-app:build",
":apps:desktop:desktop-app:installDist",
":apps:desktop:desktop-app-launcher:generateInstallers",
":apps:desktop:desktop-app-launcher:build",
//":apps:desktop:desktop-app:installDist",
// ":apps:desktop:desktop-app-launcher:generateInstallers",
":apps:desktop:webcam-app:build",
":apps:oracle-node-app:build",
":apps:http-api-app:build",
":apps:node-monitor-web-app:build",
// ":REPLACEME:build",
":apps:oracle-node-app:build",
":apps:seed-node-app:build",
":wallets:build",
).forEach {
exec {
println("Executing Build: $it")
Expand Down Expand Up @@ -69,7 +70,6 @@ tasks.register("publishAll") {
listOf(
":account:publishToMavenLocal",
":application:publishToMavenLocal",
// ":bisq-easy:publishToMavenLocal",
":bonded-roles:publishToMavenLocal",
":chat:publishToMavenLocal",
":common:publishToMavenLocal",
Expand Down
9 changes: 2 additions & 7 deletions common/src/main/java/bisq/common/monetary/Coin.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private Coin(double faceValue, String code, int precision) {
super(code + " [crypto]", faceValue, code, precision, code.equals("BSQ") ? 2 : 4);
}

private Coin(String id, long value, String code, int precision, int lowPrecision) {
public Coin(String id, long value, String code, int precision, int lowPrecision) {
super(id, value, code, precision, lowPrecision);
}

Expand Down Expand Up @@ -191,11 +191,6 @@ public Coin divide(long divisor) {
return new Coin(this.value / divisor, this.code, this.precision);
}

@Override
public double toDouble(long value) {
return MathUtils.roundDouble(BigDecimal.valueOf(value).movePointLeft(precision).doubleValue(), precision);
}

private static int derivePrecision(String code) {
if (code.equals("XMR")) return 12;
if (code.equals("BSQ")) return 2;
Expand All @@ -204,7 +199,7 @@ private static int derivePrecision(String code) {

public Coin round(int roundPrecision) {
//todo (low prio) add tests
double rounded = MathUtils.roundDouble(toDouble(value), roundPrecision);
double rounded = MathUtils.roundDouble(asDouble(), roundPrecision);
long shifted = BigDecimal.valueOf(rounded).movePointRight(precision).longValue();
return Coin.fromValue(shifted, code, precision);
}
Expand Down
9 changes: 2 additions & 7 deletions common/src/main/java/bisq/common/monetary/Fiat.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private Fiat(double faceValue, String code, int precision) {
super(code, faceValue, code, precision, 2);
}

private Fiat(String id, long value, String code, int precision, int lowPrecision) {
public Fiat(String id, long value, String code, int precision, int lowPrecision) {
super(id, value, code, precision, lowPrecision);
}

Expand Down Expand Up @@ -127,13 +127,8 @@ public Fiat divide(long divisor) {
return new Fiat(this.value / divisor, this.code, this.precision);
}

@Override
public double toDouble(long value) {
return MathUtils.roundDouble(BigDecimal.valueOf(value).movePointLeft(precision).doubleValue(), precision);
}

public Fiat round(int roundPrecision) {
double rounded = MathUtils.roundDouble(toDouble(value), roundPrecision);
double rounded = MathUtils.roundDouble(asDouble(), roundPrecision);
long shifted = BigDecimal.valueOf(rounded).movePointRight(precision).longValue();
return Fiat.fromValue(shifted, code, precision);
}
Expand Down
5 changes: 3 additions & 2 deletions common/src/main/java/bisq/common/monetary/Monetary.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ public static Monetary fromProto(bisq.common.protobuf.Monetary proto) {
};
}

public abstract double toDouble(long value);

public double toDouble(long value) {
return MathUtils.roundDouble(BigDecimal.valueOf(value).movePointLeft(precision).doubleValue(), precision);
}
public double asDouble() {
return toDouble(value);
}
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/java/bisq/common/monetary/PriceQuote.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public final class PriceQuote implements Comparable<PriceQuote>, PersistableProt
private final int lowPrecision;
private final Market market;

private PriceQuote(long value, Monetary baseSideMonetary, Monetary quoteSideMonetary) {
public PriceQuote(long value, Monetary baseSideMonetary, Monetary quoteSideMonetary) {
this.value = value;
this.baseSideMonetary = baseSideMonetary;
this.quoteSideMonetary = quoteSideMonetary;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public final class BisqEasyContract extends TwoPartyContract<BisqEasyOffer> {
private final BitcoinPaymentMethodSpec baseSidePaymentMethodSpec;
private final FiatPaymentMethodSpec quoteSidePaymentMethodSpec;
private final Optional<UserProfile> mediator;
private final PriceSpec agreedPriceSpec;
private final PriceSpec priceSpec;
private final long marketPrice;
private final long takeOfferDate;

Expand All @@ -55,7 +55,7 @@ public BisqEasyContract(long takeOfferDate,
BitcoinPaymentMethodSpec baseSidePaymentMethodSpec,
FiatPaymentMethodSpec quoteSidePaymentMethodSpec,
Optional<UserProfile> mediator,
PriceSpec agreedPriceSpec,
PriceSpec priceSpec,
long marketPrice) {
this(takeOfferDate,
offer,
Expand All @@ -66,7 +66,7 @@ public BisqEasyContract(long takeOfferDate,
baseSidePaymentMethodSpec,
quoteSidePaymentMethodSpec,
mediator,
agreedPriceSpec,
priceSpec,
marketPrice);
}

Expand All @@ -79,15 +79,15 @@ private BisqEasyContract(long takeOfferDate,
BitcoinPaymentMethodSpec baseSidePaymentMethodSpec,
FiatPaymentMethodSpec quoteSidePaymentMethodSpec,
Optional<UserProfile> mediator,
PriceSpec agreedPriceSpec,
PriceSpec priceSpec,
long marketPrice) {
super(takeOfferDate, offer, protocolType, taker);
this.baseSideAmount = baseSideAmount;
this.quoteSideAmount = quoteSideAmount;
this.baseSidePaymentMethodSpec = baseSidePaymentMethodSpec;
this.quoteSidePaymentMethodSpec = quoteSidePaymentMethodSpec;
this.mediator = mediator;
this.agreedPriceSpec = agreedPriceSpec;
this.priceSpec = priceSpec;
this.marketPrice = marketPrice;
this.takeOfferDate = takeOfferDate;

Expand Down Expand Up @@ -120,7 +120,7 @@ private bisq.contract.protobuf.BisqEasyContract.Builder getBisqEasyContractBuild
.setQuoteSideAmount(quoteSideAmount)
.setBaseSidePaymentMethodSpec(baseSidePaymentMethodSpec.toProto(serializeForHash))
.setQuoteSidePaymentMethodSpec(quoteSidePaymentMethodSpec.toProto(serializeForHash))
.setAgreedPriceSpec(agreedPriceSpec.toProto(serializeForHash))
.setPriceSpec(priceSpec.toProto(serializeForHash))
.setMarketPrice(marketPrice);
mediator.ifPresent(mediator -> builder.setMediator(mediator.toProto(serializeForHash)));
return builder;
Expand All @@ -145,7 +145,7 @@ public static BisqEasyContract fromProto(bisq.contract.protobuf.Contract proto)
bisqEasyContractProto.hasMediator() ?
Optional.of(UserProfile.fromProto(bisqEasyContractProto.getMediator())) :
Optional.empty(),
PriceSpec.fromProto(bisqEasyContractProto.getAgreedPriceSpec()),
PriceSpec.fromProto(bisqEasyContractProto.getPriceSpec()),
bisqEasyContractProto.getMarketPrice());
}
}
2 changes: 1 addition & 1 deletion contract/src/main/proto/contract.proto
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ message BisqEasyContract {
offer.PaymentMethodSpec baseSidePaymentMethodSpec = 3;
offer.PaymentMethodSpec quoteSidePaymentMethodSpec = 4;
optional user.UserProfile mediator = 12;
offer.PriceSpec agreedPriceSpec = 13;
offer.PriceSpec priceSpec = 13;
uint64 marketPrice = 14;
}

Expand Down
10 changes: 6 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ i2p-router = { module = 'net.i2p:router', version.ref = 'i2p-lib' }
jackson-core = { module = 'com.fasterxml.jackson.core:jackson-core', version.ref = 'jackson-lib' }
jackson-annotations = { module = 'com.fasterxml.jackson.core:jackson-annotations', version.ref = 'jackson-lib' }
jackson-databind = { module = 'com.fasterxml.jackson.core:jackson-databind', version.ref = 'jackson-lib' }
jackson-datatype = { module = 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8', version.ref = 'jackson-lib' }

jakarta-websocket = { module = 'jakarta.websocket:jakarta.websocket-api', version.ref = 'jakarta-lib' }

javacv = { module = "org.bytedeco:javacv-platform", version.ref = "javacv" }
Expand Down Expand Up @@ -177,13 +179,13 @@ glassfish-jersey = ['glassfish-jersey-jdk-http', 'glassfish-jersey-json-jackson'
grpc = ['grpc-protobuf', 'grpc-services', 'grpc-stub']
i2p = ['i2p-core', 'i2p-router', 'i2p-streaming']
i2p-v2 = ['i2p-core-v2', 'i2p-streaming-v2']
jackson = ['jackson-core', 'jackson-annotations', 'jackson-databind']
jackson = ['jackson-core', 'jackson-annotations', 'jackson-databind', 'jackson-datatype']
springfox-libs = ['springfox-boot-starter', 'springfox-swagger2', 'springfox-swagger-ui']
rest-api-libs = ['swagger-jaxrs2-jakarta', 'glassfish-jersey-jdk-http', 'glassfish-jersey-json-jackson',
'glassfish-jersey-inject-hk2',
'glassfish-jaxb-runtime', 'jackson-core', 'jackson-annotations', 'jackson-databind']
'glassfish-jersey-inject-hk2', 'glassfish-jaxb-runtime',
'jackson-core', 'jackson-annotations', 'jackson-databind', 'jackson-datatype']
websocket-libs = ['glassfish-jersey-json-jackson', 'glassfish-jersey-server', 'glassfish-jersey-containers-grizzly',
'glassfish-grizzly-websockets-server', 'jakarta-websocket', 'jackson-databind', 'swagger-swagger-annotations']
'glassfish-grizzly-websockets-server', 'jakarta-websocket', 'jackson-databind', 'jackson-datatype', 'swagger-swagger-annotations']

# Referenced in subproject's build.gradle > plugin block as alias: `alias(libs.plugins.protobuf)`
# Note: plugin version constraints are not supported by the java-platform plugin, so cannot be enforced there. However,
Expand Down
1 change: 1 addition & 0 deletions http-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies {
implementation("bisq:os-specific")

implementation("network:network")
implementation("network:network-identity")
implementation("bitcoind:core")
implementation("wallets:wallet")

Expand Down
Loading

0 comments on commit ee0f393

Please sign in to comment.