Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AMM SDK Updates #359

Merged
merged 21 commits into from
Sep 21, 2021
Merged
12 changes: 11 additions & 1 deletion src/main/java/org/stellar/sdk/Asset.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Base Asset class.
* @see <a href="https://www.stellar.org/developers/learn/concepts/assets.html" target="_blank">Assets</a>
*/
public abstract class Asset {
public abstract class Asset implements Comparable<Asset> {
Asset() {}

/**
Expand Down Expand Up @@ -32,6 +32,13 @@ public static Asset create(String type, String code, String issuer) {
}
}

public static Asset create(ChangeTrustAsset.Wrapper wrapped) {
Copy link
Contributor

@tamirms tamirms Sep 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if these methods are useful because calling wrapped.getAsset() is just as easy as calling Asset.create(wrapped)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, my thinking was that the conversions would be more symmetric this way. e.g. ChangeTrustAsset.create(native), or Asset.create(changeTrustAsset)

return wrapped.getAsset();
}
public static Asset create(TrustLineAsset.Wrapper wrapped) {
return wrapped.getAsset();
}

/**
* Creates one of AssetTypeCreditAlphaNum4 or AssetTypeCreditAlphaNum12 object based on a <code>code</code> length
* @param code Asset code
Expand Down Expand Up @@ -75,6 +82,7 @@ public static Asset fromXdr(org.stellar.sdk.xdr.Asset xdr) {
* <li><code>native</code></li>
* <li><code>credit_alphanum4</code></li>
* <li><code>credit_alphanum12</code></li>
* <li><code>pool_share</code></li>
* </ul>
*/
public abstract String getType();
Expand All @@ -86,4 +94,6 @@ public static Asset fromXdr(org.stellar.sdk.xdr.Asset xdr) {
* Generates XDR object from a given Asset object
*/
public abstract org.stellar.sdk.xdr.Asset toXdr();

public abstract int compareTo(Asset other);
}
37 changes: 37 additions & 0 deletions src/main/java/org/stellar/sdk/AssetAmount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.stellar.sdk;

import com.google.common.base.Objects;

public final class AssetAmount {
private final Asset asset;
private final String amount;

AssetAmount(Asset asset, String amount) {
this.asset = asset;
this.amount = amount;
}

public Asset getAsset() {
return this.asset;
}

public String getAmount() {
return this.amount;
}


public int hashCode() {
return Objects.hashCode(asset, amount);
}

@Override
public boolean equals(Object object) {
if (!(object instanceof AssetAmount)) {
return false;
}

AssetAmount o = (AssetAmount) object;
return Objects.equal(this.getAsset(), o.getAsset()) &&
Objects.equal(this.getAmount(), o.getAmount());
}
}
17 changes: 16 additions & 1 deletion src/main/java/org/stellar/sdk/AssetTypeCreditAlphaNum12.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,27 @@ public String getType() {
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();
org.stellar.sdk.xdr.AlphaNum12 credit = new org.stellar.sdk.xdr.AlphaNum12();
AssetCode12 assetCode12 = new AssetCode12();
assetCode12.setAssetCode12(Util.paddedByteArray(mCode, 12));
credit.setAssetCode(assetCode12);
credit.setIssuer(StrKey.encodeToXDRAccountId(mIssuer));
xdr.setAlphaNum12(credit);
return xdr;
}

@Override
public int compareTo(Asset other) {
if (other.getType() != "credit_alphanum12") {
return 1;
}

AssetTypeCreditAlphaNum o = (AssetTypeCreditAlphaNum) other;

if (this.getCode() != o.getCode()) {
return this.getCode().compareTo(o.getCode());
}

return this.getIssuer().compareTo(o.getIssuer());
}
}
19 changes: 18 additions & 1 deletion src/main/java/org/stellar/sdk/AssetTypeCreditAlphaNum4.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,29 @@ public String getType() {
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();
org.stellar.sdk.xdr.AlphaNum4 credit = new org.stellar.sdk.xdr.AlphaNum4();
AssetCode4 assetCode4 = new AssetCode4();
assetCode4.setAssetCode4(Util.paddedByteArray(mCode, 4));
credit.setAssetCode(assetCode4);
credit.setIssuer(StrKey.encodeToXDRAccountId(mIssuer));
xdr.setAlphaNum4(credit);
return xdr;
}

@Override
public int compareTo(Asset other) {
if (other.getType() == "credit_alphanum12") {
return -1;
} else if (other.getType() == "native") {
return 1;
}

AssetTypeCreditAlphaNum o = (AssetTypeCreditAlphaNum) other;

if (this.getCode() != o.getCode()) {
return this.getCode().compareTo(o.getCode());
}

return this.getIssuer().compareTo(o.getIssuer());
}
}
8 changes: 8 additions & 0 deletions src/main/java/org/stellar/sdk/AssetTypeNative.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,12 @@ public org.stellar.sdk.xdr.Asset toXdr() {
xdr.setDiscriminant(AssetType.ASSET_TYPE_NATIVE);
return xdr;
}

