Skip to content

Commit

Permalink
refactor(CreatePassiveOfferOperation, ManageOfferOperation): Preserve…
Browse files Browse the repository at this point in the history
… partial forward compatibility.

ManageOfferOperation actually generates MANAGE_SELL_OFFER, CreatePassiveOfferOperation actually
generates CREATE_PASSIVE_SELL_OFFER.

BREAKING CHANGE: Looking at the commit description, I recommend using the new class immediately.
  • Loading branch information
overcat committed May 14, 2019
1 parent 34edeee commit e009464
Show file tree
Hide file tree
Showing 3 changed files with 356 additions and 9 deletions.
132 changes: 128 additions & 4 deletions src/main/java/org/stellar/sdk/CreatePassiveOfferOperation.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,137 @@
package org.stellar.sdk;

import org.stellar.sdk.xdr.CreatePassiveSellOfferOp;
import org.stellar.sdk.xdr.Int64;
import org.stellar.sdk.xdr.OperationType;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* @deprecated Use {@link CreatePassiveSellOfferOperation}
* @see <a href="https://www.stellar.org/developers/learn/concepts/list-of-operations.html" target="_blank">List of Operations</a>
* @deprecated As the protocol is upgraded to version 11, CreatePassiveOffer has been renamed to CreatePassiveSellOffer, in order to avoid some unknown errors, please use {@link CreatePassiveSellOfferOperation} immediately. Will be removed in version 0.8.0.
* Represents <a href="https://www.stellar.org/developers/learn/concepts/list-of-operations.html#create-passive-offer" target="_blank">CreatePassiveOffer</a> operation.
*/
@Deprecated
public class CreatePassiveOfferOperation extends Operation {
private final Asset selling;
private final Asset buying;
private final String amount;
private final String price;

private CreatePassiveOfferOperation(Asset selling, Asset buying, String amount, String price) {
this.selling = checkNotNull(selling, "selling cannot be null");
this.buying = checkNotNull(buying, "buying cannot be null");
this.amount = checkNotNull(amount, "amount cannot be null");
this.price = checkNotNull(price, "price cannot be null");
}

/**
* The asset being sold in this operation
*/
public Asset getSelling() {
return selling;
}

/**
* The asset being bought in this operation
*/
public Asset getBuying() {
return buying;
}

/**
* Amount of selling being sold.
*/
public String getAmount() {
return amount;
}

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

@Override
org.stellar.sdk.xdr.Operation.OperationBody toOperationBody() {
return null;
CreatePassiveSellOfferOp op = new CreatePassiveSellOfferOp();
op.setSelling(selling.toXdr());
op.setBuying(buying.toXdr());
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 = new org.stellar.sdk.xdr.Operation.OperationBody();
body.setDiscriminant(OperationType.CREATE_PASSIVE_SELL_OFFER);
body.setCreatePassiveSellOfferOp(op);

return body;
}

/**
* Builds CreatePassiveOffer operation.
*
* @see CreatePassiveOfferOperation
*/
public static class Builder {

private final Asset selling;
private final Asset buying;
private final String amount;
private final String price;

private KeyPair mSourceAccount;

/**
* Construct a new CreatePassiveOffer builder from a CreatePassiveSellOfferOp XDR.
*
* @param op
*/
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();
}

/**
* Creates a new CreatePassiveOffer builder.
*
* @param selling The asset being sold in this operation
* @param buying The asset being bought in this operation
* @param amount Amount of selling being sold.
* @param price Price of 1 unit of selling in terms of buying.
* @throws ArithmeticException when amount has more than 7 decimal places.
*/
public Builder(Asset selling, Asset buying, String amount, String price) {
this.selling = checkNotNull(selling, "selling cannot be null");
this.buying = checkNotNull(buying, "buying cannot be null");
this.amount = checkNotNull(amount, "amount cannot be null");
this.price = checkNotNull(price, "price cannot be null");
}

/**
* Sets the source account for this operation.
*
* @param sourceAccount The operation's source account.
* @return Builder object so you can chain methods.
*/
public Builder setSourceAccount(KeyPair sourceAccount) {
mSourceAccount = checkNotNull(sourceAccount, "sourceAccount cannot be null");
return this;
}

/**
* Builds an operation
*/
public CreatePassiveOfferOperation build() {
CreatePassiveOfferOperation operation = new CreatePassiveOfferOperation(selling, buying, amount, price);
if (mSourceAccount != null) {
operation.setSourceAccount(mSourceAccount);
}
return operation;
}
}
}
}
158 changes: 154 additions & 4 deletions src/main/java/org/stellar/sdk/ManageOfferOperation.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,164 @@
package org.stellar.sdk;

