Skip to content

Commit

Permalink
Adapt to Protocol v11 (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat authored and bartekn committed May 14, 2019
1 parent f373ccd commit 98da775
Show file tree
Hide file tree
Showing 81 changed files with 2,128 additions and 476 deletions.
17 changes: 9 additions & 8 deletions src/main/java/org/stellar/sdk/AllowTrustOperation.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.stellar.sdk;

import org.stellar.sdk.xdr.AccountID;
import org.stellar.sdk.xdr.AllowTrustOp;
import org.stellar.sdk.xdr.AssetType;
import org.stellar.sdk.xdr.OperationType;
import org.stellar.sdk.xdr.*;

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

Expand Down Expand Up @@ -55,11 +52,15 @@ org.stellar.sdk.xdr.Operation.OperationBody toOperationBody() {
// asset
AllowTrustOp.AllowTrustOpAsset asset = new AllowTrustOp.AllowTrustOpAsset();
if (assetCode.length() <= 4) {
AssetCode4 assetCode4 = new AssetCode4();
assetCode4.setAssetCode4(Util.paddedByteArray(assetCode, 4));
asset.setDiscriminant(AssetType.ASSET_TYPE_CREDIT_ALPHANUM4);
asset.setAssetCode4(Util.paddedByteArray(assetCode, 4));
asset.setAssetCode4(assetCode4);
} else {
AssetCode12 assetCode12 = new AssetCode12();
assetCode12.setAssetCode12(Util.paddedByteArray(assetCode, 12));
asset.setDiscriminant(AssetType.ASSET_TYPE_CREDIT_ALPHANUM12);
asset.setAssetCode12(Util.paddedByteArray(assetCode, 12));
asset.setAssetCode12(assetCode12);
}
op.setAsset(asset);
// authorize
Expand All @@ -86,10 +87,10 @@ public static class Builder {
trustor = KeyPair.fromXdrPublicKey(op.getTrustor().getAccountID());
switch (op.getAsset().getDiscriminant()) {
case ASSET_TYPE_CREDIT_ALPHANUM4:
assetCode = new String(op.getAsset().getAssetCode4()).trim();
assetCode = new String(op.getAsset().getAssetCode4().getAssetCode4()).trim();
break;
case ASSET_TYPE_CREDIT_ALPHANUM12:
assetCode = new String(op.getAsset().getAssetCode12()).trim();
assetCode = new String(op.getAsset().getAssetCode12().getAssetCode12()).trim();
break;
default:
throw new RuntimeException("Unknown asset code");
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/stellar/sdk/Asset.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public static Asset fromXdr(org.stellar.sdk.xdr.Asset xdr) {
case ASSET_TYPE_NATIVE:
return new AssetTypeNative();
case ASSET_TYPE_CREDIT_ALPHANUM4:
String assetCode4 = Util.paddedByteArrayToString(xdr.getAlphaNum4().getAssetCode());
String assetCode4 = Util.paddedByteArrayToString(xdr.getAlphaNum4().getAssetCode().getAssetCode4());
KeyPair issuer4 = KeyPair.fromXdrPublicKey(
xdr.getAlphaNum4().getIssuer().getAccountID());
return new AssetTypeCreditAlphaNum4(assetCode4, issuer4);
case ASSET_TYPE_CREDIT_ALPHANUM12:
String assetCode12 = Util.paddedByteArrayToString(xdr.getAlphaNum12().getAssetCode());
String assetCode12 = Util.paddedByteArrayToString(xdr.getAlphaNum12().getAssetCode().getAssetCode12());
KeyPair issuer12 = KeyPair.fromXdrPublicKey(xdr.getAlphaNum12().getIssuer().getAccountID());
return new AssetTypeCreditAlphaNum12(assetCode12, issuer12);
default:
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/stellar/sdk/AssetTypeCreditAlphaNum12.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.stellar.sdk;

import org.stellar.sdk.xdr.AccountID;
import org.stellar.sdk.xdr.AssetCode12;
import org.stellar.sdk.xdr.AssetType;

/**
Expand Down Expand Up @@ -31,7 +32,9 @@ public org.stellar.sdk.xdr.Asset toXdr() {
org.stellar.sdk.xdr.Asset xdr = new org.stellar.sdk.xdr.Asset();
xdr.setDiscriminant(AssetType.ASSET_TYPE_CREDIT_ALPHANUM12);
org.stellar.sdk.xdr.Asset.AssetAlphaNum12 credit = new org.stellar.sdk.xdr.Asset.AssetAlphaNum12();
credit.setAssetCode(Util.paddedByteArray(mCode, 12));
AssetCode12 assetCode12 = new AssetCode12();
assetCode12.setAssetCode12(Util.paddedByteArray(mCode, 12));
credit.setAssetCode(assetCode12);
AccountID accountID = new AccountID();
accountID.setAccountID(mIssuer.getXdrPublicKey());
credit.setIssuer(accountID);
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/stellar/sdk/AssetTypeCreditAlphaNum4.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.stellar.sdk;

import org.stellar.sdk.xdr.AccountID;
import org.stellar.sdk.xdr.AssetCode4;
import org.stellar.sdk.xdr.AssetType;

/**
Expand Down Expand Up @@ -31,7 +32,9 @@ public org.stellar.sdk.xdr.Asset toXdr() {
org.stellar.sdk.xdr.Asset xdr = new org.stellar.sdk.xdr.Asset();
xdr.setDiscriminant(AssetType.ASSET_TYPE_CREDIT_ALPHANUM4);
org.stellar.sdk.xdr.Asset.AssetAlphaNum4 credit = new org.stellar.sdk.xdr.Asset.AssetAlphaNum4();
credit.setAssetCode(Util.paddedByteArray(mCode, 4));
AssetCode4 assetCode4 = new AssetCode4();
assetCode4.setAssetCode4(Util.paddedByteArray(mCode, 4));
credit.setAssetCode(assetCode4);
AccountID accountID = new AccountID();
accountID.setAccountID(mIssuer.getXdrPublicKey());
credit.setIssuer(accountID);
Expand Down
208 changes: 105 additions & 103 deletions src/main/java/org/stellar/sdk/CreatePassiveOfferOperation.java
Original file line number Diff line number Diff line change
@@ -1,135 +1,137 @@
package org.stellar.sdk;

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

import java.math.BigDecimal;
import java.math.MathContext;

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

/**
* Represents <a href="https://www.stellar.org/developers/learn/concepts/list-of-operations.html#create-passive-offer" target="_blank">CreatePassiveOffer</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, 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.
*/
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() {
CreatePassiveOfferOp op = new CreatePassiveOfferOp();
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_OFFER);
body.setCreatePassiveOfferOp(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;
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");
}

/**
* Construct a new CreatePassiveOffer builder from a CreatePassiveOfferOp XDR.
* @param op
* The asset being sold in this operation
*/
Builder(CreatePassiveOfferOp op) {
selling = Asset.fromXdr(op.getSelling());
buying = Asset.fromXdr(op.getBuying());
amount = Operation.fromXdrAmount(op.getAmount().getInt64().longValue());
price = Price.fromXdr(op.getPrice()).toString();
public Asset getSelling() {
return selling;
}

/**
* 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.
* The asset being bought in this operation
*/
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");
public Asset getBuying() {
return buying;
}

/**
* Sets the source account for this operation.
* @param sourceAccount The operation's source account.
* @return Builder object so you can chain methods.
* Amount of selling being sold.
*/
public Builder setSourceAccount(KeyPair sourceAccount) {
mSourceAccount = checkNotNull(sourceAccount, "sourceAccount cannot be null");
return this;
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() {
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 an operation
* Builds CreatePassiveOffer operation.
*
* @see CreatePassiveOfferOperation
*/
public CreatePassiveOfferOperation build() {
CreatePassiveOfferOperation operation = new CreatePassiveOfferOperation(selling, buying, amount, price);
if (mSourceAccount != null) {
operation.setSourceAccount(mSourceAccount);
}
return operation;
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;
}
}
}
}
}
Loading

0 comments on commit 98da775

Please sign in to comment.