Skip to content

Commit

Permalink
Use Price instead of String to represent prices. (#554)
Browse files Browse the repository at this point in the history
Change the type of `CreatePassiveSellOfferOperation.price`, `ManageBuyOfferOperation.price`, and `ManageBuyOfferOperation.price` from `String` to `Price`, this fixes the issue of incorrect operations parsed in certain specific scenarios.
  • Loading branch information
overcat authored Nov 1, 2023
1 parent 3e7b5cd commit badda6d
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ As this project is pre 1.0, breaking changes may happen for minor version bumps.
* Make `StrKey` public, this allows users to conveniently encode and decode Stellar keys to/from strings. ([#548](https://github.com/stellar/java-stellar-sdk/pull/548))
* Update `LedgerResponse` and `AccountResponse`, remove outdated fields, and add missing fields. ([#549](https://github.com/stellar/java-stellar-sdk/pull/549))
* Add support for muxed accounts in `PaymentOperationResponse`. ([#550](https://github.com/stellar/java-stellar-sdk/pull/550))
* Use `Price` instead of `String` to represent prices. Change the type of `CreatePassiveSellOfferOperation.price`, `ManageBuyOfferOperation.price`, and `ManageBuyOfferOperation.price` from `String` to `Price`, this fixes the issue of incorrect operations parsed in certain specific scenarios. ([#554](https://github.com/stellar/java-stellar-sdk/pull/554))

## 0.41.1
* Add `org.stellar.sdk.spi.SdkProvider`, users can implement this interface to provide their own implementation of the SDK. We provide an [Android specific implementation](https://github.com/stellar/java-stellar-sdk-android-spi), if you are integrating this SDK into an Android project, be sure to check it out. ([#543](https://github.com/stellar/java-stellar-sdk/pull/543))
Expand Down
27 changes: 16 additions & 11 deletions src/main/java/org/stellar/sdk/CreatePassiveSellOfferOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@ public class CreatePassiveSellOfferOperation extends Operation {
private final Asset selling;
private final Asset buying;
private final String amount;
private final String price;
private final Price price;

private CreatePassiveSellOfferOperation(
@NonNull Asset selling,
@NonNull Asset buying,
@NonNull String amount,
@NonNull String price) {
@NonNull Asset selling, @NonNull Asset buying, @NonNull String amount, @NonNull Price price) {
this.selling = selling;
this.buying = buying;
this.amount = amount;
Expand All @@ -47,7 +44,7 @@ public String getAmount() {
}

/** Price of 1 unit of selling in terms of buying. */
public String getPrice() {
public Price getPrice() {
return price;
}

Expand All @@ -59,7 +56,6 @@ org.stellar.sdk.xdr.Operation.OperationBody toOperationBody(AccountConverter acc
Int64 amount = new Int64();
amount.setInt64(Operation.toXdrAmount(this.amount));
op.setAmount(amount);
Price price = Price.fromString(this.price);
op.setPrice(price.toXdr());

org.stellar.sdk.xdr.Operation.OperationBody body =
Expand All @@ -80,7 +76,7 @@ public static class Builder {
private final Asset selling;
private final Asset buying;
private final String amount;
private final String price;
private final Price price;

private String mSourceAccount;

Expand All @@ -92,8 +88,8 @@ public static class Builder {
Builder(CreatePassiveSellOfferOp op) {
selling = Asset.fromXdr(op.getSelling());
buying = Asset.fromXdr(op.getBuying());
amount = Operation.fromXdrAmount(op.getAmount().getInt64().longValue());
price = Price.fromXdr(op.getPrice()).toString();
amount = Operation.fromXdrAmount(op.getAmount().getInt64());
price = Price.fromXdr(op.getPrice());
}

/**
Expand All @@ -109,13 +105,22 @@ public Builder(
@NonNull Asset selling,
@NonNull Asset buying,
@NonNull String amount,
@NonNull String price) {
@NonNull Price price) {
this.selling = selling;
this.buying = buying;
this.amount = amount;
this.price = price;
}

/** An alias for {@link Builder#Builder(Asset, Asset, String, Price)} */
public Builder(
@NonNull Asset selling,
@NonNull Asset buying,
@NonNull String amount,
@NonNull String price) {
this(selling, buying, amount, Price.fromString(price));
}

/**
* Sets the source account for this operation.
*
Expand Down
28 changes: 18 additions & 10 deletions src/main/java/org/stellar/sdk/ManageBuyOfferOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ public class ManageBuyOfferOperation extends Operation {
private final Asset selling;
private final Asset buying;
private final String amount;
private final String price;
private final Price price;
private final long offerId;

private ManageBuyOfferOperation(
@NonNull Asset selling,
@NonNull Asset buying,
@NonNull String amount,
@NonNull String price,
@NonNull Price price,
long offerId) {
this.selling = selling;
this.buying = buying;
Expand All @@ -50,7 +50,7 @@ public String getAmount() {
}

/** Price of thing being bought in terms of what you are selling. */
public String getPrice() {
public Price getPrice() {
return price;
}

Expand All @@ -67,10 +67,9 @@ org.stellar.sdk.xdr.Operation.OperationBody toOperationBody(AccountConverter acc
Int64 amount = new Int64();
amount.setInt64(Operation.toXdrAmount(this.amount));
op.setBuyAmount(amount);
Price price = Price.fromString(this.price);
op.setPrice(price.toXdr());
Int64 offerId = new Int64();
offerId.setInt64(Long.valueOf(this.offerId));
offerId.setInt64(this.offerId);
op.setOfferID(offerId);

org.stellar.sdk.xdr.Operation.OperationBody body =
Expand All @@ -92,7 +91,7 @@ public static class Builder {
private final Asset selling;
private final Asset buying;
private final String amount;
private final String price;
private final Price price;
private long offerId = 0;

private String mSourceAccount;
Expand All @@ -105,9 +104,9 @@ public static class Builder {
Builder(ManageBuyOfferOp op) {
selling = Asset.fromXdr(op.getSelling());
buying = Asset.fromXdr(op.getBuying());
amount = Operation.fromXdrAmount(op.getBuyAmount().getInt64().longValue());
price = Price.fromXdr(op.getPrice()).toString();
offerId = op.getOfferID().getInt64().longValue();
amount = Operation.fromXdrAmount(op.getBuyAmount().getInt64());
price = Price.fromXdr(op.getPrice());
offerId = op.getOfferID().getInt64();
}

/**
Expand All @@ -124,13 +123,22 @@ public Builder(
@NonNull Asset selling,
@NonNull Asset buying,
@NonNull String amount,
@NonNull String price) {
@NonNull Price price) {
this.selling = selling;
this.buying = buying;
this.amount = amount;
this.price = price;
}

/** An alias for {@link Builder#Builder(Asset, Asset, String, Price)} */
public Builder(
@NonNull Asset selling,
@NonNull Asset buying,
@NonNull String amount,
@NonNull String price) {
this(selling, buying, amount, Price.fromString(price));
}

/**
* Sets offer ID. <code>0</code> creates a new offer. Set to existing offer ID to change it.
*
Expand Down
28 changes: 18 additions & 10 deletions src/main/java/org/stellar/sdk/ManageSellOfferOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ public class ManageSellOfferOperation extends Operation {
private final Asset selling;
private final Asset buying;
private final String amount;
private final String price;
private final Price price;
private final long offerId;

private ManageSellOfferOperation(
@NonNull Asset selling,
@NonNull Asset buying,
@NonNull String amount,
@NonNull String price,
@NonNull Price price,
long offerId) {
this.selling = selling;
this.buying = buying;
Expand All @@ -50,7 +50,7 @@ public String getAmount() {
}

/** Price of 1 unit of selling in terms of buying. */
public String getPrice() {
public Price getPrice() {
return price;
}

Expand All @@ -67,10 +67,9 @@ org.stellar.sdk.xdr.Operation.OperationBody toOperationBody(AccountConverter acc
Int64 amount = new Int64();
amount.setInt64(Operation.toXdrAmount(this.amount));
op.setAmount(amount);
Price price = Price.fromString(this.price);
op.setPrice(price.toXdr());
Int64 offerId = new Int64();
offerId.setInt64(Long.valueOf(this.offerId));
offerId.setInt64(this.offerId);
op.setOfferID(offerId);

org.stellar.sdk.xdr.Operation.OperationBody body =
Expand All @@ -92,7 +91,7 @@ public static class Builder {
private final Asset selling;
private final Asset buying;
private final String amount;
private final String price;
private final Price price;
private long offerId = 0;

private String mSourceAccount;
Expand All @@ -105,9 +104,9 @@ public static class Builder {
Builder(ManageSellOfferOp op) {
selling = Asset.fromXdr(op.getSelling());
buying = Asset.fromXdr(op.getBuying());
amount = Operation.fromXdrAmount(op.getAmount().getInt64().longValue());
price = Price.fromXdr(op.getPrice()).toString();
offerId = op.getOfferID().getInt64().longValue();
amount = Operation.fromXdrAmount(op.getAmount().getInt64());
price = Price.fromXdr(op.getPrice());
offerId = op.getOfferID().getInt64();
}

/**
Expand All @@ -124,13 +123,22 @@ public Builder(
@NonNull Asset selling,
@NonNull Asset buying,
@NonNull String amount,
@NonNull String price) {
@NonNull Price price) {
this.selling = selling;
this.buying = buying;
this.amount = amount;
this.price = price;
}

/** An alias for {@link Builder#Builder(Asset, Asset, String, Price)} */
public Builder(
@NonNull Asset selling,
@NonNull Asset buying,
@NonNull String amount,
@NonNull String price) {
this(selling, buying, amount, Price.fromString(price));
}

/**
* Sets offer ID. <code>0</code> creates a new offer. Set to existing offer ID to change it.
*
Expand Down
Loading

0 comments on commit badda6d

Please sign in to comment.