@Override
public int compareTo(Asset other) {
if (other.getType() == "native") {
return 0;
}
return -1;
}
}
132 changes: 132 additions & 0 deletions src/main/java/org/stellar/sdk/ChangeTrustAsset.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package org.stellar.sdk;

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

/**
* ChangeTrustAsset class.
* @see <a href="https://www.stellar.org/developers/learn/concepts/assets.html" target="_blank">Assets</a>
*/
public abstract class ChangeTrustAsset implements Comparable<ChangeTrustAsset> {
ChangeTrustAsset() {}

/**
* Parses an asset string and returns the equivalent ChangeTrustAsset instance.
* The asset string is expected to either be "native" or a string of the form "CODE:ISSUER"
*
* @param canonicalForm Canonical string representation of an asset
*/
public static ChangeTrustAsset create(String canonicalForm) {
return new Wrapper(Asset.create(canonicalForm));
}

public static ChangeTrustAsset create(String type, String code, String issuer) {
return new Wrapper(Asset.create(type, code, issuer));
}
public static ChangeTrustAsset create(Asset asset) {
return new Wrapper(asset);
}
public static ChangeTrustAsset create(LiquidityPoolParameters params) {
return new LiquidityPoolShareChangeTrustAsset(params);
}
public static ChangeTrustAsset create(TrustLineAsset.Wrapper wrapper) {
return new Wrapper(wrapper.getAsset());
}
// Cannot convert from LiquidityPoolShareTrustLineAsset, as that only has a
// liquidity pool id, not parameters.

/**
* Creates one of AssetTypeCreditAlphaNum4 or AssetTypeCreditAlphaNum12 object based on a <code>code</code> length
* @param code ChangeTrustAsset code
* @param issuer ChangeTrustAsset issuer
*/
public static ChangeTrustAsset createNonNativeAsset(String code, String issuer) {
return ChangeTrustAsset.create(Asset.createNonNativeAsset(code, issuer));
}

/**
* Generates ChangeTrustAsset object from a given XDR object
* @param xdr XDR object
*/
public static ChangeTrustAsset fromXdr(org.stellar.sdk.xdr.ChangeTrustAsset xdr) {
// TODO: Figure out how we can re-use Asset.fromXdr here
String accountId;
switch (xdr.getDiscriminant()) {
case ASSET_TYPE_NATIVE:
return ChangeTrustAsset.create(new AssetTypeNative());
case ASSET_TYPE_CREDIT_ALPHANUM4:
String assetCode4 = Util.paddedByteArrayToString(xdr.getAlphaNum4().getAssetCode().getAssetCode4());
accountId = StrKey.encodeStellarAccountId(xdr.getAlphaNum4().getIssuer());
return ChangeTrustAsset.create(new AssetTypeCreditAlphaNum4(assetCode4, accountId));
case ASSET_TYPE_CREDIT_ALPHANUM12:
String assetCode12 = Util.paddedByteArrayToString(xdr.getAlphaNum12().getAssetCode().getAssetCode12());
accountId = StrKey.encodeStellarAccountId(xdr.getAlphaNum12().getIssuer());
return ChangeTrustAsset.create(new AssetTypeCreditAlphaNum12(assetCode12, accountId));
case ASSET_TYPE_POOL_SHARE:
return new LiquidityPoolShareChangeTrustAsset(LiquidityPoolParameters.fromXdr(xdr.getLiquidityPool()));
default:
throw new IllegalArgumentException("Unknown asset type " + xdr.getDiscriminant());
}
}

public abstract String getType();

@Override
public abstract boolean equals(Object object);

@Override
public abstract int compareTo(ChangeTrustAsset other);

/**
* Generates XDR object from a given ChangeTrustAsset object
*/
public abstract org.stellar.sdk.xdr.ChangeTrustAsset toXdr();

public static final class Wrapper extends ChangeTrustAsset {
private Asset asset;

public Wrapper(Asset baseAsset) {
super();
checkNotNull(baseAsset, "asset cannot be null");
asset = baseAsset;
}

public Asset getAsset() {
return asset;
}

@Override
public String getType() {
return asset.getType();
}

@Override
public final boolean equals(Object object) {
if (object == null || !this.getClass().equals(object.getClass())) {
return false;
}

ChangeTrustAsset.Wrapper o = (ChangeTrustAsset.Wrapper) object;
return this.getAsset().equals(o.getAsset());
}

@Override
public int compareTo(ChangeTrustAsset other) {
if (other.getType() == "pool_share") {
return -1;
}
return this.getAsset().compareTo(((ChangeTrustAsset.Wrapper) other).getAsset());
}

@Override
public org.stellar.sdk.xdr.ChangeTrustAsset toXdr() {
org.stellar.sdk.xdr.ChangeTrustAsset xdr = new org.stellar.sdk.xdr.ChangeTrustAsset();

org.stellar.sdk.xdr.Asset assetXdr = asset.toXdr();
xdr.setDiscriminant(assetXdr.getDiscriminant());
xdr.setAlphaNum4(assetXdr.getAlphaNum4());
xdr.setAlphaNum12(assetXdr.getAlphaNum12());

return xdr;
}
}
}
12 changes: 6 additions & 6 deletions src/main/java/org/stellar/sdk/ChangeTrustOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@
*/
public class ChangeTrustOperation extends Operation {

private final Asset asset;
private final ChangeTrustAsset asset;
private final String limit;

private ChangeTrustOperation(Asset asset, String limit) {
private ChangeTrustOperation(ChangeTrustAsset asset, String limit) {
this.asset = checkNotNull(asset, "asset cannot be null");
this.limit = checkNotNull(limit, "limit cannot be null");
}

/**
* The asset of the trustline. For example, if a gateway extends a trustline of up to 200 USD to a user, the line is USD.
*/
public Asset getAsset() {
public ChangeTrustAsset getAsset() {
return asset;
}

Expand Down Expand Up @@ -55,13 +55,13 @@ org.stellar.sdk.xdr.Operation.OperationBody toOperationBody(AccountConverter acc
* @see ChangeTrustOperation
*/
public static class Builder {
private final Asset asset;
private final ChangeTrustAsset asset;
private final String limit;

private String mSourceAccount;

Builder(ChangeTrustOp op) {
asset = Asset.fromXdr(op.getLine());
asset = ChangeTrustAsset.fromXdr(op.getLine());
limit = Operation.fromXdrAmount(op.getLimit().getInt64().longValue());
}

Expand All @@ -71,7 +71,7 @@ public static class Builder {
* @param limit The limit of the trustline. For example, if a gateway extends a trustline of up to 200 USD to a user, the limit is 200.
* @throws ArithmeticException when limit has more than 7 decimal places.
*/
public Builder(Asset asset, String limit) {
public Builder(ChangeTrustAsset asset, String limit) {
this.asset = checkNotNull(asset, "asset cannot be null");
this.limit = checkNotNull(limit, "limit cannot be null");
}
Expand Down
Loading