import org.stellar.sdk.xdr.*;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* @deprecated Use {@link ManageSellOfferOperation}
* Represents <a href="https://www.stellar.org/developers/learn/concepts/list-of-operations.html#manage-offer" target="_blank">ManageOffer</a> operation.
*
* @see <a href="https://www.stellar.org/developers/learn/concepts/list-of-operations.html" target="_blank">List of Operations</a>
* @deprecated As the protocol is upgraded to version 11, ManageOffer has been renamed to ManageSellOffer, in order to avoid some unknown errors, please use {@link ManageSellOfferOperation} immediately. Will be removed in version 0.8.0.
*/
@Deprecated
public class ManageOfferOperation extends Operation {

private final Asset selling;
private final Asset buying;
private final String amount;
private final String price;
private final long offerId;

private ManageOfferOperation(Asset selling, Asset buying, String amount, String price, long offerId) {
this.selling = checkNotNull(selling, "selling cannot be null");
this.buying = checkNotNull(buying, "buying cannot be null");
this.amount = checkNotNull(amount, "amount cannot be null");
this.price = checkNotNull(price, "price cannot be null");
// offerId can be null
this.offerId = offerId;
}

/**
* The asset being sold in this operation
*/
public Asset getSelling() {
return selling;
}

/**
* The asset being bought in this operation
*/
public Asset getBuying() {
return buying;
}

/**
* Amount of selling being sold.
*/
public String getAmount() {
return amount;
}

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

/**
* The ID of the offer.
*/
public long getOfferId() {
return offerId;
}

@Override
org.stellar.sdk.xdr.Operation.OperationBody toOperationBody() {
return null;
ManageSellOfferOp op = new ManageSellOfferOp();
op.setSelling(selling.toXdr());
op.setBuying(buying.toXdr());
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));
op.setOfferID(offerId);

org.stellar.sdk.xdr.Operation.OperationBody body = new org.stellar.sdk.xdr.Operation.OperationBody();
body.setDiscriminant(OperationType.MANAGE_SELL_OFFER);
body.setManageSellOfferOp(op);

return body;
}

/**
* Builds ManageOffer operation. If you want to update existing offer use
* {@link org.stellar.sdk.ManageOfferOperation.Builder#setOfferId(long)}.
*
* @see ManageOfferOperation
*/
public static class Builder {

private final Asset selling;
private final Asset buying;
private final String amount;
private final String price;
private long offerId = 0;

private KeyPair mSourceAccount;

/**
* Construct a new ManageOfferOperation builder from a ManageSellOfferOp XDR.
*
* @param op {@link ManageSellOfferOp}
*/
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();
}

/**
* Creates a new ManageOffer builder. If you want to update existing offer use
* {@link org.stellar.sdk.ManageOfferOperation.Builder#setOfferId(long)}.
*
* @param selling The asset being sold in this operation
* @param buying The asset being bought in this operation
* @param amount Amount of selling being sold.
* @param price Price of 1 unit of selling in terms of buying.
* @throws ArithmeticException when amount has more than 7 decimal places.
*/
public Builder(Asset selling, Asset buying, String amount, String price) {
this.selling = checkNotNull(selling, "selling cannot be null");
this.buying = checkNotNull(buying, "buying cannot be null");
this.amount = checkNotNull(amount, "amount cannot be null");
this.price = checkNotNull(price, "price cannot be null");
}

/**
* Sets offer ID. <code>0</code> creates a new offer. Set to existing offer ID to change it.
*
* @param offerId
*/
public Builder setOfferId(long offerId) {
this.offerId = offerId;
return this;
}

/**
* Sets the source account for this operation.
*
* @param sourceAccount The operation's source account.
* @return Builder object so you can chain methods.
*/
public Builder setSourceAccount(KeyPair sourceAccount) {
mSourceAccount = checkNotNull(sourceAccount, "sourceAccount cannot be null");
return this;
}

/**
* Builds an operation
*/
public ManageOfferOperation build() {
ManageOfferOperation operation = new ManageOfferOperation(selling, buying, amount, price, offerId);
if (mSourceAccount != null) {
operation.setSourceAccount(mSourceAccount);
}
return operation;
}
}
}
}
Loading

0 comments on commit e009464

Please sign in to comment.